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

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 -o -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 60 seconds and then repeat checking
sleep 60
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 &

5 Likes

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 Likes

Good points!

Improved script:

#!/bin/bash
#
while :
do

ping -c5 -q 8.8.8.8
status=$?;
case $status in
    0)
        echo "host is alive";
    ;;
    1)
        echo "network unreachable or host not responding to pings";
    ;;
    2)
        echo "No route to host or other error";
    ;;
esac
if [ $status -eq 0 ]; then
    /bin/echo "Internet is up"
    /bin/echo "1" > /sys/class/leds/tp-link:blue:wan/brightness
else
    /bin/echo "Internet is down"
    /bin/echo "0" > /sys/class/leds/tp-link:blue:wan/brightness
fi
sleep 30
done

3 Likes

Based on your posts, and some googling, I have made my Archer C50 to show a orange led when no internet and a green one when yes.

There is a cron entry that excecutes a simple script with a ping based on this thread idea... which in turns excecutes 2 more small scripts (internet-on.sh and internet-off.sh).

Each one turns on a green (or orange) led and turns off the orange (or green) led if called. With de bash shebangs dont worked for me, so changed to #!/bin/sh. The main script does only one ping to google DNS.

chki.sh

#!/bin/sh
#
ping -q -w 1 -c 1 8.8.8.8 && /usr/bin/internet-on.sh || /usr/bin/internet-off.sh

internet-on.sh

/bin/echo "1" > /sys/devices/platform/gpio-leds/leds/c50:green:wan/brightness
/bin/echo "0" > /sys/devices/platform/gpio-leds/leds/c50:orange:wan/brightness

Internet-off.sh

/bin/echo "1" > /sys/devices/platform/gpio-leds/leds/c50:orange:wan/brightness
/bin/echo "0" > /sys/devices/platform/gpio-leds/leds/c50:green:wan/brightness

Last but not least. Here it is the content of my crontab, specifically the last line with the */3 which means every 3 minutes.

00 06 * * Mon,Tue,Wed,Thu,Fri,Sat,Sun /usr/bin/wifi_schedule.sh start
00 23 * * Mon,Tue,Wed,Thu,Fri,Sat,Sun /usr/bin/wifi_schedule.sh stop
*/3 * * * Mon,Tue,Wed,Thu,Fri,Sat,Sun /usr/bin/chki.sh

I moved the wlan LED funcionality to WPS (which I down use)

Why not just use a netdev trigger? https://openwrt.org/docs/guide-user/base-system/led_configuration

A word of caution: ICMP ping is not guaranteed to be delivered to the host and can be dropped by any host in-between. These get dropped easily in case of an overload.

You are right, you could -- and should -- use a netdev trigger or a hotplug script if you're using PPPoE/PPPoA. However, if your WAN connection is coming in through ethernet, your connection is purely physical, as long as your cable is plugged in it will always show "on" even if your upstream internet connection is down. I am assuming that is what @pilovis and @rodoviario are trying to accomplish here.

2 Likes

You are right, but if you have the ability to change your LED definition in the board configuration file (such as DTS) you could switch active high with active low, or vice versa.

Another why not -- why not use the hotplug script set to monitor WAN instead of crontab?

What strategy do you use to check connectivity?

Correct me if I wrong... but, I think that out-of-the-box, the luci-led configuration does detect well when you plug or unplug the WAN cable... and lights the corresponding LED in either case. I have moved that funcionality to the WPS LED, so it lits when there is a cable and a link to the upstream modem.
On the other hand, the tricks and scripts used aside the luci-LED gui allow me to ping a public IP and lit red if there is no answer, and green otherwise on the WAN LED.

Sidenote: With the 3 minute interval and only one ping to google DNS I avoid blocking, but also sometimes get false positives. It will show de red no-internet if there is a small glitch, for the next 3 minutes. I will fine-tune, but it is fine for me right now.

I used this tutorial and it's just excellent!
I am Using Multiple WAN using mwan3 1st WAN has unlimited plan and other has limited plan (for Backup) I was asking if there is a way to change LED to different colour if my router is using Backup WAN.

Like, MAIN WAN (Connected) EVEN (Backup is Disconnected) = Green
Backup WAN (Connected), Main WAN (disconnected) = Blue
Both WAN Disconnected = RED

It should be fairly easy using a small shell script, mwan3 does hotplug-like events. (I'm not using mwan3 so I can't give a more concrete example, sorry.)

1 Like

I've just configured things manually, then sketched a bash script with the whole procedure for the next time I need to start from scratch.

So I didn't fully test it yet, but most credits to chatgpt :slight_smile:

EDIT: I removed the script from here, as I fixed on the gist linked above some bits that chatgpt spit out and don't want to leave around an outdated version.
Please also note that - as takimata kindly reported - this approach installs collectd, which can be considered overkilling if you just need to run a ping in a loop and reflect its output on a led.
Since I've anyway installed collectd to temporarily store timeseries of gathered data and plot graphs from them, I've gone this way.

Installing collectd and analyzing its ping statistics, just to do pings on a regular basis and react to whether they fail or not? Talk about overkill.

Ah, that explains a few things.

:rofl:
did you actually measure the overhead?

Why would I need to? Installing and running collectd is the overhead, it is entirely unnecessary, the built-in ping does exactly the same.

I reallt appreciate your hint, but I still find useful having a way to monitor and plot ping latency also from the AP: I know this comes with pros and cons and it's plenty of alternatives.
OTOH my two APs (Zyxel WSM20) until yesterday were running their stock OEM firmware and I suspect it wasn't very optimised, as they resemble teapots when touched. Now they are cold, both with collectd and without. So - in order to experiment a bit - I am going to configure one with collectd, leaving the other one without any check.
Then I need a way to measure the gap :slight_smile:

Fair enough, that is collectd's purpose.

Please recognize the context. Of course there's plenty of ways to skin a cat6, in the context of this thread you just offered the most roundabout and complicated one.