HomeAssistant > MySensors:MQTT relay actuator works but very slow



  • Hi,

    It's my first outing with MySensors and I hoping someone might be able to help me out.

    I've set up three relays connected to HA via the mysensors 5100mqttgateway. All is working except for the variable and sometimes very slow reaction times. I've experimented with different brokers and with tweaking the sketch, but still it's slow - ranging from 2s up to 30s.

    What are other's experiences with response times in this sort of set-up? Any thoughts on what the issues might be?

    Here's the sketch:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *
     * Make sure to fill in your ssid and WiFi password below for ssid & pass.
     */
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Enables and select radio type (if attached)
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_GATEWAY_MQTT_CLIENT
    // Set this node's subscribe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "jungle1-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "jungle1-in"
    // Set MQTT client id
    #define MY_MQTT_CLIENT_ID "mysensors-jungle"
    // W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
    //#define MY_W5100_SPI_EN 4
    // Enable Soft SPI for NRF radio (note different radio wiring is required)
    // The W5100 ethernet module seems to have a hard time co-operate with
    // radio on the same spi bus.
    #if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
    #define MY_SOFTSPI
    #define MY_SOFT_SPI_SCK_PIN 14
    #define MY_SOFT_SPI_MISO_PIN 16
    #define MY_SOFT_SPI_MOSI_PIN 15
    #endif
    // When W5100 is connected we have to move CE/CSN pins for NRF radio
    #ifndef MY_RF24_CE_PIN
    #define MY_RF24_CE_PIN 5
    #endif
    #ifndef MY_RF24_CS_PIN
    #define MY_RF24_CS_PIN 6
    #endif
    // Enable these if your MQTT broker requires usenrame/password
    //#define MY_MQTT_USER "username"
    //#define MY_MQTT_PASSWORD "password"
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,xxx,xxx
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,xxx,xxx
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    // MQTT broker ip address or url. Define one or the other.
    //#define MY_CONTROLLER_URL_ADDRESS "m20.cloudmqtt.com"
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, xxx, xxx
    // The MQTT broker port to to open
    #define MY_PORT 1883
    #define CHILD_ID_REL1 101
    #define CHILD_ID_REL2 102
    #define CHILD_ID_REL3 103
    #define RELAY_ON 1
    #define RELAY_OFF 0
    /*
    Ethernet use the following PINs, do not use them:
    UNO:  4, 10, 11, 12, 13
    */
    #define REL1_PIN 7
    #define REL2_PIN 6
    #define REL3_PIN 5
    /*
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    // Flash leds on rx/tx/err
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    */
    #include <Ethernet.h>
    #include <MySensors.h>
    #include <SPI.h>
    MyMessage msgREL1(CHILD_ID_REL1, V_STATUS);  // 2
    MyMessage msgREL2(CHILD_ID_REL2, V_STATUS);  // 2
    MyMessage msgREL3(CHILD_ID_REL3, V_STATUS);  // 2
    bool FirstRun = true;
    const unsigned long tUpdate=20000; //update interval
    unsigned long t0;
    byte StateREL=0; 
    byte StateRELH=0;
    bool StateREL1=false, StateREL2=false, StateREL3=false;
    bool ChgREL=false;
    void before()
    {
      pinMode(REL1_PIN, OUTPUT);
      digitalWrite(REL1_PIN, StateREL1);
      pinMode(REL2_PIN, OUTPUT);
      digitalWrite(REL2_PIN, !StateREL2);
      pinMode(REL3_PIN, OUTPUT);
      digitalWrite(REL3_PIN, !StateREL3);
    }
    void setup()
    {
    t0=millis();  
    }
    void presentation()
    {
     sendSketchInfo("JungleRelay", "2.1");
      present(CHILD_ID_REL1, S_BINARY);
      present(CHILD_ID_REL2, S_BINARY);
      present(CHILD_ID_REL3, S_BINARY);  
    }
    void loop()
    {
    	if (FirstRun) {
        Serial.println("Sending initial value");
        send(msgREL1.set(StateREL1?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller 1");
        request(CHILD_ID_REL1, V_STATUS);
        wait(2000, C_SET, V_STATUS);
        Serial.println("Sending initial value");
        send(msgREL2.set(StateREL2?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller 2");
        request(CHILD_ID_REL2, V_STATUS);
        wait(2000, C_SET, V_STATUS);
        Serial.println("Sending initial value");
        send(msgREL3.set(StateREL3?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller 3");
        request(CHILD_ID_REL3, V_STATUS);
        wait(2000, C_SET, V_STATUS);
      }
      if (((millis()-t0) > tUpdate) or (ChgREL)){
          ServerUpdate();
          t0=millis();
          ChgREL = false;
         } 
    }
    void ServerUpdate() {
          send(msgREL1.set(StateREL1 ? RELAY_ON:RELAY_OFF));
          send(msgREL2.set(StateREL2 ? RELAY_ON:RELAY_OFF));
          send(msgREL3.set(StateREL3 ? RELAY_ON:RELAY_OFF));
          }
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS) {
          if (FirstRun) {
          Serial.println("Receiving initial value from controller");
          FirstRun = false;
          } 
        StateRELH=strcmp(message.data,"1")==0;
        StateREL = StateRELH;
        ChgREL = true;
        Serial.println(message.sensor);
        switch(message.sensor) {
          case 101: {
            if (StateREL) {
              digitalWrite(REL1_PIN, HIGH);
              StateREL1 = true;
              Serial.println(message.sensor, "ON");
            }
            else {
              digitalWrite(REL1_PIN, LOW);
              StateREL1 = false;
            } 
          }
          break;
          case 102: {
            StateREL2=StateREL;
            if (StateREL) {
              digitalWrite(REL2_PIN, LOW);
              StateREL2 = true;
            }
            else {
              digitalWrite(REL2_PIN, HIGH);
              StateREL2 = false;
            }
            }
          break;
          case 103: {
            StateREL3=StateREL;
            if (StateREL) {
              digitalWrite(REL3_PIN, LOW);
              StateREL3 = true;
            }
            else {
              digitalWrite(REL3_PIN, HIGH);
              StateREL3 = false;
            }
          }
          break;
        }
        } 
    }```

Log in to reply
 

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