Skip to main content

Raspberry Pi to monitor broadband speed

The Five Challenges SMEs Face Due To Poor Internet Connectivity -  IntelligentHQ


How to use your Raspberry Pi to monitor broadband speed

Step 1: Check for updates

As usual, let’s start with checking for the latest updates. Run the following commands in Terminal:

sudo apt-get update
sudo apt-get upgrade

Step 2: Install speedtest-cli

There are various methods to measure the speed of broadband connection. We’re going to use speedtest-cli, which, in the words of its creator, is “a command line interface for testing internet bandwidth using speedtest.net.”

speedtest-cli isn’t available right away from the Raspbian archives, but we can install it from the Python Package Index (PyPI). That’s easy to do so by using a tool called pip that comes preinstalled on Raspbian Jessie and Stretch. You can make sure that you have pip by running this command:

sudo apt-get install python-pip

If you a message that reads “python-pip is already the newest version,” that means that we’re good to go.

Next, let’s use pip to install speedtest-cli:

sudo pip install speedtest-cli

With speedtest-cli now installed, you can measure your broadband speed easily with this command:

speedtest-cli

Though, for our purposes, using speedtest-cli’s simple mode is more convenient:

speedtest-cli --simple

You should see something like this:

Ping: 47.943 ms
Download: 40.93 Mbit/s
Upload: 2.33 Mbit/s

However, that output doesn’t conform to the CSV syntax. Therefore, we’ll need to parse the data and make it conform.

Step 3: Create a Python script

Let’s create a new Python file:

sudo nano speedtest.py

Here’s what we should have inside the file (you can, of course, copy-paste these lines):

import os
import re
import subprocess
import time

response = subprocess.Popen('speedtest-cli --simple', shell=True, stdout=subprocess.PIPE).stdout.read()

