Skip to content
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Fighting off a relay module
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

Fighting off a relay module

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 3 Posters 906 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moskovskiy82
    wrote on last edited by moskovskiy82
    #1

    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));
       }
    }
    
    Boots33B martinhjelmareM 2 Replies Last reply
    0
    • M moskovskiy82

      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));
         }
      }
      
      Boots33B Offline
      Boots33B Offline
      Boots33
      Hero Member
      wrote on last edited by
      #2

      @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);
        }
      
      
      1 Reply Last reply
      0
      • M moskovskiy82

        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));
           }
        }
        
        martinhjelmareM Offline
        martinhjelmareM Offline
        martinhjelmare
        Plugin Developer
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        8

        Online

        11.7k

        Users

        11.2k

        Topics

        113.0k

        Posts


        Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular