MQTT code freeze after some time.

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
bergpb
Posts: 16
Joined: Sun Jun 10, 2018 2:51 pm

Re: MQTT code freeze after some time.

Post by bergpb » Wed Jul 03, 2019 12:52 am

After freeze and press ctrl+c i have this output:
-------------------------------------------------------------
Disconnecting...
Traceback (most recent call last):
File "main.py", line 5, in <module>
File "mqtt.py", line 149, in connect
File "mqtt.py", line 143, in main
File "mqtt.py", line 140, in main
File "mqtt.py", line 131, in main
File "umqtt/simple.py", line 168, in wait_msg
KeyboardInterrupt:
-------------------------------------------------------------
Any help?

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

Re: MQTT code freeze after some time.

Post by kevinkk525 » Wed Jul 03, 2019 5:20 am

Ah right, I think I may know why it is blocking.
Try using c.check_msg() instead of c.wait_msg()
wait_msg uses a blocking socket and if there's nothing to read from the mqtt broker, it will block forever as you don't even send pings. However, it should stop blocking when you send a command but apparently that doesn't help. But check if it works if you change to check_msg().
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

bergpb
Posts: 16
Joined: Sun Jun 10, 2018 2:51 pm

Re: MQTT code freeze after some time.

Post by bergpb » Wed Jul 03, 2019 2:15 pm

I will try with check_msg() and return if works.

bergpb
Posts: 16
Joined: Sun Jun 10, 2018 2:51 pm

Re: MQTT code freeze after some time.

Post by bergpb » Thu Jul 04, 2019 1:47 am

Same using check_msg(), after some time code freeze and after press ctrl+c return this:
Disconnecting...
Traceback (most recent call last):
File "main.py", line 5, in <module>
File "mqtt.py", line 149, in connect
File "mqtt.py", line 143, in main
File "mqtt.py", line 140, in main
File "mqtt.py", line 133, in main
KeyboardInterrupt:

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

Re: MQTT code freeze after some time.

Post by kevinkk525 » Thu Jul 04, 2019 6:06 am

Can you post the code of mqtt.py?
Because I can not find these lines in the code you previously posted:
File "mqtt.py", line 149, in connect
File "mqtt.py", line 143, in main
File "mqtt.py", line 140, in main
File "mqtt.py", line 133, in main
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode


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

Re: MQTT code freeze after some time.

Post by kevinkk525 » Fri Jul 05, 2019 6:09 am

I'm not sure this could lead to problems but you should implement a short waiting time in the while loop, time.sleep_ms(10) should be enough.

The lines in your error trace are a bit strange as they point towards c.disconnect(), machine.reset() and gc.collect(). It shouldn't freeze at any of these methods.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

bergpb
Posts: 16
Joined: Sun Jun 10, 2018 2:51 pm

Re: MQTT code freeze after some time.

Post by bergpb » Sat Jul 06, 2019 5:46 pm

After a lot of tests, a solution to put sleep_ms(500) in code works fine.
I put too a check_status when a publish to a topic in every 500ms.
Now esp32 is online about some last 3 hours:
Here is the code:

Code: Select all

while True:
    c.check_msg()
    check_status(c)
    gc.collect()
    sleep_ms(500)

Post Reply