Synchronising Light switch


  • Hero Member

    I have a 12v system in my house that is used for some of the lighting so wanted to let Mysensors take control of the nightlights that until now i have had to switch on manually. There were a few things that needed to be achieved with these nodes to make them acceptable for use.

    1. I had to retain hardware switch so the lights could still be switched from the local wall plate.

    2. The hardware switch had to remain functional even if the gateway or controller were not available. we live in a semi rural area where there are few street lights so the 12v lighting has to remain operational during power outages etc.

    3. The node should make an effort to keep the controller in sync with its current state. During first boot up and when a broken up-link has been re-established.

    The code is working with Domoticz and so far has responded well to all conditions stated above.
    it uses request to check with the controllers state and sends a message if needed to sync.
    A momentary push button is used for the switch.

    /*
       Relay with button sketch
       modified to work with no uplink
       to gateway and try to maintain sync to controller
    */
    
    
    #define MY_DEBUG                               // Enable debug prints to serial monitor
    
    #define MY_RADIO_NRF24                         // Enable and select radio type attached
    
    #define MY_TRANSPORT_WAIT_READY_MS 5000        //set how long to wait for transport ready in milliseconds
    
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN  4      // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  3     // Arduino Digital I/O pin number for button 
    #define CHILD_ID 1        // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce();
    int oldValue = 0;
    bool uplinkAvailable = true;
    bool state = false;
    bool requestState;
    bool firstStart = true;
    unsigned long uplinkCheckTime ;                // holder for uplink checks
    unsigned long uplinkCheckPeriod = 30*1000;     // time between checks for uplink in milliseconds
    unsigned long returnWait = 1500;               // how long to wait for return from controller in milliseconds
    MyMessage msg(CHILD_ID, V_STATUS);
    
    void setup(){
      pinMode(BUTTON_PIN, INPUT_PULLUP);           // Setup the button pin, Activate internal pull-up
      
      debouncer.attach(BUTTON_PIN);                // After setting up the button, setup debouncer
      debouncer.interval(5);
    
      pinMode(RELAY_PIN, OUTPUT);                 // set relay pin in output mode
      digitalWrite(RELAY_PIN, RELAY_OFF);         // Make sure relay is off when starting up
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Button", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_BINARY);
    }
    
    
    void loop(){
      if (firstStart) {                            // this code is only run once at startup
        Serial.println("First run started");
        if (request( CHILD_ID, V_STATUS)) {       // request the current state of the switch on the controller and check that an ack was received
          Serial.println("uplink available");
          wait (returnWait);                      //wait needed to allow request to return from controller
          Serial.print("controller state --- ");
          Serial.println(requestState);
          if (requestState != state) {           // check that controller is corectly showing the current relay state
            send(msg.set(state), false);         // notify controller of current state
          }
        }
        else {
          Serial.println("uplink not available");
          uplinkAvailable = false;               // no uplink established
          uplinkCheckTime = millis();
        }
        firstStart = false;                     // set firstStart flag false to prevent code from running again
      }
    
      debouncer.update();
      int value = debouncer.read();                               // Get the update value
      if (value != oldValue && value == 0) {                      // check for new button push
        state =  !state;                                          // Toggle the state
        digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state
        if (!send(msg.set(state), true)) {                        //Attempt to notify controller of changed state
          Serial.println("uplink not available");
          uplinkAvailable = false;                                // no uplink available
          uplinkCheckTime = millis();                             
        }
      }
     oldValue = value;
    
      if (!uplinkAvailable && (millis() - uplinkCheckTime > uplinkCheckPeriod) ) {       // test to see if function should be called
        uplinkCheck();                                                                  // call uplink checking function
      }
    
    }
    
    /*-------------------start of functions--------------------------*/
    
    void receive(const MyMessage &message) {
      if (message.type == V_STATUS) {                                   // check to see if incoming message is for a switch
        switch (message.getCommand()) {                                 // message.getCommand will give us the command type of the incomming message
          case C_SET:                                                   //message is a set command  from controller to update relay state
            state = message.getBool();                                  // get the new state
            digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to new state
            uplinkAvailable = true;                                     //  uplink established
            /*---- Write some debug info----*/
            Serial.print("Incoming change for sensor:");
            Serial.print(message.sensor);
            Serial.print(", New status: ");
            Serial.println(message.getBool());
            break;
          case C_REQ:                                               // message is a returning request from controller
            requestState = message.getBool();                       // update requestState with returning state
            break;
        }
      }
    }
    
    void uplinkCheck() {
      if (request( CHILD_ID, V_STATUS)) {         // request the current state of the switch on the controller and check that the request was succsessful
        Serial.println("uplink re-established");
        wait (returnWait);                        //wait needed to allow request to return from controller
        if (requestState != state) {              // check that controller is corectly showing the current relay state
          send(msg.set(state), false);            // notify controller of current state no ack
          uplinkAvailable = true;                 //  uplink established
        }
      }
      uplinkCheckTime = millis();                // reset the checktime
      Serial.println("uplinkchecktime reset");
    }
    
    
    

    The wiring
    0_1496469040524_relay circuit.jpg



  • cool.
    nothing too much in code, nor in wiring.
    :+1:


  • Hero Member

    @core_c Yes I did not want it to be overly complicated or to be constantly pinging the network either.
    It is a fairly good compromise the node only starts to ping once it has determined the uplink is broken and you can decide the interval by changing uplinkCheckPeriod. At the moment it is set to 30 seconds.

    In testing the return from request usually arrived within 800 milliseconds so returnWait could also be reduced from 1500ms if it was a concern. It is only used when the node is first reconnected so felt I would rather make sure I got the correct data by adding an extra delay.


Log in to reply
 

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