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

    Count deepsleep event? (without using flash)

    Scheduled Pinned Locked Moved LoPy
    3 Posts 2 Posters 140 Views 2 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.
    • K Offline
      KenM
      last edited by KenM

      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

      1 Reply Last reply Reply Quote 0
      • K Offline
        KenM
        last edited by KenM

        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
        
        
            
        
        
        J 1 Reply Last reply Reply Quote 1
        • J Offline
          jcaron @KenM
          last edited by

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

          1 Reply Last reply Reply Quote 1

          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