Page 1 of 3
MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Tuesday 15 September 2015 23:30
by yugoos
Hi,
for anyone interested in using ledstrips controlled by domoticz, i made the following sketch..
Code: Select all
// Example sketch showing how to control an RGB Led Strip.
// This example will not remember the last rgb color set after power failure.
#include <MySensor.h>
#include <SPI.h>
#include "Adafruit_NeoPixel.h"
#define NUMPIXELS 4 // Number of connected pixels on a single datapin
#define PIN 4 // Digital output pin
#define node 254 //254 for testing purpose
#define CHILD_ID 0
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
long RGB_values[3] = {0,0,0};
MySensor gw;
void setup()
{
gw.begin(incomingMessage, node, true);
gw.sendSketchInfo("RGB Node", "1.0");
gw.present(CHILD_ID, S_RGB_LIGHT);
strip.begin();
strip.show(); // Update the strip, to start they are all 'off'
}
void loop()
{
gw.process();
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_RGB) {
// starting to process the hex code
String hexstring = message.getString(); //here goes the hex color code coming from through MySensors (like FF9A00)
long number = (long) strtol( &hexstring[0], NULL, 16);
RGB_values[0] = number >> 16;
RGB_values[1] = number >> 8 & 0xFF;
RGB_values[2] = number & 0xFF;
colorWipe(Color(RGB_values[0],RGB_values[1],RGB_values[2]), 60);
}
if (message.type==V_DIMMER) {
strip.setBrightness(round((2.55*message.getInt())));
strip.show();
}
if (message.type==V_LIGHT) {
if (message.getInt() == 0) {
strip.clear();
strip.show();
}
}
}
void colorWipe(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
/* Helper functions */
// Create a 15 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
i am not a programmer so most of this code is copy pasted from sources like adafruit and mysensors examples, nevertheless it works.
there is no lightshow added (disco button) as i have no need for this, only a fast pixel by pixel color change when choosing another color.
there is a minor issue in domoticz (by design i understand) preventing the use of the full color range, domoticz only sends the values from the slider or bright-white (rgb #ffffff), nothing in between.
can anyone suggest a fix or workaround for this?
cheers,
yuri
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Wednesday 16 September 2015 8:05
by gizmocuz
you could device the full white with the brightness, you should/could also do this for the color
(both have to be done in the sketch)
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Wednesday 16 September 2015 8:21
by ThinkPad
Nice, thanks for sharing!
But isn't a WS2812 ledstrip a bit expensive for this? If i understand correctly you set the whole strip to the same color. Isn't a 'normal' RGB-strip strip with only three channels (R, G, B) that you drive with 3x MOSFET or so easier/cheaper?
Moved to MySensors subforum btw, i think it fits better overthere

Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Wednesday 16 September 2015 9:57
by yugoos
gizmocuz wrote:you could device the full white with the brightness, you should/could also do this for the color
(both have to be done in the sketch)
Getting different white values (eg. Color temperatures) cannot be done with brightness levels alone, hardcoding temperatures using brightness would mean i can never dim the light anymore.
Hardcoding colors would also mean recompiling (and uploading) new sketches whenever a different color shade is needed..
Can you explain the reason for not sending true rgb values?
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Wednesday 16 September 2015 10:13
by yugoos
ThinkPad wrote:Nice, thanks for sharing!
But isn't a WS2812 ledstrip a bit expensive for this? If i understand correctly you set the whole strip to the same color. Isn't a 'normal' RGB-strip strip with only three channels (R, G, B) that you drive with 3x MOSFET or so easier/cheaper?
Moved to MySensors subforum btw, i think it fits better overthere

Moving is fine by me, i thought i'd show my project
Using a single digital pin and no external components for the ledstrip sounds easier to me, besides, i had these laying around already
Driving the analog strips using fet's is a simple concept hardwarewise, i understand the example sketch from mysensors, i do not understand how to configure (combine) the 4 dimmers in domoticz and selecting colors using 3-4 sliders....
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 19:01
by TheoL
@Yugoos I tried your example. But I receive a "Error Sending Switch command" when I try to turn on the strip from within Domoticz. In the Log i See the error message 2015-09-25 18:58:15.825 Error: MySensors: Light command received for unknown node_id: 0
Do you have any Idea with might be wrong??
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 19:30
by yugoos
TheoL wrote:@Yugoos I tried your example. But I receive a "Error Sending Switch command" when I try to turn on the strip from within Domoticz. In the Log i See the error message 2015-09-25 18:58:15.825 Error: MySensors: Light command received for unknown node_id: 0
Do you have any Idea with might be wrong??
Hi ThoeL,
thanx for trying my sketch..
this was working flawlessly for me, are you sure the sensor is correctly presented and recognized by domoticz?
you should see 2 values, set Level and Level nn%, the latter is dimmer/on-off

- RGB-device.png (8.07 KiB) Viewed 20858 times
please ignore the RGBW, it is the same as RGB but with an extra white-byte
edit: typo (value in stead of level)
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 21:25
by TheoL
I should thank you instead, because this is something I'm happy with if I get it to work. This will cost me less than 1/3 of the price of a Fibaro RGBW device.
33 MySensorsGW 00000302 1 RGBW Light Lighting Limitless/Applamp RGBW Off 12 100
This is what I see on my devices tab. When I go over to the the switch panel, it's presented as an On/Off device. I added a S_LIGHT to the sketch and Enabled the Serial debug in MyConfig.h. The S_LIGHT is working but, I still get the same error from Domoticz.
I'm using the latest stable Domoticz release , with the 1.5 release of MySensors.
Edit: I see the difference. My RGBW Light says Off and yours is saying Level etc. The only difference I see in the sketch is the initialization of the gw. I just use gw.begin(); I'll have to look at it tomorrow. I'm not sure what the difference in the initialization is.
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 21:52
by TheoL
And I see another difference. You are using a newer version of Domoticz. I have the latest stable one. This might be the explanation. I think I just have to wait until the next stable release will be available. I had serious problems with the latest beta release last week. The GUI stalled after a few hours and kept repeating stalling each time I rebooted my Pi.
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 22:04
by yugoos
No thanx,
do you see a color-picker on the edit page for your switch?
what happens if you change the type to dimmer,
Domoticz version should not be the problem, what version mysensors are you running?
if you check debug log (both sensor and gateway) it should report a message type at presentation to gateway and RGB commands to sensor:
t=40 for V_RGB
t=41 for V_RGBW
message type V_LIGHT (t=2) is used for dimmer level and on/off only as message to the sensor.
At the moment i am unable to test things for you as i'm fresh out of arduino's (chinese mail-bags arrive daily this week so it could me tomorrow

)
FIY, my current versions are 2.3072 (beta release), MySensors 1.5 Development branch
have you tried deleting this device from domoticz and restart the sensor?
sometimes clearing arduino eeprom (again) helps and i have had some weird behavior with wrong messagetype solved by simply restarting domoticz...
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 22:08
by yugoos
TheoL wrote:And I see another difference. You are using a newer version of Domoticz. I have the latest stable one. This might be the explanation. I think I just have to wait until the next stable release will be available. I had serious problems with the latest beta release last week. The GUI stalled after a few hours and kept repeating stalling each time I rebooted my Pi.
stable release was unstable for me with similar problems you describe...
if i recall correctly i made the sketch while running stable, it should not be the problem
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Friday 25 September 2015 22:36
by yugoos
TheoL wrote: 33 MySensorsGW 00000302 1 RGBW Light Lighting Limitless/Applamp RGBW Off 12 100
This is what I see on my devices tab. When I go over to the the switch panel, it's presented as an On/Off device. I added a S_LIGHT to the sketch and
Reading your post again i must have overlooked this part, does the S_LIGHT have a different CHILD_ID in the sketch, otherwise the child-type gets overwritten!
Also, S_LIGHT is not needed to turn on the leds, the sketch listens to V_LIGHT for Off command and V_DIMMER for On/Brightness, these commands are sent by domoticz to RGB type switches
see:
Code: Select all
void incomingMessage(const MyMessage &message) {
if (message.type==V_RGB) {
// starting to process the hex code
String hexstring = message.getString(); //here goes the hex color code coming from through MySensors (like FF9A00)
long number = (long) strtol( &hexstring[0], NULL, 16);
RGB_values[0] = number >> 16;
RGB_values[1] = number >> 8 & 0xFF;
RGB_values[2] = number & 0xFF;
colorWipe(Color(RGB_values[0],RGB_values[1],RGB_values[2]), 60);
}
if (message.type==V_DIMMER) {
strip.setBrightness(round((2.55*message.getInt())));
strip.show();
}
if (message.type==V_LIGHT) {
if (message.getInt() == 0) {
strip.clear();
strip.show();
}
}
}
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Sunday 27 September 2015 13:24
by TheoL
Hi Yuri,
I'm using a cheap RGB led to test this sketch and modified it accordingly. The only thing I see differently is that you are initializing the gw as a repeater node (the last boolean value is set to true). I'm pretty sure that that's not causing the problem that Domoticz is not recognizing the S_RGB correctly. Because I have gut feeling that Domoticz doesn't know a lot about MySensors repeater Nodes.
But other than that I see no difference. Domoticz is still showing my RGB sensor as an On/Off light without the levels. I'll postpone this project until the next stable Domoticz release. Because the only thing left that's different in your set-up and mine is the Domoticz version.
It's okay, I just MySensorfied a cheap LED moodlight. And that is working great, it would have been nice to use it as an RGB lamp as well. I'll post an article about the moodlight on the MySensors forum soon.
Thanx for your help.
Theo
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Monday 28 September 2015 18:39
by yugoos
Hi Theo,
You're right about the repeater function not affecting domoticz, in fact...domoticz should not even be aware of repeaters or any routing table.
As far as i know this should work fine with stable release of domoticz but as soon as i get me new arduino's i'll test again.
If you are willing to share your sketch i can test it too, maybe its a simple typo somewhere.
Looking forward to your project post
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Saturday 10 October 2015 15:04
by gizmocuz
Together with 'Ad' we made this sketch, it uses the FastLed library
Code: Select all
[spoiler]/*
PROJECT: MySensors / RGB test for Light & Sensor
PROGRAMMER: AWI/GizMoCuz
DATE: september 27, 2015/ last update: October 10, 2015
FILE: AWI_RGB.ino
LICENSE: Public domain
Hardware: Nano and MySensors 1.5
Special:
uses Fastled library with NeoPixel (great & fast RBG/HSV universal library) https://github.com/FastLED/FastLED
Remarks:
Fixed node-id
Added option to request/apply last light state from gateway
Domoticz typicals - 2015 10 10:
- Domoticz is using HUE values internally, there might be a slight difference then using direct RGB colors.
*/
#include <MySensor.h>
#include <SPI.h>
#include <FastLED.h>
const int stripPin = 4 ; // pin where 2812 LED strip is connected
const int numPixel = 3 ; // set to number of pixels
#define NODE_ID 254 // fixed MySensors node id
#define CHILD_ID 0 // Child Id's
CRGB leds[numPixel];
char actRGBvalue[] = "000000"; // Current RGB value
uint16_t actRGBbrightness = 0xFF ; // Controller Brightness
int actRGBonoff=0; // OnOff flag
MySensor gw;
MyMessage lastColorStatusMsg(CHILD_ID,V_VAR1);
void setup() {
FastLED.addLeds<NEOPIXEL, stripPin >(leds, numPixel); // initialize led strip
gw.begin(incomingMessage, NODE_ID, false); // initialize MySensors
gw.sendSketchInfo("AWI RGB Light", "1.1");
gw.present(CHILD_ID, S_RGB_LIGHT); // present to controller
// Flash the "hello" color sequence: R, G, B, black.
colorBars();
//Request the last stored colors settings
gw.request(CHILD_ID, V_VAR1);
}
void loop() {
gw.process(); // wait for incoming messages
}
void colorBars()
{
SendColor2AllLEDs( CRGB::Red ); FastLED.show(); delay(500);
SendColor2AllLEDs( CRGB::Green ); FastLED.show(); delay(500);
SendColor2AllLEDs( CRGB::Blue ); FastLED.show(); delay(500);
SendColor2AllLEDs( CRGB::Black ); FastLED.show(); delay(500);
}
void SendColor2AllLEDs(const CRGB lcolor)
{
for(int i = 0 ; i < numPixel ; i++) {
leds[i] = lcolor;
}
}
void SendLastColorStatus()
{
String cStatus=actRGBvalue+String("&")+String(actRGBbrightness)+String("&")+String(actRGBonoff);
gw.send(lastColorStatusMsg.set(cStatus.c_str()));
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void incomingMessage(const MyMessage &message) {
if (message.type == V_RGB) { // check for RGB type
actRGBonoff=1;
strcpy(actRGBvalue, message.getString()); // get the payload
SendColor2AllLEDs(strtol(actRGBvalue, NULL, 16));
SendLastColorStatus();
}
else if (message.type == V_DIMMER) { // if DIMMER type, adjust brightness
actRGBonoff=1;
actRGBbrightness = map(message.getLong(), 0, 100, 0, 255);
FastLED.setBrightness( actRGBbrightness );
SendLastColorStatus();
}
else if (message.type == V_STATUS) { // if on/off type, toggle brightness
actRGBonoff = message.getInt();
FastLED.setBrightness((actRGBonoff == 1)?actRGBbrightness:0);
SendLastColorStatus();
}
else if (message.type==V_VAR1) { // color status
String szMessage=message.getString();
strcpy(actRGBvalue, getValue(szMessage,'&',0).c_str());
actRGBbrightness=atoi(getValue(szMessage,'&',1).c_str());
actRGBonoff=atoi(getValue(szMessage,'&',2).c_str());
SendColor2AllLEDs(strtol(actRGBvalue, NULL, 16));
FastLED.setBrightness((actRGBonoff == 1)?actRGBbrightness:0);
}
FastLED.show();
}
[/spoiler]
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Sunday 11 October 2015 11:47
by yugoos
gizmocuz wrote:Together with 'Ad' we made this sketch, it uses the FastLed library
thanx for sharing.
Small remark...Fastled is not bundled with mysensors, it needs to be downloaded and installed separately.
Does this work on current Domoticz-stable (2.2563), i tested after Theo's comment and no message is sent to MySensors at all?
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Sunday 11 October 2015 13:31
by gizmocuz
This is stated in the code (in the remarks)
You need the latest domoticz beta as there have been many changes to the mysensors implementation lately
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Sunday 11 October 2015 14:06
by TheoL
gizmocuz wrote:This is stated in the code (in the remarks)
You need the latest domoticz beta as there have been many changes to the mysensors implementation lately
Hi Gizmocuz,
I'm just curious, do you have any idea when the new stable release - with all the MySensors goodies - is available for release?
Thanx for creating all of this great stuff.
Theo
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Sunday 11 October 2015 14:10
by gizmocuz
i hope in the very near feature.... waiting for some more changes at the moment
Re: MySensors S_RGB_LIGHT WS2812 ledstrip
Posted: Sunday 11 October 2015 16:17
by yugoos
gizmocuz wrote:This is stated in the code (in the remarks)
You need the latest domoticz beta as there have been many changes to the mysensors implementation lately
I missed that line, sorry.
I am running a beta release for mysensors stuff so i can use rgb switches...i am however a bit hesitant to upgrade to latest beta because of some problems with counter values and a corrupt database..
Thanx,
Yuri