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



  • 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



  • @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 :-)



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



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



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



  • 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)


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



  • This post is deleted!


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



Pycom on Twitter