Hardware components: | ||||||
|
× | 1 | ||||
|
× | 1 | ||||
Hand tools and fabrication machines: | ||||||
|
STORY
Project Overview
I recently built a really cool project using a Raspberry Pi Zero Wireless board and a Pimoroni Unicorn pHAT LED matrix. (It displays the status of my running unit tests in real time – check it out here). This project is designed to be “headless” – it’s not connected to any display.
This poses a problem – although I’ve configured the WiFi, I’m using DHCP and therefore don’t know what IP the Pi will be using when it boots! Without this information, I can’t SSH into the device or connect to my project remotely. I therefore needed a way to have the Pi inform me of its IP address on boot.
My solution: display the IP address on the Unicorn pHAT LED matrix in binary!Not only is this practical, but it also boosts my geek cred 😉
In this tutorial, I’ll walk you through how to replicate this solution for your own projects.
Hardware
This project uses a Raspberry Pi Zero Wireless, but it should work on virtually any Pi device with a 40-pin header.
Besides the Pi, you’ll need the following:
- Pimoroni Unicorn pHAT
- A 2×20 header
- Soldering iron
Plus the usual essentials for your Pi:
- 2 amp MicroUSB power supply
- Micro SD card with Raspian
- (optional) USB WiFi dongle, if your Pi doesn’t provide on-board WiFi
Assembly is very straight-forward – place the 2×20 header between the Pi and Unicorn pHAT and solder it together. A detailed guide to soldering your pHAT can be found here: https://learn.pimoroni.com/tutorial/sandyj/soldering-phats
Code & Dependencies
This script was designed to run on Python 3. Therefore, make sure you install Python 3 on your Pi:
sudo apt-get update
sudo apt-get install python3 python3-dev python3-pip
(It probably works on Python 2, but I have not tested this).
We’ll also need to install the unicornhat library for Python:
curl -sS https://get.pimoroni.com/unicornhat | bash
Here’s the full code listing for our Python script:
import socket
import time
import unicornhat as unicorn
# From http://commandline.org.uk/python/how-to-find-out-ip-address-in-python/
def getNetworkIp():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
return s.getsockname()[0]
# Prepare the Unicorn pHAT display
unicorn.set_layout(unicorn.PHAT)
unicorn.rotation(0)
unicorn.brightness(0.5)
# Obtain our IP address and split it into the 4 components ("octets")
ip = getNetworkIp()
octets = ip.split('.')
# Render the binary representation for each octet
y = 0
for octet in octets:
bits = '{0:08b}'.format(int(octet))
x = 0
for b in bits:
if int(b):
unicorn.set_pixel(x, y, 0, 0, 128)
x += 1
y += 1
# Render the display
unicorn.show()
# Keep the LEDs lit for 30 seconds
time.sleep(30)
It’s very straight-forward: we determine the IP address, split it into four parts (or “octets”), and then render each one in binary
Running the Script on Boot
Upload the script to your Pi – perhaps to your home folder (/home/pi
) . Lastly, add this line to your /etc/rc.local file so the script runs on boot:
sudo python3 /home/pi/ip.py &
(The ampersand on the end will prevent our script from holding up the rest of the boot process).
Booting the Pi
Now that everything is in place, let’s go ahead and boot up the Pi! If you’ve previously configured your wireless network, the Pi should join it on bootup and then light up the LEDs like this:
Read More: Binary IP address display for Raspberry Pi