Page 2 of 2

Re: MQTT code freeze after some time.

Posted: Wed Jul 03, 2019 12:52 am
by bergpb
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?

Re: MQTT code freeze after some time.

Posted: Wed Jul 03, 2019 5:20 am
by kevinkk525
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().

Re: MQTT code freeze after some time.

Posted: Wed Jul 03, 2019 2:15 pm
by bergpb
I will try with check_msg() and return if works.

Re: MQTT code freeze after some time.

Posted: Thu Jul 04, 2019 1:47 am
by bergpb
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:

Re: MQTT code freeze after some time.

Posted: Thu Jul 04, 2019 6:06 am
by kevinkk525
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

Re: MQTT code freeze after some time.

Posted: Thu Jul 04, 2019 10:18 pm
by bergpb

Re: MQTT code freeze after some time.

Posted: Fri Jul 05, 2019 6:09 am
by kevinkk525
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.

Re: MQTT code freeze after some time.

Posted: Sat Jul 06, 2019 5:46 pm
by bergpb
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)