double RelyWithButonActuator not working



  • still strugling with multiple relais without using a class. in my code I duplicate the example of MySensors. testing the 2 relais both activate of the 2 defined buttons. so clicking one button, both relais activate, the same with the other button. How can I get it work and what is wrong in the code? If I test only one relay with one button that works fine.

    /**
     * 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 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch for a "light switch" where you can control light or something 
     * else from both HA controller and a local physical button 
     * (connected between digital pin 3 and GND).
     * This node also works as a repeader for other nodes
     * http://www.mysensors.org/build/relay
     */ 
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY1_PIN  4  // Arduino Digital I/O pin number for relay 
    #define BUTTON1_PIN  3  // Arduino Digital I/O pin number for button 
    #define CHILD_ID_1 1   // Id of the sensor child
    #define RELAY2_PIN  6  // Arduino Digital I/O pin number for relay 
    #define BUTTON2_PIN  5  // Arduino Digital I/O pin number for button 
    #define CHILD_ID_2 2   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer1 = Bounce(); 
    int oldValue1=0;
    bool state1;
    Bounce debouncer2 = Bounce();
    int oldValue2=0;
    bool state2;
    MySensor gw;
    MyMessage msg1(CHILD_ID_1,V_LIGHT);
    MyMessage msg2(CHILD_ID_2,V_LIGHT);
    
    void setup()  
    {  
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay & Button", "1.0");
    
     // Setup the button
      pinMode(BUTTON1_PIN,INPUT);
      digitalWrite(BUTTON1_PIN,HIGH);
      pinMode(BUTTON2_PIN,INPUT);
      digitalWrite(BUTTON2_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer1.attach(BUTTON1_PIN);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON2_PIN);
      debouncer2.interval(5);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_1, S_LIGHT);
      digitalWrite(RELAY1_PIN, RELAY_OFF);
      pinMode(RELAY1_PIN, OUTPUT);
      
      gw.present(CHILD_ID_2, S_LIGHT);
      digitalWrite(RELAY2_PIN, RELAY_OFF);
      // Then set relay pins in output mode  
      pinMode(RELAY2_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state1 = gw.loadState(CHILD_ID_1);
      digitalWrite(RELAY1_PIN, state1?RELAY_ON:RELAY_OFF);
      state2 = gw.loadState(CHILD_ID_2);  
      digitalWrite(RELAY2_PIN, state2?RELAY_ON:RELAY_OFF);
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer1.update();
      // Get the update value
      int value1 = debouncer1.read();
      if (value1 != oldValue1 && value1==0) {
          gw.send(msg1.set(state1?false:true), true); // Send new state and request ack back
      }
      oldValue1 = value1;
    
        debouncer2.update();
      // Get the update value
      int value2 = debouncer2.read();
      if (value2 != oldValue2 && value2==0) {
          gw.send(msg2.set(state2?false:true), true); // Send new state and request ack back
      }
      oldValue2 = value2;
    } 
     
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state1 = message.getBool();
         digitalWrite(RELAY1_PIN, state1?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID_1, state1);
    
    if (message.type == V_LIGHT) {
         state2 = message.getBool();
         digitalWrite(RELAY2_PIN, state2?RELAY_ON:RELAY_OFF);
         gw.saveState(CHILD_ID_2, state2);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    }
    

  • Hero Member

    @Dick in your receive code you are not testing for which child the message is for, so both are being switched.
    You need to test what child the incoming message is for using message.sensor

    Try something like below.

    Also is there any reason you are still using MySensors V1.x

    if (message.type == V_LIGHT) {
      switch (message.sensor) {
         case CHILD_ID_1:
             // Change relay state
            state1 = message.getBool();
            digitalWrite(RELAY1_PIN, state1?RELAY_ON:RELAY_OFF);
           // Store state in eeprom
           gw.saveState(CHILD_ID_1, state1);
          break;
      case CHILD_ID_2:
          state2 = message.getBool();
         digitalWrite(RELAY2_PIN, state2?RELAY_ON:RELAY_OFF);
         gw.saveState(CHILD_ID_2, state2);
         break;
      default:
            Serial.print("Incoming sensor unknown");
        }
    }
    


  • Thanks for the solution, I tested it and works fine now. In Answer to your question why I use the old version of MySensors, at the moment I am re-configurating my ne RP with the latest version of Domoticz. After that I will connect the latest MYsensors Gateway to it. If all the nodes keep on working on the new environment, there is not an immediate need to adjust the code of all my nodes. Is my approach correct? if not please reply.


Log in to reply
 

Suggested Topics

  • 4
  • 9
  • 9
  • 2
  • 2
  • 8

54
Online

11.4k
Users

11.1k
Topics

112.6k
Posts