Sync time in MicroPython - Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 มิ.ย. 2024
  • Learn how to use the 'ntptime' library on the to synchronize your ESP32's date and time with an NTP server! No real-time clock or other external hardware is needed.
    Written version of this tutorial: bhave.sh/micropython-ntp/
    Awesome Micropython tutorials: bhave.sh
    Banner design and video by the amazing Natalie Plociennik: natalie.gallery
    0:00 Intro
    0:28 Why?
    1:01 How?
    2:01 Implementation
    3:22 Adjusting for your timezone
    5:04 Join for more Micropython!

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

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

    I finally found an understandable channel .. waiting for the next video!

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

    Do you remember wind-up clocks? Having to check the time on the clock ticking away on your bedside table every time you happened to hear the radio announce the time so you could adjust it? Of course after the apocalypse, there will be no internet so wind up clocks will be in demand again and everyone will have to re-learn how to read an analog clock. Until then I will incorporate your simple time example in most of my esp8266 or esp32 projects. Thank you!

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

    Very informative and easy to understand.Have looked at heaps of other videos on this subject and yours was the easiest to understand,well done.

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

    all your videos are really interesting and I love learning about micro python, this channels underrated for how much you can learn from it.

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

    👏👏

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

    👍👍

  • @user-dz5wg9zk8e
    @user-dz5wg9zk8e ปีที่แล้ว

    Hi Bhavesh,
    many thanks for this super video.
    I have one problem/question:
    I'm living here in germany and my problem is the switching between winter (UTC_DELAY = 1 h) and summer (UTC_DELAY = 2h) - time
    Any idea how to achieve this AUTOMATICALY!

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

      Great question! You can add some simple logic to check the date and see if it's before or after the last Sunday of the month in March/October.
      Alternatively, it may be easier to just use this API to determine if daylight savings is "true" or "false" at the current time, and use that to adjust your UTC_DELAY: worldtimeapi.org/
      For the 2nd option, please see my short video on how to make web requests with Micropython (the "Joke API" video)
      Hope this helps! Good luck 😀

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

    DAMN! explained a bit advance topic very easily and shortly😅

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

    Used a ESP8266 with MU editor, it works like a charm. However, when I use a ESP32 with Thonny editor I can not get the ntptime.settime() to work, Strange. Thanks for your tutorial

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

      I was using Micropython V1.14, had to upgrade to V1.18. Now the esp32 works also

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

    Which NTP server will it ask? Can I set a different one? Thanks for your videos!

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

      Hi Andreas, it asks "pool.ntp.org" as you can see in the source code, assigned to the "host" variable: (github.com/micropython/micropython-lib/blob/master/micropython/net/ntptime/ntptime.py )
      If you need to set it to a different server, maybe your best bet is to just copy the simple source code above and change the "host" variable to the one you want

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

      @@BhaveshKakwani Oh wow many thanks I didn't know where the sourcecode is.. I'll make something like a pull request to make it configurable, sometimes one must use a local one.

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

      @@AndreasDelleske That would be great! Lemme know if you need help with it. Should just be a simple optional parameter (with default value of pool.ntp.org) to the function, I think

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

      @@BhaveshKakwani I will: Let''s hope for the best..

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

    I have this error
    ModuleNotFoundError: No module named 'ntptime'
    And I have tryed to do pip install ntptime but can't

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

      Hi Rayos! Are you running on an ESP32? Which version of MicroPython are you using?

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

      @@BhaveshKakwani ESP32? i don'0t know it, I run it from windows.
      OH micro python is not python? :S

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

      Yes, “Micropython” is different and it’s made specifically for electronics/hardware projects. Similar to Arduino, if you are familiar with that.
      If you want to access NTP servers in normal Python, you should use the ntplib library: github.com/cf-natali/ntplib

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

    How do I make NTP work in pi pico w?

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

      Hi Freddie, looks like the "ntptime" library doesn't exist for pi pico W. But not to worry! You can do it semi-manually using sockets. There's a great example code here you can use: gist.github.com/aallan/581ecf4dc92cd53e3a415b7c33a1147c

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

    how do i get the hour minute and second separately

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

      Great question! Since the time is output as a tuple, you can treat it like any normal tuple or list in Python:
      hour = actual_time[3]
      minute = actual_time[4]
      second = actual_time[5]

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

      @@BhaveshKakwani if you don't mind me asking is their a way to get the time in a 12 hour format

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

      @@ihsanshafi1047 Yes totally. Write a simple "if" condition that does this:
      - If the hours is greater than 12, subtract 12 to convert into 12-hr format and add "PM" to it.
      - If the hours is less than 12, just add "AM" to it
      - If the hours is exactly 12, then it is 12 PM
      - If the hours is exactly 0, then it is 12 AM
      For example: 21 is greater than 12, so subtract 12 to get "9 PM"
      Another example: 6 is less than 12, so it is "6 AM"