Track Broadband Outage in Node Red

connectionActually I could have given this a range of titles – as the following is useful in a range of situations. But first let me explain my problem:

The problem: I’ve been having broadband issues. The broadband cuts out for a few minutes without warning and then reconnects.  I just wasted a lot of time getting onto my router people (TP-Link – they are quite helpful) only to discover the same thing happens with the original rubbish Plusnet router (we’re using high speed fibre-to-cabinet broadband, around 70 meg or so – but it still looks like a normal phone line connection coming into the house so not TOO many modems support this – TP-Link does).

Of course Plusnet and BT say “nothing wrong here mate” and the problem really is intermittent – not had a failure in 2 days now but last week had several in one day. 

The solution: Well, not so much a solution but at least a means to easily monitor any future issues so I can act on them if things get out of hand. The answer for now is to log these events. I set up a routine to log failures by pinging Google every 30 seconds – of course it takes the modem 2 or 3 minutes to recover from a loss of connection so that log was getting large. By version 2 I was only logging CHANGE of state.

Node-Red SQL inject

Above is how I store the info in SQLITE. I use a PING node and the SQLITE node…  and here’s that function in the middle.

context.global.shot=context.global.shot|0;

if (msg.payload===false)
{
if (context.global.shot===0)
    {
        context.global.shot=1;
        msg.topic="insert into pings (ping) VALUES(" + 0 + ")";
        return msg;
    }
}
else
{
if (context.global.shot==1)
    {
        context.global.shot=0;
        msg.topic="insert into pings (ping) VALUES(" + 1 + ")";
        return msg;
    }
}

The actual table is in a database called IOT.DB and here’s the table.

CREATE TABLE 'pings' ('thetime' DATETIME PRIMARY KEY NOT NULL DEFAULT CURRENT_TIMESTAMP, 'ping' INTEGER)

So that’s simply storing away the date and time – and 0 for disconnect, 1 for reconnect.

To make that useful what I needed was for the computer – at one minute to midnight – to send me a report FOR THAT DAY but only if there was actually something to report. That I’ve done below.

image

The first (blue) node is simply a Node-Red inject that sends an empty message at one minute to midnight every night. The second is a function to build up a SQLITE query to get all the events for that day. The last one is an email node set up to send me emails on Gmail.

var now = new Date();
function pad(n) {
    return (n < 10) ? ("0" + n) : n;
}
var today=now.getFullYear() + "-"+pad((now.getMonth()+1))+"-"+ pad(now.getDate());

msg.topic="select * from pings where thetime >= Datetime('"+ today +" 00:00:00') and thetime <= Datetime('"+ today+" 23:59:59')";
return msg;

And finally having sent that off to the SQLITE node, I get an array back of the dates and states… which I send off to myself in email.

msg.topic="Hollyberry Connection Notice";
var a=msg.payload;
msg.payload="";

if (a.length!==0)
{
    for (var i in a)
    {
    msg.payload+="\r\n"+ ((a[i].ping !== 0) ? "Regained" : "Lost") +" connection " + a[i].thetime;
    }
return(msg);  
}

And that, hopefully will keep me informed of any issues with the broadband while we’re away enjoying the sun next week. Hope you find that useful – I’m sure there are many variations of this which might be useful for general monitoring.

Node-Red constantly surprises me with how simple it is to do just about anything I want.

Facebooktwittergoogle_pluspinterestlinkedin

3 thoughts on “Track Broadband Outage in Node Red

  1. This gave me an idea to help solve an issue I have with my Pi and node red.
    Every now and again I cannot access my node red or node red UI data from my tablet, the connection times out, I look at the Pi and it's still connected to my wireless network but I cannot connect to it.

    If I try a couple of hours later t's fine or if I reboot it it's fine so I'm going to use the ping node to check for a connection and if no connection after a minute say I'll reboot the Pi.

    Also if I trigger an pushbullet notification on node red start up I should see how often it reboots, if at all.

Leave a Reply

Your email address will not be published. Required fields are marked *