Openhab2 with WS2812B neopixel LED strip not working from openhab

Below is my code for nodemcu…please suggest me the configuration for openhab…like home.items,home.sitemap and home.rule.

// Neopixel
#include <Adafruit_NeoPixel.h>

// ESP 8266
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// MQTT Subscribes
const char* SubscribeTo = “esp/1/out”;
const char* SubscribeFrom = “esp/1/in”;

// WiFi & MQTT Server
const char* ssid = “CS10-1006”;
const char* password = “redhat@rhce”;
const char* mqtt_server = “”;
const int mqttport = 1883; // use 8883 for SSL
const char *mqttuser = “openhabian”;
const char *mqttpassword = “redhat”;

WiFiClient espClient;
PubSubClient pubClient(espClient);
long lastMsg = 0;
char msg[50];
String message("#000000");
String lastMessage("#000000");

// Neopixel Config
#define NeoPIN D3
#define NUM_LEDS 30
int brightness = 150;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800);

void setup() {
Serial.begin ( 115200 );

// ##############
// NeoPixel start

// WiFi

Serial.print(“Connecting to MQTT…”);
// connecting to the mqtt server
pubClient.setServer(mqtt_server, 1883);

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived [”);
Serial.print("] “);
String color(”#");

for (int i = 0; i < length; i++) {
if(i > 0){
color = color + String((char)payload[i]);


// finding payload
if((char)payload[0] == ‘#’){
// setting color


void setup_wifi() {

// We start by connecting to a WiFi network
Serial.print("Connecting to ");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

Serial.println(“WiFi connected”);
Serial.println("IP address: ");

void loop() {
if (!pubClient.connected()) {

long now = millis();
if (now - lastMsg > 60000 || lastMessage.indexOf(message) < 0 ) {

lastMsg = now;
Serial.print("Publish message: ");
char msg[7];
message.toCharArray(msg, message.length());
pubClient.publish(SubscribeTo, msg);
lastMessage = message;


void setNeoColor(String value){
Serial.println(“Setting Neopixel…” + value);
message = value;
int number = (int) strtol( &value[1], NULL, 16);

int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;

 Serial.print("RGB: ");
Serial.print(r, DEC);
Serial.print(" ");
Serial.print(g, DEC);
Serial.print(" ");
Serial.print(b, DEC);
Serial.println(" ");

for(int i=0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color( g, r, b ) );



void reconnect() {
// Loop until we’re reconnected
while (!pubClient.connected()) {
// Attempt to connect
if (pubClient.connect(“ESP8266Client”)) {
// Once connected, publish an announcement…
pubClient.publish(SubscribeTo, “Restart”);
// … and resubscribe
} else {
Serial.print(“failed, rc=”);
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying

This is going to be a case of “if you give a man a fish, you feed him for a day, if you teach a man to fish they eat for life.”

Neither I nor anyone else on this forum is going to do this for you. The fact that you just pasted in this code as is strongly implies that you have no idea how this code works. So start with that. Once you understand the code you can come back and say “I have an ESP device that publishes to the following MQTT topics and subscribes to the following MQTT topics.”

No one here wants to try to figure that out on their own by reading some random code. In truth, I suspect there is only a minority of users on this forum who even know how to read this code.

Then review How to get started (there is no step-by-step tutorial) and all the links posted there. This will help you understand OH. Then read the MQTT binding docs which tell you how to set it up.

Neither I nor anyone else on this forum is going to code this for you. But we will help answer specific questions if you show a willingness to learn.

the quick and dirty solution for you is this:

Hardware setup:
-flash the bin with esptool into your esp8266.
-connect 3 cables to your ESP8266
(+5V,GND, 510 Ohm resistor + LED signal)

Software setup:
-connect to your new esp artnet node via browser and config your WiFi parameters
-install additional DMX and Artnet plugin

Easy going, 5 minutes work.
Enjoy OpenHAB2 with you Pixel strip


BTW: works perfect with any DMX fixtures

Hi Rich,Thanks for the reply and I really appreciate your words.I am now started working on openhab from the starting.

I have one question,it will be helpfull if you can reply.

I have switch board mounted on the wall with mechanical 1 way switch to turn on/off fan.Now I have connected the relay with esp8266 to control the switch from openhab.
Now how I can work with both relay and mechanical switch and get the status on openhab if I turn on/off the fan from mechanical switch.

Rishi Saini

Hi Chrimo,
I worked on the code and it is now working.

Thanks for the solution,I will do this also.

Rishi Saini

You will need some sort of sensor to tell you the actual state of the device. I have no idea what that would be. I prefer to use devices like sonoff or Shelly that put that all into one unified device.

Hi Rich,

I solved this and I can work with both relay and mechanical switch.

Thanks for the reply.

Now I am stuck somewhere,I have discovered my chromecast as a thing from paperUI and I can control this also from paperUI.Now how I can add discovered things with my sitemap and item files so that I can control these from my own created sitemap.

Rishi Saini

The question indicates you need to read on on some OH basics. See How to get started (there is no step-by-step tutorial) for a collection of getting started resources. The answer to your question is very fundamental to working with OH and I don’t have time to write a full answer, especially since one is already written.