Fighting off a relay module



  • Need a node with a button input (doesn't do anything to relay state) and a single relay. As this is connected to Home Assistant have to provide feedback and present the relay.
    The problem is with the code below it just starts sending the relay state in a loop without stopping.
    mys-out/67/1/1/0/2 1
    Tried everything and seems got stuck somewhere

    #define SN "MYS Vannaya Gidro"
    #define SV "2.1.1"
    
    //System settings
    #define MY_NODE_ID 67
    #define MY_RADIO_NRF24
    #define MY_DEBUG
    
    #include <Bounce2.h>
    #include <math.h>
    #include <MySensors.h>
    #include <SPI.h>
    #include <Wire.h>
    
    //SETUP PINS
    #define BUTTON_PIN1 7
    const int MQ_Pin = A3;
    #define RELAY_1  5
    
    
    //Define CHILD_IDS
    #define CHILD_ID_RELAY 1
    #define CHILD_ID_BUT1 2
    #define CHILD_ID_MQ 3
    
    //Relay VARS
    #define RELAY_ON 1
    #define RELAY_OFF 0
    //SENSORS
    
    long SENSOR_Millis = 0;
    long SENSOR_interval = 60000;
    //Buttons
    Bounce debouncer1 = Bounce(); 
    int oldValue1=-1;
    bool state;
    //Messages
    MyMessage msgRelay(CHILD_ID_RELAY, V_STATUS);
    MyMessage msgMQ(CHILD_ID_MQ, V_LEVEL);
    MyMessage msgBut1(CHILD_ID_BUT1,V_TRIPPED);
    
    //Initial for HA
    bool initialValueSent = false;
    
    void before() 
    {
      pinMode(BUTTON_PIN1, INPUT_PULLUP);
      debouncer1.attach(BUTTON_PIN1);
      debouncer1.interval(5);
      pinMode(RELAY_1, OUTPUT);
      digitalWrite(RELAY_1, loadState(1)?RELAY_ON:RELAY_OFF);
    } 
    
    void setup()  
    {  Serial.begin(115200); }
    void presentation()  
    { 
      sendSketchInfo(SN, SV);
      wait(50);
      present(CHILD_ID_RELAY, S_BINARY);
      wait(50);
      present(CHILD_ID_MQ, S_AIR_QUALITY);
      wait(50);
      present(CHILD_ID_BUT1, S_WATER_LEAK);
    }
    
    void loop() 
    {
    if (!initialValueSent) 
      {
        send(msgRelay.set(loadState(1)?RELAY_OFF:RELAY_ON));
      }
      unsigned long SENSOR_Current_Millis = millis();
      if((unsigned long)(SENSOR_Current_Millis - SENSOR_Millis) >= SENSOR_interval)
      {
        SENSOR_Millis = SENSOR_Current_Millis; 
        float mq_reading = analogRead(MQ_Pin);
        send(msgMQ.set(mq_reading, 1));
      }
    
        debouncer1.update();
          int value = debouncer1.read();
          if (value != oldValue1) 
          {
            // Send in the new value
            send(msgBut1.set(value==HIGH ? 1 : 0));
            oldValue1 = value;
          }
    }   
    
    void receive(const MyMessage &message) 
    {
      if (message.type==V_STATUS) 
      {
        if (!initialValueSent) 
        {
          initialValueSent = true;
        }
         // Update relay state to HA
      state = message.getBool();
      digitalWrite(RELAY_1, state?RELAY_ON:RELAY_OFF);
      saveState(CHILD_ID_RELAY, state);
      send(msgRelay.set(state?RELAY_ON:RELAY_OFF));
       }
    }
    

  • Hero Member

    @moskovskiy82
    you could try

    if (!initialValueSent) 
      {
        send(msgRelay.set(loadState(1)?RELAY_OFF:RELAY_ON));
        initialValueSent = true;
      }
    
    

    or perhaps

    if (!initialValueSent) 
      {
        send(msgRelay.set(loadState(1)?RELAY_OFF:RELAY_ON), true);
      }
    
    

  • Plugin Developer

    @moskovskiy82

    Add a wait call after sending the initial value. I don't understand why you want to invert the saved state before sending it to the controller.


Log in to reply
 

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