Check if Mqtt disconnects

678 views
Skip to first unread message

Toshi Bass

unread,
Jan 25, 2018, 6:09:40 AM1/25/18
to Node-RED
How can I check from within a flow if Mqtt becomes disconnected ? I guess I could check for no messages within a set time period but was wondering if there was a better way?

Reason .... I have a pi connected by Wi-fi only, very very occasionally the Wi-fi drops/disconnects and does not recover, I test this on another pi using advanced ping which notifies me via prowl of the problem, however it means the only cause of action is to turn off the power to the pi, not good as it could result in curupting the sd card.

I know node red is still running as I have a external watchdog led flashing once a second .. I also have an external button which does several things depending on the time of day, what I want to do is ..... check if Mqtt is disconnected if so I’ll program the button when activated to reboot the pi.

Nick O'Leary

unread,
Jan 25, 2018, 6:26:03 AM1/25/18
to Node-RED Mailing List
You should be able to use the Status node targeted at one of your MQTT nodes - it will receive the status update messages from the node and you can respond how you wish.


--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/24de234e-ab39-4747-826c-fc46bdeff2da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cflurin

unread,
Jan 25, 2018, 7:58:04 AM1/25/18
to Node-RED
Additionally: is your power management off?

iwconfig:
...
          Power Management:off
...

Toshi Bass

unread,
Jan 25, 2018, 11:39:57 AM1/25/18
to Node-RED
Hi Nick 

sorry for delay I had to go out... I think I'm missing something, I connected a Status node to a Debug node , I pointed the Status node at a MQTT input node with a Topic of # at that point I expected to get some response from the Status node but I get nothing if I choose all Nodes in the Status node I do get some responses but as far as I see its just from Function nodes 


Hi cflurin 

good suggestion,but I had already thought about that googled it and found out how to switch power Management: off  I actually thought that had fixed the problem but this morning I had the wlan0: carrier lost issue again, I'm waiting for a new power supply just to double check if this is the problem but as I say the pi including node-red continues to run (MQTT disconnects because its on a different pi) the pi is in easy range of the router and all my other sonoffs etc which in some cases are three to four times further away from the router don't have a problem.




On Thursday, January 25, 2018 at 11:26:03 AM UTC, Nick O'Leary wrote:
You should be able to use the Status node targeted at one of your MQTT nodes - it will receive the status update messages from the node and you can respond how you wish.
On 25 January 2018 at 11:09, Toshi Bass <toshib...@gmail.com> wrote:
How can I check from within a flow if Mqtt becomes disconnected ? I guess I could check for no messages within a set time period but was wondering if there was a better way?

Reason .... I have a pi connected by Wi-fi only, very very occasionally the Wi-fi drops/disconnects and does not recover, I test this on another pi using advanced ping which notifies me via prowl of the problem, however it means the only cause of action is to turn off the power to the pi, not good as it could result in curupting the sd card.

I know node red is still running as I have a external watchdog led flashing once a second .. I also have an external button which does several things depending on the time of day, what I want to do is ..... check if Mqtt is disconnected if so I’ll program the button when activated to reboot the pi.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Walter Kraembring

unread,
Jan 25, 2018, 12:09:02 PM1/25/18
to Node-RED
This is an old suggestion I found some years ago but it seems to help, I am running this since years in my pi 2's (seems not yet needed in pi 3's with built in wifi but it should work there as well)

Script: checkwifi.sh (change to your ip), I did put into /home/pi

#!/bin/bash

ping -c4 192.168.10.254 > /dev/null
 
if [ $? != 0 ] 
then
  echo "No network connection, restarting wlan0"
  /sbin/ifdown 'wlan0'
  sleep 5
  /sbin/ifup --force 'wlan0'
fi


I have added this line to my crontab (crontab -e)

*/5 *   * * *   root    /home/pi/checkwifi.sh

So every 5 minutes, the wifi connection is checked and if needed restarted

Colin Law

unread,
Jan 25, 2018, 12:10:58 PM1/25/18
to node...@googlegroups.com
Have you got the debug node showing the complete message?

Does the mqtt node show the green blob and Connected?

Which versions of node and node-red are you running (seen in the node red startup log).

Colin

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.

cflurin

