Raspberry PI to Detect Network Bandwidth (using NodeJS)

2 minute read

Couple of weeks ago I did an experiment on my Raspberry PI 2 to detect the speed of my ISP using a lot of Shell scripts. The end result was quite satisfying as I could post a tweet like the one below to let my ISP know that the Internet speed at my home wasn't good enough. I had few follow-ups with my ISP's support team and now the Internet speed is quite good

I thought over it to automate this and I had two options – cron jobs or move this to more managed code like NodeJS. I tried multiple libraries and finally nailed on npm library speedtest-net.

var speedTest = require('speedtest-net');
var test = speedTest({ maxTime: 5000 });
test.on('testserver', function (server) {
        pingTime = server.bestPing;
});

test.on('data', function (data) {
         var downloadSpeed = data.speeds.download + ' Mbps';
         var uploadSpeed = data.speeds.upload + ' mbps';

         console.log("Download speed", downloadSpeed);
         console.log("Upload speed", uploadSpeed);
});

test.on('error', function (error) {
         console.error(error);
});

The next step was to send a tweet to Twitter. I had two options to this – use Twitter API or adopt IFTTT platform. I preferred IFTTT as it gives me options to trigger an event and associate any action for the event. So I could, today choose, to tweet the speed and could tomorrow also have it emailed or routed to another channel.

I created an API key on IFTTT Maker Channel and created a Recipe with Trigger Channel as Maker. Event Name (as shown in below screenshot) defines the event that your code will invoke

 

 

 

 

 

 

 

As part of "THAT" action, I selected my Twitter Channel to Tweet the event data. Maker Channel allows us to send 3 values "Value1", "Value2" and "Value3"

With this, I was ready to wire code to send my Network Speed to Twitter. I changed my NodeJS code to use NPM library node-ifttt-maker as below,

var IFTTT = require('node-ifttt-maker'),
         ifttt = new IFTTT('my-ifttt-maker-api-key');
var minDownloadSpeed = 50; // 50% of promised speed 100 Mbps?

// Send Alert only when speed is less than min download speed
        if (data.speeds.download < minDownloadSpeed){     
            ifttt.request({
                event: 'speedtest',
                method: 'POST',
                params: {
                    'value1': data.server.location,
                    'value2': downloadSpeed,
                    'value3': uploadSpeed
                }
            }, function (err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('OK');
                }
            });
        }

When the speed test completes (i.e. "data" function) it sends a POST request to IFTTT Maker Channel with relevant information and the Maker Channel routes it to the preferred channel (in this case Twitter). With no code change, you can change the output to Email, Facebook, GitHub, Google Drive, OneDrive, etc.

Now you can run this NodeJS code to monitor your network.

With this, running at regular intervals (not too often though) you can monitor your network speed and decide when to stream Netflix. That's how I get a seamless streaming experience.

Caution: Ethernet Ports have a bandwidth defined. Raspberry Pi (as of 20 March 2016) does not support Gigabit Ethernet so before tweeting in public check the max bandwidth your IoT device supports and do not try this over WiFi as WiFi speeds may be slower.

Updated:

Leave a Comment