Can't set time despite sending ESP8266 seconds since epoch
Posted: Mon Nov 22, 2021 8:35 am
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.
The following is what gets sent to the Serial monitor.
Am I just converting the value wrong? Ive tried converting the number to all types of variables.
Any help is appreciated. Thanks for looking.
Code: Select all
settimeofday_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 all
Time 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.