Im trying to make a heartbeat programme between two pis, where if the node red programme on one ends then the other pi will reboot the pi with the node red running on it? The two pis need to be connected via ethernet - I was just wondering if this is something I could control in Node-Red ?
My idea would be to have node red working on both pis sending a ping through ethernet to the other node red console, when one pi sends a pi and doesnt get a response, then it sends a reboot command to the pi to restart it?
Now the node red part where the two pis are communication sounds doable , my issue is how to command a pi to reboot when it dies by another pi from node red?
If NR dies on Pi1, that doesn't mean that a ping won't work. If Ping doesn't work, then Pi1 could be hung or it could have crashed altogether and only a power off/on 'might' fix it.
To do that, you would need a relay (maybe a smart plug) that Pi2 could send a OFF/ON command to causing Pi1 to reboot.
If NR is not responsive but Ping IS working, then you could try having an EXEC node use an SSH command to restart NR on the Pi that is not working (haven't tried this myself)
You could also try one of the ssh nodes (search in the flows tab)
I have built this for my NodeMCUs in case one of them is unresponsive. Note, it’s only for the NodeMCU device, it’s not watching the PI with NodeRED
This is what I built:
On the NodeMCU I run a countdown timer that when it reaches zero will reset the NodeMCU
On NodeRED I run a countdown that when it reaches zero will send a reboot instruction to the NodeMCU
the NodeMCU sends an alive message on a regular interval, this message will reset the NodeRED countdown timer
NodeRED sends an alive message on a regular interval, this message resets the. NodeMCU countdown timer
The above will protect against many type of failures but is not 100% airtight, for that you need to add a hardware timer that connects to the hardware reset of the device and can be reset bij an alive message.
Thank you for the response, do you have a link to the NodeMCU please? How did your make a reboot instruction through Node Red to be sent to the MCU exactly?
I do not have a direct link to NodeMCU but since I am running and programming it as an Arduino device, https://www.arduino.cc/ is most likely the best place to start.
If you want to buy one, checkout E-bay, Ali Express or Bangood, many different types, will cost you between $3 and $10 a piece.
Let me post some snippits (just bits and pieces, not the full IDE)of the Arduino sketch that might be helpfull.
Make sure to use the PubSubClient for MQTT
#include <PubSubClient.h>
Subscribe to the Alive message from NodeRED
const char* Subscribe_Topic_4 = "Wallswitch/1/softreset";
client.subscribe(Subscribe_Topic_4);
Define the function to reset the arduino
void(* resetFunc) (void) = 0;
Define a watchdog timer
long WatchdogTimer = millis();
In the callback that responds to incomming MQTT messages, take action if the alive message is received
On a regular interval NodeRED sends out a message on the watchdog topic, this will reset the watchdog timer to the current time
if (strcmp(topic, Subscribe_Topic_4)==0) {
WatchdogTimer = millis();
}
If the watchdogtimer is not reset, at some point in time the difference will be >20 seconds and it is time to reset the arduino.
if (now-WatchdogTimer > 20000)
resetFunc();
}
Define the topic for the Alive Message sent out by the Arduino
const char* TopicWatchDog = "Wallswitch/1/watchdog";
Send a message to NodeRED every 10 seconds. This message will reset the watchdog timer running in NodeRED
if (now - lastMsg > 10000) {
lastMsg = now;
client.publish(TopicWatchDog, "Alive");
}
In NodeRED I have used a MyTimeOut node that counts down from 20 sec's to zero.
The input is wired to an MQTT in node and subscribed to Wallswitch/1/watchdog
The output of the timer will send a alive message to Wallswitch/1/softreset everytime the timer is reset.