Intermediate Network Tech

Raspberry Pi Internet Speed Monitor

Raspberry Pi Internet Speed Monitor

 Raspberry Pi Internet Speed Monitor

 Raspberry Pi Internet Speed Monitor

On this Raspberry Pi web velocity monitor tutorial, we can be displaying you how one can arrange your Raspberry Pi to watch your web connection and save the info to Google drive.

This tutorial is nice in case you’re focused on monitoring how your obtain velocity, add velocity, and ping are affected over time. Moreover, this might help you’re employed out what occasions your community could also be at its peak capability or in case you’re affected by a degraded web connection.

Ultimate Books of PiUltimate Books of Pi

To realize this web velocity monitor, we might be displaying you the best way to write a small Python script that interacts with a program referred to as speedtest-cli.

Speedtest-cli is what our web velocity monitor will use to watch the web connection. This program works by polling the favored speedtest.internet service to get your ping, obtain velocity, and add velocity.

You’ll find the complete tutorial proper under or in our ever-growing Raspberry Pi challenge books.

Gear Record

Under are all of the items of kit that we made use of to arrange our Raspberry Pi web velocity monitor.


Raspberry Pi 2 or three

Micro SD Card

USB Drive

Ethernet Cable (Beneficial) or Wifi dongle (Pi three has Wifi inbuilt)


Raspberry Pi Case

Putting in speedtest-cli

1. Earlier than we get began with establishing the Raspberry Pi to watch the web’s velocity, we should first ensure our Raspberry Pi is updated.

We will replace the Raspberry Pi by operating the next two instructions inside the terminal.

sudo apt-get replace
sudo apt-get improve

2. For our tutorial, we might be using a Python library referred to as speedtest-cli. This Python library acts as a command line interface that talks with speedtest.internet.

Utilizing this library, we will shortly ballot Speedtest.internet to retrieve our present add and obtain speeds in addition to our present ping. Nevertheless, earlier than we use this library, we should first set up a package deal referred to as Python pip.

The rationale we’d like the Python pip package deal is that speedtest-cli isn’t out there within the default Raspbian repository. As an alternative, we have to retrieve it from the Python Package deal Index (PyPI) which is fortunately straightforward to do because of pip.

Usually pip is pre-installed, however we’ll run the next command to be sure that it’s put in.

sudo apt-get set up python-pip

three. With pip now put in to the Raspberry Pi, we will transfer onto utilizing it to obtain the speedtest-cli python library.

Pip could be very straightforward to make use of and works the identical as apt-get. Simply run the next command on the Raspberry Pi to make use of pip to put in speedtest-cli.

sudo pip set up speedtest-cli

four. Now that we now have put in speedtest-cli we will now check it. Let’s be certain that all the things is working appropriately by operating the next command.


5. You will notice that fairly a bit of data is returned by this command. To make issues extra simple we will use the next command.

speedtest-cli –simple

As you possibly can see with the –easy argument, simply the obtain, add and ping are returned.

Writing our Speed Check Python Script

1. Now that we now have speedtest-cli put in on the Raspberry Pi, we will now proceed to write down our Python script that may regularly monitor our obtain and add speeds.

We will start creating our Python script for the Raspberry Pi web velocity monitor by operating the next command.

cd ~
sudo nano

2. Inside this file write the next strains of code. We’ll clarify every essential part of the code, so that you get an concept of how every part works.

import os
import re
import subprocess
import time

These 4 strains outline all the libraries that we’ll be counting on in our script. Under we’ll clarify how every of those libraries might be is used.

import os: The os library is utilized by the script to work together with the working system itself. For this script, we shall be utilizing it to examine if a file exists.

import re: The re library permits us to simply do common expressions by offering a library for dealing with sample searches. We use this to seek out our needed values out of the info given to us from speedtest-cli.

import subprocess: The subprocess library is important to this script, as we require it to have the ability to name one other python script.

In our case, we will probably be utilizing the subprocess library so we will launch up the speedtest-cli script and retrieve the values returned by it.

import time: We make the most of the time library in order that we will report each the date and time for every name to the speedtest-cli. This library is what is going to permit us to trace our velocity over a size of time.

response = subprocess.Popen(‘speedtest-cli –simple’, shell=True, stdout=subprocess.PIPE).stdout.learn()

On this line of code, we make the most of the subprocess library to launch a name to the speedtest-cli python script and inform it to pipe the whole lot from the speedtest-cli to stdout.

Through the use of stdout.learn() on the finish of the decision we retailer the response from speedtest-cli to our response variable.

ping = re.findall(‘Ping:s(.*?)s’, response, re.MULTILINE)
obtain = re.findall(‘Obtain:s(.*?)s’, response, re.MULTILINE)
add = re.findall(‘Add:s(.*?)s’, response, re.MULTILINE)

These three strains of code are pretty easy, and all do the identical factor. They use the re library to run a daily expression for a sure piece of textual content and discover the quantity situated subsequent to every piece of textual content.

As an example, the seek for ping finds “Ping: 47.943 ms” however solely grabs the quantity that’s between the textual content.

ping = ping[0].substitute(‘,’, ‘.’)
obtain = obtain[0].substitute(‘,’, ‘.’)
add = add[0].exchange(‘,’, ‘.’)

These strains are necessary as we use them to wash up the numbers that we grabbed from the speedtest-cli output.

Since we plan on outputting the info right into a CSV format, all quantity values have to have the commas (,) changed with decimal factors (.). In any other case, the CSV format will cut up the numbers into new columns.

Because of Pythons .exchange we will simply do that on each bit of textual content.

f = open(‘/residence/pi/speedtest/speedtest.csv’, ‘a+’)
if os.stat(‘/house/pi/speedtest/speedtest.csv’).st_size == zero:
f.write(‘Date,Time,Ping (ms),Obtain (Mbit/s),Add (Mbit/s)rn’)

This little bit of code is simple. The code is stored inside a attempt assertion in order that if any errors happen, it won’t cease the script from working.

Inside the attempt assertion, we first open up a name to our speedtest.csv file. Through the use of, “a+” within the arguments, we inform it that we need to create the file if it doesn’t exist and that any new knowledge ought to be appended to no matter is already in there.

Afterward, we make the most of the os library to verify our speedtest.csv information precise measurement in bytes.

If the file’s bytes is the same as zero, we go forward.

If the file does exist we proceed on as regular.

f.write(‘,,,,rn’.format(time.strftime(‘%m/%d/%y’), time.strftime(‘%H:%M’), ping, obtain, add))

Lastly, we print out all our knowledge separated by commas.

We use the time library’s strftime perform to insert each the present date and the present time into our formatted string.

After that, we insert our ping, obtain, and add.

Under we have now an included an instance of what the output knowledge will seem like on the primary run of our code.

Date, Time, Ping (ms), Obtain (Mbit/s),Add(Mbit/s)

three. After you have completed writing your code, it ought to find yourself wanting like what we’ve got displayed under.

import os
import re
import subprocess
import time

response = subprocess.Popen(‘speedtest-cli –simple’, shell=True, stdout=subprocess.PIPE).stdout.learn()

ping = re.findall(‘Ping:s(.*?)s’, response, re.MULTILINE)
obtain = re.findall(‘Obtain:s(.*?)s’, response, re.MULTILINE)
add = re.findall(‘Add:s(.*?)s’, response, re.MULTILINE)

ping = ping[0].exchange(‘,’, ‘.’)
obtain = obtain[0].substitute(‘,’, ‘.’)
add = add[0].substitute(‘,’, ‘.’)

f = open(‘/house/pi/speedtest/speedtest.csv’, ‘a+’)
if os.stat(‘/residence/pi/speedtest/speedtest.csv’).st_size == zero:
f.write(‘Date,Time,Ping (ms),Obtain (Mbit/s),Add (Mbit/s)rn’)

f.write(‘,,,,rn’.format(time.strftime(‘%m/%d/%y’), time.strftime(‘%H:%M’), ping, obtain, add))

four. Now you can save the file by urgent Ctrl + X then Y and eventually press Enter.

5. With our script now written we have to make a folder the place our speedtest.csv file can be saved. Run the next command to create this folder.

mkdir ~/speedtest

6. Now that we’ve made the required folder, we will go forward and check the script.

We will check our script by operating the next command.

python ~/

7. As soon as the script has completed executing you’ll be able to then take a look at the outcomes by opening up the newly created speedtest.csv.

Let’s open up this file by operating the next command on the Raspberry Pi.

nano ~/speedtest/speedtest.csv

eight. Inside this file, you must see one thing like what we’ve under. The column headers and a few rows of knowledge.

Date, Time, Ping (ms), Obtain (Mbit/s),Add(Mbit/s)

Subsequent, we might be displaying how one can take this knowledge and add it to Google Drive so you’ll be able to shortly view the info wherever you’re.

Importing Speed Check Knowledge to Google Drive

1. For this part of the information, we shall be displaying you how one can add your velocity check knowledge out of your Raspberry Pi to Google Drive.

To do that, we might want to make use of a bit of software program that permits us to work together with Google Drive by way of the command line. This piece of software program is known as gDrive and is developed by a consumer on Github. You will discover extra details about this piece of software program on gdrives builders Github.

Obtain the Raspberry Pi model of the gDrive software program by getting into the next two instructions.

cd ~
wget -O gdrive

2. Now that we now have downloaded gDrive to our Raspberry Pi we have to give it execution privileges.

We will do that by getting into the next command into the terminal, using chmod with the +x argument.

chmod +x gdrive

three. Let’s now start the method of connecting your Google account with the gDrive software.

To start out the method, we’ll use the next easy command.

./gdrive record

four. After operating gDrive with the record command, we should always now be informed that authentication is required to proceed.

Under the message must be an URL. You will have to go to this URL in an internet browser and login utilizing your Google account.

Comply with the subsequent few prompts in your net browser till you’re given a verification code. Copy this verification code into the terminal and press Enter.

5.  After urgent enter with the right verification code, it is best to now see an inventory of all of your gDrive information.

Subsequent, let’s use gDrive to create a brand new folder in your Google Drive to retailer our speedtest.csv file. We will do this through the use of the next command inside the terminal.

./gdrive mkdir speedtest

This command will present you a message saying the listing was created. This message will even present you an ID. We’ll want this ID for our subsequent few steps so write it down someplace protected.

6. Now that we’ve used the gDrive command line interface to create a folder on our Google Drive and have the ID useful we will now attempt importing one thing to it.

For this check, we might be importing our speedtest.csv file. Run the next command and be sure to exchange YOUR_FOLDER_ID with the id you retrieved within the earlier step.

./gdrive sync add ~/speedtest YOUR_FOLDER_ID

On the preliminary sync, you must see a message like under seem on the command line. This message tells you that the file has been efficiently synced to your Google Drive account.

Beginning sync…
Amassing native and distant file info…
Discovered 1 native information and zero distant information

1 distant information are lacking
[0001/0001] Importing speedtest.csv -> speedtest/speedtest.csv
Sync completed in three.119979351s

7. With Google Drive now efficiently syncing with the Raspberry Pi we will transfer onto automating the entire course of in order that we add the speedtest.csv each time we edit it.

Automating the Raspberry Pi Internet Speed Monitor

1. Now that we have now arrange each the gDrive software and written up our Python script, we have to transfer onto automating the entire course of.

To do the automation, we might be writing a easy bash script. This script shall be referred to as by crontab in order that it is going to be run routinely.

Start writing the bash script by operating the next Linux command in your Raspberry Pi.

nano ~/

2. Inside this file, we need to enter the next strains.

Just remember to exchange YOUR_FOLDER_ID with the ID that you simply acquired if you created the folder on Google drive.

python /residence/pi/
/residence/pi/gdrive sync add /house/pi/speedtest YOUR_FOLDER_ID

three. Now save the file by urgent Ctrl + X then Y after which lastly Enter.

four. Earlier than we arrange the crontab for our bash script, we should first give the script execution privileges.

We will obtain this by operating the next command inside the terminal.

sudo chmod +x ~/

5. Now with the whole lot completed, we will lastly transfer onto establishing the crontab.

Start modifying the crontab by operating the next command in your Raspberry Pi. In case you are requested what editor you need to use, we advocate you choose Nano (Sometimes quantity 2).

sudo crontab -e

6. Add the next line on the backside of the file. This line will inform crontab that it ought to run our bash script as soon as each hour.

If you wish to change the conduct of the crontab, you possibly can take a look at the Crontab Guru to work out your required values.

zero * * * * /residence/pi/

7. You must now have your velocity check monitor up and operating. It also needs to be regularly importing its outcomes to your Google Drive account.

We hope by the top of this Raspberry Pi web velocity monitor tutorial you must now have your Raspberry Pi mechanically operating web velocity exams in your connection and importing that knowledge to Google on an hourly foundation.

When you’ve got any ideas, suggestions or anything, then make sure to head over to the boards.

Ultimate Books of Pi BottomUltimate Books of Pi Bottom

Please examine your inbox for a affirmation e mail.

fbq(‘init’, ‘1349376585078668’);
fbq(‘monitor’, ‘PageView’);(perform(d, s, id)
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); = id;
js.src = ‘’;
fjs.parentNode.insertBefore(js, fjs);
(doc, ‘script’, ‘facebook-jssdk’));