I am using my Raspberry Pi to check if my Internet connection is still working. If the connection to the Internet fails, my Internet router (Fritzbox) should automatically be restarted. For that I am using a Belkin WeMo Switch which can be controlled from the Raspberry Pi via a Python.
First you need to install Python and the ouimeaux library. I used the directions from this website.
[codesyntax lang=”bash” lines=”normal”]
sudo apt-get install gcc-4.8 gcc-4.8-base python-setuptools python-dev
sudo easy_install ouimeaux
[/codesyntax]
This takes some time, so be patient.
After the WeMo support for Python has been installed, you can use the “wemo” command e.g. from inside a shell script.
I created the following script and saved it as “/home/pi/checkInternetAccess.sh”:
[codesyntax lang=”bash” lines=”normal”]
#!/bin/bash
# Check if Internetaccess is working and restart Fritzbox if this is not the case
# MU / 2015-02-23-1656
logger -t "WEMO FRITZBOX" "Check Internet access ..."
# Trying to reach "google.de"
/usr/bin/wget -q --tries=5 --timeout=10 --spider http://google.de
if [[ $? -eq 0 ]]; then
# "google.de" was successfully reached
logger -t "WEMO FRITZBOX" "Internet access OK - Nothing to do ..."
else
# "google.de" could not be reached. We assume broken Internet connection
logger -t "WEMO FRITZBOX" "Internet access not working ..."
logger -t "WEMO FRITZBOX" "Switch off Fritzbox ..."
# Clear the device cache
python /usr/local/bin/wemo clear >nul
# Turn WeMo switch with name "FRITZBOX" off
python /usr/local/bin/wemo switch "FRITZBOX" off
# Wait for one minute so that the Fritzbox can cool down a bit ;-)
sleep 1m
# Now turn on the WeMo switch again
logger -t "WEMO FRITZBOX" "Switch Fritzbox on again ..."
python /usr/local/bin/wemo switch "FRITZBOX" on
# Wait for 4 minutes (my Fritzbox needs about 3 minutes until it rebootet and reconnected to the Internet)
sleep 4m
# Now try again if the Internet connections is working ...
/usr/bin/wget -q --tries=5 --timeout=20 --spider http://google.de
if [[ $? -eq 0 ]]; then
logger -t "WEMO FRITZBOX" "Internet access OK again after restart of Fritzbox ..."
else
logger -t "WEMO FRITZBOX" "Internet access still not working after restart of Fritzbox ..."
fi
fi
[/codesyntax]
This script is called by cron every 30 minutes. For that I created the following cron entry with “contab -e”:
[codesyntax lang=”bash” lines=”normal”]
# Check Internet access and restart Fritzbox if needed
*/30 * * * * /home/pi/checkInternetAccess.sh
[/codesyntax]
The output via the “logger” command will be written to “/var/log/messages”. So every 30 minutes you should see in that file what the script is doing.
I also activated the Raspberry Pi hardware watchdog so that the Pi will be restarted as well if hangs for some reason.

