Skip to content

Instantly share code, notes, and snippets.

@sgobin
Last active January 19, 2019 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sgobin/04afdad4d092ae21b024a5551e0bcb81 to your computer and use it in GitHub Desktop.
Save sgobin/04afdad4d092ae21b024a5551e0bcb81 to your computer and use it in GitHub Desktop.
Usando uma raspberry e speedtest.net para medir a velocidade da internet e salvar em uma planilha do Google via IFTTT
import os
import re
import subprocess
import time
import requests
#Usa um Webhook do IFTTT
URL = 'https://maker.ifttt.com/trigger/{NOME DO TRIGGER}/with/key/{CHAVE}'
#speedtest-cli => pip install speedtest-cli
response = subprocess.Popen('speedtest-cli --simple', shell=True, stdout=subprocess.PIPE).stdout.read()
ping = re.findall('Ping:\s(.*?)\s', response, re.MULTILINE)
download = re.findall('Download:\s(.*?)\s', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\s', response, re.MULTILINE)
ping[0] = ping[0].replace(',', '.')
download[0] = download[0].replace(',', '.')
upload[0] = upload[0].replace(',', '.')
# Salva em um csv local também
try:
if os.stat('/home/pi/speedtest/speedtest.csv').st_size == 0:
print 'Date,Time,Ping (ms),Download (Mbit/s),Upload (Mbit/s)'
except:
pass
report = {}
report['value1'] = ping[0]
report['value2'] = download[0]
report['value3'] = upload[0]
requests.post(URL, data=report)
@sgobin
Copy link
Author

sgobin commented Oct 29, 2018

Para adicionar ao crontab:

  1. Criar um arquivo speedtest-cron.sh com
    sudo python /home/pi/speedtest.py >> /home/pi/speedtest/speedtest.csv
  2. No terminal:
    chmod +x speedtest-cron.sh
  3. Adicionar ao cron e rodar de hora em hora
    crontab -e
    0 * * * * /home/pi/speedtest-cron.sh

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