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 3337 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
Parents
  • 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

    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
Reply
  • 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
Children
  • gregoryfenton
    gregoryfenton over 8 years ago in reply to Kuya.Marc

    I just saw your post, sorry for my late reply.
    How about you open your router's attached devices page and parse it for the MAC address you are checking for?

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

    So, while I was playing dumb and pretended that we know the IP address of the device in question, you are now pretending/assuming we know the mac address.

     

    But the problem remains a generic one. It seems we're looking for a specific device, how do we recognize that device?

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

    You both are right. First we need to know the device which is connecting with Raspberry Pi. I made RPi access point following this guide. I think we need to check the log file to get the IP of the device first and using that IP we can turn ON the LED connected with GPIO but I don't know how to do that.

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

    My ISC-DHCPD shows:

    Jan 26 13:40:14 myserver dhcpd[6871]: DHCPACK on 192.168.xxx.yy to xx:yy:zz:49:62:08 via enp4s0

    in the /var/log/syslog file when a DHCP lease is given out or extended.

     

    So with

    tail -f /var/log/syslog | awk  '/DHCPACK/ { system ("/home/pi/ip_given_out"  $8 );}'

    you can have a script (/home/pi/ip_given_out) run with as an argument the IP address that was just given out. You can save that to a file

    LASTIP_FILE=/home/pi/.lastip

    echo $1 > $LASTIP_FILE

    and then use

    the_IP=`cat $LASTIP_FILE`

    in the script I've typed (and not tested) above. (some merging/mangling still required. Left as an excercise for the reader).

    • Cancel
    • Vote Up +2 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