unread,
Jan 25, 2018, 12:11:10 PM1/25/18
to Node-RED
Check /etc/network/interfaces
 
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    wireless-power off
    post-up iw wlan0 set power_save off

Toshi Bass

unread,
Jan 25, 2018, 1:28:43 PM1/25/18
to Node-RED
Hi Colin 

Raspberry pi3

Yes Debug node is set to complete message   (There is no output from the Status node  if I connect a Debug node to the MQTT node with msg.topic # I get loads of msgs as expected)

Yes MQTT node shows green blob

Jan 25 08:17:10 raspberrypi4 Node-RED[462]: 25 Jan 08:17:10 - [info] Node-RED version: v0.17.5
Jan 25 08:17:10 raspberrypi4 Node-RED[462]: 25 Jan 08:17:10 - [info] Node.js  version: v8.9.4
Jan 25 08:17:10 raspberrypi4 Node-RED[462]: 25 Jan 08:17:10 - [info] Linux 4.9.35-v7+ arm LE
Jan 25 09:07:58 raspberrypi4 Node-RED[462]: 25 Jan 09:07:58 - [info] Dashboard version 2.7.0 started at /ui
Jan 25 09:07:59 raspberrypi4 Node-RED[462]: 25 Jan 09:07:59 - [info] Settings file  : /home/pi/.node-red/settings.js
Jan 25 09:07:59 raspberrypi4 Node-RED[462]: 25 Jan 09:07:59 - [info] User directory : /home/pi/.node-red
Jan 25 09:07:59 raspberrypi4 Node-RED[462]: 25 Jan 09:07:59 - [info] Flows file     : /home/pi/.node-red/flows_raspberrypi4.json
Jan 25 09:07:59 raspberrypi4 Node-RED[462]: 25 Jan 09:07:59 - [info] Server now running at http://127.0.0.1:1880/


Hi Walter 

Seams the wifi connection is attempting to reconnect already but is un-successful here's some of the log...

Jan 25 08:04:22 raspberrypi4 dhcpcd[519]: wlan0: carrier lost
Jan 25 08:04:22 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-DISCONNECTED bssid=24:20:c7:96:4a:de reason=0 locally_generated=1
Jan 25 08:04:22 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jan 25 08:04:22 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=GB
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting address fdaa:bbcc:ddee:0:f7d0:eeea:fc60:9415/64
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Withdrawing address record for fdaa:bbcc:ddee:0:f7d0:eeea:fc60:9415 on wlan0.
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Leaving mDNS multicast group on interface wlan0.IPv6 with address fdaa:bbcc:ddee:0:f7d0:eeea:fc60:9415.
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting address 2a00:23c4:8781:ed00:bb3f:685c:5727:f38a/64
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Joining mDNS multicast group on interface wlan0.IPv6 with address fd97:6eb7:6453:1:19d4:e776:346:7050.
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Withdrawing address record for 2a00:23c4:8781:ed00:bb3f:685c:5727:f38a on wlan0.
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting default route via fe80::2620:c7ff:fe96:4adb
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting route to 2a00:23c4:8781:ed00::/64
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting route to fdaa:bbcc:ddee::/64
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting address fe80::2868:c754:5650:ae8d
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Withdrawing address record for 192.168.0.239 on wlan0.
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Leaving mDNS multicast group on interface wlan0.IPv4 with address 192.168.0.239.
Jan 25 08:04:23 raspberrypi4 avahi-daemon[456]: Interface wlan0.IPv4 no longer relevant for mDNS.
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting route to 192.168.0.0/24
Jan 25 08:04:23 raspberrypi4 dhcpcd[519]: wlan0: deleting default route via 192.168.0.254
Jan 25 08:04:23 raspberrypi4 wpa_supplicant[607]: wlan0: Trying to associate with 24:20:c7:96:4a:de (SSID='xxxxxxxx' freq=2412 MHz)
Jan 25 08:04:23 raspberrypi4 wpa_supplicant[607]: wlan0: Associated with 24:20:c7:96:4a:de
Jan 25 08:04:23 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=GB
Jan 25 08:04:24 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-DISCONNECTED bssid=24:20:c7:96:4a:de reason=0 locally_generated=1
Jan 25 08:04:24 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jan 25 08:04:24 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=GB
Jan 25 08:04:24 raspberrypi4 wpa_supplicant[607]: wlan0: Trying to associate with 24:20:c7:96:4a:de (SSID='xxxxxxxx' freq=2412 MHz)
Jan 25 08:04:24 raspberrypi4 wpa_supplicant[607]: wlan0: Associated with 24:20:c7:96:4a:de
Jan 25 08:04:25 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=GB
Jan 25 08:04:26 raspberrypi4 ntpd[748]: Deleting interface #9 wlan0, fdaa:bbcc:ddee:0:f7d0:eeea:fc60:9415#123, interface stats: received=0, sent=0, dropped=0, active_time=135186 secs
Jan 25 08:04:26 raspberrypi4 ntpd[748]: Deleting interface #8 wlan0, 2a00:23c4:8781:ed00:bb3f:685c:5727:f38a#123, interface stats: received=0, sent=0, dropped=0, active_time=135186 secs
Jan 25 08:04:26 raspberrypi4 ntpd[748]: Deleting interface #7 wlan0, fe80::2868:c754:5650:ae8d#123, interface stats: received=0, sent=0, dropped=0, active_time=135188 secs
Jan 25 08:04:26 raspberrypi4 ntpd[748]: Deleting interface #6 wlan0, 192.168.0.239#123, interface stats: received=2548, sent=2702, dropped=0, active_time=135188 secs
Jan 25 08:04:26 raspberrypi4 ntpd[748]: 193.150.34.2 interface 192.168.0.239 -> (none)
Jan 25 08:04:26 raspberrypi4 ntpd[748]: 195.219.205.9 interface 192.168.0.239 -> (none)
Jan 25 08:04:26 raspberrypi4 ntpd[748]: 178.62.16.103 interface 192.168.0.239 -> (none)
Jan 25 08:04:26 raspberrypi4 ntpd[748]: 85.199.214.100 interface 192.168.0.239 -> (none)
Jan 25 08:04:26 raspberrypi4 ntpd[748]: peers refreshed
Jan 25 08:04:30 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-DISCONNECTED bssid=24:20:c7:96:4a:de reason=0 locally_generated=1
Jan 25 08:04:30 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jan 25 08:04:30 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=GB
Jan 25 08:04:31 raspberrypi4 wpa_supplicant[607]: wlan0: Trying to associate with 24:20:c7:96:4a:de (SSID='xxxxxxxx' freq=2412 MHz)
Jan 25 08:04:31 raspberrypi4 wpa_supplicant[607]: wlan0: Associated with 24:20:c7:96:4a:de
Jan 25 08:04:31 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=GB
Jan 25 08:04:31 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-DISCONNECTED bssid=24:20:c7:96:4a:de reason=0 locally_generated=1
Jan 25 08:04:31 raspberrypi4 wpa_supplicant[607]: wlan0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="xxxxxxxx" auth_failures=1 duration=10 reason=CONN_FAILED
carry's on like this then 
Jan 25 08:06:26 raspberrypi4 Node-RED[459]: 25 Jan 08:06:26 - [info] [mqtt-broker:8102368a.f2b1c8] Disconnected from broker: mqtt://192.168.0.98:1883
Jan 25 08:06:40 raspberrypi4 Node-RED[459]: 25 Jan 08:06:40 - [error] [prowl:43fb9eca.96dee] Error: getaddrinfo ENOTFOUND api.prowlapp.com api.prowlapp.com:443
Jan 25 08:06:40 raspberrypi4 Node-RED[459]: 25 Jan 08:05:40 - [info] [prowl:43fb9eca.96dee] null calls to Prowl api during current hour.
Jan 25 08:08:39 raspberrypi4 Node-RED[459]: 25 Jan 08:08:39 - [error] [forecastio:9b960553.668018] Error: getaddrinfo ENOTFOUND api.forecast.io api.forecast.io:443

Then unfortunately that's when I switched the pi off (because the Blinds hadn't opened) I did'nt know how long the wifi hadn't been connected but it was clear as I couldnt connect to the node-red web site or my ui, but perhaps I should have left it longer to get more info.

Hi cflurin This is  /etc/network/interfaces  ive not changed anything in here the only thing I changed a few weeks ago as power save was set to on,  is in /etc/rc.local were I added a line /sbin/iw dev wlan0 set power_save off 

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Nick O'Leary

unread,
Jan 25, 2018, 1:54:41 PM1/25/18
to node...@googlegroups.com

Toshi,

What are you expecting to get from the status node? It should send a message when the status (connected/disconnected) of the mqtt node changes. What the node is subscribed to is irrelevant - that doesn't affect the node status


Toshi Bass

unread,
Jan 25, 2018, 2:44:28 PM1/25/18
to node...@googlegroups.com
Oh ok only some output when disconnect / reconnect happens .... I was expecting similar to when you target a function node like output every time a payload goes thro it. ok I will test tomorrow thanks. 

Sent from my iPhone
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/-Rzx1Q6O6YM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Jan 25, 2018, 4:54:38 PM1/25/18
to node...@googlegroups.com
You should also get an o/p when you do a full deploy as it initially connects.

Colin


To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/9013d247-c99b-43b0-b4db-f90989618f1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/-Rzx1Q6O6YM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Stephen Mann

unread,
Jan 25, 2018, 11:00:30 PM1/25/18
to Node-RED
I know I am a newbie here, but, what about the Ping node?

