• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Keeping ESP32 connected to MQTT server

Q&A Forum › Category: ESP32 › Keeping ESP32 connected to MQTT server
0 Vote Up Vote Down
ajocius asked 4 years ago

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:

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?

Question Tags: ESP32, reset
7 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

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.

0 Vote Up Vote Down
ajocius answered 4 years ago

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?

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

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…

0 Vote Up Vote Down
ajocius answered 4 years ago

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…

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Let me know your results!

0 Vote Up Vote Down
ajocius answered 4 years ago

Is there a way to save log of error messages to some text file on ESP32? I could then capture errors easier.

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

I don’t think there are many debugging tools available for MicroPython at the moment… I don’t know an easy way of finding out that error (except seeing it printed in the Serial Monitor)…

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.