All Collections
IoT Projects Tutorials
Test your internet speed using a Raspberry Pi + Ubidots
Test your internet speed using a Raspberry Pi + Ubidots

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

Raspberry Pi has become a widely used device not only for prototyping and educational purposes but also for industrial production projects within businesses. Besides the Pi's size, low cost, and fully operational Linux OS, it can also interact with other peripherals through GPIO pins (General Purpose Input/Output Pins) allowing you to code pretty robust hardware applications without having to be an expert in embedded electronics.

Following this article you'll learn how to measure your internet speed using a Raspberry Pi and send the parameters to Ubidots cloud to create the alerts for monitoring your Internet's connection throughout the day!

Requirements

To complete this tutorial, you’ll need:

  • A Raspberry Pi connected to the Internet

  • Ubidots account

Setup

This guide assumes your Raspberry Pi has been configured and is already connected to the Internet. If not, please refer to this quick start guide from the Raspberry Pi Foundation.

NOTE: If you’re using a WiFi dongle, we suggest using Wicd to manage your WiFi connection.

Connecting the Raspberry Pi to Ubidots using Python

With your Raspberry Pi connected to the internet, verify the IP address assigned to the board access using ssh in your computer's terminal:

ssh pi@{IP_Address_assigned}

User Name: pi
Password: raspberry

As you can see the image below, your access was successful, and the user now is pi@raspberrypi :

Now let's upgrade some packages and install pip, Python's packet manager:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip python-dev build-essential 

Install the below libraries:

  • requests: to make HTTP requests from Python to Ubidots

  • speedtest-cli: to measure the Internet speed from Python

pip install requests speedtest-cli

Pro Tip: FAQs and Troubleshooting - If you get a permission issue when installing the packages required, change the user mode to root using the following command: 

sudo su

Now it is time to code!

Create a Python script in your computer's terminal:

nano ubi_speed_tester.py 

And copy this code into it:

#!/usr/bin/python

import speedtest
import requests

try:
st = speedtest.Speedtest()
st.get_best_server()
payload = {'Download': round(st.download() / 1000000, 2) , 'Upload': round(st.upload() / 1000000, 2), 'Ping': round(st.results.ping, 2)}

r = requests.post('http://industrial.api.ubidots.com/api/v1.6/devices/raspberry-pi/?token=YOUR-UBIDOTS-TOKEN-HERE', data=payload)

# Print the server's response Uncomment the next line for debugging purposes
#print(r.content)
except Exception as identifier:

print(identifier)

Make sure to replace your Ubidots account token in the request URL. If you don't know how to get your Ubidots Token, please see the article below:

Now let's test the script:

python ubi_speed_tester.py

If working properly you will see a new device in your Ubidots account with three variables: Download, Upload, and Ping:

Optional Steps: Rename the Device and Variables

The names of the variables created are the same as the API labels. However, this doesn't mean their names can't be changed, so I recommend changing the names of the devices and variables to make them friendlier. Please refer to the article below to learn how to rename your variables.

Also, you can add the units to each variable:

Create a Crontab to run the script every N minutes

Now that we've tested the script, we can set it to run automatically every N minutes. For this purpose we'll use the Linux Cron tool for efficiency.

1. Make the file executable in your computer's terminal:

chmod a+x ubi_speed_tester.py 

2. Create a crontab:

For some reason, the command “crontab -e” does not work out of the box, so the work-around is to install cron manually with the below commands:

sudo apt-get install cron

then type:

crontab -e

and add the line

* * * * * python /home/pi/ubi_speed_tester.py

to run the script every minute.

3- Reboot and Check your Data in Ubidots

To reboot the Raspberry Pi you have to use as root , for this type:

sudo su

Then, type the command below to reboot the Raspberry Pi:

reboot

Wait for a minute and then go to Ubidots to start seeing the results updating every minute:

Now that your data is in Ubidots, you can create dashboards and events using your data. Here's an example:

For more information about Dashboards, refer to Create Dashboards and Widgets.

Notifications of slow/no Internet connection

To monitor your Internet's speed when away, you can create events to notify a user: if the Internet is slow or if there's no Internet.

To see more about Ubidots Events, check out this Help Center article for more details.

Conclusion

In just a few minutes you've built an easy DIY Internet Speed Tester. Now put your Raspberry Pi in a safe place behind your router and never wonder your Internet's speed again. Happy hacking :)

Other users also found helpful...


Did this answer your question?