NodeMCU MQTT LED Strip Controller Build & Config How-To Videos

You’re welcome!

I will post my version here

1 Like


everything file with file. This project made for VS CODE IDE+PLATFORMIO.
I suggest you to use this software - http://mqtt-explorer.com/
This very helpfull for debug mqtt state.

1 Like

Hi,
My configuration is same as above by Mr. Hobby,When I press reset button on nodemcu.first led blinks for a sec but LED strip not lights up.I am able to see the coomands also on openhab logs.

2019-12-08 21:38:48.725 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1_Color’ received command 298.588226,100.000000,100.000000

2019-12-08 21:38:48.741 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1_Color changed from 230.588242,100.000000,100.000000 to 298.588226,100.000000,100.000000

2019-12-08 21:38:48.902 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1’ received command {“color”: {“r”: 248,“g”: 0,“b”: 255}}

2019-12-08 21:38:48.910 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1 changed from {“color”: {“r”: 0,“g”: 39,“b”: 255}} to {“color”: {“r”: 248,“g”: 0,“b”: 255}}

you have any idea what is wrong.

Rishi

Hi,

Everything I did as per your post on the community,but I am having issue with LED strip lights not Up.When I press reset button on nodemcu first LED blinks for a sec but nothing changes when I send commands to change color or change effects.

Please help me to resolve.

Arduino console logs

22:51:58.014 -> Message arrived [home/FF_Kitchen_Cabinet_1/set] {“color”: {“r”: 100.000000,“g”: 0.000000000000,“b”: 17.2548933300000000000000}}
22:51:58.014 -> 00000000
22:51:58.014 -> Setting LEDs:
22:51:58.014 -> r: 0, g: 0, b: 0
22:51:59.163 -> Message arrived [home/FF_Kitchen_Cabinet_1/set] {“color”: {“r”: 10.5882266700000000000000,“g”: 0.000000000000,“b”: 100.000000}}
22:51:59.223 -> 0.000000
22:51:59.223 -> Setting LEDs:
22:51:59.223 -> r: 0, g: 0, b: 0
22:52:01.583 -> Message arrived [home/FF_Kitchen_Cabinet_1/set] {“color”: {“r”: 0.000000000000,“g”: 100.000000,“b”: 18.4313716700000000000000}}
22:52:01.583 -> 00000000
22:52:01.583 -> Setting LEDs:
22:52:01.583 -> r: 0, g: 0, b:

openhab logs

2019-12-08 22:51:59.819 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1’ received command {“color”: {“r”: 100.000000,“g”: 0.000000000000,“b”: 17.2548933300000000000000}}

2019-12-08 22:51:59.827 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1 changed from {“transition”: “77.0”} to {“color”: {“r”: 100.000000,“g”: 0.000000000000,“b”: 17.2548933300000000000000}}

2019-12-08 22:52:00.907 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1_Color’ received command 246.352936,100.000000,100.000000

2019-12-08 22:52:00.920 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1_Color changed from 349.647064,100.000000,100.000000 to 246.352936,100.000000,100.000000

2019-12-08 22:52:00.981 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1’ received command {“color”: {“r”: 10.5882266700000000000000,“g”: 0.000000000000,“b”: 100.000000}}

2019-12-08 22:52:00.990 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1 changed from {“color”: {“r”: 100.000000,“g”: 0.000000000000,“b”: 17.2548933300000000000000}} to {“color”: {“r”: 10.5882266700000000000000,“g”: 0.000000000000,“b”: 100.000000}}

2019-12-08 22:52:03.317 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1_Color’ received command 131.058823,100.000000,100.000000

2019-12-08 22:52:03.328 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1_Color changed from 246.352936,100.000000,100.000000 to 131.058823,100.000000,100.000000

2019-12-08 22:52:03.383 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1’ received command {“color”: {“r”: 0.000000000000,“g”: 100.000000,“b”: 18.4313716700000000000000}}

2019-12-08 22:52:03.392 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1 changed from {“color”: {“r”: 10.5882266700000000000000,“g”: 0.000000000000,“b”: 100.000000}} to {“color”: {“r”: 0.000000000000,“g”: 100.000000,“b”: 18.4313716700000000000000}}

rule file

