How to detect a connection failer between esp32 and node red

Hi, I made a esp32 that sends some info to node red ( installed on a windows11 pc ) with mqtt. The esp32 is connected with wifi to a router. The esp32 is a project that moves to different places and then de wifi connection not always perfect. And from time to time I lose connection with wifi and I don’t get the info on node red.

If the connection is perfect then it works perfectly.

Now I like to detect on node red the connection, if it is lost, then make a notification on dashboard.

I was thinking to make in the esp32 a heartbeat that is sending a signal every second to mqtt and if that heartbeat is out for 3 seconds, send a notification on dashboard.

But I don’t find the right way to do that.

The "last will & testament" (LWT) feature of MQTT allows you to detect when a device loses connection to the MQTT broker.
This relies on you being able to declare the LWT topic in your ESP32 firmware. Certainly in some ESP firmware this is set automatically.

In Node-red you have to subscribe to the LWT topic to detect when the device is offline. Typically you would see this message after 30 seconds offline.

It is of course possible to program the ESP to send a heartbeat message.
Exactly how you do this also depends of the firmware you are running.

In Node-red you only need a trigger node to respond to loss of the heartbeat:

1 Like

As jbudd implies, you don't need to do that yourself, MQTT already has the feature available via the LWT. The LWT is defined on the broker against the connection from the ESP.

I typically send a topic, say ESP/dev03 set to "Online" when my device starts up and define the connection from the device with an LWT that sends ESP/dev03 set to "Offline" when the broker has lost contact for the broker's defined keepalive period.

As already mentioned, several ways are possible. Myself I have several variants, all working since years, on all my esp32’s running ESPHome

  • one is a local timer in the esp32 that fires a hb at regular interval to Node-RED that monitors this with a trigger node. If the time out in the trigger node happens, a notification is created
  • another variant is a ask/respond setup where Node-RED via MQTT (at a regular interval) asks the esp32 for a response that also is monitored with a trigger node in the same way

I think at one stage I also experimented with a kind of self-monitoring setup in my second variant where the esp32 was able to reboot itself if the hb mechanism would fail

:white_check_mark: Don’t forget HealthChecks (HC)
It’s really simple to set up and comes with lots of useful features:

  • The free tier lets you monitor up to 20 jobs.
  • Setup is straightforward: each job is assigned a unique URL, and you just send a simple HTTP request (a “ping” or "heartbeat") to confirm everything is running.
  • The HC dashboard lets you configure grace periods, schedules, and notification preferences.

I use HC to monitor a set of ESP32-S2-Mini nodes written in MicroPython. But you don’t need MicroPython specifically—since all HC needs is a simple HTTP request, it works with nearly any language or environment.

For notifications, HC supports many integrations. I personally use email and Telegram, but Slack, Discord, and other options are available as well.

:light_bulb: Side note:
I’m also working on a MicroPython script for a couple of nodes at the far end of my garden. The script performs a WiFi scan, ranks the results by signal strength, and then attempts to connect in that order.

To make it efficient, I keep a list of my trusted access points in the script, so it discards any networks it doesn’t recognise. This way, the node always connects to the strongest available AP without wasting time on unknown ones.

Using the LWT might be ok in many cases but it just checks the connection and not that your coded functionality inside the esp32 is still operational. So if you want to “check deeper”, I would prefer using a coded hb monitoring. Then you will have a lot of additional possibilities to check your functionality, setting up internal checkpoints that can be used as required conditions before sending a hb or acknowledging a status check

2 Likes

I use both LWT and a heart beat. My esp sends a message every 10 seconds and if node red doesn't see it a msg is raised too. My esp's also look for a keep alive msg and if they don't see that they reboot.

1 Like

Using a firmware such as ESPHome really helps if you are needing this kind of thing. It deals with the basics for you - you only need configuration, node C/C++ code. But if you need something bespoke, it still lets you write a snippet of code (or use a library, etc). Configuration and code is easily reusable across many different devices as well.

With the added advantage that I've always found ESPHome to be very stable. Can't say that of other firmwares, nor my own code! :smiley:

1 Like