ESP32: WIFI Basics and sync to NTP clock (WiFi Part 1) - Arduino
Вставка
- Опубліковано 10 вер 2024
- In this series I will discuss the WiFi capabilities of the ESP32. Part 1 covers the WiFi basics and shows how to sync to an NTP clock. I used the ESP32-WROOM-32D for the demonstration.
Note: at 3:55 I just noticed that I forgot to explain the connectToNetwork() function. However, I cover that code basically in the second example at 7:33.
For the code refer to:
github.com/mo-...
For the arduino WiFi library documentation refer to:
www.arduino.cc...
Might you have problems programming the ESP32, pls refer to:
• Programming ESP32: how...
Part 2 covers how to use the ESP32 as a webserver with websockets and JSON for communication between client and server:
• ESP32: Webserver, Webs...
Correction:
08:04 When timeClient.update() is called it reaches out to the NTP server to get the latest time. Please move timeClient.update() to the last line of the setup instead so that the NTP server is pinged only once.
Thanks for a great video. I'm new to the ESP32 and tried a couple of other tutorials. Yours is the first one I got to run "right out of the box". I look forward your other ESP32 tutorials.
Great to hear! Good luck with your project
Thank for your series on ESP32 WiFi! One of the best.
Glad you enjoy it!
OMG this was such a good tutorial and very helpful in my progress towards my project
Glad it was helpful!
Thanks for share your knowledge :)
My pleasure!
Thanks a lot.
So usefull for a esp32 beginner.
Looking forward for the rest of this serie…
Glad to hear that, now working on part 2. Hope to get that online next week...
Quick question about Wi-fi stack. In SETUP section, we do settings of Wi-Fi- etc. What keeps wi-fi alive ? the library or ESP firmware in the background? When wi-fi dropped (from router) which one of those try to reconnect? SHould we worry about it as a programmer of library/firware takes care of all? Thanks,.
Just asking- is it possible to pick the time info only from any network or wifi nearby, without log in?
very interesting video, Thanks
I would like to synchronize my Ds3231 RTC every day using the ntp: How could I do that please ?
Thanks for all
regards
Thank you for sharing this! I am a noob to Arduino Land. Will your code run on an UNO R4? If so, please provide the details of how to set it up. I would like for it to display on the R4 LED matrix. Thank you, in advance!
I wish I caught this video a year ago! In case you see this post, I have a question. The "timeClient.update()" function just gets the LOCAL current time, and does not access the NTP server, is that right?
Excellent question! The function timeClient.update() does actually reach out to the NTP server to get the latest time. For demo purposes I called this function in the main loop in the video, but in practise you dont want to do that. I will add a comment to the video to call this out. Please see the updated code on my Github page where I moved the timeClient.update() function to the setup. As you will see, the local time does update automatically. However, the clock of the ESP/Arduino is not infinitely accurate, so if the exact time is important, please re-sync so once in a while.
Note:Don't request to often from an ntp server, you can be blacklisted.
Hi Hans, yes, that is a very valid point! In my example I have a code that reads out the time every second. Normally one would indeed read the time once and then track it locally. Thanks for pointing this out. Sander.
Is it posible to also request the date? I have found some codes that do it but didnt work for me.
Hi Pedro, yes, it is possible to get the date as well. This is done using the timeClient.getEpochTime() function. I have added an update on Github with date included. Pls let me know if this works for you.
Thanks a lot for this
My pleasure :-)
First comment, ur student here sir 🔥
Thanks Vance!
Thanks for this tutorial! Do you have any videos about the use of the ESP32 real-time clock? If TimeClient could (or does) use the RTC, then the time could be kept reasonably accurate in the absence of a network, or in low-power mode. This is a good starting point, using NTP initially, then set the RTC with the time value. Does timeClient make use of the RTC? Or does it do an NTP query each second to update the time? Thanks, John
Hi John, yes, you can definitely use RTC for keeping track of the time and only sync periodically with an NTP (once a day for instance). In fact, the code I show in the video here where the NTP is constantly queried really is meant for demo only as NTP servers dont like it if you query them constantly over a longer time. Here an example of such a code:
www.deviceplus.com/arduino/lets-make-a-wake-up-alarm-clock-with-esp32-and-rtc/
Good luck with your project!
Sander
thank you, just a little question: what kind of data is the value given from function? the days, hours, minutes and seconds: are they String or char array or int? i need hours in int because i have to compare with other values, without overtake RAM of ESP. i dont like to use String due to ram problems.
Yes, the gethours() function will return an int, so you can use that to compare to other values.
@@mothunderz Thank you for fast answer, i have another silly question: everytime it runs the Loop, it calls timeClient.update, that means that it asks to the server the time. So probably after some hours running Loop, server will kick you out because of too much queries! am i right?
I d like something that asks to ntp server once in Setup() and then store the time into a internal clock, so i will have correct date and time everytime i need without asking to the server again, even if in the meanwhile wifi connection will be offline. does exist something like i need?
@@ninnicuringa Yes, indeed this demo code is just meant as demonstration how to retrieve the time, but not meant to be used as clock. There are many ways to implement what you would like to do. I recommend to use the following library:
#include
you can then initiate the time class with:
ESP32Time rtc;
and in the setup you can set the time with:
rtc.setTime(seconds, minutes, hours, days, months, years);
retrieving the time can be done with a range of get functions, for instance:
rtc.getHours()
rtc.getMinutes()
etc.
Good luck with your project!
For the 2 dimensional daysoftheweek character array, is there any reason that you used 12 for the size of the elements? Was it completely arbitrary?
Well, it needs to be long enough to store all the weekdays. "Wednesday" is the longest word and that is 9 char long. That is a bit of an arbitrary number, so I used 12 instead.
may i know does this two file of ntp and wifi need to be uploaded to the board or not?
My apologies, but I dont really know what you mean. In this video I discuss two different programs. These programs can be loaded onto an ESP32 with the Arduino IDE. The two programs are
ESP32WifiShowNetworks.ino -> shows the different wifi networks available
ESP32WifiNtp.ino -> syncs the Arduino to an NTP clock
Thanks for the clear simple tutorial. I was confused as to why I did not have to load up a WiFiUdp library. I did not, and it worked fine, but was it that the WiFiUdp lirary was already there?
Thank you for your feedback. Are you referring to the ESP32WifiShowNetworks.ino file or to the ESP32WifiNtp.ino file?
@@mothunderz I think Bruce is referring to the ESP32WifiNtp.ino file, at least, I was wondering the same thing. Is the WiFiUdp library called by one of the other libraries?
@@donk73 Thank you for the explanation. This is an excellent point. I looked into this a bit and indeed if you compile for the ESP32 then the WiFiUdp library is not needed. However, if you use the code for other Arduino Platforms (like for instance the Arduino Mega), then WiFiUdp.h is required or it wont compile. My guess is that WiFiUdp.h is included in the standard ESP32 library set.
How to make alarm through esp32 and ntp
My apologies, but I do not fully understand what you want to achieve.
why not to use mdns ?
Brilliant idea, will look into it
Hi There , I got it working on a XAio ESP32 C3 but wasnt sure why the NTPClient.h ( which apprears in the library to be updated now to "NTPClient_Generic" by the same author would not install .. but the code loaded and ran anyway and is giving me the correct time with the offset I need for San Diego CA. I think it may be using another NTP client lib I loaded a few days ago perhaps.. the compiler says this was used:
ResolveLibrary(NTPClient.h)
-> candidates: [NTPClient@3.2.1] ayway I'm not going to argue with sucess.. Thanks for the thorough step by step descriprions
Interesting that it worked this way. It could be as well that the NTPClient was included in the general ESP package in the meantime. Glad it works!