rule “LED Strip Kitchen Cabinet 1 JSON Color”
when Item LEDStrip_FF_Kitchen_Cabinet_1_Color received update
then LEDStrip_FF_Kitchen_Cabinet_1.sendCommand("{“color”: {" +
"“r”: " + (LEDStrip_FF_Kitchen_Cabinet_1_Color.state as HSBType).red + “,” +
"“g”: " + (LEDStrip_FF_Kitchen_Cabinet_1_Color.state as HSBType).green + “,” +
"“b”: " + (LEDStrip_FF_Kitchen_Cabinet_1_Color.state as HSBType).blue + “}}”) end

rule "LED Strip Kitchen Cabinet 1 JSON Speed"
when Item LEDStrip_FF_Kitchen_Cabinet_1_Speed received update
then LEDStrip_FF_Kitchen_Cabinet_1.sendCommand("{\"transition\": \"" + LEDStrip_FF_Kitchen_Cabinet_1_Speed.state + "\"}") end

rule "LED Strip Kitchen Cabinet 1 JSON Intensity"
when Item LEDStrip_FF_Kitchen_Cabinet_1_Intensity received update
then LEDStrip_FF_Kitchen_Cabinet_1.sendCommand("{\"brightness\":" + LEDStrip_FF_Kitchen_Cabinet_1_Intensity.state + "}") end

@saini.rishi - thank you for posting this - it was actually broken on my LEDs too…It must’ve been a recent change to how OH stores/handles the rgb values, but apparently, the LED controller code didn’t “like” large double float values being sent to it. So, the easy fix for this is to send only the integer value in the rules, and the updated rule should be:

rule "LED Strip Group 1 (Outdoor) JSON Color Single Strip"
when Member of gLEDStrip_Outdoor_Color received update
then 
 	val source = triggeringItem.name.split("_Color").get(0)
	logInfo("LED", "Source " + source)
	val item = gLEDStrip_Outdoor.members.findFirst[ l | l.name == source ] as StringItem
	logInfo("LED", "Item " + item.name)
	item.sendCommand("{\"color\": {" + 
		"\"r\": " + (triggeringItem.state as HSBType).red.intValue.toString + "," +
		"\"g\": " + (triggeringItem.state as HSBType).green.intValue.toString + "," +
		"\"b\": " + (triggeringItem.state as HSBType).blue.intValue.toString + "}}") end

