[Gelöst] MySensors RGB LED Stripe (3 Mosfets)

Begonnen von Wiesel, 14 Mai 2018, 22:14:59

Vorheriges Thema - Nächstes Thema

Wiesel

Hallo Zusammen,

ich komme aktuell mit meinem RGB Controller mit MySensors nicht weiter.
Ich habe diesen Code hier im MySensors Forum gefunden, aber bekomme ihn nicht mit FHEM richtig zum laufen.
Es funktioniert nur Status1 und percentage2. Wird Status1 auf "on" gesetzt fängt ein Farbverlauf an. Mit Percentage von 1-10 kann ich die Art des Verlaufs ändern.

Es ist ein ganz normaler RGB Stripe den ich mit 3 Mosfets am Arduino angeschlossen habe.

Ich bin mich aktuell dabei reinzulesen aber so ein paar Tipps würden mir echt helfen wieso ich in FHEM
1. Keine Readings angezeigt bekomme und
2. den LED Streifen nicht korrekt steuern kann -> Helligkeit, Farbe (mittels Colorpicker), Farbverlauf & Speed

https://forum.mysensors.org/topic/6765/rgb-led-strip/41#

Code
//## INCLUDES ##
#define MY_DEBUG
#define MY_RADIO_NRF24
#define MY_NODE_ID AUTO

#include <MySensors.h>
#include <SPI.h>
#include <FastLED.h>

#define cID_RGB_SELECTOR 0
#define cID_CYCLE_EFFECT 1
#define cID_CYCLE_EFFECT_SPEED 2

#define PIN_RED   5
#define PIN_GREEN 6
#define PIN_BLUE  3

//## VARIABLES ##
// MySensors
#define MySensors_SketchName      "RGB LED Strip"
#define MySensors_SketchVersion   "v0.3"
MyMessage MySensors_MSG_Last_Color(cID_RGB_SELECTOR,V_VAR1);
MyMessage MySensors_MSG_RGB_Selector(cID_RGB_SELECTOR, V_LIGHT);
MyMessage MySeonsors_MSG_CYCLE_EFFECT(cID_CYCLE_EFFECT, V_LIGHT);
MyMessage MySensors_MSG_CYCLE_EFFECT_SPEED(cID_CYCLE_EFFECT_SPEED, V_DIMMER);
bool MySensors_RequestACK = false;
// Single color
int Solid_RGB_Active=0;
char Solid_RGB_Color[] = "000000";
uint16_t Solid_RGB_Brightness = 0xFF;
// Cycle effect
int Cycle_Effect_Active=0;
unsigned long Cycle_Effect_pMillis = 0;
long Cycle_Effect_Speed = 20;
static uint8_t Cycle_Effect_Current_Hue;
// Supporting
bool Status_Change = false;
bool Print_Debug = false;

