User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Using Domoticz it's possible to supply a router through the normally-closed relay output (SPDT relay); so, normally power supply is applied to the router, but if internet connection goes down, after some seconds or minutes Domoticz remove the power supply, wait for 10s, and apply power supply again to restart the router.

Script that automatically reset the router when internet connection goes down

This monitoring procedure is performed by a bash script that sends pings to two different hosts, and if they do not respond, after some seconds or minutes, sends a command to Domoticz to activate the relay output that remove power supply to the router.

First, check that 127.0.0.1 (and your network, if you prefer) are specified in the Domoticz panel Setup -> Settings -> Local Networks: in this way any connection to domoticz from the specified networks do not need authentication (no username and password is required). If you add your LAN, all devices from your LAN can enter domoticz without asking for username and password.

domoticz setup settings localnetworks
To install the bash script, copy and paste the following code into the raspberry/linux shell:

  1. #become root
  2. sudo su
  3. #download script
  4. if [ `which wget` ]; then
  5. wget -O /usr/local/sbin/netwatchdog.sh http://docs.creasol.it/netwatchdog.sh
  6. elif [ `which curl` ]; then
  7. curl -o /usr/local/sbin/netwatchdog.sh http://docs.creasol.it/netwatchdog.sh
  8. else
  9. #wget and curl not installed: install now
  10. apt install wget curl
  11. wget -O /usr/local/sbin/netwatchdog.sh http://docs.creasol.it/netwatchdog.sh
  12. fi
  13. chmod 755 /usr/local/sbin/netwatchdog.sh
  14. # prepare /etc/rc.local so it is sufficient to remove a # to let raspberry start the script at boot time
  15. sed -i 's:^exit 0:#/usr/local/sbin/netwatchdog.sh >/dev/null 2>/dev/null \&\nexit 0:' /etc/rc.local
  16. sed -i 's/amp;//' /etc/rc.local

Now, find the idx corresponding to the relay output: open the web browser to the Domoticz page, go to Setup -> Devices to list all installed devices. Look in the idx column to find the idx of the router power supply device.

How to find idx of a device

Now, copy&paste the following commands in the raspberry shell to put the script in debug mode (variable DEBUG=1 : in this way the script will print several debugging messages on the console and reduce the timings to check the script easily), edit the script (you have to write the right idx in the variable ROUTER_RELAY_IDX, check other variables, and type ctrl+x to exit) and start the script:

  1. #set DEBUG=1 in /usr/local/sbin/netwatchdog.sh
  2. sed -i 's/^DEBUG=0\(.*\)/DEBUG=1\1/' /usr/local/sbin/netwatchdog.sh
  3. #edit script
  4. nano /usr/local/sbin/netwatchdog.sh
  5. #exec script: terminate with CTRL+c
  6. /usr/local/sbin/netwatchdog.sh
  7.  

Remove the internet cable or switch-off the router: you should see that ping returns 100% packet loss and after 30s the script send a command to Domoticz to reset the router. Type ctrl+x to terminate the script.

 If it works, type the following commands to edit /usr/local/sbin/netwatchdog.sh setting DEBUG=0 and to modify /etc/rc.local so the script is executed at boot time:

  1. #set DEBUG=0 in /usr/local/sbin/netwatchdog.sh
  2. sed -i 's/^DEBUG=1\(.*\)/DEBUG=0\1/' /usr/local/sbin/netwatchdog.sh
  3. #and now let linux starting the script
  4. sed -i 's:^#/usr/local/sbin/netwatchdog.sh:/usr/local/sbin/netwatchdog.sh:' /etc/rc.local

Now you have to reboot to let linux start the netwatchdog.sh script.

Good luck.

Pin It