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

    Creating an infinite async loop

    Micropython
    2
    4
    7.1k
    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.
    • J
      jhfoo
      last edited by

      Looking to write code that will send data via mqtt perpetually. Learn recently that the publish() method is NOT synchronous, which I augmented by listening to a topic for an ack message before calling the iteration done, and the next iteration publish begins.

      So I tried to close the loop by calling the publish() method within the subscribe() handler, and the app crashed with message 'RuntimeError: maximum recursion depth exceeded'.

      How do people get around this?

      J 1 Reply Last reply Reply Quote 0
      • J
        jhfoo @jhfoo
        last edited by

        Am trying using ONE_SHOT timers. Not sure if this is the recommended approach.

        1 Reply Last reply Reply Quote 0
        • m5stackM
          m5stack
          last edited by

          could you share your code to here. maybe people can help you.

          J 1 Reply Last reply Reply Quote 0
          • J
            jhfoo @m5stack
            last edited by jhfoo

            @m5stack I will share pseudo-codes of the current implementation, which is stable so far:

            CycleTimer = Timer(-1)
            
            def main():
              # init and connect mqtt client
              doCycle()
            
            def doCycle():
              global CycleTimer
              # send stuff from mqtt
              # NOTE: do not use machine.lightsleep() as publish() is NOT synchronous
              # lightsleep() will block data from being sent
              CycleTimer.init(period=5000, mode=Timer.ONE_SHOT, callback=doCycle)    
            

            NOTE: this is power-intensive; calling lightsleep() will cause uiflow's mqtt client to crash quickly.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post