A script to flash the Pi's IP address on the PWR or ACT LED
Posted: 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 &
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