RelayWithButtonActuator



  • I try to use Relay With Button Actuator 1.4 but I have problem. When i press button everything working well. Problem is when I restart node and want to change status form gateway, it doesn't work then. When i press button only once then i can remote from getaway.
    My code:

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN_2  8
    #define RELAY_PIN  7 // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN_2  4
    #define BUTTON_PIN  3 // Arduino Digital I/O pin number for button 
    #define CHILD_ID_2 2
    #define CHILD_ID 1 // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    Bounce debouncer2 = Bounce(); 
    int oldValue2=0;
    bool state2;
    
    
    
    MySensor gw;
    MyMessage msg(CHILD_ID,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("Double Relay & Button", "0.1");
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
       // Setup the button
      pinMode(BUTTON_PIN_2,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_2,HIGH);
      
      
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      
      debouncer2.attach(BUTTON_PIN_2);
      debouncer2.interval(5);
      
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_LIGHT);
      gw.present(CHILD_ID_2, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
      
      digitalWrite(RELAY_PIN_2, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_2, OUTPUT);  
      
      
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
      
      state2 = gw.loadState(CHILD_ID_2);
      digitalWrite(RELAY_PIN_2, state2?RELAY_ON:RELAY_OFF);
      
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer.update();
      debouncer2.update();
      // Get the update value
      int value = debouncer.read();
      int value2 = debouncer2.read();
      
      if (value != oldValue && value==0) {
          gw.send(msg.set(state?false:true), true); // Send new state and request ack back
      }
      oldValue = value;
      
      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 && message.sensor == 1 && strlen(msg.getString()) != 0) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID, state);
       }
      else if(message.type == V_LIGHT && message.sensor == 2 && strlen(msg.getString()) != 0){  
        
         state2 = message.getBool();
         digitalWrite(RELAY_PIN_2, state2?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         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());
       } 
    }
    


  • I prepare sketch with 2 x button and 2 x relays and 8 x dallas. Could some one look at if it right, because i have problem remote relays after restart node. I don't know it is problem witch sketch or server software.

    Log from gataway:

    12-10-2014 18:57:55: 0;0;3;0;9;send: 0-0-2-2 s=11,c=1,t=2,pt=0,l=1,st=ok:0
    12-10-2014 18:57:55: 0;0;3;0;9;send: 0-0-2-2 s=11,c=1,t=2,pt=0,l=1,st=ok:1
    12-10-2014 18:57:54: 0;0;3;0;9;send: 0-0-2-2 s=12,c=1,t=2,pt=0,l=1,st=ok:0
    12-10-2014 18:57:54: 0;0;3;0;9;send: 0-0-2-2 s=12,c=1,t=2,pt=0,l=1,st=ok:1
    12-10-2014 18:57:53: 0;0;3;0;9;send: 0-0-2-2 s=11,c=1,t=2,pt=0,l=1,st=ok:0
    12-10-2014 18:57:52: 0;0;3;0;9;send: 0-0-2-2 s=11,c=1,t=2,pt=0,l=1,st=ok:1
    12-10-2014 18:54:39: 0;0;3;0;9;read: 2-2-0 s=0,c=1,t=0,pt=7,l=5:22.6
    12-10-2014 18:54:26: 0;0;3;0;9;send: 0-0-203-2 s=11,c=1,t=2,pt=0,l=1,st=fail:0
    12-10-2014 18:54:26: 0;0;3;0;9;send: 0-0-203-2 s=12,c=1,t=2,pt=0,l=1,st=fail:0
    12-10-2014 18:54:26: 0;0;3;0;9;send: 0-0-203-2 s=12,c=1,t=2,pt=0,l=1,st=fail:1
    12-10-2014 18:54:25: 0;0;3;0;9;send: 0-0-203-2 s=11,c=1,t=2,pt=0,l=1,st=fail:1

    Code:

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define RELAY_PIN_2  8
    #define RELAY_PIN  7 // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN_2  4
    #define BUTTON_PIN  3 // Arduino Digital I/O pin number for button 
    #define CHILD_ID_2 12
    #define CHILD_ID 11 // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    Bounce debouncer2 = Bounce(); 
    int oldValue2=0;
    bool state2;
    
    
    //temp
    #define ONE_WIRE_BUS 5 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 8
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    unsigned long SEND_FREQUENCY = 30000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
    unsigned long lastSend;
    unsigned long sendTime;
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    
    MySensor gw;
    
    //temp
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    
    
    MyMessage msg(CHILD_ID,V_LIGHT);
    MyMessage msg2(CHILD_ID_2,V_LIGHT);
    MyMessage msg3(0,V_TEMP);
    
    
    
    void setup()  
    {  
    
       // Startup OneWire 
      sensors.begin();
    
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Double Relay & Button, Temp", "0.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         gw.present(i, S_TEMP);
      }
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
       // Setup the button
      pinMode(BUTTON_PIN_2,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_2,HIGH);
      
      
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      
      debouncer2.attach(BUTTON_PIN_2);
      debouncer2.interval(5);
      
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_LIGHT);
      gw.present(CHILD_ID_2, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
      
      digitalWrite(RELAY_PIN_2, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN_2, OUTPUT);  
      
      
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
      
      state2 = gw.loadState(CHILD_ID_2);
      digitalWrite(RELAY_PIN_2, state2?RELAY_ON:RELAY_OFF);
      
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer.update();
      debouncer2.update();
      // Get the update value
      int value = debouncer.read();
      int value2 = debouncer2.read();
      
      if (value != oldValue && value==0) {
          gw.send(msg.set(state?false:true), true); // Send new state and request ack back
      }
      oldValue = value;
      
      if (value2 != oldValue2 && value2==0) {
          gw.send(msg2.set(state2?false:true), true); // Send new state and request ack back
      }
      oldValue2 = value2;  
    
      unsigned long now = millis();
      // Only send values at a maximum frequency or woken up from sleep
      if ((now - lastSend) > SEND_FREQUENCY) {
         Serial.print("dziala wysylanie temp");
        // Fetch temperatures from Dallas sensors
        sensors.requestTemperatures(); 
    
        // Read temperatures and send them to controller 
        for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
       
          // Fetch and round temperature to one decimal
          float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
       
          // Only send data if temperature has changed and no error
          if (lastTemperature[i] != temperature && temperature != -127.00) {
       
            // Send in the new temperature
            gw.send(msg3.setSensor(i).set(temperature,1));
            lastTemperature[i]=temperature;
          }
        }
      lastSend=now;
      }
    } 
     
    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 && message.sensor == 11) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID, state);
       }
      else if(message.type == V_LIGHT && message.sensor == 12){  
        
         state2 = message.getBool();
         digitalWrite(RELAY_PIN_2, state2?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         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());
       } 
    }
    


  • I have the same problem even with the one button . I can't control the light from Vera unless I have push the button after the restart . Also I noticed when i use the button to switch the light on and off from the button in Vera i can't see the status being updated . Any one experiencing the same problem ?


  • Contest Winner

    it is hard to read your code.

    edit it putting it between three back ticks (```)

    usually to the left of the 1 key on a USA keyboard



  • I second that. I have exactly the same issue. I have to physically toggle the switch before operating it from OpenHab.



  • Hello All,

    i got the same problem, node can send information to gateway but can't receive command from controller.
    do you know how to test communication between Gateway and Nodes?
    I used Serial Gateway, i connected to gateway COM port and send command like 10;2;1;0;1;1;1\n to turn on relay but my node can't turn relay on.
    When i press the button from NODE, it sent the update status to gateway,


  • Hardware Contributor

    Gateway not see ACK flag. Please fix this bag
    Lib version 1.5, Node & gate = 1.5

    RelayButton log

    send: 42-42-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    Button value:0
    Button value:1
    send: 42-42-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    Button value:0
    Button value:1
    send: 42-42-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    Button value:0
    Button value:1
    send: 42-42-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    Button value:0
    Button value:1
    send: 42-42-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:0
    Button value:0
    Button value:1
    send: 42-42-0-0 s=1,c=1,t=2,pt=2,l=2,sg=0,st=fail:1
    

    Gateway log

    2015-08-04 18:52:07 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:0
    2015-08-04 18:52:07 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:1
    2015-08-04 18:52:07 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:0
    2015-08-04 18:52:07 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:1
    2015-08-04 18:52:08 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:0
    2015-08-04 18:52:08 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:1
    2015-08-04 18:52:08 Set: Node:42; Sensor:1; Type:1; Ack:0; Sub:2; Msg:0
    

  • Admin

    That's not a gateway log...


  • Hardware Contributor

    Sory. Bad NRF transmitter


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.