Connection problems: Node red, mosquitto broker and esp32

as @craigcurtin wrote the 2 sec delay in the main loop is not a good idea. the main loop including client.loop() should be "free running" to keep wifi and mqtt happy.

Perhaps this helps (Just googled it):

https://www.norwegiancreations.com/2017/09/arduino-tutorial-using-millis-instead-of-delay/

you certainly find many other tutorials around if you search for "delay() mills()"

1 Like

And something more... Subscribing every 2 seconds is not a good idea too. This should be done once after connect (or reconnect).

I found a example on Nicks github. (Amazing how google works :wink: ) What I can read it is a pretty clean implementation including a 2 sec. timer

1 Like

@Christian-Me
@Colin
@craigcurtin

The Yes. I think I found the problem, it really must be related to delay and subscribes.

I implemented the attached github code as it is and decided to test it.

Almost 1000 messages have already been sent without disconnection. It's almost 20 minutes in sync. I am very happy. I will try to implement reading and publishing the sensors now using this code as a base.

I intend to carry out tests today. if there is no longer any doubt I give this question as resolved.

1 Like

@Colin @Christian-Me @craigcurtin
I inserted the sensors and relays.

EVERYTHING PERFECT.

Disconnections are not taking place.

The reading is momentary.

The relays are activated instantly.

It was all a problem with poorly structured programming.

I thank you all for your help, without you I would not have been able to. I give this issue as resolved.

Thank you one more time.

2 Likes

Joaovitor

If you have not already found this esp32 project for mqtt, it comes highly recommended. Handles automatic reconnect to both mqtt and WiFi. My contribution to Plapointe6ā€™s project https://github.com/plapointe6/EspMQTTClient is the counter that returns the number of times a device reconnects. I recently did a grep of my log to find the maximum number of reconnects in a day. It is 32. It is very common to see two or three reconnects in a 24 hour period.

Something else I have learned from experience: have your esp32 reboot once a day. I know, the idea will not make sense and I long resisted this recommendation, which came from my son, who has several years experience with Esp32 devices. But, I have found that all things are better at the edge if my esp32s recycle once a day.

Hope you find this helpful.

It is a easy solution to reboot every day but in my opinion this only covers other problems.

  • bad power supply: The ESP need power spikes of many 100mA (mostly around 250mA) due to wifi send which could be difficult to handle. Good bypass caps can help but most dev-board designs and commercial products suffer having this problem. The ESP32 need even more "stable" power!
  • bad wifi connection I recognize that ESPs having problem maintaining a stable wifi connection experience wd-resets. This is often in conjunction with a bad power supply: Strong wifi signals = lower power spikes = less stress. Weak signal = higher TX power = more Stress for the power supply.
  • bad software As we all know memory leaks can happen ....

Here are some of my ESPs My own hardware is OK but the ESP8285 based bulbs are rebooting "to often" even with enough free heap memory. Others are running fine for days, weeks or even month.

Rebooting every day is fine but who does press the reset? The only good way i can think of is to have a quick nap (deep sleep) in an daily interval. But this is only a "workaround" because you only hide a problem.

Every reset could end up in a frozen state (where the ESP could not boot correctly and the wd don`t kick in) especially when your main problem is the power supply.

I would suggest to investigate in finding the reason why the ESP reboots. The "reboot reason" and "reset condition" can give you a first hints) My Bulbs for example have the problem of an old firmware of mine ... but currently ota update is not working (ESP8285 have only 1MB flash) - so I have to solder in the serial connection (again) and no time :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.