If I read the question correctly, Toshi is only asking if the MQTT broker connection is good.
Ping the broker, if the payload is false, then the MQTT connection is down.

Steve Mann

Toshi Bass

unread,
Jan 26, 2018, 2:51:58 AM1/26/18
to node...@googlegroups.com
Colin ... right, that makes it a little easier to test it thanks.

Steve ... thanks for you response I already use ping from another pi to notify me that pi4 is offline, what I am after is detecting when the wi-fi has disconnected in the node-red that’s running on pi4 so that I can utilise my one and only external push button that normally does other things to become a reboot button, instead of the current situation were if and I stress if because it’s a very rare occurrence, if pi4 looses Wi-fi my only cause of action is to disconnect the power to pi4. 
I know I should and will try to fix the route cause of the problem, so having a safe way to reboot pi4 when this issue arises will help that process.

Sent from my iPhone
--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/-Rzx1Q6O6YM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Jan 26, 2018, 4:17:01 AM1/26/18
to node...@googlegroups.com
I have similar problems with wifi on pis that stops, maybe once every couple of months. The only solution I found was to reboot. I do this automatically by running a watchdog flow in node-red in the pi that has the problem and pinging the main router every few minutes. I feed a *good* ping result into a Trigger node set to send initially nothing then after 10 mins send a message. With Extend Delay if new message arrives set then the result of this is that provided the pings keep working the trigger node sends nothing, but if they stop for 10 minutes then it sends a message.  I then feed the output of the trigger node into an Exec node that runs sudo reboot.  In addition I have an Inject node that kicks the Trigger on startup, so that it is guaranteed to start the timer.

In order to allow the sudo command to run reboot without asking for a password you may need to edit the sudoers file by running

sudo visudo

then adding on the end
nodereduser ALL=(ALL) NOPASSWD: /sbin/reboot

where nodereduser is the user that runs node red

The only unfortunate side effect of the watchdog flow is that if the main router is off then the pi reboots every 10 mins, but in my case that is not an issue.  If you want it to only reboot once if the router is switched off then leave out the inject node.

Colin


On 26 January 2018 at 07:51, Toshi Bass <toshib...@gmail.com> wrote:
Colin ... right, that makes it a little easier to test it thanks.

Steve ... thanks for you response I already use ping from another pi to notify me that pi4 is offline, what I am after is detecting when the wi-fi has disconnected in the node-red that’s running on pi4 so that I can utilise my one and only external push button that normally does other things to become a reboot button, instead of the current situation were if and I stress if because it’s a very rare occurrence, if pi4 looses Wi-fi my only cause of action is to disconnect the power to pi4. 
I know I should and will try to fix the route cause of the problem, so having a safe way to reboot pi4 when this issue arises will help that process.

Sent from my iPhone

On 26 Jan 2018, at 04:00, Stephen Mann <mann....@gmail.com> wrote:

I know I am a newbie here, but, what about the Ping node?

If I read the question correctly, Toshi is only asking if the MQTT broker connection is good.
Ping the broker, if the payload is false, then the MQTT connection is down.

Steve Mann



On Thursday, January 25, 2018 at 6:09:40 AM UTC-5, Toshi Bass wrote:
How can I check from within a flow if Mqtt becomes disconnected ? I guess I could check for no messages within a set time period but was wondering if there was a better way?

