Count deepsleep event? (without using flash)



  • Hi,

    My device wakes up every few minutes from deepsleep checks a sensor and then goes back to deepsleep.

    Now I want to send a lora message every hour about its status.
    The problem is that I need some sense of relative past time after each wake up. (so I know it's time to send the update again)

    I don't want to use flash since that would mean that the device writes every 1 or 2 minutes to that flash so I need some ram.
    I only need a counter since I know it was sleeping for the time I set it, so every count is x minutes.

    The ULP has ram that doesn't get erased during deepsleep, can I write and read 1 byte to it via python?


    Current alternatives:
    A solution I think of is that I change the year +1 for every wake-up and reset it when I reach the number of counts to send the lora message. (obviously having access to a byte from ULP would be much better)
    Every 2 hour lora update and 1 minute sleep cycles means I have enough with a counter that goes up to 120, so even 7 bits is also ok.

    Another solution is to send a message between minute 58 and 60 but then I will miss some messages or maybe have two. (so I don't want to do that)

    Thanks



  • @KenM You could also use the pycom.nvs_set and pycom.nvs_get methods.

    Those write to flash, but use wear-levelling methods. The advantage is they'll survive more conditions than RTC.



  • Nevermind I figured it out:

    import CountDeepSleep
    
    print('Counting: ' + str(CountDeepSleep.NumberofDeepSleepWakeUps()))
    if CountDeepSleep.RunSpecialEvent() :
        print ('Count reached')
    else:
        machine.deepsleep(3000)
    
    from machine import RTC
    rtc = RTC()
    ResetOnCount = 4 # constant value 
    
    def StoreCount(NewCount):
        rtc.memory(str(NewCount)) # update RTC RAM memory
        WakeUps = NewCount
    
    def ResetCount():
        rtc.memory('0')
        WakeUps = 0
    
    def NumberofDeepSleepWakeUps():
        return WakeUps
    
    def RunSpecialEvent():
        return WakeUps >= ResetOnCount
    
    if rtc.memory().decode() == '' : # power on
        WakeUps = 1
    else:
        WakeUps = int(rtc.memory()) + 1  # wake from deepsleep
    
    if WakeUps >= ResetOnCount :
        rtc.memory('0')   # Reset, keep 'WakeUps' on the max value for this wake up
    else:
        StoreCount(WakeUps) # update RTC RAM memory
    
    
        
    
    

Log in to reply
 

Pycom on Twitter