Home Assistant doesn't connect to MySensors on Arduino MEGA



  • Hello,
    it's the first time that I'm using MySensors (on Arduino MEGA) with Home Assistant (on Raspberry Pi 3) and I really don't understand why this two devices don't talk with each other.

    I have upload this simple scatch on Arduino:

    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enable gateway ethernet module type
    #define MY_GATEWAY_W5100
    
    // 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 54
    #define MY_SOFT_SPI_MISO_PIN 56
    #define MY_SOFT_SPI_MOSI_PIN 55
    //#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 UDP communication
    //#define MY_USE_UDP  // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,1,70
    
    // If using static ip you can define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,1,1
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // Renewal period if using DHCP
    //#define MY_IP_RENEWAL_INTERVAL 60000
    
    // The port to keep open on node server mode / or port to contact in client mode
    #define MY_PORT 5003
    
    // Controller ip address. Enables client mode (default is "server" mode).
    // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
    //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 1, 200
    
    // The MAC address can be anything you want but should be unique on your network.
    // Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
    // Note that most of the Ardunio examples use  "DEAD BEEF FEED" for the MAC address.
    #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    
    // 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 7  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  8  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  9  // Transmit led pin
    
    #if defined(MY_USE_UDP)
    #include <EthernetUdp.h>
    #endif
    #include <Ethernet.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    
    #define RELAY_PIN  40
    #define BUTTON_PIN  8
    #define CHILD_ID 2
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    
    Bounce debouncer = Bounce();
    bool state = false;
    bool initialValueSent = false;
    
    MyMessage msg(CHILD_ID, V_STATUS);
    
    void setup()
    {
      pinMode(BUTTON_PIN, INPUT_PULLUP);
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(10);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      pinMode(RELAY_PIN, OUTPUT);
    }
    
    void presentation()  {
      sendSketchInfo("Relay+button", "1.0");
      present(CHILD_ID, S_BINARY);
    }
    
    void loop()
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        delay(500);
        send(msg.set(state?RELAY_ON:RELAY_OFF));
        delay(500);
        Serial.println("Requesting initial value from controller");
        delay(500);
        request(CHILD_ID, V_STATUS);
        wait(4000, 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);
        }
      }
    }
    
    void receive(const MyMessage &message) {
      
      Serial.println(message.type);
      delay(500);
      
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_STATUS) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        // Change relay state
        state = (bool)message.getInt();
        digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
        send(msg.set(state?RELAY_ON:RELAY_OFF));
      }
    }
    

    Debug output is this one:

    IP: 192.168.1.70
    0;255;3;0;9;MCO:BGN:STP
    0;255;3;0;9;MCO:REG:NOT NEEDED
    0;255;3;0;9;MCO:BGN:INIT OK,TSP=NA
    Sending initial value
    0;255;3;0;9;MCO:BGN:INIT GW,CP=R-NGA--,VER=2.1.1
    

    It is showed every 2 sec. in Arduino Serial Panel.

    On the other side I have writen this configuration in "configuration.yaml":

    mysensors:
      gateways:
        - device: '192.168.1.70'      
          persistence_file: 'mysensors.json'
          tcp_port: 5003    
      optimistic: false  
      persistence: true
      retain: true
      version: 2.0
    

    but Home Assitant log show me this messages:

    2017-09-07 13:43:20 ERROR (Thread-2) [mysensors.gateway_tcp] Receive from server failed. 
    2017-09-07 13:43:20 ERROR (Thread-2) [mysensors.gateway_tcp] Failed to shutdown socket at ('192.168.1.70', 5003)
    

    So what I can do?
    Thanks to all.

    Edit:
    I've enabled debug on Home Assistant and it show me this:

    2017-09-07 14:49:42 INFO (Thread-2) [mysensors.gateway_tcp] Trying to connect to ('192.168.1.70', 5003) 
    2017-09-07 14:49:42 INFO (Thread-2) [mysensors.gateway_tcp] Connected to ('192.168.1.70', 5003) 
    2017-09-07 14:49:52 DEBUG (Thread-2) [mysensors.gateway_tcp] Sending 0;255;3;0;2; 
    2017-09-07 14:49:52 ERROR (Thread-2) [mysensors.gateway_tcp] Receive from server failed. 
    2017-09-07 14:49:52 INFO (Thread-2) [mysensors.gateway_tcp] Closing socket at ('192.168.1.70', 5003). 
    2017-09-07 14:49:52 ERROR (Thread-2) [mysensors.gateway_tcp] Failed to shutdown socket at ('192.168.1.70', 5003). 
    2017-09-07 14:49:52 INFO (Thread-2) [mysensors.gateway_tcp] Socket closed at ('192.168.1.70', 5003).
    

  • Mod

    Can you connect to the gateway using myscontroller?


  • Plugin Developer

    @mrlele

    My guess is that the arduino gateway reboots every two seconds, either due to a bug in the core arduino library or due to low power availability when sending from the radio.

    See similar topic:
    https://forum.mysensors.org/topic/7187/problem-adding-nodes-to-ha/2


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 5
  • 1
  • 3

61
Online

11.4k
Users

11.1k
Topics

112.7k
Posts