Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <time.h>
- #define TIMEZONE 1 // (UTC+) Standard TimeZone in hours
- #define DST 3600 // Daily summer time offset in seconds
- const char* ssid = "********";
- const char* password = "*********";
- unsigned long updateTime = millis();
- void setup() {
- // Start Serial for debug
- Serial.begin(115200);
- Serial.println();
- Serial.println(F("Sync NTP server example."));
- WiFi.mode(WIFI_STA);
- // attempt to connect to Wifi network:
- Serial.print("Connecting Wifi: ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- // Sync system time with one of up to 3 NTP servers (after connected)
- configTime(TIMEZONE * 3600, DST, "pool.ntp.org", "time.google.com", "time.windows.com");
- }
- void loop() {
- if (millis() - updateTime > 5000) {
- // time() return the actual Unix "epoch" time (number of seconds that have elapsed since January 1, 1970)
- time_t now = time(nullptr);
- Serial.printf("\n\nEpoch Unix time: %ul", now );
- Serial.printf("\nHuman readable time: %s", ctime(&now));
- // with the already defined struct "tm" we can easy have access at days, hours, minutes etc etc etc
- // tm_sec The number of seconds after the minute, normally in the range 0 to 59
- // tm_min The number of minutes after the hour, in the range 0 to 59.
- // tm_hour The number of hours past midnight, in the range 0 to 23.
- // tm_mday The day of the month, in the range 1 to 31.
- // tm_mon The number of months since January, in the range 0 to 11.
- // tm_year The number of years since 1900.
- // tm_wday The number of days since Sunday, in the range 0 to 6.
- // tm_yday The number of days since January 1, in the range 0 to 365.
- // create local instance of "tm"
- struct tm lTime;
- // fill the structure data with actual time
- localtime_r(&now, &lTime);
- // print out data
- Serial.printf("\nDate: %02d/%02d/%04d", lTime.tm_mday, lTime.tm_mon +1 , lTime.tm_year + 1900);
- Serial.printf("\nTime: %02d:%02d:%02d", lTime.tm_hour, lTime.tm_min, lTime.tm_sec);
- updateTime = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement