Skip to content

Instantly share code, notes, and snippets.

@jlis
Last active August 2, 2023 15:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jlis/7b570314ca1538c45444d2c88de39438 to your computer and use it in GitHub Desktop.
Save jlis/7b570314ca1538c45444d2c88de39438 to your computer and use it in GitHub Desktop.
Automated speedtest using a Raspberry Pi, Cronjobs and Airtables

Automated speedtest using a Raspberry Pi, Cronjobs and Airtables

We're gonna install the Okla Speedtest (speedtest.net) CLI and a write the results into a Airtables table.

Setup

First we're gonna install all required dependecies to run the speedtest CLI.

sudo apt-get install gnupg1 apt-transport-https dirmngr jq
export INSTALL_KEY=379CE192D401AB61
export DEB_DISTRO=$(lsb_release -sc)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $INSTALL_KEY
echo "deb https://ookla.bintray.com/debian ${DEB_DISTRO} main" | sudo tee  /etc/apt/sources.list.d/speedtest.list
sudo apt-get update
sudo apt-get install speedtest

Run Speedtest CLI

To run the speedtest CLI, just call speedtest.

Set up the Airtable table

To save the results of the speedtest in a table using Airtable, simply visit this template table https://airtable.com/shrpCK2GwdX5ycnPq and press the "Copy base" button (upper right corner).

To get the base id, which is needed for the API calls to Airtable, click on the "Help" button in the table view and proceed to "API documentation". There you should find your base id and API key (by checking the "show api key" checkbox).

The speedtest script

The speedtest.sh attached to this Gist can be downloaded by running

wget https://gist.githubusercontent.com/jlis/7b570314ca1538c45444d2c88de39438/raw/speedtest.sh

Make to to make it executable using chmod +x speedtest.sh.

Now let's edit the script and insert your API key and the base id from Airtable using nano speedtest.sh.

Just replace the <API KEY> with your Airtable API key and the BASE ID FROM API DOCS with the base id you got from the API documentation earlier.

You should now be ready to run the script for the first time: ./speedtest.sh.

If everything is working, the result should be written to your Airtable table.

The cronjob

To create a cronjob for the script and run it, for example, every hour, run crontab -e to write a new cronjob.

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

This cronjob will run the speedtest.sh in the pi users home directory every hour.

The end

That's it, your Raspberry Pi should now do a speedtest every our and write the results into a table. If you have any questtions or points for improvement, please let me know.

#!/bin/bash
airtable_api_key="<API KEY>"
airtable_url="https://api.airtable.com/v0/<BASE ID FROM API DOCS>/Table%201"
echo "⚡️ Starting speedtest..."
result=$(speedtest -f json)
echo "📶 Result:"
echo "$result"
timestamp=$(echo $result | jq ".timestamp")
down=$(echo $result | jq ".download.bandwidth")
up=$(echo $result | jq ".upload.bandwidth")
ping=$(echo $result | jq ".ping.latency")
result_url=$(echo $result | jq ".result.url")
echo "📊 Starting upload to Airtable..."
curl --location --request POST "$airtable_url" \
--header "Authorization: Bearer $airtable_api_key" \
--header 'Content-Type: application/json' \
--data-raw "{
\"records\": [
{
\"fields\": {
\"Date\": $timestamp,
\"Down\": $down,
\"Up\": $up,
\"Ping (ms)\": $ping,
\"Result URL\": $result_url
}
}
]
}"
echo ""
echo "✅ Done"
@leilabd
Copy link

leilabd commented Aug 10, 2021

Thank you. This was very helpful and worked almost flawlessly. However, please note that Ookla are now (10 August 2021) distributing their version of speedtest cli differently from your example. See https://www.speedtest.net/apps/cli.

I also had some problems getting the cron job to run, which turned out to be because I had put speedtest in /usr/local/bin and cron wasn't finding it. Moved it to /usr/bin and all was well.

@wizel10
Copy link

wizel10 commented Jan 9, 2022

Thanks. Works great.
On a Pi device, I had to replace "speedtest" in line 8 to full path (in my case: /home/pi/speedtest/speedtest)

@DennisMatthiasScherf
Copy link

Thanks! I just added it to my raspberry pi. I'm intrigued how my internet connection speed changes over time.

@leilabd
Copy link

leilabd commented Aug 2, 2023

This is still working well for me. However, Airtable are deprecating the use of API keys and replacing them with Personal Access Tokens with more limited scope. The API keys will stop working in February 2024. I decided to bite the bullet and generated a PAT following the documentation at https://airtable.com/developers/web/guides/personal-access-tokens. I gave it the Scope: data.records:write and access to my Speedtest Template. I then replaced the API key with the PAT in speedtest.sh and it ran without problems and updated the template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment