🤖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

    Scheduled Pinned Locked Moved Micropython
    4 Posts 2 Posters 8.7k Views 1 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.
    • J Offline
      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 Offline
        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 Offline
          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 Offline
            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

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