RGB MQTT Help to create items and sitemap

Hello, I’m new in programing and I have started if one tutoreal, but the guy don’t post the continue to control the LED RGB.

So I’m asking for helping to continue, beacause I allready buy and made the board… Sorry about my english =[

I have follow everthing from here:

https://www.mksmarthouse.com/led-control

This is the program:

#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

/* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */

//Only edit the settings in this section

/* WIFI Settings /
// Name of wifi network
const char ssid = “****”;

// Password to wifi network
const char* password = “*****”;

/* Web Updater Settings /
// Host Name of Device
const char host = “MK-LEDStripControl1”;

// Path to access firmware update page (Not Neccessary to change)
const char* update_path = “/firmware”;

// Username to access the web update page
const char* update_username = “admin”;

// Password to access the web update page
const char* update_password = “Admin”;

/* MQTT Settings /
// Topic which listens for commands
char subscribeTopic = “MK-SmartHouse/lights/MK-LEDStripControl1”;

//MQTT Server IP Address
const char* server = “192.168.1.102”;

//Unique device ID
const char* mqttDeviceID = “MK-SmartHouseDevice7”;

/* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */

// Declare Esp8266 Pins Used For LED Strip
const int greenLed = 12;
const int redLed = 14;
const int blueLed = 13;

// initialize led values
String greenLedVal = “0”;
String redLedVal = “0”;
String blueLedVal = “0”;

// initialize led values
String greenLedValLast = “0”;
String redLedValLast = “0”;
String blueLedValLast = “0”;

ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect();

void setup()
{

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);
client.begin(server, net);
client.onMessage(messageReceived);

connect();

MDNS.begin(host);

httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();

MDNS.addService(“http”, “tcp”, 80);
}

void connect()
{
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}

while (!client.connect(mqttDeviceID))
{
delay(1000);
}

client.subscribe(subscribeTopic);
}

void loop()
{
client.loop();
delay(10);

if(!client.connected())
{
connect();
}

httpServer.handleClient();

}

void messageReceived(String &topic, String &payload)
{
String stringOne = payload;

Serial.println(stringOne);

// find first ; in the string
int firstClosingBracket = stringOne.indexOf(’;’)+1;

// find second ; in the string
int secondOpeningBracket = firstClosingBracket + 1;
int secondClosingBracket = stringOne.indexOf(’;’, secondOpeningBracket);

// find the third ; in the string
int thirdOpeningBracket = secondClosingBracket + 1;
int thirdClosingBracket = stringOne.indexOf(’;’, thirdOpeningBracket);

// using the locations of ; find values
greenLedVal = stringOne.substring(0 , (firstClosingBracket - 1));
redLedVal = stringOne.substring(firstClosingBracket , secondClosingBracket);
blueLedVal = stringOne.substring((secondClosingBracket +1) , thirdClosingBracket);

if ((blueLedVal != blueLedValLast) || (greenLedVal != greenLedValLast) || (redLedVal != redLedValLast))
{

Serial.println(blueLedVal.toInt());
Serial.println(redLedVal.toInt());
Serial.println(greenLedVal.toInt());

analogWrite(blueLed, ((blueLedVal.toInt()) * 10.23));
analogWrite(redLed, ((redLedVal.toInt()) * 10.23));
analogWrite(greenLed, ((greenLedVal.toInt()) * 10.23));

greenLedValLast = greenLedVal;
redLedValLast = redLedVal;
blueLedValLast = blueLedVal;

}
else
{

}

}

I need help to input the web control:

/etc/openhab2/items/home.items

/etc/openhab2/sitemaps/home.sitemap

/etc/openhab2/rules/home.rules

I promised my family that the LEDs would be ready for Christmas :slight_smile: , so I’m turning to the community.

