Problem with S_RGB_LIGHT



  • Hi all.

    I'm trying to set up a test sensor, with a RGB Led attached to a Nano/NRF sensor.

    For now what I have is this code borrowed from the domoticz forum, but the light doesn't show on the UI.

    Any help apreciated (and sorry for my english 😉 )

    #include <MySensor.h>
    #include <SPI.h>
    
    #define RED_PIN 3
    #define GREEN_PIN 5
    #define BLUE_PIN 6
    
    #define NODE_ID 2
    #define CHILD_ID 0
    #define SKETCH_NAME "RGB_STRIP"
    #define SKETCH_VERSION "1.0.0"
    #define NODE_REPEAT false
    
    MySensor gw;
    
    long RGB_values[3] = {0, 0, 0};
    float dimmer;
    
    void setup() {
    
    
      pinMode(RED_PIN, OUTPUT);
      pinMode(GREEN_PIN, OUTPUT);
      pinMode(BLUE_PIN, OUTPUT);
    
      gw.begin(incomingMessage, NODE_ID, NODE_REPEAT);
      gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      gw.present(CHILD_ID, S_RGB_LIGHT, "RGB Strip", false);
      gw.request(CHILD_ID, V_RGB);
    }
    
    void loop() {
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
    
      if (message.type == V_RGB) {
    
        String hexstring = message.getString();
        long number = (long) strtol( &hexstring[0], NULL, 16);
        RGB_values[0] = number >> 16;
        RGB_values[1] = number >> 8 & 0xFF;
        RGB_values[2] = number & 0xFF;
      }
      if (message.type == V_DIMMER) {
        dimmer = message.getInt();
        analogWrite(RED_PIN, int(RGB_values[0] * (dimmer / 100)));
        analogWrite(GREEN_PIN, int(RGB_values[1] * (dimmer / 100)));
        analogWrite(BLUE_PIN, int(RGB_values[2] * (dimmer / 100)));
      }
    
      if (message.type == V_LIGHT) {
        if (message.getInt() == 0) {
          digitalWrite(RED_PIN, 0);
          digitalWrite(GREEN_PIN, 0);
          digitalWrite(BLUE_PIN, 0);
    
        }
        if (message.getInt() == 1) {
          analogWrite(RED_PIN, int(RGB_values[0] * (dimmer / 100)));
          analogWrite(GREEN_PIN, int(RGB_values[1] * (dimmer / 100)));
          analogWrite(BLUE_PIN, int(RGB_values[2] * (dimmer / 100)));
        }
      }
    }
    
    

  • Plugin Developer

    @danivalencia

    Hi!

    You're not sending any initial values for each of the V_TYPES you want to use, in the end of setup. Have you read the instructions here?
    https://home-assistant.io/components/mysensors/#presentation

    Since you want to use V_RGB, V_DIMMER and V_LIGHT, you should populate all three data types. The device will show in the GUI, once you have at least populated the corresponding V_TYPE per S_TYPE that you present per sensor. In this case that's V_RGB corresponding to S_RGB_LIGHT.
    See also https://home-assistant.io/components/light.mysensors/



  • @martinhjelmare

    Thanks!

    I think I figured out the solution, but not yet fixed at all.

    I presented the three V_ types, and sent a value for each all. But now I'm dealing with the ON/OFF "order" for the light. It doesn't respond to the switch at the UI.

    #include <MySensor.h>
    #include <SPI.h>
    
    #define RED_PIN 3
    #define GREEN_PIN 5
    #define BLUE_PIN 6
    
    #define NODE_ID 2
    #define CHILD_ID 0
    #define SKETCH_NAME "RGB_LED"
    #define SKETCH_VERSION "1.0"
    
    MySensor gw;
    MyMessage msgBright(CHILD_ID, V_PERCENTAGE);
    MyMessage msgColor(CHILD_ID, V_RGB);
    MyMessage msgLight(CHILD_ID, V_LIGHT);
    long RGB_values[3] = {0, 0, 0};
    float dimmer;
    
    void setup() {
    
    
     pinMode(RED_PIN, OUTPUT);
     pinMode(GREEN_PIN, OUTPUT);
     pinMode(BLUE_PIN, OUTPUT);
    
     gw.begin(incomingMessage, NODE_ID, NODE_REPEAT);
     gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    
     gw.present(CHILD_ID, S_RGB_LIGHT);
     gw.present(CHILD_ID, S_DIMMER);
     gw.present(CHILD_ID, S_LIGHT);
    
     gw.request(CHILD_ID, V_RGB);
     gw.request(CHILD_ID, V_PERCENTAGE);
     gw.request(CHILD_ID, V_LIGHT);
    
     gw.send(msgBright.set(100));
     gw.send(msgColor.set("000000"));
     gw.send(msgLight.set(1));
    }
    
    void loop() {
     gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
    
     if (message.type == V_RGB) {
    
       String hexstring = message.getString();
       long number = (long) strtol( &hexstring[0], NULL, 16);
       RGB_values[0] = number >> 16;
       RGB_values[1] = number >> 8 & 0xFF;
       RGB_values[2] = number & 0xFF;
     }
     if (message.type == V_PERCENTAGE) {
       dimmer = message.getInt();
       analogWrite(RED_PIN, int(RGB_values[0] * (dimmer / 100)));
       analogWrite(GREEN_PIN, int(RGB_values[1] * (dimmer / 100)));
       analogWrite(BLUE_PIN, int(RGB_values[2] * (dimmer / 100)));
     }
    
     if (message.type == V_LIGHT) {
       if (message.getInt() == 0) {
         digitalWrite(RED_PIN, 0);
         digitalWrite(GREEN_PIN, 0);
         digitalWrite(BLUE_PIN, 0);
    
       }
       if (message.getInt() == 1) {
         analogWrite(RED_PIN, int(RGB_values[0] * (dimmer / 100)));
         analogWrite(GREEN_PIN, int(RGB_values[1] * (dimmer / 100)));
         analogWrite(BLUE_PIN, int(RGB_values[2] * (dimmer / 100)));
       }
     }
    }
    

    Any idea why?


  • Plugin Developer

    @danivalencia

    Yes, you are not sending back the updated state for the values after they have changed. By default home assistant will wait for feedback from the actuator for state update before updating the state in home assistant. This can be changed by setting the option optimistic to true in the config. But it's better to send back the updated state from the arduino.

    Also, you don't have to present three sensors if you only have one actual rgb light. You can have all three V_TYPES, V_RGB, V_DIMMER and V_LIGHT for the one S_TYPE, S_RGB_LIGHT.

    I'll update the docs on the web, cause I see now that the light page is not very clear about which types you actually can use.



  • @martinhjelmare

    Thanks a lot!

    So, I can remove two of the presentations, but I have to send three messages or can I send just one?

    And for the respond to the gateway, can you show me an example?

    Thanks again!


  • Plugin Developer

    @danivalencia

    I suggest still sending values for all three V_TYPES at the end of setup. But remove presentation of S_LIGHT and S_DIMMER, as you say.

    The following is the sketch I've used when I tested the code. It's based on the LED dimmer example, and I've just added a dummy RGB light actuator which will just print out the received hex string, not actually changing any RGB LEDs. So just update the relevant parts of your sketch. I think it will work if you add the update of the states, by sending in those values.

    /**
     * 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-2015 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - February 15, 2014 - Bruce Lacey
     * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
     *
     * DESCRIPTION
     * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
     * <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
     * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
     *
     * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
     * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
     * to the LED negative terminal and the MOSFET Source pin is connected to ground.
     *
     * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
     * http://www.mysensors.org/build/dimmer
     */
    
    #define SN "DimmableLED"
    #define SV "1.1"
    
    #include <MySensor.h>
    #include <SPI.h>
    
    #define CHILD_ID 1
    
    #define LED_PIN 5      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    MySensor gw;
    
    char rgb[7] = "ffffff";
    static int currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(CHILD_ID, V_PERCENTAGE);
    MyMessage lightMsg(CHILD_ID, V_STATUS);
    MyMessage rgbMsg(CHILD_ID, V_RGB);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()
    {
      Serial.println(SN);
      gw.begin(incomingMessage);
    
      // Register the LED Dimmable Light with the gateway
      gw.present(CHILD_ID, S_RGB_LIGHT);
    
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request(CHILD_ID, V_PERCENTAGE);
      gw.wait(2000);
      gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
      gw.send(dimmerMsg.set(currentLevel));
      gw.send(rgbMsg.set(rgb));
    }
    
    /***
     *  Dimmable LED main processing loop
     */
    void loop()
    {
      gw.process();
      //gw.wait(5000);
      //gw.request(CHILD_ID, V_PERCENTAGE);
    }
    
    
    
    void incomingMessage(const MyMessage &message) {
      if (message.type == V_RGB) {
        // Not implemented, just a dummy print.
        String hexstring = message.getString();
        hexstring.toCharArray(rgb, sizeof(rgb));
        Serial.print("Changing color to ");
        Serial.println(rgb);
        gw.send(rgbMsg.set(rgb));
      }
    
      if (message.type == V_STATUS || message.type == V_PERCENTAGE) {
    
        //  Retrieve the power or dim level from the incoming request message
        int requestedLevel = atoi(message.data);
    
        // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
        requestedLevel *= (message.type == V_STATUS ? 100 : 1);
    
        // Clip incoming level to valid range of 0 to 100
        requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
        requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
        Serial.print("Changing level to ");
        Serial.print(requestedLevel);
        Serial.print(", from ");
        Serial.println(currentLevel);
    
        fadeToLevel(requestedLevel);
    
        // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
        gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
    
        // hek comment: Is this really nessesary?
        gw.send(dimmerMsg.set(currentLevel));
    
    
        }
    }
    
    /***
     *  This method provides a graceful fade up/down effect
     */
    void fadeToLevel(int toLevel) {
    
      int delta = (toLevel - currentLevel) < 0 ? -1 : 1;
    
      while (currentLevel != toLevel) {
        currentLevel += delta;
        analogWrite(LED_PIN, (int)(currentLevel / 100. * 255));
        delay(FADE_DELAY);
      }
    }
    


  • @martinhjelmare

    I tried your sketch as is, just to try one leg for the RGB Led, but I'm not getting the expected result. When I change the brightness from the HA UI slider, the node gets the correct value, but...

    send: 12-12-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 12-12-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.3
    send: 12-12-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    sensor started, id=12, parent=0, distance=1
    send: 12-12-0-0 s=1,c=0,t=26,pt=0,l=0,sg=0,st=ok:
    send: 12-12-0-0 s=255,c=3,t=11,pt=0,l=11,sg=0,st=ok:DimmableLED
    send: 12-12-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.1
    send: 12-12-0-0 s=1,c=2,t=3,pt=0,l=0,sg=0,st=ok:
    send: 12-12-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=ok:0
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:0
    send: 12-12-0-0 s=1,c=1,t=40,pt=0,l=6,sg=0,st=ok:ffffff
    read: 0-0-12 s=1,c=1,t=2,pt=0,l=1,sg=0:1
    Changing level to 100, from 0
    send: 12-12-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=ok:1
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    read: 0-0-12 s=1,c=1,t=3,pt=0,l=2,sg=0:17   // <- Here I modified brightness
    Changing level to 100, from 100
    send: 12-12-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=ok:1
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    read: 0-0-12 s=1,c=1,t=3,pt=0,l=1,sg=0:9  // <- Here too
    Changing level to 100, from 100
    send: 12-12-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=ok:1
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    

    As you see, no matter what level receives, It goes to 100 forever.
    And if you try to move the brightness slider more over to the right (or the left to reduce), the values sometimes get higher than 100 (or -100), despite of the "clipping" method of the sketch.

    EDIT:

    Made a few changes to the code, to find what's failing, and I think is the way HA sends commands to the node;

    ...
    
      if (message.type == V_DIMMER) {
    
        //  Retrieve the power or dim level from the incoming request message
        int requestedLevel = atoi(message.data);
        Serial.print("Received: ");
        Serial.println(message.data);
        Serial.print("Bright: ");
        Serial.println(requestedLevel);
    ...
    

    And the log from the sensor:

    send: 12-12-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 12-12-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.3
    send: 12-12-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    sensor started, id=12, parent=0, distance=1
    send: 12-12-0-0 s=1,c=0,t=26,pt=0,l=0,sg=0,st=ok:
    send: 12-12-0-0 s=255,c=3,t=11,pt=0,l=11,sg=0,st=ok:DimmableLED
    send: 12-12-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.1
    send: 12-12-0-0 s=1,c=2,t=3,pt=0,l=0,sg=0,st=ok:
    read: 0-0-12 s=1,c=1,t=3,pt=0,l=3,sg=0:100
    Received: 100mableLED
    Bright: 100
    Changing level to 100, from 0
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    send: 12-12-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=ok:1
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    send: 12-12-0-0 s=1,c=1,t=40,pt=0,l=6,sg=0,st=ok:ffffff
    read: 0-0-12 s=1,c=1,t=3,pt=0,l=2,sg=0:45
    Received: 450mableLED
    Bright: 450
    Changing level to 100, from 100
    send: 12-12-0-0 s=1,c=1,t=3,pt=2,l=2,sg=0,st=ok:100
    

    Seems there is a string of the message (sketch name maybe) messing with the brightness data.


  • Plugin Developer

    @danivalencia

    I think this was a bug in mysensors 1.5.2 - 1.5.3. Can you try version 1.5.4?



  • @martinhjelmare

    And there was the fail !! :s

    Installed 1.5.4 and now it runs in the right manner.

    Thank you!!


  • Plugin Developer


Log in to reply
 

Suggested Topics

  • 1
  • 8
  • 5
  • 3
  • 1

15
Online

11.4k
Users

11.1k
Topics

112.7k
Posts