Hi. I’m following the tutorial on esp32 MQTT publish and subscribe with arduino IDE. it all works fine except when internet connection is lost. At that point my esp stops responding, no readings, etc. If I check through serial monitor all I can see is [E][WiFiClient.cpp:220] connect(): lwip_connect_r: 113 over and over like an infinite loop. The reconnect function works as expected because it all resumes to normal once internet connection is back.
Is this a know issue? I cant find information on internet. Do you know any non-blocking workaround to maybe check if internet is back every x amount of time but letting the rest of the code work
Hello Matt, that’s the expected behavior. Any board that you plan to run MQTT should always be trying to reconnect after loosing connection to Internet/MQTT broker. You could set a timer to only try to reconnect every X number of seconds, but I still think the best option is to leave it like that.
But is it normal that all code stops working? I’ve added to the project a relay to control a heater based on the sensor temperature readings. Whenever internet is down, the esp freezes in the state it was at that moment. It might be weird but an hour or more with no internet is common, at least here. So if the relay is high when internet goes down it will remain heating no matter the temperature setted.
In case of setting a timer, how can I implement that? The sentence that is being executed seems to be a part of the WiFi library?
thanks in advance
Just to double-check. Are you using the AsyncMqttClient.h?
You’re right that’s not a normal behavior… I was trying to say that it’s normal for the ESP32 to constantly try to reconnect to Wi-Fi and to the MQTT broker.
This is what you should see in your Serial monitor when the Wi-Fi connection is lost:
[WiFi-event] event: 0 [WiFi-event] event: 2 [WiFi-event] event: 2 [WiFi-event] event: 2 [WiFi-event] event: 4 [WiFi-event] event: 7
This is what you should see when your ESP disconnects from the MQTT broker:
Connecting to MQTT... Disconnected from MQTT. Connecting to MQTT... Disconnected from MQTT.
Any code that is in your loop should continue to run (even if the ESP is trying to reconnect)…
Can you post your code on Pastebin.com and post the link here? (remove your SSID, password, and MQTT IP address).
Thanks. I’m using pubsubclient library. Following an example from your blog. But the statement looping seems to be from the WiFi library. I’ve tried adding some print statements for debugging purposes, mainly in the reconnect function, but the code never reaches those lines. I’m on vacations now and I don’t have my laptop. Can we leave this open till next week to show you the code.
Another question, I,m using the esp-wroom-32 module, no dev board. What board shoul I choose in boards manager?
Yes, please post your full code when you come back. Those messages that I’ve mentioned were received using these examples:
You should select the option “ESP32 Dev Module” or with the “ESP32 Wrover Module” should work just fine.
Hi Rui, thanks for your help. I’ve Changed the code to use AsyncMqttClient instead of PubSubClient and it’s working properly using this library. I’m getting the same outputs that you showed me. I’ll be using this so I can follow your examples.
Trying your example I’ve saw that It tries to publish the temperature reading even if there is no internet or MQTT connection. It doesn’t seem to be a problem because it doesn’t block the code. But just to know for other implementations, is it possible to check those WiFi or MQTT event and do something accordingly?
The Async MQTT Client library automatically checks the MQTT and Wi-Fi connections in the background (asynchronously, you don’t need to worry about that). I also tries to reconnect automatically if the MQTT broker connection or Wi-Fi connect is lost.
Is it possible to stop messages from being retained in the broker? I connect to the system from my phone and even if the esp is down It shows me the states according to the lasts retained messages. I didn’t have that behavior with pubsubClient. The idea is so I can tell if the esp is offline by seeing for example 0 in temperature reading. Right now getting a retained reading, if I’m not there to check it’s difficult to see if it is an actual reading or an old one saved on the broker and the esp is offline.
What’s the QoS (Quality of Service) that your Async MQTT Client project is using? For example, in this case it’s 0 (it’s the second parameter):
mqttClient.publish("test", 0, false, "test 1");
Set the third parameter to false to ensure that your message will not be retained.
Thanks Rui. Changing the QoS to 0 didn’t work for me. I’ve found on the API reference that the third parameter corresponds to the retain flag.
mqttClient.publish("test", 2, false, "test 1");
That worked as i’m not seeing new retained messages. I need to find now how to delete old retained messages from the broker.
Exactly! You need to set it to false. I’m not sure what I was thinking in my previous message and I apologize for the confusion.