How to build a WS2812B LED strip with ESP8266 NodeMCU?

The map should work both ways, but the status string doesn’t contain the effect (it’s only sent out), You can pull out brightness/speed from the MQTT message, but need to update the item MQTT path, to access just that value. Try:

Number tvledright_brightness "TV LEDs Right Brightness" {mqtt="<[broker:home/leds/tvright:state:JSONPATH($.brightness)]" }

Since the MQTT status topic is a multi-line set of status items, and we need to pull out only the current brightness, the JSONPATH will pull that out.

That said, I never did this for my items - they seem to update whenever I make a change in my GUI/app. Are you making changes to the items via rules or something? Sending a “postUpdate” at the end of the rule should modify the status item…

Thanks for the reply, having a play around in the items/sitemap files and can get the number to appear as you described, but that also doesn’t update in the GUI, tried 3 separate devices and does the same on each of them - No changes to items or rules except from what is in your examples!

Ah, just the text shows? I think I know what the problem is there, you just need to format it into the item. So, for example:

Number tvledright_brightness "TV LEDs Right Brightness [%d]" {mqtt="<[broker:home/leds/tvright:state:JSONPATH($.brightness)]" }

That should format it as a number, and adjust the item state with it.

If that doesn’t work, I’d say the next thing to do would be to write a handler rule, based on the MQTT input string updating, and explicitly set the state for each item as you parse it out of the JSON string…

Did you get it working?

No, I didn’t. Easiest way was to just use an ESP8266 instead. There probably is a way to make it work with an ESP32, but I don’t really see the point for me. The 8266 is cheaper, and I don’t need the extra “horse power” and bluetooth for my LED controller.

Just found this - https://github.com/samguyer/FastLED

Looks like you need a special build of FastLed for esp32 support. Haven’t tried it myself yet…

I used ESP8266 to control sk6812 led strip,sk6812 led has the same procotol with ws2812b led.maybe it’s the same way to control ws2812b led strip.

Wondering if anyone would be able to point me in the right direction please? I have made the WS2812 LED strip with a NodeMCU. Everything loaded on fine. Connects to WIFi and MQTT server correctly. I added the items, sitemap, rules and transform additions to my openhab (latest version). The issue is that when trying to control the LED strip via openhab, all functions do not work. Observations are as follows.

-MQTT fx shows that the messages are being received.
-When connected to the Arduino Serial Monitor error reports “parseObject() failed” when power of fx is chosen.
-When manually publishing {“state”:“ON”,“color”:{“r”:255,“g”:255,“b”:255},“brightness”:150,“effect”:“solid”,“transistion”:0} I can use the colour picker to choose a colour and turn on/off.
-Latest libraries and boards loaded into Arduino IDE
-I feel that I have something missing as I am so close, it appears that openhab is not acting on the messages. I just am unsure what that is.

The questions I think I need help on are…

Is there any prerequisites I need installed in Openhab in relation to The JSON side of things?
Has anyone else had this issue?

Any help is greatly appreciated.

Can you post your rule code? I’m thinking you’re just creating a faulty Json object…you can also subscribe with MQTT.fx to the led strip’s set topic, so you can see exactly what openHAB is outputting to the Nodemcu.

Here is the rule code. it is copied from above.

rule "LED Strip Bedstrip JSON Color"
    when Item LEDStrip_Bedstrip_Color received update
    then LEDStrip_Bedstrip.sendCommand("{\"color\": {" + 
    		"\"r\": " + (LEDStrip_Bedstrip_Color.state as HSBType).red + "," +
    		"\"g\": " + (LEDStrip_Bedstrip_Color.state as HSBType).green + "," +
    		"\"b\": " + (LEDStrip_Bedstrip_Color.state as HSBType).blue + "}}") end

    rule "LED Strip Bedstrip JSON Speed"
    when Item LEDStrip_Bedstrip_Speed received update
    then LEDStrip_Bedstrip.sendCommand("{\"transition\": \"" + LEDStrip_Bedstrip_Speed.state + "\"}") end

    rule "LEDStrip_Bedstrip JSON Intensity"
    when Item LEDStrip_Bedstrip_Intensity received update
    then LEDStrip_Bedstrip.sendCommand("{\"brightness\":" + LEDStrip_Bedstrip_Intensity.state + "}") end

This is my items entries.

String LEDStrip_Bedstrip "Bedstrip String" {mqtt=">[mqtt:main/bedstrip/set:state:*:default]"}
    	Switch LEDStrip_Bedstrip_Power "Bedstrip Power"  [ "Switchable" ] {mqtt=">[mqtt:main/bedstrip/set:command:*:ON]"}
 	    Number LEDStrip_Bedstrip_FX "Bedstrip Fx" {mqtt=">[mqtt:main/bedstrip/set:command:*:MAP(LED.map)]"}
    	Color LEDStrip_Bedstrip_Color "Bedstrip Color"	<rgb> [ "Switchable" ]	
    	Number LEDStrip_Bedstrip_Speed "Bedstrip Speed [%.0f]"		
    	Number LEDStrip_Bedstrip_Intensity "Bedstrip Intensity [%d]"

and here is my Sitemap

	Group item=BedLight
    {
	Switch item=LEDStrip_Bedstrip_Power
	Selection item=LEDStrip_Bedstrip_FX mappings=[1="BPM",2="Noise",3="Fire",4="Rainbow",5="Twinkle",6="Glitter",7="Solid",8="Sinelon",9="Juggle",10="Confetti",11="Dots",12="Lightning",13="Candy Cane",14="Cyclon Rainbow",15="Ripple",16="Christmas Alternate",17="Police All",18="Police One",19="Random Stars",20="Sine Hue"] 
	Colorpicker item=LEDStrip_Bedstrip_Color
	Setpoint item=LEDStrip_Bedstrip_Speed minValue=5 maxValue=240 step=5
	Setpoint item=LEDStrip_Bedstrip_Intensity minValue=0 maxValue=255 step=16

The other thing that I have changed is the Transform name to LED.map. The contents are the same as above.

Ok, so, rereading your original post - you’re actually able to use the openHAB color picker to change colors/power status, correct? And, you’re seeing the MQTT messages that openHAB sends to the strip (whether it’s the power strip or color picker) in the “set” topic, right?

Can you post a couple of those messages? I’d like to see the difference between the mapped (Power/FX) and rule-built (Color/Speed/Intensity) JSON packets.

Finally, can you post the LED.map file, as well? The two controls you’re having problems with (Power/FX) have their JSOn strings built from the MAP file directly, so any formatting issues must be isolated to that file.

Correct. After manually sending this string
{“state”:“ON”,“color”:{“r”:255,“g”:255,“b”:255},“brightness”:150,“effect”:“solid”,“transistion”:0} through the MQTT fx, I can use the colour picker to choose a colour and turn on/off.

Again the LED.map file is a copy from above only with the name change.

LED.map:

1={"effect":"bpm"}
2={"effect":"noise"}
3={"effect":"fire"}
4={"effect":"rainbow"}
5={"effect":"twinkle"}
6={"effect":"glitter"}
7={"effect":"solid"}
8={"effect":"sinelon"}
9={"effect":"juggle"}
10={"effect":"confetti"}
11={"effect":"dots"}
12={"effect":"lightning"}
13={"effect":"candy cane"}
14={"effect":"cyclon rainbow"}
15={"effect":"ripple"}
16={"effect":"christmas alternate"}
17={"effect":"police all"}
18={"effect":"police one"}
19={"effect":"random stars"}
20={"effect":"sine hue"}
ON={"state":"ON"}
OFF={"state":"OFF"}

I wont get a chance to send through the messages from the MQTT until later.

Ahh, I see a problem now…your “Power” item has a hard-coded ON command in the definition, which isn’t a valid JSON string. Change it to:

Switch LEDStrip_Bedstrip_Power "Bedstrip Power"  [ "Switchable" ] {mqtt=">[mqtt:main/bedstrip/set:command:*:MAP(LED.map)]"}

(the two ON/OFF states are mapped to their proper “{state}” JSON format).

Ok, I will check when I get home. I may have changed that last night trying to diagnose the workings and non workings to see if could see any trends as to why. As I copied the code from Openhab this morning before I left for work. I may not have changed it back to the LED.map entry. If that is the case, it still won’t work. Fingers crossed :crossed_fingers: Thanks for your perseverance.

If that’s the case, then the next step in troubleshooting would be to take the exact JSON string from the MAP file and post it to your MQTT.fx (publish to the set topic)…if it doesn’t work there, we may need to start looking at your arduino code next.

Have access to the MQTT and system now.
I have updated the {mqtt=">[mqtt:main/bedstrip/set:command:*:MAP(LED.map)]"} path to be correct. I did fail to change it back last night.

The following is the message is published on the MQTT topic main/bedstrip/set when I switch the power switch to ON. MAP(LED.map)
When I tap the up arrow on Bedstrip color the MQTT topic main/bedstrip/set publishes
{“color”: {“r”: 21.202913280000000000,“g”: 100,“b”: 10.45785600}}

I think the next course of action is to reflash the NodeMCU and coy verbatim your Openhab files to see if that works, then I will try to modify to suit.

Update. I deleted my previous entries and reflashed the NodeMCU and copied item,sitemap,rules and map files in their entirety into my openhab.

Below is what was returned when I switched on the led strip power and selected JSON in MQTTfx.

*** PAYLOAD IS NOT VALID JSON DATA *** 

Unexpected character ('M' (code 77)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: java.io.StringReader@1511ecc0; line: 1, column: 2]

The frustrating part is that I am new to the Arduino world. But I’m learning fast.

Something is going on with your map transformation if you’re outputting a MQTT message with just “Map(Led.map)” in it as a string.

As a temporary workaround, you could write discrete rules for the fix and power items (matching the color rule).

Update. Finally stopped doing back flips and cart wheels.

Found the issue that dopey me didn’t have the map transformation installed. rebooted my openhab and what do you know… it works. thank you immensely @bartus for your perseverance. It is amazing what happens what the right words a poke in the right direction can do.

1 Like