Last active
July 10, 2020 14:32
-
-
Save Adobe-Android/dc2f7833e00a60f65fc4ef94555a63c7 to your computer and use it in GitHub Desktop.
Shows time and date data in the format of day of week, month, day of month, year, hour:minute:second using a Wi-Fi connection and NTP servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include "time.h" | |
const char* ssid = "SSID"; | |
const char* password = "password"; | |
const char* ntpServer = "pool.ntp.org"; | |
// Default time from NTP is GMT. Replace with your timezone offset. Expected to be measured in seconds. | |
// 3600 seconds in 1 hour. CDT is 5 hours behind GMT. 3600 * 5 = 18,000. | |
// Since it is 5 hours behind, we make this number negative. | |
const long gmtOffset_sec = -18000; | |
const int daylightOffset_sec = 0; | |
void printLocalTime() { | |
struct tm timeinfo; | |
if (!getLocalTime(&timeinfo)) { | |
Serial.println("Failed to obtain time"); | |
return; | |
} | |
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
// Connect to Wi-Fi | |
Serial.printf("Connecting to %s ", ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(" CONNECTED"); | |
// init and get time | |
configTime(cdtOffset_sec, daylightOffset_sec, ntpServer); | |
printLocalTime(); | |
// disconnect WiFi as it's no longer needed | |
WiFi.disconnect(true); | |
WiFi.mode(WIFI_OFF); | |
} | |
void loop() { | |
delay(1000); | |
printLocalTime(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment