Sensebender multiple controller



  • Hello!
    How is it possible to operate two controllers simultaneously with one Sensebender Gateway?
    i have this sketch on the sensebender, but i can only connect openhab OR mycontroller.org, not both at the same time.
    The last controller that connects to the sensebender gets the data. the other one doesn't.

    Is this due to 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-2018 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * Contribution by a-lurker and Anticimex,
     * Contribution by Norbert Truchsess <norbert.truchsess@t-online.de>
     * Contribution by Tomas Hozza <thozza@gmail.com>
     *
     *
     * DESCRIPTION
     * The EthernetGateway sends data received from sensors to the ethernet link.
     * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
     *
     * The GW code is designed for Arduino 328p / 16MHz.  ATmega168 does not have enough memory to run this program.
     *
     * LED purposes:
     * - To use the feature, uncomment MY_DEFAULT_xxx_LED_PIN in the sketch below
     * - RX (green) - blink fast on radio message received. In inclusion mode will blink fast only on presentation received
     * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
     * - ERR (red) - fast blink on error during transmission error or receive crc error
     *
     * See http://www.mysensors.org/build/ethernet_gateway for wiring instructions.
     *
     */
    
    //@changed
    // Define sketch version
    #define SKETCH_VERSION "0.4"
    //@changed
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    // Enable gateway ethernet module type
    #define MY_GATEWAY_W5100
    #define MY_GATEWAY_MAX_CLIENTS  3
    
    // 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 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,110
    
    // 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, 178, 254
    
    // 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 Arduino examples use  "DEAD BEEF FEED" for the MAC address.
    #define MY_MAC_ADDRESS XXXXXXXXXXX
    
    // 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>
    
    //@changed
    #include <SD.h>
    #include <drivers/ATSHA204/ATSHA204.cpp>
    
    Sd2Card card;
    
    #define EEPROM_VERIFICATION_ADDRESS 0x01
    
    static uint8_t num_of_leds = 5;
    static uint8_t leds[] = {LED_BLUE, LED_RED, LED_GREEN, LED_YELLOW, LED_ORANGE};
    //@changed
    
    void setup()
    {
    	// Setup locally attached sensors
     sleep(250);      // wait for the W5100
    }
    
    void presentation()
    {
    	// Present locally attached sensors here
    }
    
    void loop()
    {
    	// Send locally attached sensors data here
    }
    
    
    
    
    //@changed
    void preHwInit()
    {
    
      pinMode(MY_SWC1, INPUT_PULLUP);
      pinMode(MY_SWC2, INPUT_PULLUP);
      if (digitalRead(MY_SWC1) && digitalRead(MY_SWC2)) {
        return;
      }
    
      uint8_t tests = 0;
    
      for (int i=0; i< num_of_leds; i++) {
        pinMode(leds[i], OUTPUT);
      }
      if (digitalRead(MY_SWC1)) {
        uint8_t led_state = 0;
        while (!Serial) {
          digitalWrite(LED_BLUE, led_state);
          led_state ^= 0x01;
          delay(500);
        } // Wait for USB to be connected, before spewing out data.
      }
      digitalWrite(LED_BLUE, LOW);
      if (Serial) {
        Serial.println("Sensebender GateWay test routine");
        Serial.print("MySensors core version : ");
        Serial.println(MYSENSORS_LIBRARY_VERSION);
        Serial.print("GateWay sketch version : ");
        Serial.println(SKETCH_VERSION);
        Serial.println("----------------------------------");
        Serial.println();
      }
      if (testSha204()) {
        digitalWrite(LED_GREEN, HIGH);
        tests++;
      }
      if (testSDCard()) {
        digitalWrite(LED_YELLOW, HIGH);
        tests++;
      }
    
      if (testEEProm()) {
        digitalWrite(LED_ORANGE, HIGH);
        tests++;
      }
      if (testAnalog()) {
        digitalWrite(LED_BLUE, HIGH);
        tests++;
      }
      if (tests == 4) {
        while(1) {
          for (int i=0; i<num_of_leds; i++) {
            digitalWrite(leds[i], HIGH);
            delay(200);
            digitalWrite(leds[i], LOW);
          }
        }
      } else {
        while (1) {
          digitalWrite(LED_RED, HIGH);
          delay(200);
          digitalWrite(LED_RED, LOW);
          delay(200);
        }
      }
    
    }
    
    bool testSha204()
    {
      uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
      uint8_t ret_code;
      if (Serial) {
        Serial.print("- > SHA204 ");
      }
      atsha204_init(MY_SIGNING_ATSHA204_PIN);
      ret_code = atsha204_wakeup(rx_buffer);
    
      if (ret_code == SHA204_SUCCESS) {
        ret_code = atsha204_getSerialNumber(rx_buffer);
        if (ret_code != SHA204_SUCCESS) {
          if (Serial) {
            Serial.println(F("Failed to obtain device serial number. Response: "));
          }
          Serial.println(ret_code, HEX);
        } else {
          if (Serial) {
            Serial.print(F("Ok (serial : "));
            for (int i=0; i<9; i++) {
              if (rx_buffer[i] < 0x10) {
                Serial.print('0'); // Because Serial.print does not 0-pad HEX
              }
              Serial.print(rx_buffer[i], HEX);
            }
            Serial.println(")");
          }
          return true;
        }
      } else {
        if (Serial) {
          Serial.println(F("Failed to wakeup SHA204"));
        }
      }
      return false;
    }
    
    bool testSDCard()
    {
      if (Serial) {
        Serial.print("- > SD CARD ");
      }
      if (!card.init(SPI_HALF_SPEED, MY_SDCARD_CS)) {
        if (Serial) {
          Serial.println("SD CARD did not initialize!");
        }
      } else {
        if (Serial) {
          Serial.print("SD Card initialized correct! - ");
          Serial.print("type detected : ");
          switch(card.type()) {
          case SD_CARD_TYPE_SD1:
            Serial.println("SD1");
            break;
          case SD_CARD_TYPE_SD2:
            Serial.println("SD2");
            break;
          case SD_CARD_TYPE_SDHC:
            Serial.println("SDHC");
            break;
          default:
            Serial.println("Unknown");
          }
        }
        return true;
      }
      return false;
    }
    
    bool testEEProm()
    {
      uint8_t eeprom_d1, eeprom_d2;
      SerialUSB.print(" -> EEPROM ");
      eeprom_d1 = hwReadConfig(EEPROM_VERIFICATION_ADDRESS);
      delay(500);
      eeprom_d1 = ~eeprom_d1; // invert the bits
      hwWriteConfig(EEPROM_VERIFICATION_ADDRESS, eeprom_d1);
      delay(500);
      eeprom_d2 = hwReadConfig(EEPROM_VERIFICATION_ADDRESS);
      if (eeprom_d1 == eeprom_d2) {
        SerialUSB.println("PASSED");
        hwWriteConfig(EEPROM_VERIFICATION_ADDRESS, ~eeprom_d1);
        return true;
      }
      SerialUSB.println("FAILED!");
      return false;
    }
    
    bool testAnalog()
    {
      int bat_detect = analogRead(MY_BAT_DETECT);
      Serial.print("-> analog : ");
      Serial.print(bat_detect);
      if (bat_detect < 400 || bat_detect > 650) {
        Serial.println(" Failed");
        return false;
      }
      Serial.println(" Passed");
      return true;
    }
    //@changed```

  • Admin

    I'm not expert on the networking details in the gateway (even though I designed the hardware 🙂 ).

    But if you want to use multiple controllers, I would recommend that you use MQTT as transport layer instead, and then have a mqtt broker, like mosquitto, on your HA server.



  • if i understood everything correctly, only the auto-id feature isn't available in mqtt, right?
    I can still use autodiscovery and ota, right?


  • Admin

    To my best knowledge, you should be able to use all the standard features, together with mqtt, including auto-id assignment

    But then again, I have never used OTA yet so I can't tell you if that part works.



  • ok, i tried a username and password for mqtt binding (http://www.steves-internet-guide.com/mqtt-username-password-example/), but getting the error "connection can´t be established".
    Is it a bug? i found several threads with the same failure...



  • I experience the same behaviour with an ESP8266 wifi gateway. In the MySensors sources I found the reason why this doesn't work:
    In the file core\MyGatewayTransportEthernet.cpp in lines 98 to 106 the ethernet server is initialized:

    #if defined(MY_GATEWAY_CLIENT_MODE)
    #if defined(MY_USE_UDP)
    EthernetUDP _ethernetServer;
    #endif /* End of MY_USE_UDP */
    #elif defined(MY_GATEWAY_LINUX) /* Elif part of MY_GATEWAY_CLIENT_MODE */
    EthernetServer _ethernetServer(_ethernetGatewayPort, MY_GATEWAY_MAX_CLIENTS);
    #else /* Else part of MY_GATEWAY_CLIENT_MODE */
    EthernetServer _ethernetServer(_ethernetGatewayPort);
    #endif /* End of MY_GATEWAY_CLIENT_MODE */
    

    For my gateway the last statement without the constant MY_GATEWAY_MAX_CLIENTS is used to define the server. Thus it is clear why only one connection is kept.

    Maybe the ESP8266 examples should be updated. Or the ethernet server code should be reworked. 😕


Log in to reply
 

Suggested Topics

  • 3
  • 15
  • 2
  • 24
  • 10
  • 2

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts