System status lights on your Raspberry Pi using the PiGlow module and systemd

2014-06-24

System status lights

Recently I wanted to use my Raspberry Pi, a cheap and small computer device for hobby projects, for making a wireless radio. Wireless at least in the sense that it connects to the internet via a wifi dongle. I like my device to have some kind of interface, but I found a TFT screen a bit too expensive, so I decided to buy a Pimoroni PiGlow. The PiGlow is a small add on board for the Raspberry Pi that provides 18 individually controllable LEDs, which are placed in a three-legged swirling pattern and consists of red, orange, amber, green, blue and white LEDs. Turning these LEDs on and off from the command line is fairly easy using the Wiring Pi library. What I wanted was that when my Pi boots, it shows which services were running using these lights, in the following way:

  • Red lights: Filesystems are enabled
  • Orange lights: Wireless networking is enabled
  • Amber lights: SSH daemon is enabled
  • Green lights: Apache (HTTP) server is enabled

Configuration

The way I configured this behaviour was by writing my own systemd service scripts. For example, the file below defines the service that lights up the red LEDs when the filesystem is enabled.


[Unit]
Description=PiGlow Enabling Red1 Lights
Requires=local-fs.target
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/piglow_color red0 25

[Install]
RequiredBy=local-fs.target

Each service scripts has three sections, which are [unit], [service] and [install]. The [unit] section contains a description, it defines a requirement and an order. The requirement keyword ensures that this service starts when the service local-fs.target starts. By defining the after keyword as well, this service will only be started after local-fs.target has started and not simultaneously.

The [service] attribute defines the type of service. Here, I have used the oneshot type. This means that the service is started by running an executable, which immediately exits, in contrast to for instance a daemon service, which keeps running in the background.

Finally, the [install] attribute configures how this service should be installed. Installation is done by


systemctl enable piglow.color.red.service

Note that piglow.color.red.service should be the filename where you have written your service script. Place such files in /etc/systemd/system.

Similarly, for the green LEDs (which light up when the Apache server is enabled), I wrote the following script:


[Unit]
Description=PiGlow Enabling Green Lights
Requires=httpd.service
After=httpd.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/piglow_color green 10

[Install]
RequiredBy=httpd.service

Up next, I want the white LEDs to fade in and out when the device has any sofware updates ready to install. But I will dedicate another post on that topic. :-)

If you have questions or comments, feel free to drop a line! Like what you read? Share this page with your friends and colleagues.

Comments

Question:
What is the answer to Ten + Six?
Please answer with a whole number, i.e. 2, 3, 5, 8,...
choddles_at_gmail_dot_com
2014-10-05 00:15:27
did you progress more with this script, what else is needed for this, the above script needs more?
Question:
What is the answer to Five + Eight?
Please answer with a whole number, i.e. 2, 3, 5, 8,...