// ## Primary flow control
void setup() {
  Serial.begin(115200);
  while (!Serial) ;
  Serial.print("compiled: ");Serial.print(__DATE__);Serial.println(__TIME__);

  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);

  Event_ColorTestBars();

  request(cID_RGB_SELECTOR, V_VAR1);
  request(cID_RGB_SELECTOR, V_LIGHT);
  request(cID_CYCLE_EFFECT, V_LIGHT);
  request(cID_CYCLE_EFFECT_SPEED, V_DIMMER);
}
void loop() {
  if (Cycle_Effect_Active == 1){
    unsigned long currentMillis = millis();
    Event_RunCycleEffect(currentMillis);
  } else if (Status_Change){
    Status_Change = false;
      #ifdef MY_DEBUG
        if (Print_Debug) {Serial.println("STATUS CHANGE");}
      #endif
    if (Solid_RGB_Active == 0){
      Event_SetLEDColors( CRGB::Black );
    }else if (Solid_RGB_Active == 1){
      CHSV colorHSV = rgb2hsv_approximate(str2CRGB(Solid_RGB_Color));
      Event_SetLEDColors(CHSV(colorHSV.h, colorHSV.s, Solid_RGB_Brightness));
    }
  }
}
// ## MySensors Methods
void presentation()  {
  sendSketchInfo(MySensors_SketchName, MySensors_SketchVersion);

  present(cID_RGB_SELECTOR, S_RGB_LIGHT, "RGB Color Selector", MySensors_RequestACK);
  present(cID_CYCLE_EFFECT, S_LIGHT, "RGB Cycle Effect", MySensors_RequestACK);
  present(cID_CYCLE_EFFECT_SPEED, S_DIMMER, "RGB Cycle Effect Speed", MySensors_RequestACK);
}
void receive(const MyMessage &message){
  #ifdef MY_DEBUG
    if (message.isAck()){
      Serial.println("Got ack from gateway");
    }
  #endif
  if (message.type == V_LIGHT){
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message v_light");}
    #endif
    int current_Light_State = message.getString()[0] == '1';// Incoming on/off command sent from controller ("1" or "0")
    if (message.sensor==cID_CYCLE_EFFECT){// is Cycle Message
      if (current_Light_State==1){//turn cycle on
        Event_LightCycle(true, true, false);
        Event_SolidColor(false, false, true);
      } else {//turn cycle off
        Event_LightCycle(false, true, false);
        Event_SolidColor(false, false, true);
      }
    } else if (message.sensor==cID_RGB_SELECTOR){// is RGB Message
      if (current_Light_State==1){//turn RGB on
        Event_SolidColor(true, true, false);
        Event_LightCycle(false, false, true);
      } else {//turn RGB off
        Event_SolidColor(false, true, false);
        Event_LightCycle(false, false, true);
      }
    } else {
      #ifdef MY_DEBUG
        Serial.print("UNKNOWN Light - Message:");
        Serial.print(message.getString());
        Serial.print(" - Sensor:");
        Serial.println(message.sensor);
      #endif
    }
  } else if (message.type == V_RGB){
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message v_rgb");}
    #endif
    String szMessage=message.getString();
    strcpy(Solid_RGB_Color, getValue(szMessage,'&',0).c_str());
    Solid_RGB_Active = 1;
  }else if (message.type == V_DIMMER) {// if DIMMER type, adjust brightness
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message v_dimmer");}
    #endif
    if (message.sensor==cID_RGB_SELECTOR){// is single Message
      if (Solid_RGB_Active==1){//turn RGB on
        Event_SolidColor(true, true, false);
        Event_LightCycle(false, false, true);
      } else {//turn RGB off
        Event_SolidColor(false, true, false);
        Event_LightCycle(false, false, true);
      }
      Solid_RGB_Brightness = map(message.getLong(), 0, 100, 0, 255);
      CRGB colorRGB = str2CRGB(Solid_RGB_Color);
      CHSV colorHSV = rgb2hsv_approximate(colorRGB);
      colorHSV = CHSV(colorHSV.h, colorHSV.s, Solid_RGB_Brightness);
      Event_SetLEDColors(colorHSV);
      #ifdef MY_DEBUG
        if (Print_Debug) {
          Serial.print("colorHSV.h:");
          Serial.println(colorHSV.h);
          Serial.print("colorHSV.s:");
          Serial.println(colorHSV.s);
          Serial.print("colorHSV.v:");
          Serial.println(colorHSV.v);
        }
      #endif
      Event_SendLastColor();
    } else if (message.sensor==cID_CYCLE_EFFECT_SPEED){// is Speed dimmer Message
      Cycle_Effect_Speed = map(message.getLong(), 0, 100, 1, 202);
      #ifdef MY_DEBUG
        if (Print_Debug) {
          Serial.print("Cycle_Effect_Speed: ");
          Serial.println(Cycle_Effect_Speed);
        }
      #endif
    }
  }else if (message.type == V_STATUS) {           // if on/off type, toggle brightness
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message v_status");}
    #endif
    Solid_RGB_Active = message.getInt();
    Cycle_Effect_Active = 0;
    if (Solid_RGB_Active == 0){
      if (Print_Debug) {Serial.println("Strip OFF");}
      Event_SetLEDColors( CRGB::Black );
    }else{
      if (Print_Debug) {Serial.println("Strip ON");}
      Event_SetLEDColors(strtol(Solid_RGB_Color, NULL, 16));
    }
    //Event_SendLastColor();
  }else if (message.type==V_VAR1) {            // color status
    String szMessage=message.getString();
    #ifdef MY_DEBUG
      if (Print_Debug) {
        Serial.println("message v_var1");
        Serial.println(szMessage);
      }
    #endif
    strcpy(Solid_RGB_Color, getValue(szMessage,'&',0).c_str());
    Solid_RGB_Active = 1;
    Cycle_Effect_Active = 0;
  }
  Status_Change = true;
}
// ## Events
void Event_LightCycle(bool t, bool s, bool u) {
  Cycle_Effect_Active = (t) ? 1 : 0;
  if (u){
    send(MySeonsors_MSG_CYCLE_EFFECT.set(Cycle_Effect_Active),MySensors_RequestACK);
  }
}
void Event_SolidColor(bool t, bool s, bool u) {
  Solid_RGB_Active = (t) ? 1 : 0;
  if (u){
    send(MySensors_MSG_RGB_Selector.set(Solid_RGB_Active),MySensors_RequestACK);
  }
}
void Event_SetLEDColors( const CRGB& rgb){
  analogWrite(PIN_RED,   rgb.r );
  analogWrite(PIN_GREEN, rgb.g );
  analogWrite(PIN_BLUE,  rgb.b );
}
void Event_SendLastColor(){
  String current_status=Solid_RGB_Color+String("&")+String(Solid_RGB_Brightness)+String("&")+String(Solid_RGB_Active);
  send(MySensors_MSG_Last_Color.set(current_status.c_str()),MySensors_RequestACK);
}
void Event_RunCycleEffect(unsigned long theMills){
  if (theMills - Cycle_Effect_pMillis >= Cycle_Effect_Speed){
    Cycle_Effect_pMillis = theMills;
    Cycle_Effect_Current_Hue = Cycle_Effect_Current_Hue + 1;
    Event_SetLEDColors( CHSV( Cycle_Effect_Current_Hue, 255, 255) );
  }
}
void Event_ColorTestBars(){// Event_ColorTestBars: flashes Red, then Green, then Blue, then Black. Helpful for diagnosing if you've mis-wired which is which.
  Event_SetLEDColors( CRGB::Red );   delay(500);
  Event_SetLEDColors( CRGB::Green ); delay(500);
  Event_SetLEDColors( CRGB::Blue );  delay(500);
  Event_SetLEDColors( CRGB::Black ); delay(500);
}
// ## Helper Functions
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]) : "";
}
int x2i(char *s) {
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0';
    }else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10;
    }else {
      break;
    }
    s++;
  }
  return x;
}
char* str2char(String command){
    if(command.length()!=0){
        char *p = const_cast<char*>(command.c_str());
        return p;
    }
}
CRGB str2CRGB(String s){
  String r = str2char(s.substring(0,2));
  String g = str2char(s.substring(2,4));
  String b = str2char(s.substring(4,6));
  uint8_t red = x2i(r.c_str());
  uint8_t green = x2i(g.c_str());
  uint8_t blue = x2i(b.c_str());
  #ifdef MY_DEBUG
    if (Print_Debug) {
      Serial.print("r:");
      Serial.println(r);
      Serial.print("g:");
      Serial.println(g);
      Serial.print("b:");
      Serial.println(b);
      Serial.print("red:");
      Serial.println(red);
      Serial.print("green:");
      Serial.println(green);
      Serial.print("blue:");
      Serial.println(blue);
    }
  #endif
  CRGB colorRGB = CRGB(red, green, blue);
  return colorRGB;
}


FHEM Definition

defmod MYSENSOR_20 MYSENSORS_DEVICE 20
attr MYSENSOR_20 IODev MySensorsSerialGateway
attr MYSENSOR_20 mapReading_percentage2 2 percentage
attr MYSENSOR_20 mapReading_power 0 power
attr MYSENSOR_20 mapReading_power1 1 power
attr MYSENSOR_20 mapReading_power2 2 power
attr MYSENSOR_20 mapReading_rgb 0 rgb
attr MYSENSOR_20 mapReading_status1 1 status
attr MYSENSOR_20 mapReading_status2 2 status
attr MYSENSOR_20 mode node
attr MYSENSOR_20 setReading_percentage2 slider,0,1,100
attr MYSENSOR_20 setReading_power 1
attr MYSENSOR_20 setReading_power1 1
attr MYSENSOR_20 setReading_power2 1
attr MYSENSOR_20 setReading_rgb 1
attr MYSENSOR_20 setReading_status1 off,on
attr MYSENSOR_20 setReading_status2 off,on
attr MYSENSOR_20 version 2.2.0

setstate MYSENSOR_20 2018-05-14 21:58:25 SKETCH_NAME RGB LED Strip
setstate MYSENSOR_20 2018-05-14 21:58:25 SKETCH_VERSION v0.3
setstate MYSENSOR_20 2018-05-14 21:58:25 parentId 0



Grüße Wiesel
Raspi 4 mit FHEM und CUL / Conbee2

Wiesel

Update:

Habe mittlerweile herausgefunden, dass S_LIGHT, V_LIGHT und V_DIMMER in der aktuellen MySensors Version nicht mehr vorhanden sind.
Muss jetzt schauen wie ich diese ersetzen kann.

Ein Blick in 10_MYSENSORS_DEVICE.pm hat mir ebenfalls geholfen, welche Variablen ich überhaupt im Code verwenden darf.


Lg Wiesel
Raspi 4 mit FHEM und CUL / Conbee2

Beta-User

Vielleicht noch zur Ergänzung:
Readings werden in der Regel erst angelegt, wenn die Node was sendet - was sie hier ja nicht macht. Dann einfach ein entsprechendes "set" ausführen, dann sollte auch das Reading vorhanden sein. Also könnte ein
set MYSENSOR_20 rgb 112233
dazu führen, dass dann auch das widget angezeigt wird.

Btw.: für MySensors-spezifische Sachen gibt es einen eigenen Forumsbereich - da schaut vielleicht eher jemand vorbei, der auch MySensors-rgb im Einsatz hat. (Verschieben könntest du selbst).
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Wiesel

Hallo Beta-User,

Danke das hatte schon mal geholfen ein Reading zu erzeugen.
Ich habe jetzt alles umgeschmissen und mich dazu entschieden den Code selbst zu schreiben um erstmal einfach anzufangen, das Zusammenspiel MySensors und FHEM zu verstehen und später weitere features (Lichtlauf, etc.) hinzuzufügen.
Sobald ich meinen ersten Code fertig oder fragen habe werde ich Ihn hier posten.
Vielleicht kann ich ja dann hiermit dem ein oder anderen etwas helfen ;)

Lg Wiesel
Raspi 4 mit FHEM und CUL / Conbee2

Beta-User

Moin Wiesel,
Danke für die Rückmeldung.Ist sicher keine schlechte Idee, das Zusammenwirken von MySensor-Nodes und FHEM erst mal auszutesten. Allerdings sah dein Code doch eigentlich schon super aus, es hätte m.E. gereicht, ggf. die Typen in der Presentation() auf "zulässige" Werte zu setzen.Allerdings habe ich keine eigene Erfahrung mit RGB-Nodes (@MySensors), da kann es sein, dass es irgendwo auf dem ziemlich langen Weg zwischen Colorpicker und Stripe hakt; ich hätte hier eher mal an der Node Rückmeldung (Serial.print()) über empfangene RGB-Codes eingebaut, um zu sehen, was da ankommt.
Aber vielleicht meldet sich ja jemand, der sowas schon mal gemacht hat (es gab m.E. mind. ein Projekt mit Terrassen-Lampen) und kann Beispielcode auch mit FHEM-Integration zeigen.
Gruß, Beta-User
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Wiesel

Ich glaube es hat auch nicht korrekt funktioniert weil der Code auf Basis von Domoticz ist.
Ich habe hier im Forum auch schon etwas gefunden, das basiert aber auf einen "smarten" LED-Streifen (NeoPixel). Die lassen sich einfacher steuern.
Hätte ich das damals schon gewusst hätte ich lieber ein paar Euro mehr ausgegeben und solche geholt.

Mit meinem Code kann ich jetzt auch die Farbe frei wählen über den RGB Colorpicker und den Stripe Ein und Aus schalten. Wo es aktuell hängt ist die Helligkeit.
Das gestaltet sich schwieriger als Gedacht diese innerhalb der gewählten Farbe zu ändern. ....oder ich sehe vor lauter Bäumen den Wald nicht mehr  ;D


P.S. Wenn ich hiermit durch bin, kann ich wahrscheinlich mit den gewonnen Erkenntnissen den "fremden" Code aus meinem ersten Post problemlos abändern  ;D
Raspi 4 mit FHEM und CUL / Conbee2

Beta-User

Was m.E. noch fehlt, ist ein Percentage für das Child 0, dann könnte das klappen.

Versuch mal folgendes: Präsentiere auch noch irgend einen Sensor, der % kann unter der Child ID von cID_RGB_SELECTOR (0) (oder noch besser: erweitere die Typen in 10_MYS...pm um [level], dann sollte das auch so schon gehen).
Sollte der Slider dann nicht automatisch angezeigt werden, einfach wieder ein entspr. reading setzen. Damit sollte sich die Helligkeit dann jedenfalls mal regulieren lassen. Ob das mit der korrekten Farbwiedergabe bei Level-Änderungen klappt, siehst du ja dann.
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Beta-User

Hallo Wiesel,
könntest du bitte wirklich den code im Modul testweise etwas anpassen (ca. Zeile 121)?
S_RGB_LIGHT             => { receives => [V_RGB,V_WATT,V_PERCENTAGE], sends => [V_RGB,V_WATT,V_PERCENTAGE] }, # RGB light
Dann sollte nach einem FHEM-Neustart und einer erneuten Presentation eigentlich auch ein Slider für die Helligkeit verfügbar sein.
Wenn das paßt, sollte man es m.E. allg. einbauen.

Danke!
Gruß, Beta-User
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files

Wiesel

Hallo Beta-User,

Habe es nicht vergessen nur bisher keine Zeit das zu testen.
Werde das die Tage mal ändern und dann berichten.

Vielen Dank für die Hilfe.

Grüße Wiesel
Raspi 4 mit FHEM und CUL / Conbee2

Wiesel

So, also ich bin jetzt MySensors genug verstanden um den Code aus dem MySensors Forum umzuschreiben. Ich finde hier die Helligkeitsänderung gut gelöst.

Habe ebenfalls ein paar Änderungen in der Logik auf meine Bedürfnisse angepasst.

Das mit der Änderung im Modul habe ich jetzt nicht gemacht.

Thema ist dann gelöst  8)


/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* DESCRIPTION
*
* This sketch clears radioId, relayId and other routing information in EEPROM back to factory default
*
*/

//## FIRST DEFINES ##
#define MY_DEBUG
#define MY_RADIO_NRF24
#define MY_NODE_ID 21

//## INCLUDES ##
#include <MySensors.h>
#include <SPI.h>
#include <FastLED.h>

#define cID_RGB_SELECTOR 0
#define cID_CYCLE_EFFECT 1

#define PIN_RED   5
#define PIN_GREEN 6
#define PIN_BLUE  3

//## VARIABLES ##
// MySensors
#define MySensors_SketchName      "RGB LED Strip"
#define MySensors_SketchVersion   "v1.0"
MyMessage MySensors_MSG_Last_Color(cID_RGB_SELECTOR,V_RGB);
MyMessage MySensors_MSG_RGB_Brightness(cID_RGB_SELECTOR, V_PERCENTAGE);
MyMessage MySensors_MSG_RGB_Brightness_Status(cID_RGB_SELECTOR, V_STATUS);
MyMessage MySensors_MSG_CYCLE_EFFECT(cID_CYCLE_EFFECT, V_PERCENTAGE);
MyMessage MySensors_MSG_CYCLE_EFFECT_Status(cID_CYCLE_EFFECT, V_STATUS);

bool MySensors_RequestACK = false;
// Single color
int Solid_RGB_Active=0;
char Solid_RGB_Color[] = "000000";
uint16_t Solid_RGB_Brightness = 0xFF;
// Cycle effect
int Cycle_Effect_Active=0;
unsigned long Cycle_Effect_pMillis = 0;
long Cycle_Effect_Speed = 20;
static uint8_t Cycle_Effect_Current_Hue;
// Supporting
bool Status_Change = false;
bool Print_Debug = true;
int current_Light_State;
int ControllerBrightness;

// ## Primary flow control
void setup() {
  Serial.begin(115200);
  while (!Serial) ;
  Serial.print("compiled: ");Serial.print(__DATE__);Serial.println(__TIME__);

  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);

  Event_ColorTestBars();

  request(cID_RGB_SELECTOR, V_RGB);
  request(cID_RGB_SELECTOR, V_PERCENTAGE);
  request(cID_RGB_SELECTOR, V_STATUS);
  request(cID_CYCLE_EFFECT, V_STATUS);
  request(cID_CYCLE_EFFECT, V_PERCENTAGE);
}

void loop() {
  if (Cycle_Effect_Active == 1){
    unsigned long currentMillis = millis();
    Event_RunCycleEffect(currentMillis);
  } else if (Status_Change){
    Status_Change = false;
      #ifdef MY_DEBUG
        if (Print_Debug) {Serial.println("STATUS CHANGE");}
      #endif
    if (Solid_RGB_Active == 0){
      Event_SetLEDColors( CRGB::Black );
    }else if (Solid_RGB_Active == 1){
      CHSV colorHSV = rgb2hsv_approximate(str2CRGB(Solid_RGB_Color));
      Event_SetLEDColors(CHSV(colorHSV.h, colorHSV.s, Solid_RGB_Brightness));
    }
  }
}
// ## MySensors Methods
void presentation()  {
  sendSketchInfo(MySensors_SketchName, MySensors_SketchVersion);

  present(cID_RGB_SELECTOR, S_RGB_LIGHT, "RGB Color Selector", MySensors_RequestACK);
  present(cID_RGB_SELECTOR, S_DIMMER, "RGB Color Brightness", MySensors_RequestACK);
  present(cID_CYCLE_EFFECT, S_DIMMER, "RGB Cycle Effect", MySensors_RequestACK);
}

void receive(const MyMessage &message){
  if (Print_Debug) {
    Serial.print("Message Destination Node:  "); Serial.println(message.destination);
    Serial.print("Message Destination Child:  "); Serial.println(message.sensor);
    Serial.print("Message TYPE:  "); Serial.println(message.type);
    Serial.print("Message String:  "); Serial.println(message.getString());
  }

  #ifdef MY_DEBUG
    if (message.isAck()){
      Serial.println("Got ack from gateway");
    }
  #endif
  if (message.type == V_STATUS){
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message V_STATUS");}
    #endif
    current_Light_State = message.getString()[0] == '1';// Incoming on/off command sent from controller ("1" or "0")
    if (message.sensor==cID_CYCLE_EFFECT){// is Cycle Message
      if (Print_Debug) {Serial.println("sensor cID_CYCLE_EFFECT / V_STATUS");}
      if (current_Light_State==1){//turn cycle on
        Event_LightCycle(true, true, true);
        Event_SolidColor(false, false, true);
      } else {//turn cycle off
        Event_LightCycle(false, true, true);
        Event_SolidColor(false, false, true);
      }
    }
      else if (message.sensor==cID_RGB_SELECTOR){// is RGB Message
      if (Print_Debug) {Serial.println("sensor cID_RGB_SELECTOR / V_STATUS");}
      if (current_Light_State==1){//turn RGB on
        Event_SolidColor(true, true, true);
        Event_LightCycle(false, false, true);
      } else {//turn RGB off
        Event_SolidColor(false, true, true);
        Event_LightCycle(false, false, true);
      }
    }
      else {
      #ifdef MY_DEBUG
        Serial.print("UNKNOWN Light - Message:");
        Serial.print(message.getString());
        Serial.print(" - Sensor:");
        Serial.println(message.sensor);
      #endif
    }
  }
    else if (message.type == V_RGB){
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message v_rgb");}
    #endif
    String szMessage=message.getString();
   
    szMessage.toUpperCase();
    Serial.print("szMessage:    ");Serial.println(szMessage);
   
    strcpy(Solid_RGB_Color, getValue(szMessage,'&',0).c_str());
    if (Solid_RGB_Brightness==0) {
      Solid_RGB_Brightness = 255;
      ControllerBrightness = 100;
      Event_SendLastBrightness();
     
    }
     
    Solid_RGB_Active = 1;
    Event_SendLastColor();
   
  }
  else if (message.type == V_PERCENTAGE) {// if DIMMER type, adjust brightness
    #ifdef MY_DEBUG
      if (Print_Debug) {Serial.println("message V_PERCENTAGE");}
    #endif
    if (message.sensor==cID_RGB_SELECTOR){// is single Message
      if (Print_Debug) {Serial.println("message cID_RGB_SELECTOR / V_PERCENTAGE");}
      if (Solid_RGB_Active==0){//turn RGB on
        Event_SolidColor(true, true, true);
        Event_LightCycle(false, false, true);
      }
      ControllerBrightness = message.getInt();
      Solid_RGB_Brightness = map(message.getLong(), 0, 100, 0, 255);
      CRGB colorRGB = str2CRGB(Solid_RGB_Color);
      CHSV colorHSV = rgb2hsv_approximate(colorRGB);
      colorHSV = CHSV(colorHSV.h, colorHSV.s, Solid_RGB_Brightness);
      Event_SetLEDColors(colorHSV);
      #ifdef MY_DEBUG
        if (Print_Debug) {
          Serial.print("colorHSV.h:");
          Serial.println(colorHSV.h);
          Serial.print("colorHSV.s:");
          Serial.println(colorHSV.s);
          Serial.print("colorHSV.v:");
          Serial.println(colorHSV.v);
        }
      #endif

      Event_SendLastBrightness();
      if (ControllerBrightness==0){
        Solid_RGB_Active = 0;
        Event_SolidColor(false, true, true);
      }
     
    } else if (message.sensor==cID_CYCLE_EFFECT){// is Speed dimmer Message
      if (Print_Debug) {Serial.println("message cID_CYCLE_EFFECT / V_PERCENTAGE");}
      Cycle_Effect_Speed = map(message.getLong(), 0, 100, 1, 202);
      #ifdef MY_DEBUG
        if (Print_Debug) {Serial.println(Cycle_Effect_Speed);}
        if (Print_Debug) {Serial.print("current_Light_State"); Serial.println(current_Light_State);}
      #endif
       
     
      if (message.getLong()==0){//turn cycle off if percent = 0
        Event_LightCycle(false, true, true);
        Event_SolidColor(false, false, true);
      } else {
        Event_LightCycle(true, true, true);
        Event_SolidColor(false, false, true);
      }

      Event_SendLastCycleSpeed();
     
    }
  }
  Status_Change = true;
}