Reason .... I have a pi connected by Wi-fi only, very very occasionally the Wi-fi drops/disconnects and does not recover, I test this on another pi using advanced ping which notifies me via prowl of the problem, however it means the only cause of action is to turn off the power to the pi, not good as it could result in curupting the sd card.

I know node red is still running as I have a external watchdog led flashing once a second .. I also have an external button which does several things depending on the time of day, what I want to do is ..... check if Mqtt is disconnected if so I’ll program the button when activated to reboot the pi.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/-Rzx1Q6O6YM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/4dfb7340-89c7-4328-9e24-f6be5ca23d33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

cflurin

unread,
Jan 26, 2018, 4:53:38 AM1/26/18
to Node-RED

The only unfortunate side effect of the watchdog flow is that if the main router is off then the pi reboots every 10 mins, but in my case that is not an issue.  If you want it to only reboot once if the router is switched off then leave out the inject node.


To avoid ping repeating actions I use a function that count "ping false".
1. sometime the ping node returns false because the pi or the net is busy
2. after 3 "ping false" the function sends the output once. 

var msg1 = {};
var ip = msg.topic;

var count = flow.get('count') || {};
if (typeof count[ip] === "undefined") {
    count[ip] = 0;
}

if (msg.payload === false) {
    count[ip] = count[ip] + 1;
    if (count[ip] > 3) {
        msg1 = null;
    } else if (count[ip] > 2) {
        msg1.payload = ip + " ping failed, count " + count[ip];
        msg1.topic = "ping failed";
    } else {
        msg1 = null;
    }
} else {
    if (count[ip] > 2) {
        msg1.payload = ip + " ping ok";
        msg1.topic = "ping ok";
        count[ip] = 0;
    } else {
        msg1 = null;
    }
}
flow.set('count', count);

return msg1;

Colin Law

unread,
Jan 26, 2018, 5:26:53 AM1/26/18
to node...@googlegroups.com
On 26 January 2018 at 09:53, cflurin <fluri...@gmail.com> wrote:

The only unfortunate side effect of the watchdog flow is that if the main router is off then the pi reboots every 10 mins, but in my case that is not an issue.  If you want it to only reboot once if the router is switched off then leave out the inject node.


To avoid ping repeating actions I use a function that count "ping false".
1. sometime the ping node returns false because the pi or the net is busy
2. after 3 "ping false" the function sends the output once. 


In my flow, using the ping and trigger nodes, if you set the ping rate to, for example, every two minutes, and the trigger timeout to 8 minutes then it would need 3 or 4 pings in a row to cause the reboot.  You can adjust the rate and timeout to get the effect you want.

Colin
 

cflurin

unread,
Jan 26, 2018, 5:31:47 AM1/26/18
to Node-RED
Right, but the function's msg is null (no output) until ping is true again. 
 

Colin Law

unread,
Jan 26, 2018, 5:37:39 AM1/26/18
to node...@googlegroups.com
If you make it trigger a reboot then won't it re-trigger after it has booted if the ping continues to fail?  I think it's operation is exactly the same as mine.

Colin

 
 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

cflurin

unread,
Jan 26, 2018, 5:40:50 AM1/26/18
to Node-RED
I see, right. In my case I don't reboot but I send an email.


On Friday, January 26, 2018 at 11:37:39 AM UTC+1, Colin Law wrote:
On 26 January 2018 at 10:31, cflurin <fluri...@gmail.com> wrote:


On Friday, January 26, 2018 at 11:26:53 AM UTC+1, Colin Law wrote:
On 26 January 2018 at 09:53, cflurin <fluri...@gmail.com> wrote:

The only unfortunate side effect of the watchdog flow is that if the main router is off then the pi reboots every 10 mins, but in my case that is not an issue.  If you want it to only reboot once if the router is switched off then leave out the inject node.


To avoid ping repeating actions I use a function that count "ping false".
1. sometime the ping node returns false because the pi or the net is busy
2. after 3 "ping false" the function sends the output once. 


In my flow, using the ping and trigger nodes, if you set the ping rate to, for example, every two minutes, and the trigger timeout to 8 minutes then it would need 3 or 4 pings in a row to cause the reboot.  You can adjust the rate and timeout to get the effect you want.

Colin

