Introduction: 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.
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.
Attachments
16 People Made This Project!
- gexall made it!
- csierra67 made it!
- DonH119 made it!
- leomoura made it!
- Slushpuppy154 made it!
- chrvoigt made it!
- Vicenteroca made it!
- meki01 made it!
- Fred_Flintstone made it!
See 7 More
48 Comments
2 years ago on Step 1
This is the same code with DST support for world's time zones.
https://github.com/banzy/ArduinoCode/tree/main/NTP_Clock
Reply 3 months ago
Great project! Not sure what's happening with mine, though, but it seems that the time is coming through with an odd hour. I did change the TIME_ZONE at the top to -5 (New York; EST) and then changed the zone code in the loop() in the if refresh||start test. Ultimately fixed it by adding code from another project which sets up the timezone early in the process. Also changed the display to 24-hr since there's no "AM/PM" indicator. Works well; revised code below. Thanks for a great project!
https://github.com/RichCini/NTP-for-ESP32
Reply 3 months ago
Cool, I will check your additions. Cheers.
Reply 2 years ago
Nice, thank you!
Greg
1 year ago
Hi,
This is a great project and I always wanted to do this. I have two Heltech WiFi Lora V2, but I am
getting Warning/errors. I get a warning
during compiling, but it finishes with a Hard reset, please see below. Nothing comes on the screen either. Should I change anything?
--------------------------------------------
libraries\U8g2\src\clib\mui_u8g2.c: In function 'mui_u8g2_u8_vmm_draw_wm_pi':
\libraries\U8g2\src\clib\mui_u8g2.c:560:13: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
char *s = "999";
^~~~~
--------------------------------------------
Hash of data verified.
Compressed 3072 bytes to 129...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (129 compressed) at 0x00008000 in 0.1 seconds (effective 390.2 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
-----------------------------------------------
Thanks in advance.
Givi
1 year ago
Great tutorial. I like this board, too. One thing I would like to do is implement a battery status indicator between the date and the time lines on the display. In the examples for this board, there is one named 'Battery Power'. The code is there, I just need to incorporate it into your code somehow. I've tried that with other projects and just haven't had much luck.
Reply 1 year ago
Thank you very much! Best wishes!
Greg
1 year ago on Step 1
Managed to download the file by right clicking and then selecting "Save Link As", and then moving the file into the Arduino folder. Then added the U8g2 library to the Arduino IDE. Connecting to the Wifi_Kit_32 required installing SiLabs CP210x USB driver. Once all this was done, and the sketch was edited with SSID & PW credentials, and the time zone was altered, the app installed without any problems and runs as described.
Reply 1 year ago
Hi streborx47,
I'm sorry for the difficulties you had with this model, and am sorry for the delayed response as I've been ill.
Anyway, it appears as if you were successful in completing the model, congratulations!
Greg
Question 1 year ago on Step 1
When I click on the .ino file to download it, the file tries to open in Acrobat instead of downloading, and Acrobat says it can't open the *.ino format(?)
2 years ago
Great code! Compiled first try with no issues and ran beautifully. Thank you for putting this together so us new programmers can learn from what you did.
Reply 2 years ago
Thank you so very much Slushpuppy154, I'm so glad you succeeded in this project!
Greg
Question 2 years ago on Step 1
Apologies, a rather Newbie question!
I don't know where to download the library or .ino file to!
'Manage Libraries' seems to put them into:
/Users/dnfharris/Documents/Arduino/libraries/***
but that doesn't seem to be the correct path (or I haven't set the correct path). Some .ino files seem to work better from
/Users/dnfharris/Documents/Arduino/***
I get many messages saying (eg for a U8g2lib library)
"U8g2lib.h: No such file or directory
Invalid library found in /Users/dnfharris/Documents/Arduino/libraries/U8g2: no headers files (.h) found in /Users/dnfharris/Documents/Arduino/libraries/U8g2"
Is there a guide anywhere as to how to set paths or where I should put new libraries?
Answer 2 years ago
Hi dnfh1949,
There are a number GitHub and Arduino tutorials on setting up your computer environment for this project, and they differ greatly depending on whether you are using Windows, MacOS, etc.
Greg
Question 2 years ago
Super project. i have a question:the sieriell print from npt clock is the time in 24 hour format at the OLED display s the 12 hour format. is this RTC specific or can this change, if so at wich point
Answer 2 years ago
I read the post from Trev_A and did the same change:
"strftime(chBuffer, sizeof(chBuffer),
"%I:%M:%S", tmPointer);" to
"strftime(chBuffer, sizeof(chBuffer),
"%H:%M:%S", tmPointer);"
it works fine now with 24 hour display format
Reply 2 years ago
Hi Johannes_Liebert!
Sorry, I've been on vacation but it appears you solved the problem, great work!
Greg
Question 2 years ago on Introduction
I've tried to show at same time 2 timezones (timezone -3 and +2 ) but not sucess
i've tried to call :
void loop ()
function1
function2
(TIME_ZONE * 3600UL) and (TIME_ZONE_2 * 3600UL)
i'd like to refresh the display and show something like:
Country one, time is ....
Country two, time is ....
thanks in advance and congratulations , amazing project
2 years ago
Great project! I'm only just dipping my virtual toes into ESP32 land and this is very helpful to get over that "where the heck do I even start with this thing?" hump.
Reply 2 years ago
Thanks Katoggen, I'm glad you enjoyed it!
Greg