However, while there are plenty of online examples that show how to configure the ESP32 to connect to the Internet and synchronize the time with a time server, most of them require you to adjust for daylight savings time manually twice a year, which seems quite ridiculous for any 21st-century electronic device.
Here we will present you with a solution that will allow the ESP32 to show the correct time, for any place in the world, year-round. But first, let’s take a look at the example SimpleTime in the Arduino IDE and see what it does and how we can improve on it.
The SimpleTime Example
If you use the Arduino IDE with the ESP32, you will find that it has an example called SimpleTime that looks like this:
You can access this example by configuring the Arduino IDE to use an ESP32 board, from the Tools menu, such as by selecting the ESP32 Dev Module. Once you have done so, you can access the SimpleTime example from the Tools menu under Examples, ESP32, Time, and Simple Time.
One might expect this official example to demonstrate the best way of doing things, but there is one huge problem in this example. To configure the time zone and daylight savings time, you have to change the following two lines:
Setting the time zone using gmtOffset_sec is inconvenient because you have to find GMT offset for your particular location. You can do this by consulting the Greenwich Mean Time website. Fortunately, you only need to do this once. The daylightOffset_sec presents a larger problem, however, because you need to adjust it twice a year. This may require you to take down your project and hook it up to the Arduino IDE twice a year, making it entirely inappropriate for an IoT project that you want to set up and forget.
Looking for a Better Approach
So what would be a better way to configure the time zone and daylight savings time? To answer this question, look no further than how your Windows PC handles the situation:
Notice how instead of having to give it time offsets from GMT, we tell Windows what our time zone and location is—in our case Mountain Time (US & Canada)—and then check the checkbox Automatically adjust clock for Daylight Saving Time. From that point on, Windows handles it automatically for us year-round. This is the kind of convenience we want from our ESP32.
Creating a Location Database for the ESP32
Doing the same thing on the ESP32 requires some legwork. we started with a blog post that described how to set up the time zone using the TZ environment variable. For our location, it happened to be MST7MDT,M3.2.0,M11.1.0, according to this time zone table. While this did solve the problem of having to manually adjust for daylight savings time, we wanted the process to be simpler, without the need to look up a string of numbers and letters for each location.
We converted the data from the website into a database you can use directly from within your Arduino sketches. Follow this link to directly download a zip file with the Arduino sketch.
The database itself is stored in the file time_zone.h. Using the database, the code for SimpleTime looks like this:
We have highlighted the modifications to the SimpleTime example in yellow. This line will allow the sketch to use the database:
This line configures your location:
There are 461 possible locations. You can find the available locations by taking a look at the time_zone.h file, or by looking at the time zone listing on this website.
Finally, the following line tells the ESP32 to synchronize the time with an Internet server using the Network Time Protocol (NTP):
The particular Internet time-server can be configured using the following line, although the default should work fine for you:
You also want to make sure to add the information for your wireless access point on the following two lines:
Replace YOUR_SSID with the name of your wireless network and YOUR_PASS with the password used to connect to it.
Finishing up
Once you have completed the configuration steps, upload the sketch to the ESP32 and it will use the correct time year-round without any intervention!
While this code gives you a good starting point, there are more user-friendly ways to accomplish the same thing. For example, you might want to allow the user to configure everything from a webpage rather than having it done at compile time. Doing so is quite a bit more work, as you would need to do the following things in your Arduino sketch:
Configure the ESP32 as a wireless access point for initial configuration Launch a web server to present the user with a configuration page Create an HTML form to allow the user to enter the wireless network information and to select their location Create the code to process the user’s choices and store it in non-volatile memory At board power-on: Read the configuration from non-volatile memory Connect to the wireless network and synchronize the time
A project that demonstrates these steps is the ESP32 Dali Clock. It was this project that led us to find a better way to do time synchronization on the ESP32.
Check out the source code for the ESP32 Dali Clock to see how to show a full-featured web configuration page. The configuration page shows a pull-down menu to allow the user to select their location and time zone and also allows for the user to set the time manually using a time and calendar widget, for a non-connected alternative!
Location-Based Network Time Synchronization on the ESP32
You now have all the information you need to be able to automatically set your ESP32’s clock to the correct time, and also adjust it for daylight savings time. This is perfect for IoT projects that you don’t want to have to adjust manually.