Introduction: ESP8266 Led Strip MQTT Control Lights WS2812

Picture of 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.

Comments

Sanjay JudeD (author)2017-11-15

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

RamakrishnanM3 (author)2017-08-04

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

RamakrishnanM3 (author)2017-08-04

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

Alcov (author)2016-10-09

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);
}
}

About This Instructable

10,384views

26favorites

License:

More by billm950:ESP8266 Water Leak Detector MQTT HomieESP8266 Led Strip MQTT Control Lights WS2812ESP8266 Soil Moisture Sensor for $7
Add instructable to: