Skip to content

Instantly share code, notes, and snippets.

@me-no-dev
Last active July 21, 2016 10:35
Show Gist options
  • Save me-no-dev/73dc073fccf174c697b65eee73b4706b to your computer and use it in GitHub Desktop.
Save me-no-dev/73dc073fccf174c697b65eee73b4706b to your computer and use it in GitHub Desktop.

Revisions

  1. me-no-dev revised this gist Jul 21, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ESP8266_Time.ino
    Original file line number Diff line number Diff line change
    @@ -65,6 +65,7 @@ bool initTime(){

    void setup(){
    Serial.begin(115200);
    Serial.setDebugOutput(true);
    Serial.println();
    Serial.print("Sketch Compile ");
    printTime(getCompileTime());
  2. me-no-dev revised this gist Jul 21, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ESP8266_Time.ino
    Original file line number Diff line number Diff line change
    @@ -65,9 +65,9 @@ bool initTime(){

    void setup(){
    Serial.begin(115200);
    Serial.print("Sketch Compiled At: ");
    printTime(getCompileTime());
    Serial.println();
    Serial.print("Sketch Compile ");
    printTime(getCompileTime());
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  3. me-no-dev revised this gist Jul 21, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ESP8266_Time.ino
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #include <ESP8266WiFi.h>
    #include <time.h>

    const char * ssid = "********";
  4. me-no-dev revised this gist Jul 21, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ESP8266_Time.ino
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,8 @@ void printTime(struct tm * t){
    Serial.printf("Time: %s\n", result);
    }

    extern "C" void system_set_os_print(uint8 onoff);

    bool initTime(){
    time_t rawtime;
    configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  5. me-no-dev created this gist Jul 21, 2016.
    82 changes: 82 additions & 0 deletions ESP8266_Time.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    #include <time.h>

    const char * ssid = "********";
    const char * password = "********";

    #define getCompileTime() _getCompileTime(__DATE__, __TIME__)
    struct tm * _getCompileTime(const char * compile_date, const char * compile_time){
    const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

    int year = atoi(compile_date+7),
    month = 0,
    day = atoi(compile_date+4),
    hour = atoi(compile_time),
    minute = atoi(compile_time+3),
    second = atoi(compile_time+6);

    int i;
    for(i=0; i<12; i++){
    if(memcmp(compile_date, months[i], 3) == 0){
    month = i;
    }
    }

    static struct tm * timeinfo = (struct tm *)malloc(sizeof(struct tm));
    timeinfo->tm_year = year - 1900;
    timeinfo->tm_mon = month;
    timeinfo->tm_mday = day;
    timeinfo->tm_hour = hour;
    timeinfo->tm_min = minute;
    timeinfo->tm_sec = second;
    mktime(timeinfo);
    return timeinfo;
    }

    struct tm * getCurrentTime(){
    time_t rawtime;
    time (&rawtime);
    return localtime(&rawtime);
    }

    void printTime(struct tm * t){
    static char result[64];
    // see: http://www.cplusplus.com/reference/ctime/strftime/
    strftime (result,64,"%A, %B %d %Y %H:%M:%S",t);
    Serial.printf("Time: %s\n", result);
    }

    bool initTime(){
    time_t rawtime;
    configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
    uint8_t i = 0;
    system_set_os_print(0);
    while(time(&rawtime) == 0 && i++ < 100) delay(10);
    system_set_os_print(1);
    if(i==100){
    Serial.println("Time Init Failed");
    return false;
    }
    printTime(getCurrentTime());
    return true;
    }

    void setup(){
    Serial.begin(115200);
    Serial.print("Sketch Compiled At: ");
    printTime(getCompileTime());
    Serial.println();
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    os_printf("WiFi Failed!\n");
    while(1){
    delay(100);
    }
    }
    initTime();
    }

    void loop(){
    delay(1000);
    printTime(getCurrentTime());
    }