WiFi Kit 32 NTP Clock

About: You fail only when you decide to quit learning.

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.

Step 1: 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.

Share

    Recommendations

    • Toys Contest

      Toys Contest
    • First Time Author

      First Time Author
    • PCB Contest

      PCB Contest

    8 Discussions

    0
    None
    JensH29

    2 months ago

    sorry, I have to correct the nonsense I wrote regarding a locate() function, which doesn't exist. In fact, none of any locale.h which I found for Arduino offered a working 'setlocale (LC_TIME, "de_DE", ...)' function

    0
    None
    JensH29

    2 months ago on Step 1

    Hi Greg,

    with your code, I did my very first step into arduino programming and finally succeeded in adopting it to display with German wording for the days and months. It was a bit complicated, because the locate() function doesn't work for Arduino.

    Python's and Java's handling of string variables is much easier...

    If you're interested, just write a note to j811195 at hotmail dot com.

    0
    None
    rruggiero

    6 months ago

    This is a great instructable but, can you tell me which library the required include files are supplied with?

    1 reply
    0
    None
    gzumwaltrruggiero

    Reply 6 months ago

    The libraries have the same names as the ".h" files, thus you will need to add "time.lib", "WiFi.hlib, "WiFiUdp.lib" and "U8g2lib.lib" to your sketch. On a Mac, these libraries are located in "Documents/Arduino/libraries".

    Hope this helps!

    Greg

    0
    None
    SESupply

    7 months ago

    Works perfectly - seems like a very useful little device!

    1 reply
    0
    None
    gzumwaltSESupply

    Reply 7 months ago

    I’ve had a lot of fun using this board, good price also.

    0
    None
    enaidoo

    8 months ago

    Great. worked perfectly first time. Thanks a lot.

    1 reply
    0
    None
    gzumwaltenaidoo

    Reply 8 months ago

    Congratulations, great work!

    Greg