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.
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.
As you see in the code, I have the following additional functions configured:
- If I press and hold the
upkey for one second, the display shows the current system uptime for 10 seconds
- Similarly, if I press and hold the
downkey, 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.