Introduction: Test You Internet Speed Using a Raspberry Pi + Ubidots

About: Community manager @Ubidots, a startup empowering makers and hardware engineers to create applications for the Internet of Things #IoT

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/Ourput 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 the Ubidots cloud to create the alerts for monitoring your internet's connection throughout the day!

Step 1: Requirements

To complete this tutorial, you’ll need:

Step 2: Setup

This guide assumes your Raspberry Pi has been configured and is already connected to the Internet. If not configured you can quickly do so using 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.

Step 3: Connecting the NanoPi 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 computers terminal:

> ssh pi@{IP_Address_assigned}

User Name: pi
Password: raspberry

As you can see the image above, 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
  • pyspeedtest: to measure the Internet speed from Python

> pip install requests pyspeedtest

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

Step 4: Now It Is Time to Code!

Create a Python script in your computer's terminal:

> nano ubi_speed_tester.py

And copy the code provided in this article. 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.

Step 5: Optional Steps: Rename the Device and Variables

The names of the variables created are the same as the API labels, which are the IDs used by the API. 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. To know how to rename your variables names, see the article below:

You can also add the units to each variable.

Step 6: 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

Step 7: Ubidots Dashboard

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

Bar chart widget

To see more about Ubidots Dashboards, see the Help Center.

Step 8: Notifications of Slow/no Internet

To monitor your internet's speed when away, we added some events to notify a user: if the Internet is slow or if there's no Internet.

  • Value-based Event (Internet is slow)
  • Activity-based Event (Internet is inactive)

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

Step 9: Conclusion

I 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 :)