How to build a WS2812B LED strip with ESP8266 NodeMCU?

Haha, I think we’ve all realized that at some point or another - sometimes this feels more like a second job when I get home and less like a hobby :wink:

Don’t worry though - you will get the hang of it. My suggestion would be to start with the Demo configuration - there’s enough there to start with. Just comment out the stuff you don’t need yet, and change the sitemaps/rules and items to match your setup as time goes on. Also, definitely install the tail log viewer (from openhabian) so you can have the log open and scrolling live as you experiment (you’ll start seeing how the different pieces in OH interact, and also catch any errors that might be preventing your stuff from working).

Lastly, this forum is full of helpful people who won’t hesitate to answer any question (and won’t make you feel like a noob at that).

True! I just don’t know where to start. I got 2 Sonoffs relay thingy, 30 feet of RGB ws2812b led strip and many NodeMCU and 1 TPLink smart light switch on the way from amazon. I would love to be able to hook up all of that and make it work. I’m going to go through all the tutorials and beginners link I can find.

thank again

JD

Hey Bartus, firstly thank you for your guides, videos, designs and overall excellent hard work on this. I have ordered 5 of the recommended NodeMCU from Aliexpress, 10 of your riser from PCBWay and 1 BTF 5m 30 LED strip from Amazon (Will expand up to 10 of everything once I have one working).

It’ll take about a month for everything to get to me in the UK but I have 2 questions whilst I’m waiting if you could assist?

For the DC adapter connector, is it this type? MALAYAS 5.5x2.1mm DC Power Connector Male Female 12V that I’m looking for?

And secondly for your 3D printed enclosure, do you know if there is a UK based printing company I could use or will I have to have them printed in the US and shipped here? I’ve only bought 1 3D printed thing and that was from etsy, so not too up on how it works.

Thanks!

Daryl.

Thank you for the kind words!! I love sharing my work and seeing others using it! :slight_smile:

You’ve got the correct connector (DC plug) - don’t forget to tin the wires, as they seem to sit more snug in the terminal block rather than pull out.

As for getting the enclosure 3d printed, you have several options. The best/cheapest would be to find a local “maker” or “hacker” space, download the STL files from my Thingiverse page and have them print it for you. They should only charge you for the price of the plastic (for reference, a 1Kg roll costs ~$20 and could print at least 20 of these) and maybe for printer time, depending on the place. Option 2 would be to find someone local on 3dhubs.com or makexyz.com (but don’t let them charge you more than ~$10, this is a very easy print).

If you have problems getting it printed locally, let me know - I can do it for you for the cost of shipping and an adult beverage! :slight_smile:

Enjoy and let me know how you make out with this, looking forward to seeing some photos of pretty LEDs!

-B.K.

Fantastic thank you, I’ll take a look around and see what I can find. The waiting on the shipping will be the hardest part for now… if only we had teleporters haha.

@bartus Awesome work!

Can anyone here help with a problem I have with an ESP32 instead of an ESP8266? I am using bartuses code, and it works fine on a ESP8266 that I have (just some of the effects don’t seem to work). I have now tried porting the code to an ESP32, because I have a few of them lying around, and the devices crashes in a loop. I haven’t changed much on the code side, just the imports for Wifi and mDNS:

ESP8266:

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "FastLED.h"
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

ESP32:

#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include "FastLED.h"
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

The program of course compiles just fine.

I then see the following in the console over and over and over again:

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:956
load:0x40078000,len:0
load:0x40078000,len:13076
entry 0x40078a58

Connecting to pwlan
.......
WiFi connected
IP address: 
192.168.2.123
Ready
IP Address: 192.168.2.123
Attempting MQTT connection...connected
Setting LEDs:
r: 0, g: 0, b: 0
Guru Meditation Error: Core  0 panic'ed (Interrupt wdt timeout on CPU0)
Register dump:
PC      : 0x40085b7b  PS      : 0x00060034  A0      : 0x80086545  A1      : 0x3ffc0510  
A2      : 0x3ffc1128  A3      : 0x3ffc0588  A4      : 0x00000008  A5      : 0x3ffd4bc4  
A6      : 0x00000017  A7      : 0x00000000  A8      : 0x0000abab  A9      : 0x0000cdcd  
A10     : 0xb33fffff  A11     : 0x0000abab  A12     : 0x00000000  A13     : 0x00000001  
A14     : 0x00060023  A15     : 0x00000000  SAR     : 0x0000001e  EXCCAUSE: 0x00000005  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace: 0x40085b7b:0x3ffc0510 0x40086542:0x3ffc0530 0x400850c1:0x3ffc0550 0x40084482:0x3ffc0570 0x400843cd:0x3ffc05c0 0x400849ae:0x3ffc05e0 0x40081625:0x3ffc0610 0x400e310b:0x00000000

Does anyone have any idea?

It looks like you’re hitting the watchdog timer (wdt) and the ESP32 is resetting…I wonder if the libraries you’re using may have different behavior on the ESP32 (I do have one, but haven’t tried it out on this code myself, yet)…let me look at what mine does and get back to you.

Hey @bartus - Thanks for your write up on this topic, despite a few setbacks I have now my first LEDs set up on my shelving controlled through OH :laughing:

One thing I am unsure about though is the status of the items in OH - For example when you change the effect, it does not update on the UI, same with the intensity and the speed. Being very new to OH I am still trying to feel my way around, but is it possible to publish the status of the LEDS to the MQTT server each time a button is pressed so it updates? Is it a just a case of adding the subscribe topic to the item line?

For the FX for example, I tried this:

Number tvledright_fx "TV LEDs Right Fx" {mqtt=">[broker:home/leds/tvright/set:command:*:MAP(leds.map)],<[broker:home/leds/tvright/fx:state:default:*:MAP(leds.map)]" }

Where state topic is “home/leds/tvright” and set topic is “home/leds/tvright/set”, but it doesn’t seem to work, does the mapping function work with both pub and sub?

Any pointers would be appreciated!

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.