ESP8266 Led Strip MQTT Control Lights WS2812

46K6112

Intro: ESP8266 Led Strip MQTT Control Lights WS2812

I always wanted under bed led lights so that I can control the mood or even on the family room underneath the tv to get very subtle lighting. I could write rules in OpenHAB and it takes care of the rest. This is a rather simple setup to get everything accomplished over MQTT thanks to the power of Homie Framework with only a few lines of code.

Things to consider before getting started. Depending on the size of your LED Strip of WS2812s, you will need to adjust.

Each LED light consumes about 60 mA, so if you are powering 60, the minimum Amperage would be as follows. As you add more lights, the power requirements also increase.

60 LEDs × 60 mA ÷ 1,000 = 3.6 Amps minimum

Parts List:

STEP 1: Wiring

The wiring schematic is fairly simple.

Note: Please make sure you have the polarity right on the power supply or you can damage the LEDs and/or ESP8266.

From Power Supply

  • VCC Line to Wemos D1 5V Pin and to Positive/Red Wire on LED Strip
  • GND Line to Wemos D1 GND Pin and to Ground/White Wire on LED Strip
  • Add 1000uf Capacitor on Ground/5V line close to LED Strip

From Wemos D1

  • Connect D1 Pin to Data/Green Wire on LED Strip.

Thats it. :)

STEP 2: Programming

Dependencies for Arduino are Homie 2.0 which you can get from their Github and the Adafruit Neopixel Library

Sketch for Arduino

#include <Homie.h>
#include <Adafruit_NeoPixel.h>

#define FW_NAME "led-control" #define FW_VERSION "1.0.0"

#define PIN D1 #define LED_COUNT 60

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800); int delayval = 500; int showType = 0; int SoffitR; int SoffitG; int SoffitB; int numPixels = LED_COUNT;

HomieNode ledNode("led", "led");

bool lightOnHandler(HomieRange range, String value) { if (value == "off") { colorWipe(strip.Color(0, 0, 0), 50); // Black/off Homie.setNodeProperty(ledNode, "color").send("0,0,0,0"); } else if (value == "red") { colorWipe(strip.Color(255, 0, 0), 50); // Red Homie.setNodeProperty(ledNode, "color").send("0,255,0,0"); } else if (value == "green") { colorWipe(strip.Color(0, 255, 0), 50); // Green Homie.setNodeProperty(ledNode, "color").send("0,0,255,0"); } else if (value == "blue") { colorWipe(strip.Color(0, 0, 255), 50); // Blue Homie.setNodeProperty(ledNode, "color").send("0,0,255,0"); } else { //value.trim(); Serial.print (value); //Serial.flush(); // split string at every "," and store in proper variable // convert final result to integer SoffitR = value.substring(0,value.indexOf(',')).toInt(); SoffitG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt(); SoffitB = value.substring(value.lastIndexOf(',')+1).toInt(); colorWipe(strip.Color(SoffitR, SoffitG, SoffitB), 50); Homie.setNodeProperty(ledNode, "color").send(value); } return true; } int i;

void colorWipe(uint32_t c, uint8_t wait) { for (uint16_t i=0; i

void setup() { Serial.begin(115200); Serial.println(FW_NAME FW_VERSION); strip.begin();

Homie_setFirmware(FW_NAME, FW_VERSION); ledNode.advertise("color").settable(lightOnHandler);

Homie.setup();

}

void loop() { Homie.loop(); }

JSON Config File

{
"name": "LED for TV Stand", "device_id": "family-room-tv-led", "wifi": { "ssid": "MYSSDID", "password": "MYWIFIPASSWORD" }, "mqtt": { "host": "MY MQTT SERVER", "port": 1883, "base_topic": "devices/", "auth": true, "username": "sensor", "password": "sensor1" }, "ota": { "enabled": true } }

OpenHAB

Items

Color TVLedLight "Bed Color" <colorlight> (All)
String TVLedLightColor (All) {mqtt=">[OpenHab:devices/family-room-tv-led/led/color/set:command:*:default]"}

Sitemap

Text label="Family Room" icon="sofa" {
Frame label="Switches"

Switch item=wifiplug_living_room

Colorpicker item=TVLedLight

}

}

Rules

import org.openhab.core.library.types.*

rule "Set RGB value TVLedLight"
when

Item TVLedLight changed

then

val hsbValue = TVLedLight.state as HSBType

val brightness = hsbValue.brightness.intValue

val redValue = ((((hsbValue.red.intValue * 255) / 100) * brightness) / 100).toString

val greenValue = ((((hsbValue.green.intValue * 255) / 100) * brightness) / 100).toString

val blueValue = ((((hsbValue.blue.intValue *255) / 100) * brightness) / 100).toString

val color = redValue + "," + greenValue + "," + blueValue

sendCommand( TVLedLightColor, color )

end

Give it a test run.


Thanks for reading.

* I maybe compensated for some links.

8 Comments

Are there any experiences about the speed of processing the command from MQTT Broker? Is it possible to do something like animations with the setup?

Maybe there are some new versions of libs and so on...

I tried to make this tutorial and end up in a error of the compiler

'class HomieInternals::HomieClass' has no member names 'setNodeProperty'

Does someone has a idea?

how to go ahead with this project. I have a NodeMCU and compiling the code in Aurdino. I get this error -

#include <ArduinoJson.h> not found.

Where does the JSON file go

The error means that you need to install the ArduinoJson library. Check out:

https://arduinojson.org/doc/installation/

Also where does the JSON config file go. Sorry for being so dumb.

Hi am new to this hobby. Am working on something similar. Based on instructions from other videos I had bought a nodeMCU ESP8266 controller. Can I use this instead of Wemos D1. Also can you please do a step by step video for complete noobies like me. Thank you

Hi nice instruction to get my tiny led strip running with MQTT commands!

I found a small bug in your Arduino IDE script:

int i;

void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i=0; i

Needs to be:

int i;

void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) { //<strip.numPixels
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}