Relay Actuator - send periodic status as heart beat.



  • Hi,
    i successfully added heartbeat sending to RelayActuator.ino and relay sensor is sending data every one minute - but domoticz don't update last seen - this is only visible on gateway log. So i decided to send relay status as heartbeat - i have relay with two relay switches.

    Source code:

    #define MY_RADIO_RFM69
    #define MY_RFM69_NEW_DRIVER
    
    #define MY_NODE_ID 3
    
    
    // Enable repeater functionality for this node
    //#define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    
    
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    #define MY_DEBUG_VERBOSE
    
    long double last_heartbeat_time = millis();
    long double HEARTBEAT_TIME = 30000;
    
    int oldValue=0;
    bool state;
    
    MyMessage msg(2,V_LIGHT);
    MyMessage msg2;
    
    
    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);
      }
    }
    
    
    void loop() 
    {
       long double temp = (millis() - last_heartbeat_time);
      if (temp > HEARTBEAT_TIME) {
      // If it exceeds the heartbeat time then send a heartbeat
      sendHeartbeat();
      send(msg.set(msg2.getBool()?1:0)); 
      last_heartbeat_time = millis();
      Serial.println("Sent heartbeat" );
      }
    }
    
    void receive(const MyMessage &message) {
    
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
         msg2 = message;
       } 
    }
    
    

    but state is send only for one switch (second) and when i try to change switch 1 - this change is send aslo as relay 2. I added msg2 for export message from recieve to loop.

    How to send status for both relays as heartbeat?


  • Hardware Contributor

    @seal - sorry to say, this is a known problem in Domoticz. Is it a feature or is it a bug - depends on who you ask.
    This was just up: https://forum.mysensors.org/topic/7806/error-customercounter-hardware-12-nothing-received-for-more-than-5-minutes.

    This is me asking in DOmoticz forum: https://www.domoticz.com/forum/viewtopic.php?f=42&t=9775



  • Hi, yout post is quit old but I changed your code a bit. Now it sends the status of both relays:

    long double last_heartbeat_time = millis();
    long double HEARTBEAT_TIME = 30000;
    
    int oldValue = 0;
    bool state;
    
    MyMessage msg1a(1, V_STATUS);
    MyMessage msg1b;
    
    MyMessage msg2a(2, V_STATUS);
    MyMessage msg2b;
    
    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_BINARY);
      }
    }
    
    
    void loop()
    {
      long double temp = (millis() - last_heartbeat_time);
      if (temp > HEARTBEAT_TIME) {
        // If it exceeds the heartbeat time then send a heartbeat
        sendHeartbeat();
        send(msg1a.set(msg1b.getBool() ? 1 : 0));
        send(msg2a.set(msg2b.getBool() ? 1 : 0));
        last_heartbeat_time = millis();
        Serial.println("Sent heartbeat" );
      }
    }
    
    void receive(const MyMessage &message) {
    
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {
        // Change relay state
        digitalWrite(message.sensor - 1 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
    
        if (message.sensor == 1) {
          msg1b = message;
        }
    
        if (message.sensor == 2) {
          msg2b = message;
        }
    
      }
    }
    

    rg Denis


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 2
  • 3
  • 2
  • 5

24
Online

11.2k
Users

11.1k
Topics

112.5k
Posts