Skip to content

uPesy ships directly only in France !

uPesy products available on Amazon for EU

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Connaître l’heure exacte depuis ESP32 avec Internet en MicroPython

(Updated at 01/23/2023)

And yes, the ESP32 can retrieve the exact time and date with just an Internet connection. As soon as the ESP32 is completely turned off, it loses track of time. It is like an oven or microwave clock that displays 00:00 when there was a power failure. The most classical way is then to use an external RTC with a battery permanently connected

Note

This is typically what he had in the old computer towers to maintain the BIOS clock with a CMOS battery.

But this requires additional equipment, more space and costs more.

Synchronizing the clock is very useful when the ESP32 wakes up from time to time to communicate with WEB services, and wants to date its packets with the right date. It can also be useful to have an alarm clock that executes a task at a specific time.

Note

An external RTC clock may be useful if the ESP32 does not have an Internet connection. It is possible to use an NTP server to adjust the RTC clock and synchronize it with the real time 😉.

NTP server: What is it for?

Time synchronization between computers

NTP (Network Time Protocol) servers are designed to synchronize the clocks of computers on a computer network. Their purpose is to ensure that all computers on the network share the same time, which is important for various applications, such as file management or computer security. It is also to avoid errors due to a lack of synchronization of clocks.

If you want to learn more about the NTP protocol and its architecture, I recommend this excellent video from Computerphile .

Note

In summary, the NTP protocol allows the ESP32 to retrieve the current date with great accuracy.

The ESP32 will need to rely on reliable NTP servers that provide accurate time around the world. Many of these servers use signals from GPS satellites to get very accurate time measurements. The most popular is “pool.ntp.org “.

The representation of time in computing: timestamp, epoch time and Unix?

For computers, a date in text form is not very convenient to understand, especially since the format varies from country to country. That’s why they use timestamps . A timestamp specifies the exact date and time of an event and is presented as a sequence of numbers. Of course, this timestamp can be re-formatted into a date that is more understandable to humans.

Unix time (Epoch Time) is the amount of seconds that have elapsed since midnight on January 1, 1970. This reference is often used as a starting point to determine the date and time of an event using a timestamp

A timestamp equal to 1601054743 means that 1601054743s have elapsed since January 1st 1970. In text format, the timestamp is equivalent to Tuesday, December 13, 2022 at 10h12m25s

Take into account the time difference (UTC, GMT)

To know the local time in a given country, it is necessary to take into account the time difference that applies there. The reference time is UTC or GMT, which is equivalent to a time difference of +0. This difference is then adjusted according to the local time zone. In France, the time difference is +1 in winter and +2 in summer.

Get the date from an Internet (NTP server) with an ESP32 in MicroPython

MicroPython includes the basic ntptime who takes care of everything!

import network
import time
import socket
import ntptime
from machine import RTC

ssid = 'upesy-ap'
password = 'cupcakes'

if __name__=="__main__":
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)

    while wlan.isconnected() == False:
        print('.', end = " ")
        time.sleep_ms(500)

    print("Wi-Fi Config: ", wlan.ifconfig())

    ntptime.settime()
    datetime = time 
    
      

This section is available to premium members only. You still have 64% to discover.

Subscribe for only 5$/month

Already subscribed? Sign in