WiFi Kit 32 NTP Clock

17,779

36

40

Introduction: WiFi Kit 32 NTP Clock

About: Our grandkids keep me busy!

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.

13 People Made This Project!

Recommendations

  • Edible Art Challenge

    Edible Art Challenge
  • 3D Printed Student Design Challenge

    3D Printed Student Design Challenge
  • Reclaimed Materials Contest

    Reclaimed Materials Contest

40 Comments

0
Slushpuppy154
Slushpuppy154

8 months 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.

0
gzumwalt
gzumwalt

Reply 8 months ago

Thank you so very much Slushpuppy154, I'm so glad you succeeded in this project!

Greg

0
gzumwalt
gzumwalt

Reply 10 months ago

Nice, thank you!

Greg

0
dnfh1949
dnfh1949

Question 1 year 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?

0
gzumwalt
gzumwalt

Answer 1 year 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

0
Johannes_Liebert
Johannes_Liebert

Question 1 year 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

20201019_105131.jpg
0
Johannes_Liebert
Johannes_Liebert

Answer 1 year 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

20201019_174429.jpg
0
gzumwalt
gzumwalt

Reply 1 year ago

Hi Johannes_Liebert!

Sorry, I've been on vacation but it appears you solved the problem, great work!

Greg

0
leomoura
leomoura

Question 1 year 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

0
Katoggen
Katoggen

1 year 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.

0
gzumwalt
gzumwalt

Reply 1 year ago

Thanks Katoggen, I'm glad you enjoyed it!

Greg

0
mrk_123new
mrk_123new

1 year ago

Hi Greg. Thanks for sharing and works great. Does your code support day light savings?
Thanks,
Mark

0
benoitmorrissette
benoitmorrissette

Question 1 year ago on Introduction

In the line : IPAddress ipNtpServer(129, 6, 15, 28);

where does the function "ipNtpServer()" comes from??? Which include file? Can't find it on my system!

0
CarlosV170
CarlosV170

Question 2 years ago

Hi, I try to check in Arduino ide but has some error in the
line 139 - WiFi.localIP().toString().toCharArray(chIp, sizeof(chIp) - 1);
error: 'class IPAddress' has no member named 'toString'
Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"
C:\Users\Familia\Downloads\wifi_kit_32_ntp_clock\wifi_kit_32_ntp_clock.ino: In function 'void setup()':
wifi_kit_32_ntp_clock:139:22: error: 'class IPAddress' has no member named 'toString'
WiFi.localIP().toString().toCharArray(chIp, sizeof(chIp) - 1);
^
C:\Users\Familia\Downloads\wifi_kit_32_ntp_clock\wifi_kit_32_ntp_clock.ino: In function 'void loop()':
wifi_kit_32_ntp_clock:243:31: error: variable 'loop()::timeval tvTimeValue' has initializer but incomplete type
struct timeval tvTimeValue = {0, 0};
^
wifi_kit_32_ntp_clock:255:47: error: 'settimeofday' was not declared in this scope
settimeofday(& tvTimeValue, NULL);
^
wifi_kit_32_ntp_clock:271:20: error: 'class WiFiClass' has no member named 'mode'
WiFi.mode(WIFI_OFF);
^
wifi_kit_32_ntp_clock:271:25: error: 'WIFI_OFF' was not declared in this scope
WiFi.mode(WIFI_OFF);
^
wifi_kit_32_ntp_clock:288:23: error: aggregate 'loop()::timeval tvTimeValue' has incomplete type and cannot be defined
struct timeval tvTimeValue;
^
wifi_kit_32_ntp_clock:289:39: error: 'gettimeofday' was not declared in this scope
gettimeofday(& tvTimeValue, NULL);
^
Multiple libraries were found for "U8g2lib.h"
Used: D:\OneDrive\Documents\Arduino\libraries\U8g2
Not used: D:\OneDrive\Documents\Arduino\libraries\U8g2-2.26.14
exit status 1
'class IPAddress' has no member named 'toString'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

0
gzumwalt
gzumwalt

Answer 2 years ago

I'm sorry for the difficulties, it appears as if a library is missing somewhere, but I cannot determine which from the report you provided.

Greg

0
WikiJM
WikiJM

Question 2 years ago

Hi,

Thanks for those instructions :-)

I encounters few issues during the compilation and injection.
At the end of the compilation, I get this error: "variable 'loop()::timeval tvTimeValue' has initializer but incomplete type".

Here is the complete error log:
https://gist.github.com/wikijm/3bda4dfe18dd4bcfc46...

Could you please give me a track or some clues?

Sincerely,
WikiJM

0
dsSidhu
dsSidhu

2 years ago

Nice example, no hassles ,no file found message,no debug.
oly thing time lap is 2hrs late, how to set it