Right, but the function's msg is null (no output) until ping is true again. 

If you make it trigger a reboot then won't it re-trigger after it has booted if the ping continues to fail?  I think it's operation is exactly the same as mine.

Colin

 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Jan 26, 2018, 5:42:33 AM1/26/18
to node...@googlegroups.com
Just a note (intended to be helpful) about your function, I think you could simplify it somewhat if you just stored the count in the context rather than a message containing the count.  Also I suggest storing it in the node context rather than the flow.  If you wanted two of these, pinging different hosts, on the same flow then using the flow context the count variable would be common between them which would cause a certain amount of confusion.

var count = context.get('count') || 0

then you don't need to the typeof check and the access is simpler.

A minor pint is that it is (I think) considered good practice to re-use the existing msg object rather than creating a new one if it does not complicate the code to do that.

Colin

On 26 January 2018 at 09:53, cflurin <fluri...@gmail.com> wrote:

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

cflurin

unread,
Jan 26, 2018, 5:52:50 AM1/26/18
to Node-RED
flow or global? well all my ping nodes are in the same flow and the count as an index = ip-address, so it works for me.
msg or msg1? right, msg can be reused.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Jan 26, 2018, 6:23:01 AM1/26/18
to node...@googlegroups.com
On 26 January 2018 at 10:52, cflurin <fluri...@gmail.com> wrote:
flow or global? well all my ping nodes are in the same flow and the count as an index = ip-address, so it works for me.

Ah, I see what you are doing now, yes it does work, you are right.  I still think it would be simpler to use context.get/set and save the count rather than an object.  What is the advantage of using flow context and saving all the counts in one object?  The disadvantage is more complex code.

Colin
 

Colin Law

unread,
Jan 26, 2018, 7:13:42 AM1/26/18
to node...@googlegroups.com
I have just realised, are you feeding multiple ping nodes into this one function node?  If so then yes, that is a good way to do it.  Even so, I don't see why you are using flow.get/set instead of context.get/set.  The only reason to use flow is if you want to share it across multiple nodes in the same tab.

Colin
 

Colin
 

cflurin

unread,
Jan 26, 2018, 7:21:11 AM1/26/18
to Node-RED
Correct, I'm using six ping nodes with the same function.

Probably it would also work with a context object too. So just change flow by context.
 

cflurin

unread,
Jan 26, 2018, 7:28:09 AM1/26/18
to Node-RED

Correct, I'm using six ping nodes with the same function.

Probably it would also work with a context object too. So just change flow by context.
 
*change = replace 

cflurin

unread,
Jan 26, 2018, 7:40:38 AM1/26/18
to Node-RED
Now I remember the reason I use flow instead of context. I use "flow.count" in an another node.

Toshi Bass

unread,
Jan 26, 2018, 9:16:19 AM1/26/18
to Node-RED
Well I got both methods working like : checking MQTT via status node and checking router (used is online node instead of ping node)  i am going to leave them both running and make a decision later which to ultimately use thanks for all your help and advice.

Colin Law

unread,
Jan 26, 2018, 10:25:37 AM1/26/18
to node...@googlegroups.com
On 26 January 2018 at 14:16, Toshi Bass <toshib...@gmail.com> wrote:
Well I got both methods working like : checking MQTT via status node and checking router (used is online node instead of ping node)  i am going to leave them both running and make a decision later which to ultimately use thanks for all your help and advice.

The isonline node tells you whether you are connected to the internet.  I though you just needed to know that you were connected to the local network via wifi. If so then use ping, otherwise it will reboot the pi when all is working locally but you cannot get out onto the internet.

Colin
 





On Friday, January 26, 2018 at 12:40:38 PM UTC, cflurin wrote:
Now I remember the reason I use flow instead of context. I use "flow.count" in an another node.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
Jan 26, 2018, 10:46:39 AM1/26/18
to node...@googlegroups.com
Colin

I think isonline actually checks for a tcp connection to a port - so can be pointed at a local server just as well as a remote one.
(The point being to check if the service behind the port is online, not just the machine being up)

Colin Law

unread,
Jan 26, 2018, 11:09:31 AM1/26/18
to node...@googlegroups.com
Dave

I see you are right, I thought that when I tried it some time ago it did not allow entry of a specific url. Either I mis-remember, or it was a different node, or the feature has been added.  node-red-contrib-isonline now looks very useful, and easier to use than ping.

