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

Moderator: igrr

User avatar
By Bill2k
#92891 I use configtime() from the sntp library when my sketch has internet access. I'm only using this method if I do not have internet access. I'm including this for the ability to set the time with or without internet.

I was able to accomplish setting the time through the browser using mktime().

Thanks for the suggestion!
User avatar
By Inq720
#92897
Bill2k wrote: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.


I too use both. If a station connection is available, I start the NTP process, but if the first connection comes in via SoftAP, JavaScript startup code goes out to the browser to send the time. As a backup, if a Station starts up later, I start the NTP process.

But for the problem at hand...

Client side JavaScript sends a number from Number(new Date()). There's a myriad of ways to get it there and/or process it. Some use built-in stuff, other roll their own. In my case, I use a WebSocket and send binary back/forth between server and browsers and have my own Clock class. My guess is you're running into an integer overflow issue. If you check the number derived from the JavaScript above, you'll find the number is far larger than an unsigned 32bit integer... (u32). itoa() won't cut it!

Again there are multiple ways to address it.
  1. you can lop off the last three string digits
  2. while still on the browser side divide by 1000
    With these two ways, you are losing the milliseconds.
  3. I finally settled on using a 64 bit variable. (Note Serial.printf can't display these, but that's another story)

Code: Select all    sint64 start = strtoll(hack, NULL, 10);
    start -= millis();
   
    _StartSeconds = (u32)(start / 1000);                          // Here's your seconds since
    _StartFraction = (u16)(start % 1000);                         // Here's your milli-seconds
    LOG(LL_INQCLOCK, "Manual Time Sync");
    _State = MANUAL;


BTW - I find fretting about the milliseconds a waste of time. No pun intended. Expecting the browser to be accurate is a waste of time. Even NTP is very inconsistent. I've seen round trips being milliseconds to as large as seconds. Expecting the actual clock hack to be at half the round trip time is even optimistic. YMMV.

Hope this helps.