jbudd
Posts: 1500
Joined: Mon Dec 16, 2013 10:23 am

A script to flash the Pi's IP address on the PWR or ACT LED

Tue Jul 07, 2020 2:01 pm

This might be useful for people setting up a Pi without a monitor.

I have tested it on a Pi 4 and Pi Zero W. It should work on Pi 3B, probably not earlier models.

Save the code to a file /home/pi/flaship and make it executable
chmod +x /home/pi/flaship

To run this automatically at startup I suggest you put this lne in /etc/rc.local immediately above exit 0:
timeout --signal=SIGHUP 120s /home/pi/flaship >/dev/null 2>&1 &

Code: Select all

#! /bin/bash
#############################################
# Script to flash IP V4 address on PWR or ACT led
#
# Each 3 digit group of IP to be displayed is shown by:
# A long flash then
# Digits shown by the number of short flashes (zero is 10 flashes)
# A short pause
# so .102 is 1 long flash, 1 flash, pause, 10 flashes, pause, 2 flashes
#
# "Heartbeat" flashes means not yet got a valid IP
#############################################
NWINTERFACE=                              # blank or eg "wlan0", "eth0"
SHOWIPGROUP1=Y                            # 1st group eg 192    }
SHOWIPGROUP2=Y                            # 2nd group eg 168    }
SHOWIPGROUP3=Y                            # 3rd group eg 0      } 192.168.0.22
SHOWIPGROUP4=Y                            # 4th group eg 22     }

# Should not need to change anything below here
DIR="/sys/class/leds"                     # where the led control files are
MAXTRIES=20                               # Attempts to discover a valid IP ~3s each
LONGPAUSE=3                               # Long flash between IP groups (seconds)
SHORTPAUSE=0.6                            # Pause between digits
DOT=0.15                                  # Duration of a single short flash
#############################################
# Functions

setled() {                                # Adjustments to LED settings
   echo $2 | sudo tee $DIR/$LED/$1 >/dev/null
}

clr() {                                   # Turn LED off
   setled trigger none
   setled brightness $off
   sleep $LONGPAUSE
}

longflash() {                             # long flash between IP groups
   setled brightness $on
   sleep $LONGPAUSE
   setled brightness $off
   sleep $DOT
}

showdigit() {                             # Flash a number of times
   printf " %d  "  "$1"
   local i=$( (( "$1" == 0 )) && echo 10 || echo "$1")
   while (( i > 0 ))
   do
      setled brightness $on
      sleep $DOT
      setled brightness $off
      sleep $DOT
      (( i = i - 1 ))
   done
   sleep $SHORTPAUSE                      # pause between digits
}

validip() {     # Most of this function from Mitch Frazier linuxjournal.com
   IP=$(ifconfig $NWINTERFACE | grep broadcast |head -1 |tr -s ' '| cut -d ' ' -f 3)  # only the first if more than 1
   local  stat=1
   local  ip=$IP
   if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]  # not full regex for IP
   then
      OIFS=$IFS
      IFS='.'
      ip=($ip)
      IFS=$OIFS
      [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
      stat=$?
   fi
   return $stat
}

checkmodel() {
   model=$(cat /sys/firmware/devicetree/base/model | cut -d " " -f 3)
   if [[ "$model" == "Zero" ]]
   then
      on=0; off=1                         # Pi Zero LED on/off logic is reversed
      LED=led0                            # Use the ACT LED
   else
      on=1; off=0
      LED=led1                            # Use the PWR LED
   fi
}

#############################################
# Main code

checkmodel                                # Ensure the right LED control logic

saved_trigger=$(cat $DIR/$LED/trigger | sed -e '{s/^.*\[//}' | sed -e '{s/\].*//}')  # Make a note of the current trigger
trap 'setled trigger $saved_trigger; exit' EXIT SIGINT SIGHUP    # Put it back when we exit

i=0
setled trigger heartbeat
while ! validip $IP                       # get IP address
do
   (( i = i +1 ))
   if (( i > $MAXTRIES ))
   then
      echo Too many attempts to find IP - giving up
      exit 1
   fi
   sleep $LONGPAUSE
done
printf "IP address is %s\n" $IP

clr                                       # Turn led off for a while first

for group in {1..4}                       # Each of four groups of ip address
do
   showthis=SHOWIPGROUP${group}           # do we want to show this IP group?
   if [ ${!showthis} == "Y" ]
   then
      longflash                           # long flash between groups
      this=$(echo $IP | cut -d '.' -f $group)   # get this group
      printf "IP group %d (%d) " $group $this
      for (( j=0; j<${#this}; j++ ))      # each digit in turn
      do
         showdigit "${this:$j:1}"         # display the digit
      done
      printf "\n"
   fi
done

clr                                       # Turn led off for a while at the end
echo Finished

User avatar
neilgl
Posts: 2546
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Tue Jul 07, 2020 2:09 pm

Looks good - sort of like Morse code?

jbudd
Posts: 1500
Joined: Mon Dec 16, 2013 10:23 am

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Tue Jul 07, 2020 2:25 pm

No Morse code, you just have to count the short flashes

fanoush
Posts: 626
Joined: Mon Feb 27, 2012 2:37 pm

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Tue Jul 07, 2020 2:48 pm

with morse there may be phone apps like this https://play.google.com/store/apps/deta ... ator&hl=en (just found, not tried) to detect the blinking and decode the IP for you

drushtx
Posts: 48
Joined: Wed Mar 21, 2012 6:16 pm
Location: Houston, Texas
Contact: Website Yahoo Messenger Facebook YouTube

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Thu Sep 03, 2020 5:28 am

Hello jbudd,

I have a weekly AMA on YouTube featuring the Pi. Every week, we explore how to use the Pi as a learning tool to prepare for various CompTIA certifications, we use WireShark on Pis to explore protocols and process, we install productivity tools and even just some fun tools. This Friday (4 Sept), I would like to start talking about bash scripting and your script is really slick so I plan to include it in the lesson this week (or if time doesn't permit, on 11 Sept). I will give you full acclamation, of course.

See archives of our streams archive at: https://www.youtube.com/user/TotalSemin ... e_view=503

Typically, I use a tutorial from a website or similar resource so I can refer our audience to it so they can follow written step-by-step or where they can copy/paste code such as your script. For them moment, I will refer them to this thread.

My question is: do you have a website or other web accessible location outside of this thread where I might refer folks to visit?

Many thanks,

David

User avatar
HermannSW
Posts: 3180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Thu Sep 03, 2020 6:23 am

viewtopic.php?f=66&t=259366
As you I started with signaling on led.
I ended letting (headless) Pi espeak its IP over headphone jacket on boot.
https://stamm-wilbrandt.de/en/Raspberry_camera.html
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/raspiraw
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/working_with_FPGAs

jbudd
Posts: 1500
Joined: Mon Dec 16, 2013 10:23 am

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Thu Sep 03, 2020 7:34 am

Hi David, by all means use the script.

I don't have a website to refer you to.

If you come up with improvements please let me know.
The script features:
Inconsistent use of brackets.
$i used as a loop counter is bad programming form.
The code
showthis=SHOWIPGROUP${group}
if [ ${!showthis} == "Y" ]
(Set a variable to the name of another variable then get it's value) is particularly cryptic and unfriendly for beginners. I'm certain you could improve this!
jb
Last edited by jbudd on Thu Sep 03, 2020 10:57 am, edited 1 time in total.

User avatar
HawaiianPi
Posts: 6001
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Thu Sep 03, 2020 9:35 am

jbudd wrote:
Tue Jul 07, 2020 2:01 pm
This might be useful for people setting up a Pi without a monitor.
I just ssh user@hostname.local when setting up a new system (pi@raspberrypi.local with Raspbian/Raspberry Pi OS).

Although I do recall a previous discussion on the forums about doing something like this.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

drushtx
Posts: 48
Joined: Wed Mar 21, 2012 6:16 pm
Location: Houston, Texas
Contact: Website Yahoo Messenger Facebook YouTube

Re: A script to flash the Pi's IP address on the PWR or ACT LED

Thu Sep 03, 2020 1:29 pm

Hi David, by all means use the script.

I don't have a website to refer you to.
Many thanks, jbudd!

Return to “General discussion”