I’m trying to get the sketch from gizmocuz to work with MySensors 2.0. I have made the necessary conversion from MySensors 1.5 to 2.0. It compiles without errors.
When I upload this sketch to the Arduino Nano and run it, I see in Domoticz this:
- 2016-08-16_13-49-22.png (171.93 KiB) Viewed 7469 times
I have made a dimmer switch in Domoticz and I see the “colorwheel”
- 2016-08-16_13-50-22.png (263.47 KiB) Viewed 7469 times
If I run the sketch the “SendColor2AllLEDs” works. And then it stops because I don’t get any information from Domoticz to this Node. So, Domoticz is not sending commands, or it is not receiving commands.
Maybe (pretty possible) that I have overlooked something but I don’t know what.
Sketch:
Code: Select all
// PROJECT: MySensors / RGB test for Light & Sensor
// PROGRAMMER: AWI/GizMoCuz
// DATE: september 27, 2015/ last update: October 10, 2015
// LAST UPDATE: August 16, 2016 by Ritmeester
// FILE: AWI_RGB.ino
// LICENSE: Public domain
// Hardware: Nano and MySensors 2.0
// 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.
// Enable debug prints to serial monitor
// #define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE
#include <MySensors.h>
#include <SPI.h>
#include <FastLED.h>
#define LED_PIN 4 // Pin to connect WS2812B (strip)
#define NUM_LEDS 10
#define COLOR_ORDER GRB // It's GRB for WS2812B
#define LED_TYPE WS2812B // What kind of strip are you using (APA102, WS2801 or WS@2812B
#define NODE_ID 254 // fixed MySensors node id
#define CHILD_ID 0 // Child Id's
CRGB leds[NUM_LEDS];
char actRGBvalue[] = "000000"; // Current RGB value
uint16_t actRGBbrightness = 0xFF ; // Controller Brightness
int actRGBonoff = 0; // OnOff flag
MyMessage lastColorStatusMsg(CHILD_ID, V_VAR1);
void presentation() {
sendSketchInfo("AWI RGB Light", "1.1");
present(CHILD_ID, S_RGB_LIGHT); // present to controller
}
void setup() {
LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812B - initialize led strip
// Flash the "hello" color sequence: R, G, B, black.
colorBars();
//Request the last stored colors settings
request(CHILD_ID, V_VAR1);
}
void receive(const MyMessage &message) {
}
void loop() {
}
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 < NUM_LEDS ; i++) {
leds[i] = lcolor;
}
}
void SendLastColorStatus()
{
String cStatus = actRGBvalue + String("&") + String(actRGBbrightness) + String("&") + String(actRGBonoff);
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();
}