Raspberrypi LED-Daemon
2025 - Jul 13
Driving Neopixel/WS2812-Leds on a raspberrypi turns out to be a root only job since you need to talk to the bus-System directly. Since I did'nt want to run the rest of my project as root I decided to implement a LED-Daemon that can be accessed via Socket.
In my setup I use pin 18
as control pin for the ws281x-LEDs. As result the daemon listens on port 9999 for incomming commands:
color | R G B | set all LEDs to color. Color values each 0..255, seperated by space |
ledcolor | N R G B | set color value of a specific LED. N==position of the LED, R G B: colorvalues 0..255 |
off | turn all leds off |
In future releases there might be additional commands but for in a first iteration this very simple server is perfectly adequate.
Code of the server:
# LED strip configuration:
= 12 # Number of LED pixels.
= 18 # GPIO pin connected to the pixels (18 is PWM).
= 800000 # LED signal frequency in hertz (usually 800khz)
= 10 # DMA channel to use for generating the signal (try 10)
= False # True to invert the signal (when using NPN transistor level shift)
= 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
# Create an instance of the PixelStrip class
=
"""Set the color of the entire LED strip."""
"""Set the color of the entire LED strip."""
"""Handle incoming client connections."""
=
break
=
, , , = # RGB to WS2812 format
=
, , , , = # RGB to WS2812 format
"""Start the socket server to listen for commands."""
=
# Listen on port 9999
=
,
=
# Turn off LEDs on
On the client side we can now send commands to control the LEDs. E.g. color 255 0 0
would light up all LEDs of the LED-strip red.
A minimal client example would look like:
# Connect to localhost
=
# Example usage
# Set to red
# Turn off