Skip to content

Instantly share code, notes, and snippets.

@luc-github
Created September 28, 2017 14:25
Show Gist options
  • Select an option

  • Save luc-github/9511ed8e4bf8adb0f6cf40e81381aa53 to your computer and use it in GitHub Desktop.

Select an option

Save luc-github/9511ed8e4bf8adb0f6cf40e81381aa53 to your computer and use it in GitHub Desktop.
Test time script
#include <time.h>
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
const char* ssid = "your-ssid";
const char* password = "your-password";
long timezone = 8;
#ifdef ARDUINO_ARCH_ESP8266
bool getLocalTime(struct tm * info, uint32_t ms)
{
uint32_t count = ms / 10;
time_t now;
time(&now);
localtime_r(&now, info);
if(info->tm_year > (2016 - 1900)){
return true;
}
while(count--) {
delay(10);
time(&now);
localtime_r(&now, info);
if(info->tm_year > (2016 - 1900)){
return true;
}
}
return false;
}
#endif
void setup(){
byte d1 = 0;
time_t now;
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
configTime(3600*timezone, d1*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
Serial.println(getenv("TZ"));
struct tm tmstruct ;
getLocalTime(&tmstruct, 1000);
Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct.tm_year)+1900,( tmstruct.tm_mon)+1, tmstruct.tm_mday,tmstruct.tm_hour , tmstruct.tm_min, tmstruct.tm_sec);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment