mqtt_as + WDT

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
pidou46
Posts: 101
Joined: Sat May 28, 2016 7:01 pm

mqtt_as + WDT

Post by pidou46 » Thu Dec 10, 2020 8:59 pm

Hello,

I have a ESP32 micropython mcu publishing messages to a mosquitto broker.

To ensure the mcu run fulltime, I have combined mqtt_as client with WDT.

I feed the WDT in the infinite loop every 5s

Code: Select all

async def main(client):
    await client.connect()
    wdt = WDT(timeout=10000)
    grid=uPZEM(1,pins=[15,27])
    pv=uPZEM(2,pins=[14,12])
    while True:
        await asyncio.sleep(5)
        wdt.feed()
        now=time.time()+MP_OFFSET
        grid_data=[now,CLIENT_NAME,"grid"]
        pv_data=[now,CLIENT_NAME,"pv"]
        grid_data.extend(grid.read_input_registers(1))
        pv_data.extend(pv.read_input_registers(1))
        print('publish grid', grid_data)
        print('publish pv', pv_data)
        # If WiFi is down the following will pause for the duration.
        await client.publish(CLIENT_NAME+'/grid', json.dumps(grid_data), qos = 1)
        await client.publish(CLIENT_NAME+'/pv', json.dumps(pv_data), qos = 1)
        
If I shutdown the broker, the connection is broken, the WDT is not feed anymore so mcu is rebooting and try to reconnect but end with:

OSError: -1

Here is the traceback:
publish pv [946685061, 'OptimPV', 'pv', 235.1, 0.123, 0.0, 69749, 50.0, 0.0]
publish grid [946685067, 'OptimPV', 'grid', 235.2, 2.481, 535.8, 2766759, 50.0, 0.92]
publish pv [946685067, 'OptimPV', 'pv', 235.2, 0.123, 0.0, 69749, 50.0, 0.0]
RAM free 4069376 alloc 28864
publish grid [946685073, 'OptimPV', 'grid', 235.4, 2.49, 538.5, 2766760, 50.0, 0.92]
publish pv [946685073, 'OptimPV', 'pv', 235.4, 0.124, 0.0, 69749, 50.0, 0.0]
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
E (283820) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (283820) task_wdt: - mp_task (CPU 0)
E (283820) task_wdt: Tasks currently running:
E (283820) task_wdt: CPU 0: mp_task
E (283820) task_wdt: CPU 1: IDLE1
E (283820) task_wdt: Aborting.
abort() was called at PC 0x4011955c on core 0

ELF file SHA256: 0000000000000000000000000000000000000000000000000000000000000000

Backtrace: 0x400941ff:0x3ffbe510 0x40094555:0x3ffbe530 0x4011955c:0x3ffbe550 0x40086ccd:0x3ffbe570 0x40095d0d:0x3ffce080 0x4008714f:0x3ffce0c0 0x40087201:0x3ffce0f0 0x401d4df0:0x3ffce110 0x4011d219:0x3ffce160 0x400fabb2:0x3ffce180 0x400f49b6:0x3ffce1b0 0x400f49ee:0x3ffce1d0 0x400e414a:0x3ffce1f0 0x400dfe6d:0x3ffce220 0x400dffa9:0x3ffce240 0x400ee097:0x3ffce260 0x400e420c:0x3ffce300 0x400dfe6d:0x3ffce330 0x400dffa9:0x3ffce350 0x400ee097:0x3ffce370 0x400e420c:0x3ffce410 0x400dfe6d:0x3ffce440 0x400ee011:0x3ffce460 0x400e420c:0x3ffce500 0x400dfe6d:0x3ffce570 0x400dffa9:0x3ffce590 0x400ee097:0x3ffce5b0 0x400e420c:0x3ffce650 0x400dfe6d:0x3ffce6d0 0x400dfe96:0x3ffce6f0 0x400dff07:0x3ffce710 0x400eb261:0x3ffce7a0 0x400eb5a7:0x3ffce7d0 0x400e002e:0x3ffce8b0 0x400ee351:0x3ffce8f0 0x400e420c:0x3ffce990 0x400dfe6d:0x3ffce9f0 0x400dfe96:0x3ffcea10 0x40107cab:0x3ffcea30 0x40107edd:0x3ffceac0 0x40107f3b:0x3ffceae0 0x400f78ab:0x3ffceb00 0x400955d2:0x3ffceb30

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5008
ho 0 tail 12 room 4
load:0x40078000,len:11608
ho 0 tail 12 room 4
load:0x40080400,len:6120
entry 0x400806bc
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
WebREPL daemon started on ws://0.0.0.0:8266
Started webrepl in normal mode
Checking WiFi integrity.
Got reliable connection
Connecting to broker.
Traceback (most recent call last):
File "main.py", line 7, in <module>
File "OptimPV.py", line 60, in <module>
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "OptimPV.py", line 34, in main
File "mqtt_as.py", line 519, in connect
File "mqtt_as.py", line 248, in _connect
File "mqtt_as.py", line 186, in _as_write
OSError: -1

As advised in the doc I have added a 5s sleep in the main.py to let mcu finish startup.
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016

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

Re: mqtt_as + WDT

Post by kevinkk525 » Thu Dec 10, 2020 9:37 pm

You got 2 problems in your code:

1) WDT:

Code: Select all

# If WiFi is down the following will pause for the duration.
        await client.publish(CLIENT_NAME+'/grid', json.dumps(grid_data), qos = 1)
        await client.publish(CLIENT_NAME+'/pv', json.dumps(pv_data), qos = 1)
Your code bascially already says that if the wifi is down, the wdt will reset the device every time. Is that intended?

2) client.publish needs bytes object as message
await client.publish(CLIENT_NAME+'/grid', json.dumps(grid_data).encode(), qos = 1)
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

pidou46
Posts: 101
Joined: Sat May 28, 2016 7:01 pm

Re: mqtt_as + WDT

Post by pidou46 » Sat Dec 12, 2020 5:34 pm

Thank you for your answer,

Reading the doc, I have understand mqtt_as should reconnect automatically when wifi or mqtt broker reconnect.

But for some reason, it doesn't work in my case, it's the reason why I added the WDT.

I will investigate to get more information to debug.

Thanks for the hint about byte conversion for message publishing, I don't really understand without conversion right now?
nodemcu V2 (amica)
micropython firmware Daily build 05/31/2016

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

Re: mqtt_as + WDT

Post by kevinkk525 » Sat Dec 12, 2020 7:02 pm

Maybe mqtt_as couldn't correctly reconnect because your publish doesn't work correctly.
You need to convert the string to a byte by

Code: Select all

message="my mqtt message"
message=message.encode() # now it is a byte-object
After that both should work and you can remove the WDT
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply