WLED RGBs are great for being able to switch effects and colours easily, but it still means using a browser or an app, what if you want to automate which effect runs and when using code?
It turns out that WLED has a fair few ways to automate, with APIs that allow you to control your RGB strips and matrix from your computer, smart home, or services like IFTTT.
If you haven’t already, build your WLED RGB LED controller and hook up your strips!
JSON API and Serial
The most capable and complex API is via JSON, which can be submitted over HTTP using POST, or as simple text strings over serial.
You submit instructions in the form of the following:
{"seg":{"fx":"r","pal":"r"}}
There are several important parameters, which I will list, but it is well worth looking at the complete instructions to get an idea of what is possible too:
Seg= the Segment you are enquiring or setting, though theIDis optional, and therefore the instruction will apply to all.fx= Effect to apply, either a number orrfor random.pal= Palette to apply, either a number orrfor random.ps= Select a Preset, either a number orrfor random.
You can get your current configuration using {"on":"true","v":true} (this actually switches your LEDs on, but in doing so we request our config too as the response).
For example, forgot your IP address? Get the IP address of your board via serial using the above command, then look for the IP in the response.
WLED Segments
Using JSON you are also able to modify the Segments. This is usually used to set individual or ranges of pixel colours …
{"seg":{"i":["FF0000","00FF00","0000FF"]}}
Red, Green, Blue
{"seg":{"i":[0,"FF0000",2,"00FF00",4,"0000FF"]}}
Red, blank, Green, Blank, Blue
Set a range
{"seg":{"i":[0,8,"FF0000",10,18,"0000FF"]}}
Send multiple api calls of up to 256 colors at once
{"seg": {"i":[0,"CC0000","00CC00","0000CC","CC0000"...]}}
{"seg": {"i":[256,"CC0000","00CC00","0000CC","CC0000"...]}}
{"seg": {"i":[512,"CC0000","00CC00","0000CC","CC0000"...]}}
(Do not make several calls in parallel)
If you are trying to set a lot of LEDs and it fails, split it into multiple sequential requests and use Hex strings rather than decimal.
Setting segments is also useful to change the name of a segment, for example to set segment zero:
{"seg":[{"id":0,"n":"New Segment Name"}]}
This will come in handy later …
Playlists
Much like your favourite tunes, you can also have playlists of effects.
Example playlist API call:
{ "playlist": { "ps": [26, 20, 18, 20], "dur": [30, 20, 10, 50], "transition": 0, "repeat": 10, "end": 21 } }
The WLED Playlist object:
| JSON key | Description |
|---|---|
| ps | Array of preset IDs |
| dur | Time delay in 1/10s (defaults to 10 seconds) |
| transition | Transition time |
| repeat | Times to repeat the entire playlist. Defaults to infinite. |
| end | Preset to apply after the playlist finished. Defaults to the last preset of the playlist. |
Plain HTTP URL API
If you do not have the capacity to do HTTP POST, or the memory to build large strings, then the plain URL option is also available. This is handy for doing “web hook” style interactions, for example you could set your LEDs to red when you have a Slack notification waiting.
The main parameters are:
A= BrightnessFX= EffectFP= PalettePL= Preset
WSLED Automation with Neo6502
Having regular HTTP URL API gives us the opportunity to control our LED strip using the Neo6502 or C64!
C64 BASIC with WiFi
You will need your C64 or emulator hooked up to a user port Wifi adapter, so check out my updated C64 WiFi Tutorial first!
Note the special up per and lower case of the URLs, this is due to the WLED URLs being case sensitive but also C64 being weird when it comes to character case. When the code runs the character set is chosen, reversing the appearance and meaning of the upper and lower characters.
Swap the IP address for the address of your WLED board too.
When run, this program will allow you to select between preset 1, 2 or 3.
1 POKE 53272,23
2 PRINT CHR$(5): PRINT CHR$(147): PRINT CHR$(19);: REM clear screen, white text
3 OPEN2,2,4,CHR$(8)+CHR$(0):REM OPEN THE SERIAL FROM USER PORT
4 PRINT "STARTING"
5 PRINT#2,"AT":GOSUB 29
6 FOR I = 1 TO 5
7 GOSUB 37
8 NEXT
9 PRINT "CONNECTING TO WIFI"
10 PRINT#2,"ATC1":GOSUB 29
11 FOR I = 1 TO 5
12 GOSUB 37
13 NEXT
14 PRINT "GETTING CONNECTION INFO"
15 PRINT#2,"ATI":GOSUB 29
16 FOR I = 1 TO 10
17 GOSUB 37
18 NEXT
19 GOSUB 40
20 U$="": I$="": GET I$
21 IF I$="1" THEN PRINT "1 SELECTED": U$="ATGETHTTP://192.168.0.100/win&A=64&PL=1": GOSUB 29
22 IF I$="2" THEN PRINT "2 SELECTED": U$="ATGETHTTP://192.168.0.100/win&A=64&PL=2": GOSUB 29
23 IF I$="3" THEN PRINT "3 SELECTED": U$="ATGETHTTP://192.168.0.100/win&A=64&PL=3": GOSUB 29
24 IF I$ = "" THEN GOTO 20: REM LOOP BACK TO START OF THE MAIN LOOP AGAIN
25 FOR I = 1 TO 5
26 GOSUB 37
27 NEXT
28 CLOSE 2: END
29 PRINT#2,U$ + CHR$(10)+CHR$(13);:U$=""
30 PRINT ""
31 IF PEEK(673) AND 1 THEN PRINT ".";: GOTO 31: REM STILL TRANSMITTING
32 FOR I = 1 TO 5
33 GOSUB 37
34 NEXT
35 RETURN
36 REM GET THE RESPONSE UNTIL NOTHING WAITING
37 GET#2,S$:IF S$<>"" THEN PRINT S$;:GOTO 37
38 FOR W=1 TO 2000: NEXT
39 RETURN
40 PRINT CHR$(147)
41 PRINT CHR$(5): PRINT CHR$(147): PRINT CHR$(19);: REM clear screen, white text
42 PRINT "SELECT 1, 2 OR 3"
43 RETURN
Neo6502 BASIC:
Check out the Neo6502 WiFi tutorial then try this. It will change the effects one after another with a small delay between them:
uconfig 115200
buffer=alloc(500) :buflen=0
protocol$=""
host$=""
path$=""
call wget( "http://192.168.0.100/win&A=64&FX=4" )
print ""
wait 500
call serialsend("AT+CIPCLOSE")
call wget( "http://192.168.0.100/win&A=32&FX=102&FP=22" )
print ""
wait 500
call serialsend("AT+CIPCLOSE")
call wget( "http://192.168.0.100/win&A=32&FX=102&FP=22" )
print ""
wait 500
call serialsend("AT+CIPCLOSE")
call wget( "http://192.168.0.100/win&A=64&PL=1" )
print ""
wait 500
call serialsend("AT+CIPCLOSE")
call wget( "http://192.168.0.100/win&A=64&PL=2" )
print ""
wait 500
call serialsend("AT+CIPCLOSE")
call wget( "http://192.168.0.100/win&A=64&PL=3" )
print ""
wait 500
call serialsend("AT+CIPCLOSE")
print "DONE"
print ""
end
proc serialread( )
while uhasdata( ) = 0
wait 1
wend
while uhasdata( ) = -1
ureceive buffer+buflen,1
buflen=buflen+1
wend
for c = 0 to buflen
ch = peek(buffer+c)
print chr$(ch);
next
buflen = 0
endproc
proc serialsend(st$ )
usend st$ +chr$(13) +chr$(10)
wait 50
endproc
proc spliturl(url$)
protocol$=""
host$=""
path$=""
protocolend = instr(url$,"/")+1
protocol$=mid$(url$,1,protocolend)
url$=mid$(url$,protocolend+1)
pathstart=instr(url$,"/")
host$=mid$(url$,1,pathstart-1)
path$=mid$(url$,pathstart)
endproc
proc wget(url$)
call spliturl(url$)
print "protocol: " + protocol$
print "host: " + host$
print "path: " + path$
call serialsend( "AT+CIPSTART="+chr$(34)+"TCP"+chr$(34)+","+chr$(34)+host$+chr$(34)+",80" )
call serialread( )
s$="GET " + path$ + " HTTP/1.1" + chr$(13) + chr$(10) + "Host: " + host$
call serialsend( "AT+CIPSEND=" + str$(len(s$)+4))
call serialread( )
call serialsend( s$ +chr$(13) +chr$(10)+chr$(13) +chr$(10))
call serialread( )
endproc
Next Up
Now we are equipped with working WLED controller boards and the required API programming knowledge, it is time to put it all together in the final part of this mini-series!