Hi! I’ve done a similar setup for my Christmas lights using LED strips, too! (https://www.thingiverse.com/thing:2690563) - definitely pretty and worth the effort :slight_smile:

I used bruhautomation’s ESP8266 code (https://github.com/bruhautomation/ESP-MQTT-JSON-Digital-LEDs) and I already have rules/sitemap/items entries for those, but let me try to help you adapt them to your code/setup.

Try this:

.items file entry:

String LEDStrip_1 "LED Strip String" {mqtt=">[broker:MK-SmartHouse/lights/MK-LEDStripControl1:state:*:default]"}
Color LEDStrip_1_Color "LEDStrip 1 Color" <rgb>

.sitemap entry:
Colorpicker item=LEDStrip_1_Color

.rules entry:

rule "LED Strip 1 Color"
when
	Item LEDStrip_1_Color received update
then
	LEDStrip_1.sendCommand( (LEDStrip_1_Color.state as HSBType).green + ";" +
		(LEDStrip_1_Color.state as HSBType).red + ";" +
		(LEDStrip_1_Color.state as HSBType).blue + ";")
end

Provided you have your MQTT broker set up correctly, this should work with the code you posted and at least get you started controlling these LED strips (though I would suggest looking at the other code I posted the link to - it has a lot of cool effects, control of brightness, speed, color temperature, etc…)

Bartus, Thanks a lot!

But i can’t turn it on, how I can be sure that my ESP is connected to my wifi and in mqtt server?

@koltipelto you can use an external tool like MQTT-Spy or MQTT-FX, to connect to your MQTT broker, subscribe to the topic for your lights, and publish the command strings directly to it. That way, you’ll be able to tell whether the strip is connecting to the server, or if you have a problem on the openHAB side.

Thanks!

Its working now! I reinstall the mqtt on OH2.

I was lokking at your LED configuration, it’s possible to use it on openhab? It’s hard? Cause i wanna all the flash, all the cool stuff that you use on my :slight_smile:

I don’t think you would have any problems running the same code I run on your ESP board. You would only have to figure out which pin on your board actually outputs the signal to the LED strip, and modify the code to output to that pin (the easiest thing to do is just to try each pin and see which one makes your LEDs light up :slight_smile: )

And yes! I use my LED Strips with openHAB:

Like I said in an earlier post, check out my thingiverse post: https://www.thingiverse.com/thing:2690563

Load the code with the instructions I posted there (and make sure to change the IO channel to match your board output).

Then, use the following items/sitemap/rules:

.items:

String LEDStrip_FF_Office_Window "Office String" {mqtt=">[broker:home/FF_Office_Window/set:state:*:default]"}
Switch LEDStrip_FF_Office_Window_Power "Office Power" (Status){mqtt=">[broker:home/FF_Office_Window/set:command:*:MAP(LEDStripEffectJSON.map)]"}
Number LEDStrip_FF_Office_Window_FX "Office Fx" (Status){mqtt=">[broker:home/FF_Office_Window/set:command:*:MAP(LEDStripEffectJSON.map)]"}
Color LEDStrip_FF_Office_Window_Color "Office Color"	<rgb>	(Status)
Number LEDStrip_FF_Office_Window_Speed "Office Speed [%.0f]"		(Status)
Number LEDStrip_FF_Office_Window_Intensity "Office Intensity [%d]" (Status)

.sitemap:

Switch item=LEDStrip_FF_Office_Window_Power
Selection item=LEDStrip_FF_Office_Window_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"] // visibility=[MonoPrice_Z4_PWR==ON] icon="receiver"
Colorpicker item=LEDStrip_FF_Office_Window_Color
Setpoint item=LEDStrip_FF_Office_Window_Speed minValue=5 maxValue=240 step=5
Setpoint item=LEDStrip_FF_Office_Window_Intensity minValue=0 maxValue=255 step=16

.map file (LEDStripEffectJSON.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"}

.rules:

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

rule "LED Strip Office Window JSON Speed"
when
	Item LEDStrip_FF_Office_Window_Speed received update
then
	LEDStrip_FF_Office_Window.sendCommand("{\"transition\": \"" + LEDStrip_FF_Office_Window_Speed.state + "\"}")
end

rule "LED Strip Office Window JSON Intensity"
when
	Item LEDStrip_FF_Office_Window_Intensity received update
then
	LEDStrip_FF_Office_Window.sendCommand("{\"brightness\":" + LEDStrip_FF_Office_Window_Intensity.state + "}")
end

(Change FF_Office_Window to your LEDstrip name, and also the MQTT topic).

Hope this gives you a good start, let me know if you need more help!

1 Like

Sorry to my ignorance, but how i can set the .map file?

And i was uploading your code, and this error:

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

/************ WIFI and MQTT Information (CHANGE THESE FOR YOUR SETUP) ********/
const char
ssid = “ArduinoStarek”; //type your WIFI information inside the quotes
const char
password = "
";
const char
mqtt_server = “192.168.15.30”;
const char
mqtt_username = “admin”;
const char
mqtt_password = “admin”;
const int mqtt_port = 1883;

Map file is easy - in your openHAB directory (where you have the items, sitemaps, etc folders) open the “transform” folder, and create a new file with the filename I used, and copy the text I posted into it.

For the Arduino error, you’re using the wrong library for MQTT. - make sure you’re using PubSubClient by Nick O’Leary, instead (just go into Sketch-Include Library-Manage Libraries and search for that one). You will first need to delete the other library you’re using. I’m not sure if the original code you used will work with the different library, though.

Bartus, Thanks!

I managed to upload the program and he already communicated with my mqtt, I already received the answer, but I can not control it …

I think I see the problem! You need to send commands to the “bruh/porch/set” MQTT topic (change the topic in the .items file). The “bruh/porch” topic is used by the ESP8266 to send status (which is what you see in MQTT.fx right now). Try this, go to the Publish tab in MQTT.fx, and send the following JSON string:

{"state":"ON"}

to the bruh/porch/set topic. You should see the LEDs come on full solid color. If that works, then you should only need to change the MQTT topic in your .items definition and everything should work!

Bartus, where I change the pin’s that my leds is set? In the program?

I had changed just here, its right?

I think one of my erros is here, because i publish on mqtt and it say on, but nafem…

!

2017-12-24 16:43:44,842  INFO --- MqttFX ClientModel             : attempt to add PublishTopic
2017-12-24 16:43:44,842  INFO --- MqttFX ClientModel             : addPublishTopic : bruh/porch/set
2017-12-24 16:43:44,842  INFO --- MqttFX ClientModel             : sucessfully published message {"state":"ON"} to topic bruh/porch/set (QoS 0, Retained false)
2017-12-24 16:43:44,843  INFO --- MqttFX ClientModel             : messageArrived() with topic: bruh/porch/set
2017-12-24 16:43:44,844  INFO --- MqttFX ClientModel             : messageArrived() added: message #3 to topic 'bruh/porch/set'
2017-12-24 16:44:17,273  INFO --- PublishController              : publish
2017-12-24 16:44:17,274  INFO --- MqttFX ClientModel             : attempt to add PublishTopic
2017-12-24 16:44:17,274  INFO --- MqttFX ClientModel             : sucessfully published message {"state":"ON"} to topic bruh/porch (QoS 0, Retained false)
2017-12-24 16:44:17,277  INFO --- MqttFX ClientModel             : messageArrived() with topic: bruh/porch
2017-12-24 16:44:17,277  INFO --- MqttFX ClientModel             : messageArrived() added: message #4 to topic 'bruh/porch'

And the .items worls now:
image

I think now its just the out comand from esp :slight_smile:

My led strip is the RGB and i’m using the pin 14 - 12 - 13 and i’m using the ESP 12F

I was reading your program and I see that is the led strip WS2811…

I have the normal strip if 12V in, R, G ,B.

Its possible to changed it? Change the head of the program to works if my strip?

Ah, that will definitely be a problem - the code I’m using has single-LED effects, so it needs a strip with addressable RGB LEDs (like WS2811, WS2812, APA102, etc…). Your PCB only allows control of full-string LED strings that take separate R, G and B voltages to set a color for all of the LEDs. What you need to do is get a WS2811 or similar strip, and connect the data pin to one of your channels (12, 13 or 14). Then, you just need to set the right number in the “#define DATA_PIN 5” line (change 5 to whatever pin you’re connecting to. For now, you won’t be able to use this code with your strip, I’m sorry!

I really wondered, I really want to thank you for your attention and patience! I want to wish you a Merry Christmas and a Happy New Year!

Thanks a lot!!!

No problem, glad to help, just keep learning/improving (that’s what I try to do). Merry Christmas and a Happy New Year to you and your family!!

Bartus, I buy the strip, but i’ts possible to biuld the command board without Bi-Directional Module?

Hi Bartus,

I followed your steps and almost everything works perfectly apart from changing colors. I bought all the parts as suggested by BruhAutomation and it worked correctly in Home Assistant so it should work in OpenHAB, too. When I try to change a color I get this error:

2018-02-15 11:36:17.369 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '{"color": {"r": 100,"g": 18.00,"b": 26.200000000000}}' to a command type which item 'Cab
inet1' accepts: [OnOffType, RefreshType].

Any idea what’s the problem?

Cheers,
Slaw

@slaw425 Just for completeness, as in the other topic, I’m linking the how-to post with my latest configuration here: NodeMCU MQTT LED Strip Controller Build & Config How-To Videos

Thanks, man. It’s very useful. What’s strange it works correctly for the other led strip that I have - exactly the same configuration and hardware.

Can you post your items/sitemap/rules for those two strips? I’m sure it’s just a small typo/paste error, nothing major…