RPI script to verify internet connection

All kinds of 'OS' scripts
Post Reply
User avatar
Teknor
Posts: 42
Joined: Monday 08 February 2016 14:26
Target OS: Raspberry Pi
Domoticz version: 3.48
Location: Norway
Contact:

RPI script to verify internet connection

Post by Teknor » Friday 08 December 2017 15:43

Hi
I have a RPI in my summerhouse (weather station, 10x temperature sensors, a few switches and Pikrellcam) for monitoring purposes. I use a stationary 4G router for internet connection. This connection is mostly stable, but occationally (a few times per year) there is need for a reboot of the 4G router. Does anybody have a script which checks for internet access, and if this is down runs a json command?

I think I need something like:

A job runs every 5 minute and checks for internet access
If internet access -> Do nothing
if internet access fails three times in a row, then run http://localhost/json.htm?type=command& ... tchcmd=Off
wait 1 minute
then run http://localhost/json.htm?type=command& ... itchcmd=On

The switch with IDX 46 is connected to a relay powering the 4G router.

Can somebody please point me in the right direction for writing such a script?
Raspberry Pi 3 | EasyESP NodeMCU v3 | RFXTRX433e | Mysensors NRF24L01 | MotionEYE | PikrellCam

User avatar
Teknor
Posts: 42
Joined: Monday 08 February 2016 14:26
Target OS: Raspberry Pi
Domoticz version: 3.48
Location: Norway
Contact:

Re: RPI script to verify internet connection

Post by Teknor » Sunday 10 December 2017 15:38

Found a script I modified. Works like intended.

Code: Select all

#!/bin/ash
FirstIP="8.8.8.8" #(Google public DNS)
SecondIP="208.67.222.222" #(OpenDNS public DNS)
IDX="208"
DomoIP="192.168.1.103"
DomoPort="8080"

ping="ping -c 5 -w 3 -q "$FirstIP""
if $ping | grep -E "min/avg/max" > /dev/null
then
    echo "First IP ("$FirstIP") responds, do nothing"
else
    echo "--> No response from first IP ("$FirstIP"), now trying second one ("$SecondIP")"
      ping="ping -c 5 -w 3 -q "$SecondIP""
      if $ping | grep -E "min/avg/max" > /dev/null
      then
      echo "Second IP ("$SecondIP") is responding, do nothing"

   else
      echo "--> Both IP's not responding, toggling power to modem"
      # Send off command to switch twice to be sure
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=Off'
      sleep 2
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=Off'
      sleep 30

      # Send on command to switch, twice to be sure
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=On'
      sleep 2
      wget -O /dev/null - -q -t 1 'http://'$DomoIP':'$DomoPort'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd=On'
      sleep 2
   fi
fi
Raspberry Pi 3 | EasyESP NodeMCU v3 | RFXTRX433e | Mysensors NRF24L01 | MotionEYE | PikrellCam

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest