element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Upcoming Project: LED IP Status Indicator. How?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 14 replies
  • Subscribers 677 subscribers
  • Views 3336 views
  • Users 0 members are here
  • raspberry
  • pi
  • raspberry_pi
  • learning
  • raspberrypi
  • rpi
  • linux
Related

Upcoming Project: LED IP Status Indicator. How?

Kuya.Marc
Kuya.Marc over 13 years ago

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

  • Sign in to reply
  • Cancel
  • morgaine
    morgaine over 13 years ago

    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

     

    LED_CONTROL_CMD="switch_led"

    LED_CONTROL_CMD="echo"

    BASE_LED_NUMBER="5"

    INTERVAL="30"

     

    HOSTS="

            google.com

            slashdot.org

            no-such-host.net

    "

     

    while true

    do

            LED_NO="$BASE_LED_NUMBER"

            for TARGET in $HOSTS

            do

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

                    then

                            "$LED_CONTROL_CMD" "$LED_NO" 1

                    else

                            "$LED_CONTROL_CMD" "$LED_NO" 0

                    fi

                    LED_NO=$(($LED_NO + 1))

            done

            sleep "$INTERVAL"

    done

     

    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. image

     

    Morgaine.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • morgaine
    morgaine over 13 years ago in reply to morgaine

    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. image

     

    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.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • morgaine
    morgaine over 13 years ago in reply to morgaine

    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.

     

    Morgaine.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • morgaine
    morgaine over 13 years ago in reply to morgaine

    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:

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Kuya.Marc
    Kuya.Marc over 13 years ago in reply to morgaine

    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

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jhz
    jhz over 8 years ago

    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!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • rew
    rew over 8 years ago in reply to jhz

    I would write  a shell script:

     

    #!/bin/sh

    the_IP=192.168.1.12

    thepin=1

    gpio mode $thepin out

    while true; do

        if ping -c 1 $the_IP ; then

           gpio write $thepin 1

      else

          gpio write $thepin 0

      fi

    done

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • jhz
    jhz over 8 years ago in reply to rew

    Hi, Thank you very much for the code. You made my day.

    Can you give me a hint if we don't know the IP of other device, then how it will look like?

    Once again thank you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • rew
    rew over 8 years ago in reply to jhz

    Well then... Can you give me a hint as to how we could know it is THE device.... :-)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • gregoryfenton
    gregoryfenton over 8 years ago in reply to jhz

    How about something like this: https://github.com/drkblog/findmacs which scans an IP range and returns all the MAC addresses it finds.

    Simply parse for the desired MAC address and toggle the LED accordingly.

     

    It would be much easier to reserve an IP address for the device in your router though.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube