Search code examples
micropythonesp32

ESP32 - using MicroPython: How to update clock after reboot?


I have an ESP32 board, with Micropython installed properly, and connecting to local the internet.

Since it have to do a scheduled jobs- its clock need to be synced. After boot it get a generic date 1/1/2000.

How can Micropython updates int clock using ntp.pool.org or other?

Guy


Solution

  • From this post on the Micropython forum:

    There is primitive NTP support in the ESP32 port (borrowed from the ESP8266 port). In short:

    import ntptime 
    ntptime.settime()  # Synchronise the system time using NTP
    

    Caveat: There is no timezone support so the system time will be set to UTC.

    From this discussion, you need to make sure you have an active internet connection before trying to set the time, and you should expect to get occasional timeout errors and handle these appropriately.

    For reference the source of the ntptime module is here