ping = re.findall('Ping:\s(.*?)\s', response, re.MULTILINE)
download = re.findall('Download:\s(.*?)\s', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\s', response, re.MULTILINE)

ping[0] = ping[0].replace(',', '.')
download[0] = download[0].replace(',', '.')
upload[0] = upload[0].replace(',', '.')

try:
    if os.stat('/home/pi/speedtest/speedtest.csv').st_size == 0:
        print 'Date,Time,Ping (ms),Download (Mbit/s),Upload (Mbit/s)'
except:
    pass

print '{},{},{},{},{}'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M'), ping[0], download[0], upload[0])

(I’m assuming that you’re saving the script in the /home/pi/ directory – if not, just change the path here: if os.stat(‘/home/pi/speedtest/speedtest.csv’).st_size == 0:.)

After you have the lines in place, you can save the file and exit the editor by pressing Ctrl+X, Y, and Enter.

The script runs speedtest-cli in its simple mode, parses the output, and prints it out in CSV format. You can run the script with the following command:

python speedtest.py

And if you do, you should see a line like this:

10/26/17,10:18,47.943,40.93,2.33

Alright, we’re ready to move on!

Step 4: Create a directory

Let’s create a directory for our CSV file:

mkdir speedtest

If you’re wondering why we need a directory for a single file, it’s because we’re going to sync that directory to Google Drive. Once synced, everything inside the directory will correspond the contents of our similar Google Drive directory.

Now, if we run our Python script like this:

python speedtest.py >> speedtest/speedtest.csv

…we will have a CSV file with broadband speed data in our new directory.

If you check the contents of the file (for example, cat speedtest/speedtest.csv), you can see lines like these:

Date,Time,Ping (ms),Download (Mbit/s),Upload (Mbit/s)
10/26/17,10:18,47.943,40.93,2.33

Great!

Step 5: Integrate the script with Google Drive

In order to integrate our script with Google Drive, we can use GitHub user Petter Rasmussen’s Google Drive CLI Client. Let’s download its Raspberry Pi version by running the following command:

wget -O gdrive https://docs.google.com/uc?id=0B3X9GlR6EmbnVXNLanp4ZFRRbzg&export=download

Next, let’s assign the file executable rights:

chmod +x gdrive

We’ll need to tell Google Drive to allow Google Drive CLI Client to connect to our account. We can do so by running Google Drive CLI Client with any parameter. For example, this command lists the contents of a Google Drive account:

./gdrive list

Just follow the instructions: Go to the URL in your browser, sign in to your Google account, and allow “GDrive (…) to [v]iew and manage the files in your Google Drive.” Then you’re ready to enter the verification code.

Finally, gdrive will list the contents of your Google Drive.

Now that we have our speedtest directory on the Raspberry Pi, let’s create a corresponding directory to our Google Drive:

./gdrive mkdir speedtest

The program returns the ID of our new directory. Copy that, since we’ll need it.

Next, we’re going to sync our two speedtest directories:

./gdrive sync upload speedtest ID

Just replace ID with the ID your speedtest directory.

If everything went as supposed, you’ll now see a directory called speedtest in your Google Drive. In the directory, there’s the file we created earlier (speedtest.csv):

 

Now, if we run the Python script again, new broadband speed data will be appended to the end of the file:

python speedtest.py >> speedtest/speedtest.csv

And if we run the sync command again, we can see our updated file in our Google Drive:

./gdrive sync upload speedtest ID

(Again, remember to replace ID with the ID of your speedtest directory.)

 

Step 6: Automate everything

We’re almost ready! All we have left to do is to make everything work automatically. For this, we’ll use cron, which makes it possible to schedule commands to run at specified times, like once an hour.

Let’s make a short shell script containing the commands we need to run once an hour:

sudo nano speedtest-cron.sh

Add in the following, familiar commands (I’m assuming that you’ve done everything in the /home/pi/ directory – if not, just change the paths):

sudo python /home/pi/speedtest.py >> /home/pi/speedtest/speedtest.csv
/home/pi/gdrive sync upload speedtest ID

(And, once again, remember to replace ID with the ID of your speedtest directory.)

Then save and exit with Ctrl+X, Y, and Enter.

Let’s assign the script executable rights:

sudo chmod +x speedtest-cron.sh

Now we’re ready to test the script:

./speedtest-cron.sh

Alright, let’s create our cron job:

crontab -e

Type the following line in the editor and save and exit by pressing Ctrl+X, Y, Enter:

0 * * * * /home/pi/speedtest-cron.sh

Now our broadband speed monitor is ready. It will log the results of the speed test once an hour at the top of the hour. You can open the CSV file that’s in your Google Drive using Microsoft Excel or Google Sheets. If you prefer the latter, you’ll have to import the file (File > Import…).

 

 

 

 

 

Comments

Popular posts from this blog

How to Setup Fail2ban on the Raspberry Pi or use the Pi- Hole setup

    In this Raspberry Pi Fail2ban tutorial, we will be showing you how to set up and configure the Fail2ban software on your Raspberry Pi. Fail2ban is a crucial piece of software when it comes to improving the security of your Raspberry Pi. It is especially useful if you have your Raspberry Pi publicly accessible via the internet as it is an active and learning form of defense. For those who do not know what Fail2ban is , it is a piece of software that attempts to block malicious connections to your device, which in our case is our Raspberry Pi. It is important if you have SSH or even a web server that is publicly accessible. Fail2ban works by continually scanning your log files and looking for signs of potential attacks. These include attacks such as too many password failures as well as scanning for exploits and much more. Once it finds unusual activity it then automatically updates your firewall to ban that IP address. Equipment You will need the following eq...

Network Printer + Apple AirPrint Server on your Raspberry Pi ******Featured Information********

        On this project, we will be showing you how to set up your Raspberry Pi as an AirPrint server.  AirPrint is a printing protocol designed by Apple so that their devices would not need to download or install drivers. By following this guide, you will be able to add AirPrint functionality to any printer that is connected to your Raspberry Pi. This project is an excellent way of adding extra functionality to both cheap and old printers. Implementing AirPrint support will allow you to print from your Apple device to your connected printer easily. Before following this project, you will need to have first set up the CUPS software on your device. Cups is what will act as the brains of your AirPrint server on your Raspberry Pi. It will handle the communication between your Raspberry Pi and the connected printer.   Equipment List Below are the pieces of equipment that you will need for this tutorial on installing AirPrint on your Raspberry Pi. Recomme...

HiMovies App Policy

   Privacy PolicyYour privacy is important to us. It is HiMovies App Policypolicy to respect your privacy regarding any information we may collect from you across our website, HiMovies App Policyand other sites we own and operate.We only ask for personal information when we truly need it to provide a service to you. We collect it by fair and lawful means, with your knowledge and consent. We also let you know why we’re collecting it and how it will be used.We only retain collected information for as long as necessary to provide you with your requested service. What data we store, we’ll protect within commercially acceptable means to prevent loss and theft, as well as unauthorised access, disclosure, copying, use or modification.We don’t share any personally identifying information publicly or with third-parties, except when required to by law.Our website may link to external sites that are not operated by us. Please be aware that we have no control over the conte...

Most Visited Of the Week

AdGuard Home on your Raspberry Pi

      Equipment Below you can view all of the equipment we used for running AdGuard Home on the Raspberry Pi. Recommended Raspberry Pi Micro SD Card (8GB+) Network Connection Optional Raspberry Pi Case USB Keyboard USB Mouse We tested this tutorial on a Pi 400 running the latest version of Raspberry Pi OS Buster. Installing AdGuard Home to your Raspberry Pi This section will show you how to install the AdGuard home software to your Raspberry Pi. This is a relatively simple process as we can download the compiled version of the ad blocker directly from their website. Before beginning, we highly recommend that you set up your Raspberry Pi with a static IP address. As AdGuard Home works by acting as a DNS server, it must retain the same IP address every time it reboots. 1. Our first step is to update and upgrade our operating system. We can update the package list and upgrade all of the installed packages using the following two commands. sudo apt update sudo apt ful...

Network Printer + Apple AirPrint Server on your Raspberry Pi ******Featured Information********

        On this project, we will be showing you how to set up your Raspberry Pi as an AirPrint server.  AirPrint is a printing protocol designed by Apple so that their devices would not need to download or install drivers. By following this guide, you will be able to add AirPrint functionality to any printer that is connected to your Raspberry Pi. This project is an excellent way of adding extra functionality to both cheap and old printers. Implementing AirPrint support will allow you to print from your Apple device to your connected printer easily. Before following this project, you will need to have first set up the CUPS software on your device. Cups is what will act as the brains of your AirPrint server on your Raspberry Pi. It will handle the communication between your Raspberry Pi and the connected printer.   Equipment List Below are the pieces of equipment that you will need for this tutorial on installing AirPrint on your Raspberry Pi. Recomme...

How to Setup Fail2ban on the Raspberry Pi or use the Pi- Hole setup

    In this Raspberry Pi Fail2ban tutorial, we will be showing you how to set up and configure the Fail2ban software on your Raspberry Pi. Fail2ban is a crucial piece of software when it comes to improving the security of your Raspberry Pi. It is especially useful if you have your Raspberry Pi publicly accessible via the internet as it is an active and learning form of defense. For those who do not know what Fail2ban is , it is a piece of software that attempts to block malicious connections to your device, which in our case is our Raspberry Pi. It is important if you have SSH or even a web server that is publicly accessible. Fail2ban works by continually scanning your log files and looking for signs of potential attacks. These include attacks such as too many password failures as well as scanning for exploits and much more. Once it finds unusual activity it then automatically updates your firewall to ban that IP address. Equipment You will need the following eq...