WiFi Kit 32 NTP Clock

by gzumwalt in Circuits > Software

28160 Views, 46 Favorites, 0 Comments

WiFi Kit 32 NTP Clock

IMG_9733.JPG
Wifi Kit 32 NTP Clock

WiFi Kit 32 NTP Clock is another of my learning forays into the numerous features of the ESP32.

During startup, the clock obtains the current date and time from an ntp server, adjusts for time zones (see the Software step), writes the result to the ESP32 rtc (real time clock), then uses the ESP32 rtc to update the built in OLED display with the date and time.

The source code is included in the Software step and should be documented well enough to explain how it works. If you have any questions, please feel free to comment and I will do my best to answer them.

In the interest of full disclosure, I receive no samples or compensation for the use of this board.

Software.

The software requires the include files "time.h", "WiFi.h", "WiFiUdp.h" and "U8g2lib.h", and as such these libraries must be included in the Arduino environment.

The software connects to an ntp time server via wifi and a udp port, obtains the ntp time from the server, adjusts then writes the time to the ESP32 rtc (real time clock), then displays the date and time on the built in OLED display.

Upon startup, the code initializes the serial port, wifi, graphics and udp port. The serial port is used during initialization to display when the wifi is connected and when the ntp time has been received from the ntp server. Wifi is used to communicate with the ntp server. The graphics is used to display the initialization and operational displays on the built in OLED. Finally, the udp port receives the ntp time from the ntp server.

The main loop performs two major functions; obtains the time from the ntp server and to update the oled. The time is obtained from the ntp server only once, and upon receipt, is adjusted for time zone then written into the ESP32 rtc (real time clock), after which wifi is turned off. The OLED is updated once per pass, and there is a 200ms delay in the main loop so the OLED is updated 5 times a second.

Before compiling and downloading the code, adjust the following settings:

1) TIME_ZONE - currently set to -6 for Oklahoma (my home state), adjust to your timezone.

2) chPassword - currently set to "YourWifiPassword", adjust to your wifi password.

3) chSSID - currently set to "YourWifiSsid", adjust to your wifi ssid.

Next, connect the board to a USB port on your computer via a USB to micro USB cable, then compile and download the software. While awaiting the download, start the serial monitor. When the download is complete, the software will send status messages to the serial monitor as shown in the video and, if successful, the OLED will display the date and time.