The awesome WLED project not only comes with tons of built-in effects and colour compositions, it can also be controlled through a whole bunch of network protocols. One of them is UDP realtime control. It lets you set individual LEDs directly from outside, for example from a PowerShell script.
The protocol is binary, so you have to build the data bytes yourself. That took a lot of (ugly) type casting to [byte[]]. Maybe there’s a more elegant way, but the whole thing is only a proof of concept anyway: it’s meant to show that and how it works, nothing more.
Packet layout
<1> protocol identifier
<5> back to normal WLED operation after five seconds
<0> LED index, 0 is the first LED
<red>
<green>
<blue>
<1> LED index, 1 is the second LED
<red>
<green>
<blue>
<2> LED index, 2 is the third LED
<red>
<green>
<blue>
...
No terminating sequence is needed, and the LED indexes don’t have to be consecutive.
PowerShell example
The script turns the first 10 LEDs red, waits one second, turns the next 10 green, waits again and then turns another 10 blue. After five seconds everything is back to normal. You need to adjust the IP address of your WLED controller; you’ll find it in the WLED app or in your router.
# IP of your WLED controller
$WledIP = '192.168.178.96'
# UDP port (21324 is the default)
$WledPort = 21324
# create socket
$client = new-object net.sockets.udpclient(0)
# first 10 LEDs (index 0 through 9) in some red-ish colour
$data = [byte[]]( [byte[]](1,5) + [byte[]] (0..9 | foreach {[byte[]]($_,200,30,30)}) )
[void] $client.send($data, $data.length, $WledIP, $WledPort)
sleep -seconds 1
# next 10 LEDs (index 10 through 19) in some sort of green
$data = [byte[]]( [byte[]](1,5) + [byte[]] (10..19 | foreach {[byte[]]($_,30,200,30)}) )
[void] $client.send($data, $data.length, $WledIP, $WledPort)
sleep -seconds 1
# another 10 LEDs (index 20 through 29) in blue
$data = [byte[]]( [byte[]](1,5) + [byte[]] (20..29 | foreach {[byte[]]($_,30,30,200)}) )
[void] $client.send($data, $data.length, $WledIP, $WledPort)
Five seconds after the last packet, the controller goes back to its normal programme.
I tested the script on Windows with PowerShell 5 and PowerShell 7.
Running it
If you’ve never run a PowerShell script before, there are two ways.
Paste it: open PowerShell (Start menu, type “PowerShell”), copy the code and paste it into the window with a right-click. The lines run straight away. That’s the quickest way to try it out.
As a file: save the code in a text file with the extension .ps1, for example wled.ps1. By default, Windows PowerShell 5 refuses to run script files. For a single call you can get around that without changing the setting permanently:
powershell -ExecutionPolicy Bypass -File .\wled.ps1
With PowerShell 7 the command is pwsh instead of powershell:
pwsh -File .\wled.ps1
Have fun!
This post originally started as a gist on GitHub. More on WLED: a glowing moon with WLED and WLED in an IKEA Malm bed.