I'm using an ESP32 to make a clock, so I need a way to get the current time and keep track of daylights savings, leap year etc. I'll have the ESP connected to wifi, so I could fetch the time from NTP somehow (side note: not sure if I can use this library or if mpython is different than micropython). I don't know what to do with that time though since it's just time since epoch.
I believe the ESP32 has an RTC and I see machine.RTC. As far as timezone, do I just need a way to prompt the user for tzinfo? I can do that by setting up a simple web server.
I think I'm dancing around a solution but don't really know how to fit it all together. Thanks for the help!
Help setting the time
-
- Posts: 9
- Joined: Fri Oct 08, 2021 5:37 pm
Re: Help setting the time
For me, this works on ESP8266 so I assume it also works on ESP32 .. let me know if it does.
Using esp8266-20210902-v1.17.bin
As far as I understand, there is not timezone support (yet). But you get UTC and just add or substract your hours, as a quick fix.
Using esp8266-20210902-v1.17.bin
Code: Select all
import ntptime
import time
NTP_SERVER = '0.ch.pool.ntp.org'
ntptime.host = NTP_SERVER
try:
print('Local time before synchronization:%s' % str(time.localtime()))
ntptime.settime() # gets time in UTC
print('Local time after synchronization:%s' % str(time.localtime()))
except:
print('Time synchronization ERROR')
pass
Code: Select all
TZ_OFFSET = 2
print('%s:%s' % (time.localtime()[3] + TZ_OFFSET, time.localtime()[4]))