Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Bill2k
#92884 So I'm designing a clock sketch using an ESP8266. I programmed a webpage that figures out the seconds since epoch and sends it to the esp8266 without any issue. But immediately after receiving the string containing the number of seconds since epoch, that number gets screwed up, which screws up setting the proper time. The following is my code that isn't working as expected. Ill try and include all relevant code.

Code: Select allsettimeofday_cb(time_is_set);  // This function gets called every time the time is set

// Part of function that handles the javascript derived seconds since epoch
else if (server.argName(i) == "synctime") {
        char buff[20];
        server.arg("synctime").toCharArray(buff, sizeof(buff) - 1);
        Serial.print("Time sent from browser: "); Serial.println(buff);
        time_t rtc = (uint32_t)buff;
        timeval tv = { rtc, 0 };
        settimeofday(&tv, nullptr);
}

void time_is_set(bool from_sntp) {
  lastTimeSet = time(nullptr);

if (from_sntp) {
    strcpy(whoSetTime, "SNTP");
  }
  else {
    strcpy(whoSetTime, "USER");
  }

Serial.println((uint32_t)lastTimeSet);
  Serial.print(F("Time was set by ")); Serial.print(whoSetTime); Serial.print(F(" @ ")); Serial.println(ctime(&lastTimeSet));
}


The following is what gets sent to the Serial monitor.
Code: Select allTime sent from browser: 1637587376
1073741408
Time was set by USER @ Sat Jan 10 13:30:08 2004


Am I just converting the value wrong? Ive tried converting the number to all types of variables.

Any help is appreciated. Thanks for looking.
User avatar
By Bill2k
#92889 My sketch already has a function to get the time from an ntp server. I wanted to add this method to set the time in case the clock gets mounted in an area without wifi. It was a 'plan b'.

I think I'm gonna try to do something with mktime to see if that accomplishes what I want.