ESP32: WIFI Basics and sync to NTP clock (WiFi Part 1) - Arduino

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ส.ค. 2021
  • 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-thunderz/Esp32W...
    For the arduino WiFi library documentation refer to:
    www.arduino.cc/en/Reference/WiFi
    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.

ความคิดเห็น • 45

  • @donk73
    @donk73 ปีที่แล้ว +1

    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.

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      Great to hear! Good luck with your project

  • @andrevanvlaanderen6666
    @andrevanvlaanderen6666 2 ปีที่แล้ว

    Thanks a lot.
    So usefull for a esp32 beginner.
    Looking forward for the rest of this serie…

    • @mothunderz
      @mothunderz  2 ปีที่แล้ว +2

      Glad to hear that, now working on part 2. Hope to get that online next week...

  • @jonborys8157
    @jonborys8157 5 หลายเดือนก่อน

    Thank for your series on ESP32 WiFi! One of the best.

    • @mothunderz
      @mothunderz  3 หลายเดือนก่อน

      Glad you enjoy it!

  • @Mystical-TEDDY_
    @Mystical-TEDDY_ ปีที่แล้ว +1

    OMG this was such a good tutorial and very helpful in my progress towards my project

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      Glad it was helpful!

  • @vladimirastrelin1719
    @vladimirastrelin1719 3 หลายเดือนก่อน

    Thank you! Your tutorial is just amazing, very clear and thorough.

    • @mothunderz
      @mothunderz  หลายเดือนก่อน

      Unfortunately I have not looked into Indicate yet

  • @sherryyaninadelgadoorrillo8311
    @sherryyaninadelgadoorrillo8311 2 ปีที่แล้ว +2

    Thanks for share your knowledge :)

  • @MrSikkandar
    @MrSikkandar 2 ปีที่แล้ว

    Thanks a lot for this

  • @johnburgess2084
    @johnburgess2084 ปีที่แล้ว

    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

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      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

  • @user-mr3mf8lo7y
    @user-mr3mf8lo7y หลายเดือนก่อน

    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,.

  • @t1d100
    @t1d100 หลายเดือนก่อน

    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!

  • @Blink-ut7uh
    @Blink-ut7uh 2 ปีที่แล้ว

    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?

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      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.

  • @ninnicuringa
    @ninnicuringa 2 ปีที่แล้ว

    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.

    • @mothunderz
      @mothunderz  2 ปีที่แล้ว +1

      Yes, the gethours() function will return an int, so you can use that to compare to other values.

    • @ninnicuringa
      @ninnicuringa 2 ปีที่แล้ว +1

      @@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?

    • @mothunderz
      @mothunderz  2 ปีที่แล้ว

      @@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!

  • @jim5148
    @jim5148 ปีที่แล้ว +1

    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?

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      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.

  • @balaw1980
    @balaw1980 ปีที่แล้ว

    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?

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      Thank you for your feedback. Are you referring to the ESP32WifiShowNetworks.ino file or to the ESP32WifiNtp.ino file?

    • @donk73
      @donk73 ปีที่แล้ว

      @@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?

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      @@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.

  • @vancefd7399
    @vancefd7399 2 ปีที่แล้ว

    First comment, ur student here sir 🔥

  • @digihz_data
    @digihz_data 2 ปีที่แล้ว +2

    Note:Don't request to often from an ntp server, you can be blacklisted.

    • @mothunderz
      @mothunderz  2 ปีที่แล้ว

      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.

  • @PedroGonzalez-jj2dk
    @PedroGonzalez-jj2dk 2 ปีที่แล้ว +1

    Is it posible to also request the date? I have found some codes that do it but didnt work for me.

    • @mothunderz
      @mothunderz  2 ปีที่แล้ว +2

      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.

  • @Janed0e.34
    @Janed0e.34 ปีที่แล้ว

    may i know does this two file of ntp and wifi need to be uploaded to the board or not?

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      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

  • @sureshnaidu1376
    @sureshnaidu1376 2 ปีที่แล้ว

    How to make alarm through esp32 and ntp

    • @mothunderz
      @mothunderz  2 ปีที่แล้ว

      My apologies, but I do not fully understand what you want to achieve.

  • @nfcradio4622
    @nfcradio4622 ปีที่แล้ว

    why not to use mdns ?

    • @mothunderz
      @mothunderz  ปีที่แล้ว

      Brilliant idea, will look into it

  • @jimsnodgrass8454
    @jimsnodgrass8454 11 หลายเดือนก่อน

    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

    • @mothunderz
      @mothunderz  11 หลายเดือนก่อน

      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!