Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    [Solved] Trying to run command exactly at :00, :15, :30 and :45 (every 15mins)

    Scheduled Pinned Locked Moved Projects
    pysensetimewipy2.0question
    9 Posts 3 Posters 2.1k 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.
    • B Offline
      bmueller77
      last edited by bmueller77

      Hi There,

      for my first WiPy-project i'd like to send a MQTT Message (temperature, etc.) from my WiPy 2.0 with Pisense-Board at exactly :00, :15, :30 and :45 to prevent time offsets over time. Actually my idea is, to run my main script, which connects to Wifi, collects data from sensors which actually takes 30-40 seconds. Time differs especially at Wifi reconnect. So i want to do a deepsleep for 13 minutes, then wait until full, half or quarter hours are reached to go on with the script but i do not have a clue, how to script this. Thanks in advance for your help.

      Best Regards

      bmueller

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @bmueller77
        last edited by

        @bmueller77 before addressing your question, a remark: if your WiPy is awake 1 or 2 minutes out of 15, you are going to have limited battery life. Not even counting your sensors, your average current is going to be somewhere between 3 and 7 mA, which would drain a relatively large 2500 mAh battery in about 2 to 5 weeks.

        Next, what exactly needs to be synchronised? Getting the data from the sensors, or actually sending the data?

        You will have to do an NTP syne after you wake up and connect to WiFi to get a precise time. You can then simply sleep for the difference between the time you want and the current time.

        B 1 Reply Last reply Reply Quote 0
        • B Offline
          bmueller77
          last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • B Offline
            bmueller77 @jcaron
            last edited by

            @jcaron
            Hi, thanks for the remark. I'll plan to wake the WiPy as short as possible and/or maybe just wake it all 30 minutes.
            Well, it's not necessary for me to send the data in exact 15min cycles but i want to prevent a growing offset. As WiFi connection takes different times, there could be an offset of up to 30 seconds per hour. I just want to make sure, that these time differences will be automatically corrected.

            1 Reply Last reply Reply Quote 0
            • B Offline
              bmueller77
              last edited by bmueller77

              Took a while but i made it. For other users, here's the code

              from machine import RTC
              rtc = RTC()
              rtc.ntp_sync("78.46.52.71",15)
              while rtc.synced() != True:
                  time.sleep(1)
              rtc.now()
              go = 0
              runeveryxseconds = 900 #intervall waiting for (here every 15 minutes)
              while go == 0:
                  t = rtc.now()
                  minute = '{:02d}'.format(t[4])
                  second = '{:02d}'.format(t[5])
                  tstamp = ((int(minute) * 60) + int(second) + 3600)
                  print("Time to launch: " + str(runeveryxseconds - tstamp % runeveryxseconds))
                  if tstamp % runeveryxseconds == 0:
                      go = 1
                  else:
                      go = 0
                  time.sleep(0.8)
              
              J 1 Reply Last reply Reply Quote 0
              • J Offline
                jcaron @bmueller77
                last edited by

                @bmueller77 Depending on your exact needs, you could directly sleep for the remaining time instead of looping...

                B 1 Reply Last reply Reply Quote 0
                • B Offline
                  bmueller77 @jcaron
                  last edited by bmueller77

                  @jcaron
                  I am reading sensor data from Pysense and sending them via MQTT, then i do

                  machine.deepsleep(873000)
                  machine.reset()
                  

                  I added the reset because sometimes, the WiPy did not start the loop after waking up.

                  L 1 Reply Last reply Reply Quote 0
                  • L Offline
                    livius @bmueller77
                    last edited by livius

                    @bmueller77 said in [Solved] Trying to run command exactly at :00, :15, :30 and :45 (every 15mins):

                    I added the reset because sometimes, the WiPy did not start the loop after waking up.

                    what do you mean here?
                    Deepsleep is like reset your whole script start from beggining (after time end).
                    It never back from point where you have finished

                    think about people first, then computers

                    B 1 Reply Last reply Reply Quote 0
                    • B Offline
                      bmueller77 @livius
                      last edited by bmueller77

                      @livius Hi. I have the issue, that the device does not start again after several cycles. That was a try to fix it...

                      Update: Fixed the issue using WDT :-)

                      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
                      • 1 / 1
                      • First post
                        Last post

                      Pycom on Twitter

                      Pycom