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 to stop unexpectedly. Sometimes it happened once a week, sometimes it kept 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 your book with following code:
print(‘Failed to connect to MQTT broker. Reconnecting…’)
client = connect_and_subscribe()
except OSError as e:
if (time.time() – last_message) > message_interval:
msg = b’Hello #%d’ % counter
last_message = time.time()
counter += 1
except OSError as e:
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?
Do you have the exact error message that it’s printed in the Terminal when that crash happens?
That exact error message would be crucial to understand what happened and prepare the code to restart the ESP every time that happens.
Unfortunately I do not have message. ESP32 is running on power supply, not connected to PC. As such I do not get message back. Is there any way to get error message without having PC connected to ESP32 for weeks?
I have therefore thought that ESP32 could try to connect to mqtt server per defined intervals and if connection is missing, then do command machine.reset() . Resetting ESP32 would run initial script to connect to WiFi and mqtt. Isn’t that good work around?
Unfortunately I don’t think there’s an easy way to find that error… It would require to have the computer with the serial monitor open.
To me it doesn’t look like it’s a problem with the MQTT connection or Wi-Fi connection getting lost.
It looks like your code is experiencing a different error (it might be a memory error for example). The best way to fix this project is to know the exact error message and creating an exception to restart your board when that happens.
With my MQTT example, the ESP32 is always connected to the MQTT broker, in case the connection is lost it keeps trying to restart the board and reconnect to the MQTT broker ( with restart_and_reconnect() function).
You can try shutting down your MQTT broker, and you’ll see your board restarting over and over again until it can connect to the MQTT broker.
So, the ESP32 already does that, it looks like you get a different error that crashes your board…
Thanks, will have to make some test environment with separate esp connected to PC. Current module in use is running without errors for two weeks now…
Is there a way to save log of error messages to some text file on ESP32? I could then capture errors easier.