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:

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

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

[code]#set DEBUG=1 in /usr/local/sbin/netwatchdog.sh
sed -i 's/^DEBUG=0\(.*\)/DEBUG=1\1/' /usr/local/sbin/netwatchdog.sh
#edit script
nano /usr/local/sbin/netwatchdog.sh[/code]

and finally start the script in debug mode (reset the router after only 20s of ping failures), writing some information in the console and other information in the logfile:

[code]/usr/local/sbin/netwatchdog.sh[/code]

Remove the internet cable or switch-off the router: you should see that ping returns 100% packet loss and after 20s the script send a command to Domoticz to reset the router. Type ctrl+c 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:

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

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

Good luck.

Need help? join the DomBus channel on Telegram!