WiFi Kit 32 NTP Clock

12,049

29

36

Introduction: WiFi Kit 32 NTP Clock

About: Hoping to win more Instructables T-Shirts!

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.

8 People Made This Project!

Recommendations

  • Battery Powered Contest

    Battery Powered Contest
  • Plywood Challenge

    Plywood Challenge
  • Plastic Contest

    Plastic Contest

36 Discussions

0
dnfh1949
dnfh1949

Question 8 days 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
Johannes_Liebert
Johannes_Liebert

Question 6 weeks 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 5 weeks 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 5 weeks ago

Hi Johannes_Liebert!

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

Greg

0
leomoura
leomoura

Question 8 weeks 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

2 months 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 2 months ago

Thanks Katoggen, I'm glad you enjoyed it!

Greg

0
mrk_123new
mrk_123new

6 months ago

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

0
benoitmorrissette
benoitmorrissette

Question 7 months 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!

1
CarlosV170
CarlosV170

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

Sooo, Oklahoma is a state? When the heck did that happen?

1
gzumwalt
gzumwalt

Reply 1 year ago

Ahhhh, living rent free in yet another cavernous cranium...

0
WikiJM
WikiJM

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

1 year ago

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

1
robiv8
robiv8

Question 1 year ago

One more question!
I live in Germany, can one change the date to German language?
I mean the Tue in Di ore Sun in So

0
gzumwalt
gzumwalt

Answer 1 year ago

hi daisyd15,

The answer provided by Gruntfuttock sounds like a great start!

You could also use two arrays, one for the day of the week, and a second for the month, to contain the text, for example:

char* DayOfWeek[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

Then display the day of the week as:

sprintf(chBuffer, "%s", DayOfWeek[n]);

Where "n" is the day of the week 0, through 6.

Best wishes!

Greg
0
Gruntfuttock
Gruntfuttock

Answer 1 year ago

To see how the date string is generated, take a look at http://www.cplusplus.com/reference/ctime/strftime/

You can see that the sketch uses this line:
strftime(chBuffer, sizeof(chBuffer), "%a, %d %b %Y", tmPointer);
to assemble a string with the date in the format DAY, dd MMM yyyy

You want to change the values substituted for %a and %b to their German equivalents, but I don't think strftime() knows how to do that (it can substitute locale-specific values for some of the placeholders, but not, I think, %a or %b). I think you will have to put in a look-up table, perhaps using switch...case or an array holding the day and month abbreviations.

You might also want to change the time format to 24-hr: try changing
strftime(chBuffer, sizeof(chBuffer), "%I:%M:%S", tmPointer);
to
strftime(chBuffer, sizeof(chBuffer), "%H:%M:%S", tmPointer);

0
robiv8
robiv8

Reply 1 year ago

Thank you Sir, i will try it and reply.