[How To] Switch on a led when internet connection is available


#1

In this example I used a green led to indicate that internet connection is available and a red one to warn when internet connection is not available (Router ADB P.DG A4001N1), but if you have just one led, you can set a fixed light when internet is available and a flash light when it is not available.

let's create the script:

nano /root/internet-check.sh

#!/bin/sh
#
#start looping
while [ true ]; do 
#checking if internet is available (google)
/usr/bin/wget -q --spider http://google.com > /dev/null
if [[ $? -eq 0 ]]; then
#internet is available, switch on the green led and off the red one
/bin/echo "0" > /sys/devices/gpio-leds/leds/A4001N1:red:inet/brightness
/bin/echo "1" > /sys/devices/gpio-leds/leds/A4001N1:green:inet/brightness
else
#internet is not available, switch off the green led and on the red one
/bin/echo "0" > /sys/devices/gpio-leds/leds/A4001N1:green:inet/brightness
/bin/echo "1" > /sys/devices/gpio-leds/leds/A4001N1:red:inet/brightness
fi
#sleep 30 seconds and then repeat checking
sleep 30
done

note: change ".../sys/devices/gpio-leds/leds/A4001N1:green:inet/..." with the correct path for your router.

then give the right permissions to the above script:

chmod 755 /root/internet-check.sh

test the script:

/bin/sh /root/internet-check.sh

to launch the script at the startup, put the following line right before the "exit 0" line in the "/etc/rc.local" file

/bin/sh /root/internet-check.sh &


#2

Two minor points:

Obviously relies on nano, which is an "aftermarket" package.

Although perfectly valid, I believe a single ping to 1.1.1.1 or 8.8.8.8 with a short timeout would suffice and deliver the same result and not rely on the availability or reachability* of the web server. Also, for pppoe connections, this could be done in a hotplug script.

*) Is that a word? What I'm getting at is that google.com might be blocked upstream, or feel offended by getting a request every 30 seconds. DNS servers might be blocked too, but you're already relying on DNS resolution at this point anyway.


#3

Good points!