Skip to content

Instantly share code, notes, and snippets.

@mikespook
Last active May 29, 2021 16:40
Show Gist options
  • Save mikespook/6197dc566a4ac9fa1281491b8ab4e559 to your computer and use it in GitHub Desktop.
Save mikespook/6197dc566a4ac9fa1281491b8ab4e559 to your computer and use it in GitHub Desktop.
Sync ESP8266 board date/time with NTP
#include <time.h>
#include <TZ.h>
#include <ESP8266WiFi.h>
#include <StackThunk.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
WiFiClientSecure client;
static const char digicert[] = "-----BEGIN CERTIFICATE-----\n"
"MIIHQDCCBiigAwIBAgIQD9B43Ujxor1NDyupa2A4/jANBgkqhkiG9w0BAQsFADBN\n"
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMScwJQYDVQQDEx5E\n"
"aWdpQ2VydCBTSEEyIFNlY3VyZSBTZXJ2ZXIgQ0EwHhcNMTgxMTI4MDAwMDAwWhcN\n"
"MjAxMjAyMTIwMDAwWjCBpTELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3Ju\n"
"aWExFDASBgNVBAcTC0xvcyBBbmdlbGVzMTwwOgYDVQQKEzNJbnRlcm5ldCBDb3Jw\n"
"b3JhdGlvbiBmb3IgQXNzaWduZWQgTmFtZXMgYW5kIE51bWJlcnMxEzARBgNVBAsT\n"
"ClRlY2hub2xvZ3kxGDAWBgNVBAMTD3d3dy5leGFtcGxlLm9yZzCCASIwDQYJKoZI\n"
"hvcNAQEBBQADggEPADCCAQoCggEBANDwEnSgliByCGUZElpdStA6jGaPoCkrp9vV\n"
"rAzPpXGSFUIVsAeSdjF11yeOTVBqddF7U14nqu3rpGA68o5FGGtFM1yFEaogEv5g\n"
"rJ1MRY/d0w4+dw8JwoVlNMci+3QTuUKf9yH28JxEdG3J37Mfj2C3cREGkGNBnY80\n"
"eyRJRqzy8I0LSPTTkhr3okXuzOXXg38ugr1x3SgZWDNuEaE6oGpyYJIBWZ9jF3pJ\n"
"QnucP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VGAAAEAwBIMEYCIQCEZIG3IR36Gkj1dq5L6EaGVycX\n"
"sHvpO7dKV0JsooTEbAIhALuTtf4wxGTkFkx8blhTV+7sf6pFT78ORo7+cP39jkJC\n"
"AHYAh3W/51l8+IxDmV+9827/Vo1HVjb/SrVgwbTq/16ggw8AAAFnXDGWFQAABAMA\n"
"RzBFAiBvqnfSHKeUwGMtLrOG3UGLQIoaL3+uZsGTX3MfSJNQEQIhANL5nUiGBR6g\n"
"l0QlCzzqzvorGXyB/yd7nttYttzo8EpOAHYAb1N2rDHwMRnYmQCkURX/dxUcEdkC\n"
"wQApBo2yCJo32RMAAAFnXDGWnAAABAMARzBFAiEA5Hn7Q4SOyqHkT+kDsHq7ku7z\n"
"RDuM7P4UDX2ft2Mpny0CIE13WtxJAUr0aASFYZ/XjSAMMfrB0/RxClvWVss9LHKM\n"
"MA0GCSqGSIb3DQEBCwUAA4IBAQBzcIXvQEGnakPVeJx7VUjmvGuZhrr7DQOLeP4R\n"
"8CmgDM1pFAvGBHiyzvCH1QGdxFl6cf7wbp7BoLCRLR/qPVXFMwUMzcE1GLBqaGZM\n"
"v1Yh2lvZSLmMNSGRXdx113pGLCInpm/TOhfrvr0TxRImc8BdozWJavsn1N2qdHQu\n"
"N+UBO6bQMLCD0KHEdSGFsuX6ZwAworxTg02/1qiDu7zW7RyzHvFYA4IAjpzvkPIa\n"
"X6KjBtpdvp/aXabmL95YgBjT8WJ7pqOfrqhpcmOBZa6Cg6O1l4qbIFH/Gj9hQB5I\n"
"0Gs4+eH6F9h3SojmPTYkT+8KuZ9w84Mn+M8qBXUQoYoKgIjN\n"
"-----END CERTIFICATE-----";
X509List* cert;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
connectWiFi("itea", "iTea0987612345");
setDateTime();
setupSecure();
}
void loop() {
getDateTime();
getURL("https://example.org");
delay(1000);
}
void blink(int t) {
for(int i = 0; i < t; i ++) {
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
}
}
void getDateTime() {
time_t now = time(nullptr);
struct tm * timeinfo;
timeinfo = localtime(&now);
Serial.printf("%s %s", tzname[0], asctime(timeinfo));
}
void setDateTime() {
configTime(TZ_Pacific_Auckland, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
blink(1);
now = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.printf("%s %s", tzname[0], asctime(&timeinfo));
}
void connectWiFi(const char* ssid, const char* password) {
Serial.printf("\nConnecting to %s", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
blink(1);
}
Serial.print("\nWiFi connected, IP address: ");
Serial.print(WiFi.localIP().toString());
Serial.println();
}
void setupSecure() {
cert = new X509List(digicert);
client.setTrustAnchors(cert);
}
void getURL(String url) {
HTTPClient http;
if (http.begin(client, url)) {
Serial.print("[HTTP] GET ... ");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.printf("Payload: %d byte\n", payload.length());
blink(2);
}
} else {
Serial.printf("failed, error: %d %s\n", httpCode, http.errorToString(httpCode).c_str());
blink(5);
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
blink(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment