How to reset system clock over BT Serial

Posted on
  • Espruino 1.4 flash v1.86 / 262144 I have the Serial over BT working fine, my question is when my board is deployed, how can I fine tune the system clock?

    I think I understand I can send the exact time when I send my code over to the board by clicking the correct IDE setting, and could add hours on to adjust for daylight saving or TZs etc.

    My concern is if the battery is changed/disconnected how would I go about sending the correct time and date to the system over BT?

    I read about Clock but I have doubts that this var eg clk is just that, a var I keep persisting, or does it indeed update the system clock?

    var Clock = require("clock").Clock;
     var clk=new Clock(2016,4,15,23,45,0,0);   // Initialise with specific date
    
  • Yes, the Clock class just works off of whatever the time currently is, and stores an offset as a variable inside it.

    Instead, you can use the setTimefunction: http://www.espruino.com/Reference#l__glo­bal_setTime

    Just make the first argument the number of seconds since 1970 and you're good to go!

  • My IDE is set to transfer my computer time to my Espruino (Settings/Communications/Set Current Time), as long as I have a battery installed at the same time, I can unhook USB and continue with Serial1 on Bluetooth (HC-05).

    The problem is the time transferred is GMT without TZs or Summer Time adjustments.

    This leaves a 2 hour gap in my case. i.e. My computer tells me it is 15:00 hrs, but 13:00 hrs gets transferred to my Espruino. Now, maybe there is something in the guts of my computer I can change, but I doubt it.

    So I made this for now, posting it here in case anyone else is searching for a work-around.

    function addMins(mins) {
      newTime = getTime() + (mins * 60); 
      setTime(newTime);
    
      // if(dbg) {date = new Date(); console.log(date.toString());}
      
    }
    

    As I cannot believe that I am the only one who wants (almost) correct time sync with their devices I will ask : Any chance the IDE Set Current Time setting can have a GMT +/- hour diff option? OR maybe I have missed something?

  • Yes, it wouldn't be too painful for someone to add this

    However perhaps timezone support in Date would be better?

    Although (and one of the reasons I've been slow to do this) - I'm unsure how Daylight Saving Time would work. Last thing I want to do is to add all the calendar offsets for each timezone into Espruino.

    Perhaps just a module called timezoneGB or something that had a setInterval that ran once an hour and updated the timezone based on the current date?

  • I see your point. Yes, the Daylight Saving (DST) would be a pain -- I even wonder if that would be worth the bother. As my device returns to base or gets in BT range I was going to reset the time as the argument in that func applies negative figures.

    I suppose as more devices are made wifi or LoRa capable then this need would diminish anyhow.

    Brilliant piece of kit all the same.

  • Something I do with the esp8266 is to sync the clock to UTC using NTP and then receive periodic MQTT messages with the TZ offset. By separating the two I end up with a nice monotonic UTC clock for calculations and when I want to display time I add the TZ offset. Sync'ing the TZ offset is not time critical, so no special protocol necessary (I happen to use MQTT).

  • @tve, great solution - (another) excellent separation of concern(s)! (as another discussion about separation of concerns).

  • Just a quick idea - if you have WiFi access, I just grab the Date header out of the HTTP response when I contact my server. Not as accurate as NTP, but more than good enough for most things - and I can do it without having to have access to the internet.

  • Hmm...

    See, I've got a computer that has the clock set to the wrong time zone and the wrong time, such that it thinks it's in the UK and set to the time it's currently set to in the US. Needs to be corrected every couple months cause the clock drifts since I can't sync the time. That is running a webserver that returns the current time using a PHP page.

    It sucks - I recommend doing it some better way. I'd love to move the stuff on that system onto the raspberry pi.

    How do you put in a time-zone offset? I get time like this - but I can't figure out how to adjust the time that it gets from the server.

    
    function getDate() {
    	var date="";
    	require("http").get(dateurl, function(res) {
    		res.on('data',function (data) {date+=data;});
    		res.on('close',function() {clk=new Clock(date);});
    	});
    }
    
    
  • Once you've parsed the date into a Date object, it's got a ms field. You just add/subtract the number of milliseconds you need - 60*60*1000 is one hour.

  • I don't parse it to a date object, I just feed it to the Clock constructor... ?

    So what I need to do is create a Date, a new clock with blank time, and then parse the string to a Date, and then adjust the ms field of Date, and then pass that to setClock?

  • The clock module actually just passes arguments on to Date, so you can pass in whatever Date can take. Probably easiest is:

    new Clock((new Date(date)).getTime() + 60*60*1000)
    

    getTime is the standard compliant way of getting the milliseconds :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to reset system clock over BT Serial

Posted by Avatar for CoffeeCups @CoffeeCups

Actions