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

    WDT feeding in the background, is this a good idea?

    Scheduled Pinned Locked Moved Getting Started
    wdt
    5 Posts 2 Posters 1.6k 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.
    • B Offline
      BetterAuto Pybytes Beta
      last edited by

      I want to keep my WDT fed but I would rather not write wdt.feed() all over the place. Is the code below a good idea or will it negate the purpose of having a WDT?

      My gut tells me this is wrong but I don't know enough to be sure.

      class WDT(object):
          def __init__(self, timeout = 10000):
              from machine import WDT
              self.wdt = WDT(timeout)
      
      
          def feeder_func(delay, id):
              from time import sleep
              while True:
                  sleep(delay)
                  self.wdt.feed()
      
      
          # TODO How do you kill a thread?
          def start()
              import _thread
              _thread.start_new_thread(feeder_func, 0)
      
      
          def stop(self):
              self.wdt.stop()
      

      Usage:
      >>> wdt = WDT()
      >>> wdt.start() # Being fed in the background...
      >>> wdt.stop()

      1 Reply Last reply Reply Quote 0
      • RobTuDelftR Offline
        RobTuDelft
        last edited by

        Why not just put it once in your main loop?
        Anyway, I've notifced crashes/freezes happen due to memory errors (if the rest of your code is well written). So the thread will also stop working when this happens, and the board will reset.

        Without data you're just another person with an opinion.
        Hackster | About me

        1 Reply Last reply Reply Quote 0
        • B Offline
          BetterAuto Pybytes Beta
          last edited by

          I don't have a main loop. I have a main.py that executes once then the board goes back to sleep. So this would catch what you believe is the most common failure, that's good.

          Is there a guideline for how often to slip your feeders into the code? Before every statement? Every tenth statement? Only before expensive operations? Something else?

          RobTuDelftR 1 Reply Last reply Reply Quote 0
          • RobTuDelftR Offline
            RobTuDelft @BetterAuto
            last edited by

            @BetterAuto If you use deepsleep that is also some kind of loop. The way I use the WDT is to check if it can finish the loop, so 1 feed at the end. This assumes you somewhat know a maximum duration the loop can take. So I would just put it before putting the device to sleep. That way, if it hangs it will re-run the program until it succeeds.

            Without data you're just another person with an opinion.
            Hackster | About me

            1 Reply Last reply Reply Quote 0
            • B Offline
              BetterAuto Pybytes Beta
              last edited by

              I will stop the WDT just before deep sleep, and waking from sleep requires a reset, so it's not looped for me. main.py can take anywhere from a few seconds to several minutes to execute, so I don't know the maximum duration ahead of time.

              It would seem I would need to feed it all over the place, so I'll just fire up my copy and pasting.

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

              Pycom on Twitter

              Pycom