Raspberry Pi Zero 2 WH — $18
I2C 20×4 LCD Display — $5
Shadowbox Frame — $7
Doing a geeky project for under $30?? Priceless…
Ah, the Raspberry Pi. That $35 single board computer everyone was scalping for 3x what they were worth during the chip shortages. Well, I used to own several of them… and unfortunately no longer do. I will say, for the MSRP price they aren’t a bad option. The whole ecosystem is quite attractive for many reasons, and the brand receives praise left and right for it. I will indeed say, they’re basically swiss army knives for a hacker. A whole miniature linux system, with a quad core 64 bit CPU and often 1 – 4 GB of RAM. IMO the 8 GB is a waste of money, of course, I tend to like lean configurations so perhaps I just feel that way because I’d never use 4 GB on a Pi let alone 8. AND, if I did need 8 GB or more, I’d use a DDR4 mini PC, not a Pi!
Anywho, in the spirit of what the Pi is all about, I wanted something cheap to hack on. I have a Pi 5, but it pulls full time duty as a server. And, what can I say? It works so well for this, and the small size and lower power requirements are part of that attraction for me. Now, PCIe gigabit ethernet, and PCIe NVME storage are a pretty strong motivation for my willingness to keep the Pi 5 4 GB I’ve got employed as a server. Without those, I’d use a thin client or old laptop in a heartbeat. Oh yeah, the spirit of the Pi, that’s where I started blabbing right?
So the Pi Zero, it’s like an original 2012 Pi, but with optional Wifi. You loose onboard ethernet (but it was USB anyway on the early models, and you do have a USB port to add a NIC…) but you get a very small package still boasting full 40 pin GPIO. They refreshed the Pi Zero in late 2021 with the Pi Zero 2. If you want WiFi and BT, you want the Zero 2 W. Want pre-soldered GPIO pins too? Get the WH.
** NOW a little PSA here, I bought a Pi Zero 2 WH on Amazon… so that came /w a soldered GPIO pin header. Quite handy, even has color coded spots at the base of each pin so you know what is GPIO, 5v, Ground, etc… Except, mine was put on upside down. Took me forever to figure this out, and I would have been pretty pissed if I needed to RMA it because some shoddy reseller is doing these headders themselves to save 30 cents and mislabeling the pins. I don’t care now that I know, but being largely for the education market this is a bit discouraging to see. If I were in the same situation as a young kid, the Pi may very well have gone in the bin.
You can get a pack of two 20 character / column x 4 row LCD screens, with pre-soldered i2c “backpack” for about ten bucks. And, you can get it in green, red, blue, whatever you want. I went with the OG, green LCD.
So… what does it do? Well, it’s an excuse to have another Linux box in your fleet, I mean, what more do you want?? But since you asked, it does anything you tell it to. Right now, mine spends five seconds showing me the date, time, and my web server uptime. Then it shows me local weather for another five seconds. There’s more in the pipe though, and trying out new code is incredibly easy.
What makes this clock… tick?? Python.
#!/usr/bin/env python
import drivers
from time import sleep, strftime
import argparse
import requests
import subprocess
def get_uptime():
try:
# Run the 'uptime -p' command and capture the output
#result = subprocess.run(['uptime', '-p'], capture_output=True, text=True, check=True)
result = subprocess.run(['cat', '/tmp/uptime'], capture_output=True, text=True, check=True)
uptime_str = result.stdout.strip() # E.g., "up 1 day, 1 hour, 45 minutes"
## # Use awk to format it as "up 1d 1h 45m"
## formatted_uptime = subprocess.run(
## ['awk', '{print "WWW up ", $2 " weeks", $4 "d", $6 "h"}'], input=uptime_str, text=True, capture_output=True
## ).stdout.strip()
## The above works, when you've had < 7 days up... then we need the following... (and yes, I could have made this MUCH more elegant)
# Use awk to format and convert weeks into days, then calculate total days
formatted_uptime = subprocess.run(
['awk', '{week_days=($2*7); total_days=week_days+$4; print "HTTPD.lan up", total_days "d", $6 "h"}'],
input=uptime_str, text=True, capture_output=True
).stdout.strip()
return formatted_uptime
except subprocess.CalledProcessError as e:
print(f"Error getting uptime: {e}")
return "Uptime not available"
# Load the driver
lcd = drivers.Lcd()
# Weather API settings
API_KEY = "000000000000000000000" ## The API keys are free, just sign up. Painless or I wouldn't have bothered.
ZIP_CODE = "00000" ## Your Zip code here!
COUNTRY_CODE = "US"
WEATHER_URL = f"http://api.openweathermap.org/data/2.5/weather?zip={ZIP_CODE},{COUNTRY_CODE}&appid={API_KEY}&units=imperial"
# Function to fetch weather data
def get_weather():
try:
response = requests.get(WEATHER_URL)
data = response.json()
if data and data["cod"] == 200:
temp = round(data["main"]["temp"])
humidity = data["main"]["humidity"]
wind_speed = round(data["wind"]["speed"])
wind_dir = data["wind"].get("deg", "N/A")
return temp, humidity, wind_speed, wind_dir
except Exception as e:
print("Error fetching weather:", e)
return None, None, None, None
# Parse command-line arguments
parser = argparse.ArgumentParser(description="LCD Display Script")
parser.add_argument("--wc", action="store_true", help="Only display weather and clock pages in rotation")
args = parser.parse_args()
try:
while True:
# Date/Time page
lcd.lcd_clear()
lcd.lcd_display_string(strftime("Today is %A,"), 1)
lcd.lcd_display_string(strftime(" %B %d"), 2)
# Display uptime on the 4th row
uptime = get_uptime() # Call the function and store the uptime
lcd.lcd_display_string(f"{uptime}", 4)
# Continuously update the time (third row)
for _ in range(10): # Display for ~10 seconds
lcd.lcd_display_string(strftime(" %I:%M:%S %p"), 3)
sleep(1)
# Weather page
if args.wc: # Include weather in both modes (if --wc is passed)
temp, humidity, wind_speed, wind_dir = get_weather()
if temp is not None:
lcd.lcd_clear()
lcd.lcd_display_string(" Boscawen, NH ", 1)
lcd.lcd_display_string(f" Temp: {temp}F ", 2)
lcd.lcd_display_string(f" {humidity}% Humidity", 3)
lcd.lcd_display_string(f" Wind: {wind_speed}mph", 4)
sleep(5)
except KeyboardInterrupt:
print(" ~ Clearing ~ ")
lcd.lcd_clear()
|
Now, I’m not really much of a programmer. Nope. But, ugly or not there it is. I suggest you do what I did, and start here: The Raspberry Pi Guy has a page with sample code and some other helpful stuff on Github. Using the 16×2 code on a 20×4 is as easy as changing 16 to 20 and 2 to 4. Well, gotta add lines 3 and 4 below 1 and 2. But not rocket surgery.
I recommend using the overlay FS and read only /boot partition if you do something like this to avoid accidental SD card filesystem corruption from unsafe shutdowns. I actually added a systemd service so that on target of reboot, halt or shutdown a shell script will kill the python process, then launch another which blanks the screen and replaces the text with “IT IS NOW SAFE TO TURN OFF YOUR COMPUTER” — if you know, you know. About 1 second after that hits the LCD, the Pi powers off and the Act LED goes dark. The LCD will stay lit, and retain the last thing printed on it as long as power is connected.
Now, the BEST thing to do for your filesystem / SD card is to power off via SSH before unplugging any Pi. However, to power my “clock” up, all I do is plug it in. If you put in your crontab a line starting with @reboot, you’ll be able to easily start scripts at boot. I did this as root, because I think you need to be root to use the GPIO. Probably a way around this, but this runs nothing other than the display stuff at the moment.
Cron on the Pi Zero 2 W. aka PiFrame:
@reboot /root/lcd/bens3.py –wc
@reboot curl -s https://ben.lostgeek.net/uptime.txt -o /tmp/uptime
0 * * * * curl -s https://ben.lostgeek.net/uptime.txt -o /tmp/uptime
What this does is at boot, we pull uptime from a text file on my webserver and we start up the python program with the –wc arg, “weather clock”. This applies to the code above, so I left it as is. Only one more part is needed.
Cron on the server:
0 * * * * uptime -p > /var/www/html/ben/uptime.txt
This puts an up to date uptime file in my web directory once an hour. And the keen observers among us probably noticed that the Zero also will refresh this information at the top of each hour too. Easy peasy.