Setting up a Raspberry Pi Server Status Display

3 minute read

I’ve been running a Raspberry Pi Model B as a development server for several years now. Originally, I had it mounted in a basic Adafruit Pi Box Enclosure. Although this worked fine for a headless server build, I missed having a display which would allow me check the server status, or safely shut it down without logging in. After some digging, I found this 16x2 LCD and Keypad kit on Adafruit which seemed perfect for the job. Conveniently, it fit the Adafruit enclosure by simply removing the lid and replacing it with the LCD plate. Once setup and running, it made my Raspberry Pi into a respectable home server.

Raspberry Pi Server Status Display

Once I assembled the kit and installed it in the Pi enclosure, I had to figure out how to implement the status display. As always, Adafruit has many examples to learn from on their product pages. I tried various iterations of their code, and eventually developed the script shown below. When run in the background, the script displays the system time and load average on the display. It continues to monitor for a keypress every 2/10s of a second. If any key is pressed, the backlight is turned on for 10 seconds so I can check the status. The Adafruit examples had no built in delay, which made the code running in the background absorb 100% of processor time. I found by adding a slight delay into the code using the Python time.sleep function, this dropped processor use to almost zero.

#!/usr/bin/python

from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
import os, time
from time import sleep, strftime
from datetime import datetime, timedelta

# Initialize the LCD using the pins 
lcd = Adafruit_CharLCDPlate()

time.sleep(1)
pressed_time = None
start_time = time.time()

while True:
	if lcd.buttonPressed(lcd.DOWN):
		if pressed_time is None:
			pressed_time = time.time()
		elif time.time() - pressed_time >= 1.0:
			lcd.clear()
			lcd.message("  "+datetime.now().strftime('%b %d %H:%M\n'))
			lcd.message("    SHUTDOWN    ")
			time.sleep(2)
			lcd.backlight(lcd.OFF)
			os.system("sudo poweroff")
	elif lcd.buttonPressed(lcd.UP):
		if pressed_time is None:
        		pressed_time = time.time()
        	elif time.time() - pressed_time >= 1.0:
        		with open('/proc/uptime','r') as f:
				uptime_seconds = float(f.readline().split()[0])
				t = datetime(1,1,1) + timedelta(seconds = uptime_seconds)
				lcd.clear()
          			lcd.message("  "+datetime.now().strftime('%b %d %H:%M\n'))
           			lcd.message(" "+"%dd %02dh %02dm %02ds" % (t.day-1, t.hour, t.minute, t.second))
	elif lcd.buttonPressed(lcd.RIGHT):
		if pressed_time is None:
			pressed_time = time.time()
	elif lcd.buttonPressed(lcd.LEFT):
		if pressed_time is None:
			pressed_time = time.time()
	elif lcd.buttonPressed(lcd.SELECT):
		if pressed_time is None:
			pressed_time = time.time()
	else:
		pressed_time = None

	time.sleep(0.2)

	if pressed_time <> None:
		lcd.backlight(lcd.ON)
		start_time = time.time()

	if time.time() - start_time >= 10:
		lcd.backlight(lcd.OFF)
		start_time = time.time()
		# Extract loadavg for clock display
		with open('/proc/loadavg','r') as f:
			loadavg = f.readline()
			lcd.clear()
			lcd.message("  "+datetime.now().strftime('%b %d %H:%M\n'))
			lcd.message(" "+loadavg[0:14])

As you see in the code, I have the following additional functions configured:

  • If I press and hold the up key for one second, the display shows the current system uptime for 10 seconds
  • Similarly, if I press and hold the down key, the system performs a safe shutdown of the server

I haven’t defined any use for the other keys, but with the core routines now completed, adding more functions is easy. One other point to note, as this script is running in the background, I limit myself to Python functions only, rather than calling out to external code. Any system calls to external functions would greatly increase the overall system load. Also, to ensure the script runs at boot time, I have added the following reference in the /etc/rc.local file on my system.

# Start Adafruit status display script
/home/pi/Adafruit_CharLCDPlate/Adafruit_CharLCD_Clock.py &

I hope you found this post useful, or at least interesting. If you would like to contact me or provide feedback, use the Contact page, or message me on Twitter. Thanks!