The first just sets the pins to the right
mode and makes sure they’re turned off:
pins="7 8 25 24 23 18 15 14"
for x in $pins
do
gpio -g mode $x out
gpio -g write $x 0
done
The second grabs the IP address from
ifconfig
, converts it to binary, then pads
it out with leading zeros, if necessary.
ipaddress=`ifconfig eth0 | grep
'inet ' | awk '{print $2}' | cut
-f4 -d'.'`
binary=`echo
"ibase=10;obase=2;$ipaddress" | bc`
paddedBinary=`printf %08d $binary`
The next part uses
cut
to extract the
part we want from this binary string
and outputs it to the appropriate pin.
bit=1
for x in $pins
do
out=`echo $paddedBinary | cut
-b$bit`
gpio -g write $x $out
bit=$((bit+1))
done
And, finally, we tell the script to sleep for
five minutes, then turn the LEDs off.
sleep 5m
for x in $pins
do
gpio -g write $x 0
done
That’s it! Save this into a file called
showIP.sh
, make it executable with:
chmod a+x showIP.sh
and type
sudo ./showIP.sh
to display
your IP. To get this run automatically on
boot, you just need to add the line:
/home/pi/showIP.sh &
to
rc.local
. See the ‘Camera controller’
section for details on how to do this.
Gertboards and Arduinos
Connecting directly to your Pi’s GPIO pins can
provide you with basic input and output control,
but there are limitations. There are two additional
items that you can obtain to help you interact
more precisely with the world around you.
The Gertboard is a fairly complete expansion
pack for connecting between your Pi and
the real world, including a micro controller,
and a range of input and output options.
Meanwhile, the Arduino is a micro controller that
can connect to your Pi (or any other computer)
via the USB port. Typically, it comes assembled,
but kit forms are also available. In its raw form,
it has fewer features than the Gertboard (which
includes an Arduino microcontroller), but it can
be expanded with a huge range of shields.
■
Figure 3. The simple circuit in all its glory.
28
Raspberry Pi User Guide.indd 28
08/07/2014 14:44