Set RTC time on boot from timezonedb.com

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
alanb
Posts: 6
Joined: Sat Oct 17, 2015 3:39 pm

Set RTC time on boot from timezonedb.com

Post by alanb » Sun Oct 25, 2015 4:02 pm

I hacked up a module that gets time from the timezonedb.com, then makes it easy to sync your WiPy's real-time clock (RTC) to it. This is not that accurate because it makes no attempt to account for network latencies, but +/- a few seconds is fine for many applications.

Note, this module has a horrible bare-bones HTTP GET implementation using bare sockets. It is probably brittle and would benefit from being replaced by urllib, requests or some proper library. Ideas appreciated ...

Usage is as such:
  1. Register for an API key at timezonedb.com, which is free for non-commercial use and <1 time request/sec.
  2. Download nettime.py to your WiPy. (Optionally, set the default APIKEY variable in nettime.py to your API key.)
    nettime.py.zip
    (1.37 KiB) Downloaded 315 times
  3. Add something like the following to your boot.py so that your WiPy sets its RTC on every POWER_ON or HARD_RESET reset:

    Code: Select all

    if network.WLAN().isconnected() and machine.reset_cause()<=machine.HARD_RESET:
        rtc = nettime.sync_rtc_to_network_time(key='myapikey')
    
Last edited by alanb on Mon Oct 26, 2015 1:32 am, edited 4 times in total.

User avatar
andrew
Posts: 22
Joined: Sun Aug 10, 2014 9:22 am

Re: Set RTC time on boot from timezonedb.com

Post by andrew » Sun Oct 25, 2015 4:21 pm

Nice!

Andrew

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Set RTC time on boot from timezonedb.com

Post by danicampora » Sun Oct 25, 2015 8:17 pm

Add something like the following to your boot.py so that your WiPy sets its RTC on every full reset:
The RTC time is kept across power saving modes, so even after a DEEPSLEEP reset it will keep the correct time.

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: Set RTC time on boot from timezonedb.com

Post by danicampora » Sun Oct 25, 2015 8:18 pm

Very nice alanb, thanks for sharing :-)

alanb
Posts: 6
Joined: Sat Oct 17, 2015 3:39 pm

Re: Set RTC time on boot from timezonedb.com

Post by alanb » Mon Oct 26, 2015 1:32 am

danicampora wrote: The RTC time is kept across power saving modes, so even after a DEEPSLEEP reset it will keep the correct time.
Thanks for the tip. I updated my boot.py to only set the RTC if the reset reason was POWER_ON or HARD_RESET.

Post Reply