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 (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/
elif [ `which curl` ]; then
curl -o /usr/local/sbin/
#wget and curl not installed: install now
apt install wget curl
wget -O /usr/local/sbin/
chmod 755 /usr/local/sbin/
# 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/ >/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/
sed -i 's/^DEBUG=0\(.*\)/DEBUG=1\1/' /usr/local/sbin/
#edit script
nano /usr/local/sbin/[/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:


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/ 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/
sed -i 's/^DEBUG=1\(.*\)/DEBUG=0\1/' /usr/local/sbin/
#and now let linux starting the script
sed -i 's:^#/usr/local/sbin/' /etc/rc.local[/code]

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

Good luck.

Need help? join the DomBus channel on Telegram!