Pete Hoffswell
Published © MIT

Trending Internet Speedtest to ThingSpeak

Internet Speed tests are great, but what about monitoring them over time? Let's trend the results to Thingspeak.

IntermediateFull instructions provided2 hours1,096
Trending Internet Speedtest to ThingSpeak

Things used in this project

Hardware components

Linux Computer
×1

Software apps and online services

ThingSpeak - ThingSpeak Data Logger

Story

Read more

Code

speedtestupdate.py

Python
Run a speed test, and update your Thingspeak graph
#!/usr/bin/env python
#
# speedtestupdate.py
# 2016, pete@hoffswell.com
import os
from httplib2 import Http
from urllib.parse import urlencode

# Set your key to your ThingSpeak API Key
key = "YOUR KEY HERE"

def main():
  try:
    ping, download, upload = get_speedtest_results()
  except ValueError as err:
    print ('Error: ' ,err)
  else:
    post_thingspeak(ping,download,upload)


def get_speedtest_results():
# Run Speed test

   print ('Running Speedtest...')
   ping = download = upload = None
   # Change your server ID HERE to use a server close to you.
   # run speedtest-cli --servers for a server list
   with os.popen('speedtest-cli --server 6125 --simple') as speedtest_output:

     for line in speedtest_output:
       label, value, unit = line.split()
       if 'Ping' in label:
         ping = float(value)
       elif 'Download' in label:
         download = float(value)
       elif 'Upload' in label:
         upload = float(value)

   if all((ping, download, upload)): # if all 3 values were parsed
     print ('Speedtest Done -  ping ', ping, 'up ', upload, 'down ',download)
     return ping, download, upload
   else:
     raise ValueError('TEST FAILED')


def post_thingspeak(ping,up,down):
# Post to ThingSpeak
   #print('Posting', ping, ' ', up, ' ', down, ' to ThingSpeak')
   http = Http()
   data = dict(api_key=key, field1=ping, field2=up, field3=down)
   resp, content = http.request("https://api.thingspeak.com/update.json", "POST", urlencode(data))

if __name__ == '__main__':
  main()

Credits

Pete Hoffswell

Pete Hoffswell

10 projects • 58 followers
Network Engineer for All the People. Internet advocate. Microcontroller Fan. Maker.

Comments