how to WatchDog in ESP8266?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

how to WatchDog in ESP8266?

Post by VladVons » Thu Feb 27, 2020 11:15 am

esp8266-v1.12 firmware
https://docs.micropython.org/en/latest/ ... e.WDT.html

Code: Select all

import machine

WD = machine.WDT(timeout=2000)
TypeError: function doesn't take keyword arguments

WD = machine.WDT(0, 2000)
TypeError: function expected at most 1 arguments, got 2 

WD = machine.WDT(2000)
ValueError:
I wrote my own based on hardware timer

Code: Select all

# software watchdog, to prevent ESP freezing
class TWDog:
    def __init__(self, aID: int, aTOut: int):
        self._TOut = aTOut
        self._Cnt  = 0
        Timer = machine.Timer(aID)
        Timer.init(period = int(1 * 1000), mode = machine.Timer.PERIODIC, callback = self._CallBack)

    def _CallBack(self, aTimer):
        self._Cnt += 1
        if (self._Cnt >= self._TOut):
            print("WDT timeout")
            aTimer.deinit()
            machine.reset()

    def Feed(self):
        self._Cnt = 0
        
WDog = TWDog(0, 60)
Last edited by VladVons on Thu Feb 27, 2020 4:28 pm, edited 3 times in total.

User avatar
jimmo
Posts: 1381
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: how to WatchDog asyncio application in ESP8266?

Post by jimmo » Thu Feb 27, 2020 12:29 pm

On ESP8266 the machine.WDT constructor takes either no arguments at all, or a single "id" argument, which must be zero.

so you should be able to use

Code: Select all

WD = machine.WDT()
VladVons wrote:
Thu Feb 27, 2020 11:15 am
OK, I wrote my own based on hardware timer, but with an asyncio it immediately reboots a programm
Without an asyncio it works fine
At first glance that looks fine, I'm not sure why it's interacting badly with asyncio. Would need to see the whole program I think.

VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: how to WatchDog in ESP8266?

Post by VladVons » Thu Feb 27, 2020 4:44 pm

1)
jimmo wrote:
Thu Feb 27, 2020 12:29 pm
On ESP8266 the machine.WDT constructor takes either no arguments at all, or a single "id" argument, which must be zero.
so you should be able to use
WD = machine.WDT()
What is a default timeout value for no arguments?


2)
When i run a hardware timer and asyncroniously call gc.collect() than system reboots.
I think 11K of free memory causes it.
I tested small application with only timer and async (25k of free RAM) - it is ok

Obviously i should freeze my project's libraries into firmware to reduce RAM consumption.
It is new for me....

kevinkk525
Posts: 638
Joined: Sat Feb 03, 2018 7:02 pm

Re: how to WatchDog in ESP8266?

Post by kevinkk525 » Thu Feb 27, 2020 7:09 pm

I'm not sure the esp8266 hardware wdt can be used like that. I checked it a few months back and I think it was used by the micropython VM? So not for use for the user..

I wrote a software WDT that is still in use by all my esp8266 units and it works without any problems with uasyncio https://github.com/kevinkk525/pysmartno ... atchdog.py
The linked code still has some project specific code in it, that would need to be removed.
However I posted a clean code probably a few times on the forum already, so you might find it if you use the search :D

My esp8266 units typically run with 5-9kb of free RAM, so this shouldn't be an issue, unless your RAM is extremely fragmented but then you should receive a memory exception.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: how to WatchDog in ESP8266?

Post by VladVons » Fri Feb 28, 2020 6:58 am

I installed esp-open-sdk, micropython sources and finanly have my own firmware with all my libraries integrated.
(if some one needs a linux batch script to build firmvare write me privat)

The free RAM size inceased from 11k to 19k, but application still crashes when i use hardware timer.

without a timer invoked application works stable for a long time.
I can send misc requests to http. mqtt, etc

Code: Select all

rst cause:2, boot mode:(3,6)
Attachments
microCrash.png
microCrash.png (32.63 KiB) Viewed 208 times

Post Reply