0

I'm using ESP8266-7 as the only micocontroller in my project and I have a tiny problem with it. there are sometimes that the internet line is broken and my ESP tries to reconnect to the broker. while this happens, the program freezes until the internet gets back online and ESP reconnects to the broker. (in more specific way, my program waits 15 seconds in .connect(device_ID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str()) every time.)

I'm using PubSubClient.hlibrary and as I have tested, this problem occurs when I call mqtt_client.connect(device_ID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str()). this way the program runs this function and waits till the process finishes. this may take up to 15 seconds.

how can I try to connect to the broker, without using this code or is there any other solution for me in which my program does not freezes?

can ESP connects to the broker in the background while my program is still running?

M.Navidi
  • 113
  • 1
  • 10
  • Usually there is a Connect Timeout on these kind of libraries/modules, but if you are using this one (https://github.com/knolleary/pubsubclient) there is none. For cases like this, I use the Watchdog Timer to abort the connect if its taking to long and reset the board....not sure if your use case would allow that. – JD Allen Oct 27 '20 at 16:54
  • thanks for your prompt response, but as I said it is my only micro controller on the board and I don't want to shut it down. I want everything to run smoothly without any pauses and I want to connect to the broker in the background of the program. I don't want internet connection, interrupt my program by any means. do I have any chance of doing this? – M.Navidi Oct 28 '20 at 04:29
  • it depends on how your app is written. For example, I have a Pool Temp sensor that connects every five minutes to send its data. It first connects to WiFi, and if that looks good, attempts to connect to the MQTT Broker. I have a Watchdog Timer for 90 seconds, and if that whole WiFi + MQTT Broker connects doesn't happen in that time frame, it resets the board. There is no rule that says your Sensor has to be connected all the time (especially if it is just sending data, and not receiving!) – JD Allen Oct 28 '20 at 19:05
  • that's true. but unfortunately in my case, I have many other thing to control with my ESP; and it's not just checking a sensor. so there can not be any freeze time in my case. Do you have any suggestions or solutions for me? – M.Navidi Oct 31 '20 at 08:15
  • JD's rules for IOT #1: Sensors should just Sense. The whole point of a PubSub architecture is that nodes can be single purpose, and other nodes can subscribe to the sensor data and operator on it. Your life will be far far easier if you just let the sensors sense, and have other nodes do stuff with the data. In my house IOT system, I have a Docker server where my MQTT broker runs, and just create Containers to do stuff with the data. Adding functionality is as easy as just adding another Container and have it also subscribe to the data. https://diysmarthome.io/ – JD Allen Oct 31 '20 at 23:00
  • It's not just a sensor. It's a product, a small home appliance which carry out the commands even from broker. So it must always be connected to the broker to take care of new commands from there too. So I can not have it to be reset by WDT, and freezing for 15 seconds also is not allowed, because freezing conflicts with the correct functionality of the product. – M.Navidi Nov 01 '20 at 04:54

1 Answers1

0

I find problem: reason is Wifi timeout We can set timeout Wifi, example : WiFiClient wifiClient; wifiClient.setTimeout(100); it 's working