///////////////////////////// ## Events ## /////////////////////////////
void Event_LightCycle(bool t, bool s, bool u) {
      #ifdef MY_DEBUG
      if (Print_Debug) {
        Serial.println("Event_LightCycle");
      }
    #endif
  Cycle_Effect_Active = (t) ? 1 : 0;
  if (u){
    send(MySensors_MSG_CYCLE_EFFECT_Status.set(Cycle_Effect_Active),MySensors_RequestACK);
  }
}
void Event_SolidColor(bool t, bool s, bool u) {
  #ifdef MY_DEBUG
      if (Print_Debug) {
        Serial.println("Event_SolidColor");
      }
    #endif
  Solid_RGB_Active = (t) ? 1 : 0;
  if (u){
    send(MySensors_MSG_RGB_Brightness_Status.set(Solid_RGB_Active),MySensors_RequestACK);
  }
}
void Event_SetLEDColors( const CRGB& rgb){
  analogWrite(PIN_RED,   rgb.r );
  analogWrite(PIN_GREEN, rgb.g );
  analogWrite(PIN_BLUE,  rgb.b );
}

void Event_SendLastColor(){
  #ifdef MY_DEBUG
      if (Print_Debug) {
        Serial.println("Event_SendLastColor");
      }
    #endif
  //String current_status=Solid_RGB_Color+String("&")+String(Solid_RGB_Brightness)+String("&")+String(Solid_RGB_Active);
  String current_status=Solid_RGB_Color;
  send(MySensors_MSG_Last_Color.set(current_status.c_str()),MySensors_RequestACK);
}

void Event_SendLastBrightness(){
  #ifdef MY_DEBUG
    if (Print_Debug) {
      Serial.println("Event_SendLastBrightness");
      }
  #endif
  //int current_brightness = map(Solid_RGB_Brightness,0,255,0,100);
  send(MySensors_MSG_RGB_Brightness.set(ControllerBrightness),MySensors_RequestACK);
}

