Navigation

    • Register
    • Login
    • Search
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. Cameron Payne
    3. Posts
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Posts made by Cameron Payne

    • Issue with Relay + Scene selector sketch

      Hey all ,
      Im having a strange issue, i combined the relay and scene sector sketch and everything works well the buttons trigger and the scene and i can control the relay. The only problem is that when i control the relay ( In home Assistant ) it also activates the scene 😕
      I have attached my sketch and home assistant automation
      Any help would be greatly appreciated 😄

      Sketch

      // Override Setting for Manual Node ID to 2
      #define MY_NODE_ID 15
      
      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      
      // Enable repeater functionality for this node
      #define MY_REPEATER_FEATURE
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <Keypad.h>
      
      #define RELAY_1  3          // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 2  // Total number of attached relays: 4
      
      // Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
      #define RELAY_ON 0          // GPIO value to write to turn on attached relay
      #define RELAY_OFF 1         // GPIO value to write to turn off attached relay
      #define SN "Scene Controller"
      #define SV "2.0"
      #define KEYPAD_CHILD_ID 95
      
      bool initialValueSent = false;
      
      //Init MyMessage for Each Child ID
      MyMessage msg1(1, V_LIGHT);
      MyMessage msg2(2, V_LIGHT);
      MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
      MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
      
      const byte ROWS = 4; //four rows
      const byte COLS = 1; //one column
      char keys[ROWS][COLS] = {
        {'1'},
        {'2'},
        {'3'},
        {'4'}
      };
      
      byte rowPins[ROWS] = {A3, A4, A1, A2}; //connect to the row pinouts of the keypad
      byte colPins[COLS] = {A5}; //connect to the column pinouts of the keypad
      
      Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
      byte lastState;
      
      void before() { 
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);   
          // Set relay to last known state (using eeprom storage) 
          digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
      }
      
      void setup() {
        
      }
      
      void presentation()  
      {   
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Relay", "1.0");
      
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
          // Register all sensors to gw (they will be created as child devices)
          present(sensor, S_LIGHT);
          present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
          keypad.addEventListener(keypadEvent);
        }
      }
      
      
      void loop() 
      {
        if (!initialValueSent) {
          Serial.println("Sending initial value");
          send(msg1.set(loadState(1)?RELAY_OFF:RELAY_ON),true);
          wait(1000);
          send(msg2.set(loadState(2)?RELAY_OFF:RELAY_ON),true);
          wait(1000);
          Serial.println("Sending initial value: Completed");
          wait(5000);
        }
        {
        char key = keypad.getKey();
      }
      }
      
      void receive(const MyMessage &message) {
        Serial.println("=============== Receive Start =======================");
        if (message.isAck()) {
           Serial.println(">>>>> ACK <<<<<");
           Serial.println("This is an ack from gateway");
           Serial.println("<<<<<< ACK >>>>>>");
        }
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LIGHT) {
          Serial.println(">>>>> V_LIGHT <<<<<");
          if (!initialValueSent) {
            Serial.println("Receiving initial value from controller");
            initialValueSent = true;
          }
           // Update relay state to HA
           digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
           switch (message.sensor) {
              case 1:
                Serial.print("Incoming change for sensor 1");
                send(msg1.set(message.getBool()?RELAY_OFF:RELAY_ON));
                break;
              case 2:
                Serial.print("Incoming change for sensor 2");
                send(msg2.set(message.getBool()?RELAY_OFF:RELAY_ON));
                break;                   
              default: 
                Serial.println("Default Case: Receiving Other Sensor Child ID");
              break;
           }
           // Store state in Arduino eeprom
           saveState(message.sensor, message.getBool());
           Serial.print("Saved State for sensor: ");
           Serial.print( message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
           Serial.println("<<<<<< V_LIGHT >>>>>>");
         } 
         Serial.println("=============== Receive END =======================");
        
      }
      void keypadEvent(KeypadEvent key) {
        switch (keypad.getState()) {
      
          case PRESSED:
            lastState = 1;
            break;
      
          case HOLD:
            lastState = 2;
            break;
      
          case RELEASED:
            int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
            if (lastState == 2) {
              //keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
              send(scene2.set(keyInt));
            } else {
            send(scene.set(keyInt));
            }
            //break;
       }
      }```
      
      Home Assistant 
      
      

      automation:

      • alias: "scene selector"
        trigger:

        • platform: state
          entity_id: sensor.relay_15_95
          to: '1'
          action:
        • service: light.turn_off
          data:
          entity_id: light.cams_room_tv
      • alias: "scence selector2"
        trigger:

        • platform: state
          entity_id: sensor.relay_15_95_2
          to: '1'
          action:
        • service: light.turn_on
          data:
          entity_id: light.cams_room_tv
      posted in Troubleshooting
      Cameron Payne
      Cameron Payne
    • 2x Relay, 1x Reed Switch on 1 node for Home Assistant

      Hey!
      N00b ahead warning!

      I had a lot of trouble trying to get 2 relays and a reed switch working on one node in Home Assistant, but after hours of trying i finally got some code that works

      It's very messy and is a big mash of sample code and other projects code but it works.

      Would be nice if it would remember the state after a power off but that is way beyond me

      If anyone ends up using this and cleans it up feel free to post it back here 😛

      Thanks!

      /*
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * http://www.mysensors.org/build/relay
       */
      
      #define MY_DEBUG
      #define MY_RADIO_NRF24
      #define MY_REPEATER_FEATURE
      #define MY_NODE_ID 43
      #include <SPI.h>
      #include <MySensors.h>
      #include <Bounce2.h>
      
      #define RELAY_PIN1  3
      #define RELAY_PIN2  4
      #define BUTTON_PIN1  6 
      #define CHILD_ID1 1
      #define CHILD_ID2 2
      #define CHILD_ID3 3
      #define RELAY_ON 1
      #define RELAY_OFF 0
       
      
      Bounce debouncer = Bounce();
      bool state = false;
      bool state2 = false;
      bool initialValueSent = false;
      int oldValue=-1;
      
      MyMessage msg(CHILD_ID1, V_STATUS);
      MyMessage msg2(CHILD_ID2, V_STATUS);
      MyMessage msg3(CHILD_ID3,V_TRIPPED);
      
      void setup()
      {
      
        // Make sure relays are off when starting up
        digitalWrite(RELAY_PIN1, RELAY_OFF);
        digitalWrite(RELAY_PIN2, RELAY_OFF);
        pinMode(RELAY_PIN1, OUTPUT);
        pinMode(RELAY_PIN2, OUTPUT);
        pinMode(BUTTON_PIN1,INPUT);
        
        // Activate internal pull-up
        digitalWrite(BUTTON_PIN1,HIGH);
      
        // After setting up the button, setup debouncer
        debouncer.attach(BUTTON_PIN1);
        debouncer.interval(5);
      }
      
      void presentation()  {
        sendSketchInfo("Relay+Reed", "1.0");
        wait(100);
        present(CHILD_ID1, S_BINARY);
        wait(100);
        present(CHILD_ID2, S_BINARY);
        wait(100);
        present(CHILD_ID3, S_DOOR); 
      }
      
      void loop()
      {
        if (!initialValueSent) {
          Serial.println("Sending initial value");
          send(msg.set(state?RELAY_ON:RELAY_OFF));
          send(msg2.set(state2?RELAY_ON:RELAY_OFF));
          Serial.println("Requesting initial value from controller");
          request(CHILD_ID1, V_STATUS);
          wait(2000, C_SET, V_STATUS);
          request(CHILD_ID2, V_STATUS);
          wait(2000, C_SET, V_STATUS);
        }
        if (debouncer.update()) {
          if (debouncer.read()==LOW) {
            state = !state;
        //    // Send new state and request ack back
         //   send(msg.set(state?RELAY_ON:RELAY_OFF), true);
          }
          {
        debouncer.update();
        // Get the update value
        int value = debouncer.read();
      
        if (value != oldValue) {
           // Send in the new value
           send(msg3.set(value==HIGH ? 1 : 0));
           oldValue = value;
        }
        }
      }
      
      }
      
      void receive(const MyMessage &message) {
        if (message.isAck()) {
           Serial.println("This is an ack from gateway");
        }
      
        if (message.type == V_STATUS && message.sensor ==1) {
          if (!initialValueSent) {
            Serial.println("Receiving initial value from controller");
            initialValueSent = true;
          }
          // Change relay state
          state = (bool)message.getInt();
          digitalWrite(RELAY_PIN1, state?RELAY_OFF:RELAY_ON);
          send(msg.set(state?RELAY_ON:RELAY_OFF));
        }
         
        if (message.type == V_STATUS && message.sensor ==2) {
          if (!initialValueSent) {
            Serial.println("Receiving initial value from controller");
            initialValueSent = true;
          }
          // Change relay state
          state2 = (bool)message.getInt();
          digitalWrite(RELAY_PIN2, state2?RELAY_OFF:RELAY_ON);
          send(msg2.set(state2?RELAY_ON:RELAY_OFF));
        }
      }
      
      posted in My Project
      Cameron Payne
      Cameron Payne
    • RE: MySensored 230v motion sensor with light level

      @korttoma love this and works great! But is there any way to remove the relay from the equation so you don't get that clicking noise each time the sensor goes off?

      Thanks!

      posted in My Project
      Cameron Payne
      Cameron Payne