Combine DimmableLEDActuator sketch with RelayActuator Sketch



  • HI all,

    i trying to combine this dimmerLEDactuator with Relay actuator to control another Light, but relays seem not working and when i push on to relay, LED also turn on. here my code:

    /**
     * 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 "DimmableLEDRelay"
    #define SV "1.1"
    
    #define NODE_ID 1
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define LED_CHILD 0
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    #define RELAY_PIN  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_CHILD 1
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    MySensor gw;
    
    MyMessage relayMsg(RELAY_CHILD,V_STATUS);
    
    static int currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(0, V_DIMMER);
    MyMessage lightMsg(0, V_LIGHT);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      //Serial.println( SN ); 
      gw.begin(incomingMessage, NODE_ID);
     
      
      // Register the LED Dimmable Light with the gateway
      gw.present(LED_CHILD, S_DIMMER );
      gw.present(RELAY_CHILD, S_LIGHT);
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( 0, V_DIMMER );
    
       // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT); 
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    
    
    void incomingMessage(const MyMessage &message) {
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
        
        //  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_LIGHT ? 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 );
    
        // Change relay state
         digitalWrite(RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
        
        // 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 );
      }
    }```


  • @rsaeful said:

    if (message.type == V_LIGHT || message.type == V_DIMMER) {

    I think that you must to separate the both input messages in two anidate if, like:

    if (message.type == V_LIGHT) {
    // Here the code for the relay
    }else if ( message.type == V_DIMMER) {
    // Here the code for the dimmer
    }

    I hope that it will be useful for you



  • @logger02 Thank you for your reply, already change as your recommendation, but now LED not working (on), relay now working. when i push bulb icon for LED on domoticz, Relay will on and off. hmm i think this only how we write the logic, but still figure out how to 🙂



  • Could you paste the code?



  • @logger02 her the code

    #define SN "DimmableLEDRelay"
    #define SV "1.1"
    
    #define NODE_ID 1
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define LED_CHILD 0
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    #define RELAY_PIN  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_CHILD 1
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    MySensor gw;
    
    MyMessage relayMsg(RELAY_CHILD,V_STATUS);
    
    static int currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(0, V_DIMMER);
    MyMessage lightMsg(0, V_LIGHT);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      //Serial.println( SN ); 
      gw.begin(incomingMessage, NODE_ID);
     
      
      // Register the LED Dimmable Light with the gateway
      gw.present(LED_CHILD, S_DIMMER );
      gw.present(RELAY_CHILD, S_LIGHT);
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( 0, V_DIMMER );
    
       // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT); 
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    
    
    void incomingMessage(const MyMessage &message) {
      if (message.type == V_DIMMER) {
        
        //  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_LIGHT ? 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) );
    
        }
        else (message.type == V_LIGHT);{
        // Change relay state
         digitalWrite(RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
        
        } 
       
    }
    
    /***
     *  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 );
      }
    }
    

    appreciate your help


  • Contest Winner

    @rsaeful You need to handle messages separately as suggested by @logger02. To do this you first have to make a distinct in the sensor for which you receive a message. Then the type of message.

    void incoming( const MyMessage &message ) {
      if ( message.sensor ==  LED_CHILD && !message.isAck() ) {
        if ( message.type == V_LIGHT ) {
          // handle VLIGHT
          if ( message.getBool() == true ) {
            // turn on led
          }
          else {
             // turn off led
          }
        }
        else if ( message.type == V_DIMMER ) {
           // handle dimmer (look at build examples)
        }
      }
      else if ( message.sensor == RELAY_CHILD && !message.isAck() ) {
        // look for message type and handle RELAY state (you might need to do inverted on/off 
      } 
      
    }
    

    Hope it compiles and or helps. Just typed it in here.


  • Contest Winner

    @rsaeful also better to handle fade code in your main loop. Too much logic in message handler might mess up communication to that node.



  • @TheoL i will try to re-write the code and report back here. thank you so much



  • @TheoL almost there, Relay working well, Dimmer is also good but for turn off and on LED light still not yet is still on event i push off or set to minimum level, already 2.20 AM here, better take nap first. i need to understand more about messaging and Little difficult as i not fully understand programing. once again thank you to you all, almost go there.

    my last code:

    #define SN "DimmableLEDRelay"
    #define SV "1.1"
    
    #define NODE_ID 3
    
    #include <MySensor.h> 
    #include <SPI.h>
    
    #define LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
    #define LED_CHILD 0
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    #define LIGHT_OFF 0
    #define LIGHT_ON 1
    
    #define RELAY_PIN  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_CHILD 1
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    MySensor gw;
    
    MyMessage relayMsg(RELAY_CHILD,V_LIGHT);
    
    static int currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(LED_CHILD, V_DIMMER);
    MyMessage lightMsg(LED_CHILD, V_LIGHT);
    
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      //Serial.println( SN ); 
      gw.begin(incomingMessage, NODE_ID);
     
      
      // Register the LED Dimmable Light with the gateway
      gw.present(LED_CHILD, S_DIMMER );
      gw.present(RELAY_CHILD, S_LIGHT);
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( 0, V_DIMMER );
    
       // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT); 
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();
    }
    
    
    
    void incomingMessage(const MyMessage &message) {
      if ( message.sensor ==  LED_CHILD && !message.isAck() ) {
        if ( message.type == V_LIGHT ) {
          // handle VLIGHT
          Serial.println( "V_LIGHT command received..." );
        
       
          if ( message.getBool() == true ) {
               // turn on led
               // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                  int lightState = message.getString()[0] == '1';
                  //int newLevel = 0;
                  lightState==LIGHT_ON   ;     
              }
          else {
                // turn off led
                   int lightState = message.getString()[0] == '1';
                   lightState==LIGHT_OFF;  
                   //gw.send(dimmerMsg.set(0), true);
          }
        }
        else if ( message.type == V_DIMMER ) {
           // handle dimmer (look at build examples)
            //  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_LIGHT ? 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) );
        }
      }
      else if ( message.sensor == RELAY_CHILD && !message.isAck() ) {
        // look for message type and handle RELAY state (you might need to do inverted on/off 
        digitalWrite(RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
      } 
      
    
       
    }
    
    /***
     *  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 );
      }
    }
    
    

  • Contest Winner

    @rsaeful
    better use gw.wait() i.s.o. of delay. during the gw.wait() you are still receiving and sending MyS messages. delay is blocking

    So the function will look like this

    void fadeToLevel( int toLevel ) {
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
        while ( currentLevel != toLevel ) {
         currentLevel += delta;
         analogWrite( LED_PIN, (int)(currentLevel * 2.55));
         gw.wait( FADE_DELAY );
      }
    }
    

Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 5
  • 8
  • 6

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts