Set RTC time on boot from timezonedb.com
Posted: 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:
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:
- Register for an API key at timezonedb.com, which is free for non-commercial use and <1 time request/sec.
- Download nettime.py to your WiPy. (Optionally, set the default APIKEY variable in nettime.py to your API key.)
- 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')