🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    MicroPython Clock Task with RTC and NTP sync

    Scheduled Pinned Locked Moved PROJECTS
    6 Posts 5 Posters 26.5k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      M5MPy
      last edited by

      Just new to MicroPhyton, I just collected some code pieces in the web to get a "Clock" background task coded with RTC and NTP Sync by just some code lines.
      BTW. No flickering on the display etc.

      Please feel free to comment; need to learn ;-)

      # RTC Clock with NTP Sync for M5STACK
      # References:
      # https://forum.micropython.org/viewtopic.php?t=4329
      # Lobo Version to init RTC and timezone
      # https://forum.micropython.org/viewtopic.php?f=18&t=3553&p=21616&hilit=timezone#p21616
      from m5stack import machine
      from m5stack import lcd
      from time import strftime
      import utime
      import _thread
      
      # move to lowest line on M5STACK display
      lcd.setCursor(0, 227)
      lcd.setColor(lcd.WHITE)
      lcd.print("RTC Clock 1")
      
      # initiate rtc
      rtc = machine.RTC()
      print("Synchronize time from NTP server with TZ=Germany ...")
      rtc.ntp_sync(server="hr.pool.ntp.org", tz="CET-1CEST,M3.5.0,M10.5.0/3")
      
      def watch():
          while True:
              # start position for Date
              if not rtc.synced():                                                            # set color to sync status    
                  lcd.setColor(lcd.RED)
              else: 
                  lcd.setColor(lcd.GREEN)
      #       lcd.setCursor(92, 227)                                                          # uncomment if you need date on display
      #       lcd.print("Date {}".format(utime.strftime("%Y-%m-%d", utime.localtime())))      # uncomment if needed
              # start position for time only
              lcd.setCursor(213, 227)                                                         # uncomment if date active (see upper lines)
              lcd.print(" Time {}".format(utime.strftime('%H:%M:%S', utime.localtime())))
              utime.sleep(1)
      
      _thread.start_new_thread ("clock",watch, ())
      
      1 Reply Last reply Reply Quote 5
      • I Offline
        ItHasU
        last edited by

        Thanks for your example. Where did you found the tz argument ? I cannot find any exhausitive documentation on the function.

        1 Reply Last reply Reply Quote 0
        • S Offline
          simm
          last edited by simm

          @ithasu,

          check this link uclibc timezone list

          automation

          1 Reply Last reply Reply Quote 0
          • M Offline
            M5MPy
            last edited by

            yes, already answered, thanks to @simm

            1 Reply Last reply Reply Quote 0
            • f3rn4nd0dF Offline
              f3rn4nd0d
              last edited by f3rn4nd0d

              Just a little comment. I've used this code to have a tiny watch on a M5stick and it did not work till I added the connection to internet. So the code in my case looks like this:

              import wifisetup
              wifisetup.auto_connect()
              from m5stack import *
              from m5ui import *
              ...

              and then the rest of the code is similar, changing timezone and whatnot.

              Thanks for the code and best regards / Fernando

              K 1 Reply Last reply Reply Quote 0
              • K Offline
                kalon33 @f3rn4nd0d
                last edited by

                @f3rn4nd0d I got "no module named 'wifisetup'" error, and without that, no 'strftime' in time... What am I missing in Mycropython for my M5Stack Gray?

                Thanks for your help.

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • First post
                  Last post