void Event_SendLastCycleSpeed(){
  #ifdef MY_DEBUG
    if (Print_Debug) {
      Serial.println("Event_SendLastCycleSpeed");
      }
  #endif
  int CycleSpeed = map(Cycle_Effect_Speed,0,202,0,100);
  send(MySensors_MSG_CYCLE_EFFECT.set(CycleSpeed),MySensors_RequestACK);
}
void Event_RunCycleEffect(unsigned long theMills){
  #ifdef MY_DEBUG
      if (Print_Debug) {
        Serial.println("Event_RunCycleEffect");
      }
    #endif
  if (theMills - Cycle_Effect_pMillis >= Cycle_Effect_Speed){
    Cycle_Effect_pMillis = theMills;
    Cycle_Effect_Current_Hue = Cycle_Effect_Current_Hue + 1;
    Event_SetLEDColors( CHSV( Cycle_Effect_Current_Hue, 255, ControllerBrightness) );
  }
}
void Event_ColorTestBars(){// Event_ColorTestBars: flashes Red, then Green, then Blue, then Black. Helpful for diagnosing if you've mis-wired which is which.
  #ifdef MY_DEBUG
      if (Print_Debug) {
        Serial.println("Event_ColorTestBars");
      }
    #endif
  Event_SetLEDColors( CRGB::Red );   delay(500);
  Event_SetLEDColors( CRGB::Green ); delay(500);
  Event_SetLEDColors( CRGB::Blue );  delay(500);
  Event_SetLEDColors( CRGB::Black ); delay(500);
}
// ## Helper Functions
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]) : "";
}
int x2i(char *s) {
  int x = 0;
  for(;;) {
    char c = *s;
    if (c >= '0' && c <= '9') {
      x *= 16;
      x += c - '0';
    }else if (c >= 'A' && c <= 'F') {
      x *= 16;
      x += (c - 'A') + 10;
    }else {
      break;
    }
    s++;
  }
  return x;
}
char* str2char(String command){
    if(command.length()!=0){
        char *p = const_cast<char*>(command.c_str());
        return p;
    }
}
CRGB str2CRGB(String s){
  String r = str2char(s.substring(0,2));
  String g = str2char(s.substring(2,4));
  String b = str2char(s.substring(4,6));
  uint8_t red = x2i(r.c_str());
  uint8_t green = x2i(g.c_str());
  uint8_t blue = x2i(b.c_str());
  #ifdef MY_DEBUG
    if (Print_Debug) {
      Serial.print("r:");
      Serial.println(r);
      Serial.print("g:");
      Serial.println(g);
      Serial.print("b:");
      Serial.println(b);
      Serial.print("red:");
      Serial.println(red);
      Serial.print("green:");
      Serial.println(green);
      Serial.print("blue:");
      Serial.println(blue);
    }
  #endif
  CRGB colorRGB = CRGB(red, green, blue);
  return colorRGB;
}


Raspi 4 mit FHEM und CUL / Conbee2

Ranseyer

Sorry für OT Frage: Mit welcher PWM Frequenz läuft das ?  (Also ob das deutlich über dem Audobereich ist?)
FHEM mit FTUI. Homematic-Funk für Thermostate und Licht. MySensors als Basis für eigene HW.
Zentrale ist der MAPLE-CUL mit RFM69+HModUART-AddOn.
Doku zu meinen Projekten: Github/Ranseyer. Platinen falls verfügbar gerne auf Anfrage.
Support: gerne wenn ich Zeit+Lust habe im Forum. Nicht per PN!

Wiesel

Hallo Ranseyer,

Ich gehe mal davon aus, dass es die Standard PWM Frequenz des arduino pro mini  ist.
Die mysensors library ändert diese nicht und ich hab auch nichts geändert.... zumindest nicht das ich wüsste ;D

https://arduino-info.wikispaces.com/Arduino-PWM-Frequency

ZitatIf you use the default values set by the Arduino Diecimila's bootloader, these are your PWM frequencies:

Arduino Pins 5 and 6: 1kHz
Arduino Pins 9, 10, 11, and 3: 500Hz

Gruß Wiesel
Raspi 4 mit FHEM und CUL / Conbee2

Beta-User

Zitat von: Wiesel am 26 Mai 2018, 14:07:32
Das mit der Änderung im Modul habe ich jetzt nicht gemacht.
Schade eigentlich.Da aber die Präsentation eines Dimmers zum richtigen Ergebnis geführt hat, sollte das genauso gehen, wenn man gleich die V_Percentage-Readings für RGB(W)-Devices mit anlegt. Würde m.E. Sinn machen.
Meinungen von anderen dazu?
Server: HP-elitedesk@Debian 12, aktuelles FHEM@ConfigDB | CUL_HM (VCCU) | MQTT2: MiLight@ESP-GW, BT@OpenMQTTGw | MySensors: seriell, v.a. 2.3.1@RS485 | ZWave | ZigBee@deCONZ | SIGNALduino | MapleCUN | RHASSPY
svn: u.a MySensors, Weekday-&RandomTimer, Twilight,  div. attrTemplate-files