Thanks

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Walter Kraembring

unread,
Jan 26, 2018, 12:19:04 PM1/26/18
to Node-RED
Myself I have a bit different solution but in a way similar

As part of my home automation & security system, I am running four Pi3's with Motion video monitoring software, each Pi handles 2 video cameras. In every Pi I have a Python script with a number of threads running to allow me to change mode of operation from Node-RED (NR). The alarm system (Verisure) that is integrated with NR can have 3 modes of operation: unarmed - armedhome - armed (away) and we do not record video on motion detect while we are home. The mode change is done completely automatic via the script. The script handles also other commands. In addition, it sends information back to NR. MQTT is used for this two-way communication with NR.

One of the commands used is a pure heartbeat sent to the script that responds back if everything is fine. In NR I have a trigger node (for each pi or service I wanna monitor) and if this triggers, it sends me an alarm via telegram. So as long there is no alarm in my IPhone, I know that the connection and the FUNCTIONALITY of the service is working as expected and running fine, not only that a port and a wifi connection seems up

So what I'm trying to say, the best is if you can monitor a service that is expected to deliver data at certain interval, then you know the whole chain is intact, this is the way I monitor all my services regardless if they are running in the same pi or distributed

Toshi Bass

unread,
Jan 26, 2018, 1:13:44 PM1/26/18
to Node-RED
Walter 

In reality I do the same thing I monitor all my pi's and get notification if one goes of line for whatever reason the point is what do you do when that actually happens, for whatever reason I very very occasional one of my pi's that only connects via wifi drops it connection (apparently I am not the only person that suffers from this), I can see from the log it attempts several times to reconnect but its un-successful, I know for sure node-red etc is still running its just the wifi connection that has stopped,  the only way to reboot that pi is to disconnect the power, but its well known that pulling the power to a functioning pi can and does corrupt the sd card so its to be avoided as much as possible, and that's what the whole of this topic is about, finding some way of checking the wifi connection is connected or not on the same same pi in node-red and if its not to automatically reboot it, NOTE I know I could easterly put a external button on the pi connected to a GPIO pin and monitor that which when pressed could reboot the pi (but aesthetically I don't want another button) my first idea was to use the one existing button re-purpose it on detecting the problem to reboot the pi , however after getting some great ideas from this topic I ended up with a automated reboot which means it should not need intervention from me like if am away from home for instance.   

whilst I want to get to the bottom of this loss of wifi to trace the issue I need something like the above to enable me to recreate and recover from the issue safely.

cflurin

unread,
Jan 26, 2018, 2:05:26 PM1/26/18
to Node-RED

... for whatever reason I very very occasional one of my pi's that only connects via wifi drops it connection ...

How long does the pi run? days, weeks, months?

I' running my pis for months without any issue. Raspbian stretch 4.9.59-v7+, node.js v8.9.4, node-red v0.17.5
So, there may be an hardware issue or it depends on your os, node versions.

Colin Law

unread,
Jan 26, 2018, 5:05:57 PM1/26/18
to node...@googlegroups.com
I have the same issue, maybe once every couple of months.   The wifi drops out a lot more often but usually re-connects, just very occasionally it won't.  Apart from the wifi not re-connecting everything else continues ok.  A reboot always fixes it.  I believe I only see it on the Pis that are distant from the router and so are prone to dropout in the first place.  Now that I have implemented the watchdog which automatically reboots if the wifi stays off for several minutes then it is not an issue.

Colin
 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Walter Kraembring

unread,
Jan 27, 2018, 1:11:24 AM1/27/18
to Node-RED
Toshi, got it,
I experienced this earlier on with one pi 2 equipped with a usb wifi dongle. In my case, the script I wrote above helped out fine (just taking wifi down/up), it was not needed with a full reboot. I suspected some wifi dongle driver was the cause.
With pi3's with built in wifi I have not seen a similar problem

Colin Law

unread,
Jan 27, 2018, 8:22:06 AM1/27/18
to node...@googlegroups.com
Although I said that the only fix for me was to reboot, in fact I did not try taking the wifi down/up. A reboot is not a big deal on these systems so I just went for that.

I think the ones I have seen it on have all been with usb wifi, so indeed that could be a factor.

Colin
 

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages