Hello all,
i a’m trying to control a Neopicel led strip with Openhab2.
Now i saw this guid on the internet:
But i a’m unable to get it working.
I have a ESP8266-01 and a ESP-01S RGB Led controller. The ESP8266-01 receives a IP adres from the router.
When i turn on the red led bij pressing the ON button or with the slider i do see a comment in the openhab events.log that the red light has been turned on. But the light won’t turn on in real life.
I have used MQTT.fx to verify that Moqsuitto is receiving the comments, and the are received.
My items file:
Group All
Color RGBLed "NeoPixel Color" (All)
String RGBLedColor (All) {mqtt=">[broker:OpenHab/RGB:command:*:default]"}
Switch NEO_RED "Red" <red> {mqtt=">[broker:OpenHab/RGB:command:ON:255,0,0]"}
Switch NEO_YELLOW "Yellow" <yellow> {mqtt=">[broker:OpenHab/RGB:command:ON:100,96,0]"}
Switch NEO_BLUE "Blue" <darkblue> {mqtt=">[broker:OpenHab/RGB:command:ON:0,0,255]"}
Dimmer NEO_SLIDERRED "Neopixel Red [%d %%]" <red> {mqtt=">[broker:OpenHab/RGB/RED:command:*:default]"}
Dimmer NEO_SLIDERGREEN "Neopixel Green [%d %%]" <green> {mqtt=">[broker:OpenHab/RGB/GREEN:command:*:default]"}
Dimmer NEO_SLIDERBLUE "Neopixel Blue [%d %%]" <blue> {mqtt=">[broker:OpenHab/RGB/BLUE:command:*:default]"}
Number W_RSSI "RSSI [%d dbm]" <signal> {mqtt="<[broker:home/nb/weer/RSSI:state:default]"}
String W_IP "IP [%s]" <network> {mqtt="<[broker:home/nb/weer/IP:state:default]"}
String W_version "Software versie [%s]" <version> {mqtt="<[broker:home/nb/weer/version:state:default]"}
String W_MAC "MAC [%s]" <mac> (Wweather) {mqtt="<[broker:home/nb/weer/mac:state:default]"}
Number W_IP_Uptime "Uptime [%d min]" <clock> (Wweather) {mqtt="<[broker:home/nb/weer/uptime:state:default]"}
My sitemap file:
sitemap NeoPixel label="NeoPixel"
{
Frame label="NeoPixel" {
Colorpicker item=RGBLed icon="slider"
//-----
Switch item=NEO_RED mappings=[ON="ON"]
Switch item=NEO_YELLOW mappings=[ON="ON"]
Switch item=NEO_BLUE mappings=[ON="ON"]
//Switch item=NEO_RAINBOWCYCLE mappings=[ON="ON"]
//Switch item=NEO_RAINBOWTHEATER mappings=[ON="ON"]
Slider item=NEO_SLIDERRED
Slider item=NEO_SLIDERGREEN
Slider item=NEO_SLIDERBLUE
//-----
}
Frame label="Info" {
Text item=W_RSSI
Text item=W_IP
Text item=W_version
Text item=W_MAC
Text item=W_IP_Uptime
}
}
My rules file:
import org.eclipse.smarthome.core.library.types.DecimalType
import org.eclipse.smarthome.core.library.types.HSBType
rule "Set HSB value of item RGBLed to RGB color value"
when
Item RGBLed changed
then
val hsbValue = RGBLed.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( RGBLedColor, color)
end
//part of Neopixel conf files
rule "Send RGB items from slider"
when
Item NEO_SLIDERRED changed or
Item NEO_SLIDERGREEN changed or
Item NEO_SLIDERBLUE changed
then
val redValue= Math::round(2.55* (NEO_SLIDERRED.state as DecimalType).intValue)
val greenValue= Math::round(2.55* (NEO_SLIDERGREEN.state as DecimalType).intValue) //
val blueValue= Math::round(2.55* (NEO_SLIDERBLUE.state as Number).intValue)
val color = redValue + "," + greenValue + "," + blueValue
sendCommand( RGBLedColor, color)
end
Arduino code:
#include "Adafruit_NeoPixel.h"
#include "ESP8266WiFi.h"
#include "PubSubClient.h" //Imroy
#include "userdata.h"
#define PIXEL_PIN 2 // Digital IO pin connected to the NeoPixels. that is D4
#define PIXEL_COUNT 5 // 60 pixels in the NeoPixel strip
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick & rings
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
/* MQTT Topic Settings */
// Enter the topic for your LED strip
String topic = "OpenHab/RGB/#";//this is your 'catch all' for MQTT topics
String IP;
String MAC;
/* Network Settings */
// Change for your WiFi network. DO NOT CHANGE HERE, CHANGE IN "userdata" tab
const char *ssid = mySSID; // cannot be longer than 32 characters!
const char *pass = PASSW; //
// Update these with values suitable for your network.
IPAddress server( myMQTT); //ipaddres MQTT server
// STARTUP DEFAULTS
long TXinterval = TRANSMITINTERVAL; // can set this in userdata.h
int signalStrength; // WiFi signal strength
long lastMinute = -1; // timestamp last minute
long upTime = 0; // uptime in minutes
byte scene;
// Initializes the color to zero
uint32_t color = 0;
// Initializes update status to false
volatile bool updateLights = false;
#define BUFFER_SIZE 100
// OTA configuration
//
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char* host = HOST; //set this in userdata.h
// function prototypes required by Arduino IDE 1.6.7, but we are now way passed that
void setupOTA(void);
//--------------------------------
void callback(const MQTT::Publish & pub) {
if (pub.has_stream()) {
uint8_t buf[BUFFER_SIZE];
int read;
while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) {
Serial.write(buf, read);
}
pub.payload_stream()->stop();
Serial.println("*");
} else
Serial.print(pub.topic());
Serial.print(" ");
Serial.println(pub.payload_string());
//----------------
// Check if the control topic came in and split payload into R,G,B values
if (pub.topic() == "OpenHab/RGB") {
String values = pub.payload_string();
int c1 = pub.payload_string().indexOf(',');
int c2 = pub.payload_string().indexOf(',', c1 + 1);
int red = pub.payload_string().toInt();
int green = pub.payload_string().substring(c1 + 1).toInt();
int blue = pub.payload_string().substring(c2 + 1).toInt();
color = strip.Color(red, green, blue);
updateLights = true;
scene=0;
} else {
// check if a scene setting topic came in
if (pub.topic() == "OpenHab/RGB/scene") {
scene=pub.payload_string().toInt();
}
}
//------------------
}
WiFiClient wclient;
PubSubClient client(wclient, server);
void setup() {
// Setup console
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
strip.begin();
setupOTA();
MAC = WiFi.macAddress();
}
void changeColor(uint32_t c) {
if (updateLights) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
updateLights = false;
}
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("arduinoClient")) {
client.set_callback(callback);
client.subscribe(topic);
}
}
}
if (client.connected())
client.loop();
changeColor(color);
//-----
if (millis() % (TXinterval * 1000) == 0) {
client.publish("home/nb/weer/version", VERSION); // no quotes or "String" as it is a macro
signalStrength = WiFi.RSSI();
client.publish("home/nb/weer/RSSI", String(signalStrength));// these topics are my choice, can change them, but then do as well in yr itemsfile
IP = WiFi.localIP().toString();
client.publish("home/nb/weer/IP", String(IP));
client.publish("home/nb/weer/mac", String(MAC));
client.publish("home/nb/weer/uptime", String(upTime));
//Serial.println(MAC);
}
// INCREASE UPTIME
if (lastMinute != (millis() / 60000)) { // another minute passed ?
lastMinute = millis() / 60000;
upTime++;
}
switch (scene) {
case 1: rainbow(20);
break;
case 2: redblue();
break;
case 3: bluedot();
break;
case 4: rainbowCycle(20);
break;
case 5:theaterChaseRainbow(50);
break;
}
}
Can someone please tell me what i a’m doing wrong here.
Greet