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.

  1. sudo apt-get install gcc-4.8 gcc-4.8-base python-setuptools python-dev
  2. sudo easy_install ouimeaux

 

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”:

 

  1. #!/bin/bash
  2. # Check if Internetaccess is working and restart Fritzbox if this is not the case
  3. # MU / 2015-02-23-1656
  4. logger -t "WEMO FRITZBOX" "Check Internet access ..."
  5. # Trying to reach "google.de"
  6. /usr/bin/wget -q --tries=5 --timeout=10 --spider http://google.de
  7. if [[ $? -eq 0 ]]; then
  8.       # "google.de" was successfully reached
  9. logger -t "WEMO FRITZBOX" "Internet access OK - Nothing to do ..."
  10. else
  11.       # "google.de" could not be reached. We assume broken Internet connection
  12. logger -t "WEMO FRITZBOX" "Internet access not working ..."
  13. logger -t "WEMO FRITZBOX" "Switch off Fritzbox ..."
  14. # Clear the device cache
  15. python /usr/local/bin/wemo clear >nul
  16. # Turn WeMo switch with name "FRITZBOX" off
  17. python /usr/local/bin/wemo switch "FRITZBOX" off
  18.       # Wait for one minute so that the Fritzbox can cool down a bit 😉
  19. sleep 1m
  20.      # Now turn on the WeMo switch again
  21. logger -t "WEMO FRITZBOX" "Switch Fritzbox on again ..."
  22. python /usr/local/bin/wemo switch "FRITZBOX" on
  23.       # Wait for 4 minutes (my Fritzbox needs about 3 minutes until it rebootet and reconnected to the Internet)
  24. sleep 4m
  25.       # Now try again if the Internet connections is working ...
  26. /usr/bin/wget -q --tries=5 --timeout=20 --spider http://google.de
  27. if [[ $? -eq 0 ]]; then
  28. logger -t "WEMO FRITZBOX" "Internet access OK again after restart of Fritzbox ..."
  29. else
  30. logger -t "WEMO FRITZBOX" "Internet access still not working after restart of Fritzbox ..."
  31. fi
  32. fi

This script is called by cron every 30 minutes. For that I created the following cron entry with “contab -e”:

 

  1. # Check Internet access and restart Fritzbox if needed
  2. */30 * * * * /home/pi/checkInternetAccess.sh

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.

 

Tagged with:  

Leave a Reply

© 2000-2015 Michael Urspringer