14 Replies Latest reply on Jan 26, 2017 8:57 AM by Roger Wolff

    Upcoming Project: LED IP Status Indicator. How?

      The Situation


      I've configured my Broadband Internet Router to have fixed DHCP addresses for allowed devices (being used by household relatives). Before shutting off the Internet (so I can sleep), I typically ping IP addresses to see if they're connected. These are Wi-Fi connections, therefore, I can't physically see a LED light up on the router, itself.


      The Configuration


      I have the Raspberry Pi, Model B at 512MB, that has a physical LAN connect to my Broadband Internet Router, which also is a Wi-Fi access point. I am able to manually ping IP addresses when logging in via SSH.


      The Project


      Having a LED (connected via GPIO with resistor) light up when a specific IP address is active, without requiring a SSH or direct login. In other words, its a hardware version of the ping utility.


      The Questions


      1. Can this be a BASH script that runs without SSH login?
      2. Can this be compiled from C source code? (I'm not used to Python, yet.)
      3. Can this run as a system service, whereas it's transparent to Raspberry Pi operations (Raspbian, XBMC, etc.)?


      The Summary


      I am not asking someone to write the code as I'm a veteran C programmer with 15+ years of Linux experience. I've never written code to test, and constantly retest of a specific IP address is alive or not. This project may be anywhere from extremely easy to extremely hard, but I know the Raspberry Pi can do it.


      I will give credit to those that wish to make the initial code for me, as this project can be beneficial in many ways, such as checking if a specific device (computer, smartphone, etc.) is online or not.  Thanks in advance!


      Kuya Marc

        • Re: Upcoming Project: LED IP Status Indicator. How?

          Hi Marc. :-)


          Given a GPIO LED control program (of which there are millions because it's the "Hello world" of hardware interfacing ARM boards like Pi), it's very easy to do the network testing in a shell script.  Let's assume that you have a program called switch_led which takes two numeric arguments, the first a LED number and the second either 0 or 1 to switch that LED off or on.


          A one-liner can then test the network target and control the LED accordingly, eg. for target some_host and LED number 5:


          if ping -n -c 1 some_host >/dev/null 2>&1; then switch_led 5 1 ; else switch_led 5 0 ; fi


          And then you can wrap that up in a loop to perform your operation at whatever intervals you desire, and you have yourself a simple network monitor with LED display.  For example, to check every 30 seconds:


          while true; do some_task_to_be_repeated; sleep 30; done


          So, putting it all in a file to avoid one-liners and with a few embellishments to check a list of targets and light up numerically ascending LEDs for them, we have something like:


          #! /bin/sh













          while true



                  for TARGET in $HOSTS


                          if ping -n -c 1 $TARGET  >/dev/null 2>&1


                                  "$LED_CONTROL_CMD" "$LED_NO" 1


                                  "$LED_CONTROL_CMD" "$LED_NO" 0


                          LED_NO=$(($LED_NO + 1))


                  sleep "$INTERVAL"



          Running the above script outputs every 30 seconds the text:


          5 1

          6 1

          7 0


          because I've overridden the setup of the "switch_led" command by "echo" so that you can see what it's doing.  Comment out the LED_CONTROL_CMD="echo" on line 4 and it'll work properly, calling "switch_led" to light up LEDs 5, 6, 7, ..... depending on the ping success for the targets in the ordered list.


          You'll notice that no-such-host.net turns LED 7 off (because DNS lookup failed, but it would do the same if the domain name existed but the host were down), whereas LEDs 5 and 6 light up unless Google and Slashdot are having a bad day.


          Now you just need to find something to do the "switch_led" operation, which can also be done in the shell or you might want to write a tiny C proggie to do it as an exercise. Some examples here.


          'Hope this helps.



            • Re: Upcoming Project: LED IP Status Indicator. How?

              There are numerous way of making such a script run permanently as a service, typically by installing it somewhere central and then starting/stopping it with a standard-format script in the /etc/init.d directory, although the details of this vary a bit depending on distro.


              An alternative approach that has less variation between distros is to make a one-shot script instead of the one shown by removing its outer while loop, and invoking the one-shot script from crontab at a set interval.  That's typically configured by invoking "crontab -e" as user root, which opens up the default editor on the cron config file   You may have to run "man crontab" to figure out the line format to put in the file, but typically five space-separated asterisk fields followed by the commandline will make cron invoke said command every minute.  Provide a full pathname for the command.


              As an example (but check that your crontab is the same before using it!), on my system a crontab entry like this would work, once a minute:


              # MIN     HOUR     DAY     MONTH     DAYOFWEEK     COMMAND

              *             *              *          *                *                          /usr/local/bin/monitor_hosts


              Since commands that are invoked from root's crontab will naturally run as root, checking the crontab man page first regarding format and double-checking everything is strongly advised.


              PS. If the crontab command isn't found then cron isn't installed so you'll have to install some version of it first.  dcron is a good one.

              PS2. Only use root's crontab if your program needs to run as root to switch the LEDs, otherwise use an appropriate non-root crontab.

                • Re: Upcoming Project: LED IP Status Indicator. How?

                  I think it might be worth highlighting just how well standalone network monitoring actually suits the Pi.  It uses its best features without stumbling on any of its limitations, and that's the mark of appropriate engineering.  Short summary:


                  • Low cost solution, so low you could have 2 or 3 if you desire redundancy.
                  • Low power consumption owing to low duty cycle and no video output needed.
                  • Headless and therefore probably not affected by the USB hardware problems.
                  • Very low network bandwidth needed, doesn't stress Ethernet/USB transport.
                  • Requires almost no CPU power, so a lowly ARM11 is entirely perfect.
                  • Can easily be extended to provide more informative display or a web page.
                  • Can easily be extended to send alarms rather than just passively display.


                  I have a variety of network monitors of various types here at home, the most useful probably being those that not only display and log network issues, but that also send an audio message through my multi-room audio system.  An audio report such as "ISP xxx's DNS server is down" coming over the hifi (repeated periodically) is very effective in drawing attention to a problem locally.  You may need to use ducking if your average level of ambient audio is high.


                  I think this type of application is a good match for Pi.



                • Re: Upcoming Project: LED IP Status Indicator. How?

                  This is just a repeat of the little shell script example but using Element 14's new GitHub Gist facility.  I poked the code into GitHub as gist: 4182524 and with a bit of luck it'll be displayed below as a gist source inclusion:


                    • Re: Upcoming Project: LED IP Status Indicator. How?

                      Thanks you, Morgaine Dinova!


                      You gave me a lot of helpful answers. I'm looking at embedding it as a service, so it will be transparent regardless which SD Card is using, with the exception of OpenELEC. I'm looking at encoding it into a binary (rather than a script) in Raspbian and Arch Linux ARM. For this project, at first, I just want to know who's using my WPA-encrypted Wi-Fi access point so I know when to turn off (when no humans are using my connection). I'm specifying "humans" because my Androids don't complain if the Wi-Fi vanishes.


                      Thanks for the input!


                      Marc Miranda

                  • Re: Upcoming Project: LED IP Status Indicator. How?

                    Hi Folks, I am doing a project in which I required something same. I am using my Raspberry Pi as access point and I want to turn ON an LED (connected with GPIO) when a device connect with the Raspberry Pi. Please help!