User avatar
RPIgoodSBC
Posts: 126
Joined: Tue Mar 20, 2018 11:37 am

Four letter pHAT showing IP on rpi-4B

Sun Oct 20, 2019 3:15 pm

This is my first program on python adapted from pimoroni github examples for fourletter pHAT.

https://github.com/pimoroni/fourletter-phat

https://pinout.xyz/pinout/four_letter_phat

The program shows the current IP on rpi-4B using python3 and the ducoterra github branch that add "reverse" functionality to write to upside-down display.
You must install and use this library...

https://github.com/ducoterra/fourletter-phat

for reverse the output display.

Code: Select all

#!/usr/bin/env python

import time
import fourletterphat
import HT16K33
import reverser
# Add "reverse" functionality to write to upside-down display.
# You must install and use this library... https://github.com/ducoterra/fourletter-phat
from subprocess import Popen, PIPE

print("""
Four Letter pHAT: ip-reverser.py

This program works ok on rpi-4B using python3.
Needs hostname (version 3.21 works) with output hostname -I support.

If you want run it on boot add a line like below to /etc/rc.local before exit0 line.

python3 /home/pi/where/you/want/ip-reverser.py &

This example will display your Pi's IP on fourletter pHAT.
Dots in IP address are shown like blank spaces.

Four Letter pHAT is a four 14-segment display that can be used to display text
numbers, and a host of other characters.Its Matrices are a retro-style green
similar to old digital alarm clock and are driven by the HT16K33 chip over I2C.

https://github.com/pimoroni/fourletter-phat
http://docs.pimoroni.com/fourletterphat/
https://pinout.xyz/pinout/four_letter_phat

Press Ctrl+C to exit.
""")

while True:
    ip = Popen(["hostname", "-I"], stdout=PIPE)
    ip = ip.stdout.read(14).decode('utf-8')
    # hostname -I returns an IPv4 address and a IPv6 address prefixlen 64 and prefixlen 128.
    # Strip down ip.stdout.read() for example 14 to show only the IPv4 address.
    fourletterphat.clear()
    fourletterphat.set_brightness(10) # values 0...15
    fourletterphat.set_reverse() # Add "reverse" functionality to write to upside-down display.
    fourletterphat.scroll_print(ip,0.5) # 0.5 are velocity scroll on seconds.
    fourletterphat.show()
    time.sleep(0.25)
    

The program is working fine on pi4 using python3.
If you unplug the ethernet cable fourletter display goes off after a few seconds and when you plug again shows after a few seconds the current ip.
Works perfect on wifi but if you have wifi plus ethernet the ip shown is the ethernet interface.

If you want run on boot read the instructions on the script.
Booting rpi4 you can see some characters on the LCD that are the long output of hostname -I (IPv4 address and a IPv6 address prefixlen 64 and prefixlen 128) but after a few seconds about 10 seconds you can see the short IPv4 address scrolling on the LCD.
I have used value 14 long to show ip that must be enough to avoid show all output from hostname -I command but you can change the value on the script.
For my use in all cases 14 has been the best value since my internal ip is usually xxx.xxx.x.xxx and this way avoid first character of IPv6 after IPv4 output from hostname -I

Of course you can use in the script... ip = ip.stdout.read().decode('utf-8') and you obtain all the long output that gives hostname -I

Hope this helps someone with same pHAT and need to read reverse output ip on fourletter pHAT and will be more comfortable knowing the ip to connect by ssh with the pi.

Return to “General discussion”