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 = “192.168.0.106”;
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
Serial.println();
strip.setBrightness(brightness);
strip.begin();
strip.show();
delay(50);

// WiFi
setup_wifi();

// MQTT
Serial.print(“Connecting to MQTT…”);
// connecting to the mqtt server
pubClient.setServer(mqtt_server, 1883);
pubClient.setCallback(callback);
Serial.println(“done!”);
}

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

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

Serial.println();

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

}

void setup_wifi() {

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

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}

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

void loop() {
if (!pubClient.connected()) {
delay(100);
reconnect();
}
pubClient.loop();

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

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

}
delay(10);
}

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 ) );
}
strip.show();

Serial.println("on.");

}

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

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.

Hi,
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

Cheers
Chrimo

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
Thanks

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.

Regards,
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.

Hi Rich,

Hope you are doing well.

I have one query related to openHAB. Can we say openHAB is compliant with oneM2M standard?

Regards,
Rishi Saini

It’s considered bad form to reopen a five year old thread.

As to oneM2M, I doubt OH has any support for it. I can’t find any home automation devices available for same that use it so there wouldn’t be much use in supporting it. OH certainly isn’t implemented using it.

If someone has an actual device that users this I’m sure an add-on count be created or it could be bridged using MQTT.