The Red, Green, Blue LEDs of a Sonoff B1 Lamp can be controlled by sending individual R, G, B vales, OR a single 24bit combined RGB value.
At the bottom of the Annex Toolkit UDP Console tab are 3 sliders which broadcasts a 24bit combined RGB value to the subnet.
These can be used as a UDP RGB lamp controller for the simple scripts below.
Don't let the relative simplicity fool you... the identical script could be run on multiple devices to provide impressive synchronized lighting control.
This first script is for use on a Sonoff B1 Lamp:
Basic:
'Simple UDP Sonoff B1 Lamp - developed on Annex 1.41 beta2 - Electroguard
sonoffb1.init
sonoffb1.rgb 0 'RGB leds OFF
udp.begin(5001) 'change port number if wished, but must match with Toolkit UDP Console port setting
onudp udpRX
wait
udpRX:
sonoffb1.rgb val(udp.read$)
return
'----------- End ------------
This next script is for any device with neopixels connected to gpio2
Basic:
'Simple UDP Neopixels - developed on Annex 1.41 beta2 - Electroguard
neo.setup 12 'number of neopixels, example is for a 12 pixel ring, (Data In connected to gpio2)
neo.strip 0,11,0 'neopixels off
udp.begin(5001) 'change port number if wished, but must match with Toolkit UDP Console port setting
onudp udpRX
wait
udpRX:
neo.strip 0,11, val(udp.read$)
return
'----------- End ------------
This last script uses individual R,G,B signals, and has it's own browser R,G,B sliders for local control.
The sliders also broadcast their value changes to any other listening UDP devices on the same subnet.
So multiple devices can be controlled by each other and/or the Toolkit UDP Console.
Basic:
' UDP R,G,B Slider for Sonoff B1 / neopixels / RGB LEDs - developed on Annex 1.41 beta2 - Electroguard
title$ = "R,G,B UDP Controller"
localIP$ = WORD$(IP$,1)
netIP$ = WORD$(localIP$,1,".") + "." + WORD$(localIP$,2,".") + "." + WORD$(localIP$,3,".") + "."
udpport = 5001 'change to suit your own preference, but don't forget to do the same for all nodes
udp.begin(udpport)
onudp udpRX
r=0:g=0:b=0 'define original R,G,B values
pixels = 12 'declare how many pixels to be addressed (12 for this example)
neo.setup pixels 'initalise for declared number of pixels
neo.strip 0, pixels-1 ,r,g,b 'turn all pixels off
sonoffb1.init
sonoffb1.rgb r,g,b 'turn all leds off
pwm(12)=r 'turn RGN LED gpio's off
pwm(15)=g
pwm(13)=b
onhtmlchange changeit
gosub screen 'send screen output to browser
wait
udpRX:
x=val(udp.read$) 'read incoming 24bit combined RGB value b=(x and &hff) 'extract just the B value g=(x >> 8) and &hff 'extract just the G value r=(x >> 16) and &hff 'extract just the R value gosub changeit return changeit: udp.write netIP$+"255", udpport, str$(r)+","+str$(g)+","+str$(b) pwm(12)=r*4 'pwm values are 0 to 1023, but R,G,B values are only 0 to 255, so need multiplying by 4 pwm(15)=g*4 pwm(13)=b*4 neo.strip 0,11,r,g,b sonoffb1.rgb r,g,b pause 20 return screen: cls a$="" a$=a$ + |<br><br>| a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 90%'>| a$=a$ + |<font color="teal">|+title$+|</font><br><br><br>| a$=a$ + |</div>| a$=a$ + |<div style='display: table; margin-right: auto; margin-left: auto;font-size: 120%'>| a$=a$ + replace$(slider$(r, 0, 255,"R"), "onchange", "oninput") + |<font color="red">|+" Red"+|</font><br><br>| a$=a$ + replace$(slider$(g, 0, 255,"G"), "onchange", "oninput") + |<font color="green">|+" Green"+|</font><br><br>| a$=a$ + replace$(slider$(b, 0, 255,"B"), "onchange", "oninput") + |<font color="blue">|+" Blue"+|</font><br><br>| a$=a$ + cssid$("R","height:10px; box-shadow: inset 0 15px red;") a$=a$ + cssid$("G","height:10px; box-shadow: inset 0 15px green;") a$=a$ + cssid$("B","height:10px; box-shadow: inset 0 15px blue;") a$=a$ + |</div>| html a$ a$="" return end '-----------end------------
Note:
Pay attention to the s w e e t active slider...
replace$(slider$(r, 0, 255,"R"), "onchange", "oninput")
which branches on immediate change to the branch declared with onhtmlchange ie:changeit in the above example (thanks CiccioCB)