ESP32 loosing connection to mqtt server

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

ESP32 loosing connection to mqtt server

Post by ajocius » Tue Feb 26, 2019 1:00 pm

I am using Micropython on ESP32 board that has few sensors connected. The task for ESP32 is to read sensors and send data to MQTT server. I got it all working, however process used do stop unexpectedly. Sometimes it happens once a week, sometimes it kept stable for few weeks. I was only guessing that this could be due to drop of wifi connection or missing connection to MQTT server (or possibly something else). My workaround was just to press Reset button on ESP32 and all was working again (this particular ESP32 was monitoring temp in the greenhouse). I have then read code example in the book from Random Nerd Tutorials with following code:
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()

try:
client = connect_and_subscribe()
except OSError as e:
restart_and_reconnect()

while True:
try:
client.check_msg()
if (time.time() - last_message) > message_interval:
msg = b'Hello #%d' % counter
client.publish(topic_pub, msg)
last_message = time.time()
counter += 1
except OSError as e:
restart_and_reconnect()
So I thought ESP32 will reset if it does not reach MQTT server. But my process is possibly "not right" for this to work. In order to read sensor data, I used Node-Red to generate mqtt message every 30 min. ESP32 has subscription to this particular message and does reading of sensor data and sends it back to MQTT server. What I would like to add to code is attempt to connect to MQTT let say every 30 min and if it does not succeed for whatever reason (being drop of wifi or mqtt connection) it would reset ESP32 with machine.reset() command. Can this be done?

uxhamby
Posts: 34
Joined: Thu Nov 14, 2019 9:47 pm

Re: ESP32 loosing connection to mqtt server

Post by uxhamby » Sun Apr 12, 2020 4:00 pm

Hi,

I am looking for a solution to this problem as well.

In my case, I notice that reliability is better the physically closer the ESP32 is to the to the wifi access point, making me suspect that wifi disconnection is my problem. I originally set this up to monitor the temp at my next door neighbours house, while she winters in Florida. This puts the ESP32 at the very edge of my wifi coverage area. Reliability was dismal. When I brought the thing home and set it up in my workshop, within 2 meters of the a/p, it would run for days or weeks but then eventually loose the connection to either wifi or mqtt, I know not which but proximity to the a/p makes me suspect wifi as I say.

Disappointed that no one has responded here in the past two months. It occurs to me that if you have the RandomNerd book, you might be better to post the question on his private forum, no?

My approach is going to be to write a wifi connect routine with built in interrupt based periodic error checking to reconnect to keep wifi connection reliable.

I am surprised that this hasn't already been addressed. I wasn't actually looking for another project just now, so if such a robust wifi connect package does already exist, that I could simply d/l and install, I would be very happy with that.

Best of luck, cheers and godspeed.

Brian H.

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

Re: ESP32 loosing connection to mqtt server

Post by kevinkk525 » Sun Apr 12, 2020 5:49 pm

Sorry sometimes I read posts and either can't answer directly or forget to answer. Since it is then marked as read, I won't look at it again. You could have sent another message to get attention, just like now.

So for a mqtt library that keeps you connected to the broker and wifi reliably the is the excellent library of Peter Hinch: https://github.com/peterhinch/micropython-mqtt
It uses uasyncio so you might need to learn how to program using uasyncio but there's currently no better and easier approach.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply