This is a small script, intended to be run from systemd, that controls an external LED based on WiFi status.
Note: The pin that drives the LED is identified by the GPIO Number not the pysical pin number.
The script runs on a 1 second loop. It uses the external wpa_cli
command to get the status of the given WiFi interface. It looks for
the WiFi status, not the Ethernet link up/down status. There are many
possible states returned by wpa_cli. This scripts maps them into
three states:
- Off, inactive, error, etc. LED off
- Establishing link. LED blinks
- Link Up. LED on
These states are printed to STDOUT
, so they will show up in
journalctl
. They are numbered from zero for internal reasons.
There are three necessary configuration items:
-
WiFi interface. Usually something like
wlan0
-
GPIO pin number for LED
-
Brigtness. This is a number from 1 to 255, with 255 being the brightest.
The script can be configured in one of four ways:
-
Command line args. The args are in the same order as listed above. The CLI args would only be used for testing or as arguments in the ExecStart statement in the systemd unit file
-
Environment Variables. See systemd unit file
-
Environment File. See defaults file
-
Defaults that I chose and hard-coded:
- Interface: wlan0
- GPIO Pin: 21
- Brightness: 128
- Python 3.5
- pigpio
- pigpio from PyPi
Place wifi-led.py
somewhere from where
systemd can run it, such as /usr/local/bin
.
Modify examples/systemd/wifi-led.service
and place it in
/etc/systemd/system
See also the
README
If using an environment file, modify examples/default/wifi-led
(See
the README). Place the file where
appropriate for your OS. In Raspbian this is /etc/default/
.
sudo systemctl enable wifi-led.service
sudo systemctl start wifi-led.service
sudo systemctl status wifi-led.service
If the service failed or out of curiosity, use
journalctl
to investigate:
sudo journalctl -xe -u wifi-led.service