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?

6
  • Usually there is a Connect Timeout on these kind of libraries/modules, but if you are using this one (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. Commented Oct 27, 2020 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? Commented Oct 28, 2020 at 4: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!) Commented Oct 28, 2020 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? Commented Oct 31, 2020 at 8: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. diysmarthome.io Commented Oct 31, 2020 at 23:00

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.