(adding the intValue.toString to each color string.

I’ve tested this and it works on my strips. I’ll update the first post, so others don’t have similar issues.

Everything works up til the OH 2.5 integration.
I can change all settings by sending messages using a MQTT client.

Now i try to get it working in OH
I created ledstrips.items:

Group gLEDStrips "LED Strips"
Group gLEDStrips_Power "LED Strips Power"
Group gLEDStrips_FX "LED Strips FX"
Group gLEDStrips_Color "LED Strips Color"
Group gLEDStrips_Speed "LED Strips Speed"
Group gLEDStrips_Intensity "LED Strips Intensity"

String  strip02             "LED strip 02 String"           (gLEDStrips)    {mqtt=">[broker:home/strip02/set:state:*:default]"}
Switch  strip02_Power       "LED strip 02 Power"            (gLEDStrips_Power)    {mqtt=">[broker:home/strip02/set:command:*:MAP(LEDStripEffectJSON.map)]"}
Number  strip02_FX          "LED strip 02 Fx"               (gLEDStrips_FX)    {mqtt=">[broker:home/strip02/set:command:*:MAP(LEDStripEffectJSON.map)]"}
Color   strip02_Color       "LED strip 02 Color"	<rgb>   (gLEDStrips_Color)	
Number  strip02_Speed       "LED strip 02 Speed [%.0f]"     (gLEDStrips_Speed)
Number  strip02_Intensity   "LED strip 02 Intensity [%d]"   (gLEDStrips_Intensity)

But i am a bit confused with this channel notation … in my other items files for mqtt items i have something like this :

Switch   TasmotaRGB01TasmotaRGB01Power   "Tasmota RGB01 power"   (gLights)  {channel="mqtt:topic:e04dbf9d:TasmotaRGB01_Power"}
Color    TasmotaRGB01TasmotaRGB01HSB     "Tasmota RGB01 hsb"    {channel="mqtt:topic:e04dbf9d:TasmotaRGB01_HSB"}

I also wonder why there are no ‘things’ created before the items ?

Anyway, using the color picker from my sitemap i see this in my eventlog

2020-01-02 21:19:16.450 [ome.event.ItemCommandEvent] - Item 'strip02_Color' received command 115,78,100
2020-01-02 21:19:16.458 [vent.ItemStateChangedEvent] - strip02_Color changed from 7,82,100 to 115,78,100
2020-01-02 21:19:16.476 [ome.event.ItemCommandEvent] - Item 'strip02' received command {"color": {"r": 28,"g": 100,"b": 22}}
2020-01-02 21:19:16.483 [vent.ItemStateChangedEvent] - strip02 changed from {"color": {"r": 100,"g": 27,"b": 18}} to {"color": {"r": 28,"g": 100,"b": 22}}

But nothing arrives on the MQTT server.
The {"color": {"r": 28,"g": 100,"b": 22}} message is correctly formatted, as if i sent it using my mqtt client it works.

What am i doing wrong here ?

In OH2.5 (and mqtt 2.x) there are two ways to control an mqtt based item. You can define a channel between the thing and the item, this is at the end of the item definition. In this case, if you send a command to the item, the channel gets the command and send to the thing device as is. It is good for example a Power: ON command. The other way, if you left your item as a “dummy” item, just like a placeholder, and define a rule, when the item changes, the rule fires and send information well formatted to the mqtt channel directly. Do you use Tasmota on your controller? Just because this topic use a custom code on the controller, where a bit different everything. Is tasmota use RGB formatted color command? As far as I know, the color picker item on sitemap generates HSB type color code, what is differents from RGB one. In this case, you can use the rule based solution with translating between the two kind of color representations. If you read back on this topic, you’ll see the rule examples, how we handle this conversion.

Thanks for the reply zskadar !

I am using the setup from this topic. the NodeMCU as from Bartus’ tutorials. With the updated (slightly altered) OH config files in the first post.

The tasmota items are just bulbs, but as they use the other way of connecting to mqtt (which works in my case) i mentioned them as reference.

So i do have these rules also:

pi@raspberrypi:/etc/openhab2/rules $ cat ledstrips.rules
rule "LED Strip group JSON Color Single Strip"
when Member of gLEDStrips_Color received update
then
        val source = triggeringItem.name.split("_Color").get(0)
        logInfo("LED", "Source " + source)
        val item = gLEDStrips.members.findFirst[ l | l.name == source ] as StringItem
        logInfo("LED", "Item " + item.name)
        item.sendCommand("{\"color\": {" +
                "\"r\": " + (triggeringItem.state as HSBType).red.intValue.toString + "," +
                "\"g\": " + (triggeringItem.state as HSBType).green.intValue.toString + "," +
                "\"b\": " + (triggeringItem.state as HSBType).blue.intValue.toString + "}}")
end

rule "LED Strip group JSON Speed Single Strip"
when Member of gLEDStrips_Speed received update
then
        val source = triggeringItem.name.split("_Speed").get(0)
        val value = triggeringItem.state as Number
        logInfo("LED", "Source " + source)
        val item = gLEDStrips.members.findFirst[ l | l.name == source ] as StringItem
        item.sendCommand("{\"transition\": \"" + (value / 100 * 240).intValue.toString + "\"}")
end

rule "LED Strip group JSON Intensity Single Strip"
when Member of gLEDStrips_Intensity received update
then
        val source = triggeringItem.name.split("_Intensity").get(0)
        val value = triggeringItem.state as Number
        logInfo("LED", "Source " + source)
        val item = gLEDStrips.members.findFirst[ l | l.name == source ] as StringItem
        item.sendCommand("{\"brightness\":" + (value / 100 * 255).intValue.toString + "}")

I think the rules are ok, as the logs seem to show the right info lines. But the messages do not arrive on the mqtt, and thus are not picked up by the NodeMCU. (sending these messages manually to the mqtt, they do get picked up by the nodemcu and the ledstrips change)

what still confuses me is this:
{mqtt=">[broker:home/strip02/set:state:*:default]"}
How does this know where my mqtt server is ?

What version of MQTT do you use? I think your channel definition is v1.x. Could you please check my config here:

There is a separate broker.things file with the mqtt server definition, after that there is the .things file, what refers the broker thing. The channel refers to the channel definition in the .things file instead of the exact mqtt topic name. This is what have to use in .items file. In mqtt v1.x we have used the exact topic. That’s changed in v2.x

in my addons.cfg i have ‘mqtt’ under binding … so that should load the v2 in OH2.5

Under things i have no config files, but i did create my broker using PaperUI

perhaps this helps for insight:

openhab> smarthome:things list
...
mqtt:topic:1725b961 (Type=Thing, Status=ONLINE, Label=Sensor01, Bridge=mqtt:systemBroker:embedded-mqtt-broker)
mqtt:topic:e04dbf9d (Type=Thing, Status=ONLINE, Label=TasmotaRGB01, Bridge=mqtt:systemBroker:embedded-mqtt-broker)
mqtt:topic:6c4533a3 (Type=Thing, Status=ONLINE, Label=TasmotaRGBW02, Bridge=mqtt:systemBroker:embedded-mqtt-broker)
mqtt:topic:64c2b27f (Type=Thing, Status=ONLINE, Label=plug1, Bridge=mqtt:systemBroker:embedded-mqtt-broker)
mqtt:systemBroker:embedded-mqtt-broker (Type=Bridge, Status=ONLINE, Label=MQTT Broker, Bridge=null)
mqtt:topic:76f26690 (Type=Thing, Status=ONLINE, Label=TasmotaRGBW01, Bridge=mqtt:systemBroker:embedded-mqtt-broker)

Ok, I’m a bit confused, it seems you use the system broker, I’ve got also, but I not use that currently. My things bound to the mqtt broker directly.

    openhab> things list | grep -i broker
...
    mqtt:broker:sentinel (Type=Bridge, Status=ONLINE, Label=MQTT Broker: Mosquitto server, Bridge=null)
    mqtt:topic:sentinel:t-hLED (Type=Thing, Status=ONLINE, Label=Hifi LED, Bridge=mqtt:broker:sentinel)
    mqtt:systemBroker:sentinel-system-broker (Type=Bridge, Status=ONLINE, Label=MQTT Broker, Bridge=null)
...

On Paper UI, the description of the system broker is the following:
System MQTT Broker

A system configured and therefore read-only broker connection. Properties are reflecting the configuration and internal connection status.

What does read-only means? May you cannot send commands via this broker connection? Can you replace system broker to the embedded one in your things definition? I use external (mosquitto) mqtt server instead of the embedded one.

Hmm, well the Embedded system MQTT Broker is not so read-only as in the description. The tasmota topics work perfectly and connecting to it with a MQTT client works as well.

I tried

{mqtt=">[systemBroker:home/strip02/set:state:*:default]"}

{mqtt=">[systemBroker:embedded-mqtt-broker:home/strip02/set:state:*:default]"}

and

{mqtt=">[embedded-mqtt-broker:home/strip02/set:state:*:default]"}

but that doesn’t fix it. Nothing arrives on the mqtt

Did you try something like this?
{channel="mqtt:topic:systemBroker:embedded-mqtt-broker:76f26690"}

where would the home/strip02/set:state:*:default fit in then ?

Here is my thing definition and the channel details on Paper UI:

There is the mqtt command topic, what points to the exact topic itself. Under the item, the channel definition concatenates “mqtt:topic” + broker connection + thing + channel.
In my case it looks like this: {channel=“mqtt:topic:sentinel:t-hLED:hLED”}

I think you have to follow this syntax.

Ok i will try that tonight, that’s how the Tasmotas are working. (as well as the BRUH 15$ multisensor)

But that’s the other approach than the one with dummy items and rules by Bartus. Here we have the generic MQTT Thing.

So i ll try your setup
https://community.openhab.org/t/nodemcu-mqtt-led-strip-controller-build-config-how-to-videos/39902/93

I think you just try to compile your channel definition under the item properly. To be honest I’ve tried this channel based approach but I’ve failed. The rule based solution works fine.

Hi Bartus,

I am setting up my first LEDStrip just as you showed it in the video. But it does not work! I am using the new MQTT 2.x version. I think my problem is in the .items file where you define the MQTT connections for the items {mqtt=">[broker:Home/LivingRoom/Desklamp/set:state::default]"}*. I tried to add the channels on the PaperUI but how can I send your JSON commands? Could you please try it with the MQTT 2.x version? Also I can not publish a message with MQTT.fx. I am connected to the broker, I can see :Home/LivingRoom/Desklamp topic when the NodeMCU comes up. I tried it like this: Home/LivingRoom/DeskLamp/set {“state”:“ON”} on the Publish field, but the NodeMCU does not seems to get the message.

Please help me Bartus, you are my last hope!

I have one question. Is this example support state updates when using groups ? Im affraid that not. NodeMCU sends message to state topic in format which is not understadable for openhab. Here is what Im getting form NodeMCU un state topic:

{"state":"OFF","color":{"r":255,"g":255,"b":255},"brightness":64,"effect":"solid","transition":0}