Zachary J. Fields
Published © CC BY-NC

Router Mod: ISP Bandwith Detector

Are you getting your money's worth from your ISP? This project lets you send speed tests results to IFTTT directly from your OpenWRT router.

BeginnerProtip3 hours2,906
Router Mod: ISP Bandwith Detector

Things used in this project

Hardware components

TP-LINK Archer C7 AC1750 Dual Band Wireless AC Gigabit Router
×1

Software apps and online services

OpenWrt 15.05 (Chaos Calmer)
Maker service
IFTTT Maker service
Google Sheets
Google Sheets

Story

Read more

Custom parts and enclosures

Unlocked Firmware

Install this over the factory image using the firmware upgrade page hosted on the router.

OpenWrt 15.05 (Chaos Calmer)

Install this over the unlocked firmware using the firmware upgrade page hosted on the router. YOU MUST INSTALL VERSION 15.05, THERE IS A BUG IN 15.05.1

Code

speedtest-ifttt.sh

SH
You will copy it to the `/root` directory (~) of the device running OpenWrt
#!/usr/bin/env ash
###########################################################################
# Originally written by: Henrik Bengtsson, 2014
# https://github.com/HenrikBengtsson/speedtest-cli-extras
# Modified to use IFTTT by: Alasdair Allan, 2015
# Modified by: Zachary J. Fields, 2016
# - ported from Bourne-again shell (bash) to Almquist shell (ash) syntax
# - updated file locations to respect OpenWrt installation
# License: GPL (>= 2.1) [http://www.gnu.org/licenses/gpl.html]
###########################################################################

# Character for separating values
# (commas are not safe, because some servers return speeds with commas)
sep=";"

# Temporary file holding speedtest-cli output
user=$USER
if test -z $user; then
  user=$USERNAME
fi
log=/tmp/$user/speedtest-csv.log

# Local functions
str_extract() {
 pattern=$1
 # Extract
 res=`grep "$pattern" $log | sed "s/$pattern//g"`
 # Drop trailing ...
 res=`echo $res | sed 's/[.][.][.]//g'`
 # Trim
 res=`echo $res | sed 's/^ *//g' | sed 's/ *$//g'`
 echo $res
}

# Display header?
if test "$1" = "--header"; then
  start="start"
  stop="stop"
  from="from"
  from_ip="from_ip"
  server="server"
  server_dist="server_dist"
  server_ping="server_ping"
  download="download"
  upload="upload"
  share_url="share_url"
else
  mkdir -p `dirname $log`

  start=`date +"%Y-%m-%d %H:%M:%S"`

  if test -n "$SPEEDTEST_CSV_SKIP" && test -f "$log"; then
    # Reuse existing results (useful for debugging)
    1>&2 echo "** Reusing existing results: $log"
  else
    # Query Speedtest
    /usr/bin/speedtest-cli --share > $log
  fi
  
  stop=`date +"%Y-%m-%d %H:%M:%S"`
  
  # Parse
  from=`str_extract "Testing from "`
  from_ip=`echo $from | sed 's/.*(//g' | sed 's/).*//g'`
  from=`echo $from | sed 's/ (.*//g'`
  
  server=`str_extract "Hosted by "`
  server_ping=`echo $server | sed 's/.*: //g'`
  server=`echo $server | sed 's/: .*//g'`
  server_dist=`echo $server | sed 's/.*\\[//g' | sed 's/\\].*//g'`
  server=`echo $server | sed 's/ \\[.*//g'`
  
  download=`str_extract "Download: "`
  upload=`str_extract "Upload: "`
  share_url=`str_extract "Share results: "`
fi

# Standardize units?
if test "$1" = "--standardize"; then
  download=`echo $download | sed 's/Mbits/Mbit/'`
  upload=`echo $upload | sed 's/Mbits/Mbit/'`
fi

# Send to IFTTT
event="<YOUR IFTTT RECIPE EVENT NAME>"
secret_key="<YOUR IFTTT CHANNEL KEY>"
value1=`echo $server_ping | cut -d" " -f1`
value2=`echo $download | cut -d" " -f1`
value3=`echo $upload | cut -d" " -f1` 
json="{\"value1\":\"${value1}\",\"value2\":\"${value2}\",\"value3\":\"${value3}\"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/${event}/with/key/${secret_key}

Credits

Zachary J. Fields

Zachary J. Fields

18 projects • 149 followers
I like to make stuff.
Thanks to Alasdair Allan.

Comments