MySensors 2.0 USB gateway

Hi,

Currently I’m not able to get MySensors Arduino Mega 2560 v3 working with Home Assistant.

Arduino Code:

// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable serial gateway
#define MY_GATEWAY_SERIAL

// 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

// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
//#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED

#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>

// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE

#define RELAY_1  14  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_2  15
#define RELAY_3  16
#define RELAY_4  17

#define NUMBER_OF_RELAYS 4 // 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 BUTTON_PIN 5
#define BUTTON2_PIN 6
#define BUTTON3_PIN 7
#define BUTTON4_PIN 8


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);
  }
}

Bounce debouncer = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();

void setup() {
  // Setup locally attached sensors
  delay(10000);
  // Setup the button.

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON2_PIN, INPUT_PULLUP);
  pinMode(BUTTON3_PIN, INPUT_PULLUP);
  pinMode(BUTTON4_PIN, INPUT_PULLUP);
  
  // After setting up the button, setup debouncer.
  
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5);
  debouncer2.attach(BUTTON2_PIN);
  debouncer2.interval(5);
  debouncer3.attach(BUTTON3_PIN);
  debouncer3.interval(5);
  debouncer4.attach(BUTTON4_PIN);
  debouncer4.interval(5);

  //presentation();
}
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);
  }
}

MyMessage msg(1, V_LIGHT);
MyMessage msg2(2, V_LIGHT);
MyMessage msg3(3, V_LIGHT);
MyMessage msg4(4, V_LIGHT);

void loop() {
  // Send locally attached sensor data here
  if (debouncer.update()) {
    // Get the update value.
    int value = debouncer.read();
    // Send in the new value.
    if (value == LOW) {
      saveState(1, !loadState(1));
      digitalWrite(RELAY_1, loadState(1) ? RELAY_ON : RELAY_OFF);
      send(msg.set(loadState(1)));
    }
  }

  if (debouncer2.update()) {
      int value2 = debouncer2.read();
    if(value2 == LOW){
         saveState(2, !loadState(2));
         digitalWrite(RELAY_2, loadState(2)?RELAY_ON:RELAY_OFF);
         send(msg2.set(loadState(2)));
         }
  }
  if (debouncer3.update()) {
      int value3 = debouncer3.read();
    if(value3 == LOW){
         saveState(3, !loadState(3));
         digitalWrite(RELAY_3, loadState(3)?RELAY_ON:RELAY_OFF);
         send(msg3.set(loadState(3)));
         }
  }
  if (debouncer4.update()) {
      int value4 = debouncer4.read();
    if(value4 == LOW){
         saveState(4, !loadState(4));
         digitalWrite(RELAY_4, loadState(4)?RELAY_ON:RELAY_OFF);
         send(msg4.set(loadState(4)));
         }
  }

}


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());
  }
}

Arduino is working properly but HA doesn’t see any switches or relays.

Config:

mysensors:
  gateways:
    - device: '/dev/ttyACM0'
      persistence_file: 'mysensors.json'
      baud_rate: 115200
      nodes:
        1:
          name: 'test'
  optimistic: true
  retain: true
  version: '2.0'

Console:

➜ /config hassio hw info
audio: {}
disk: []
gpio: []
input:
- PC Speaker
- Power Button
serial:
- /dev/serial/by-id/usb-Arduino__www.arduino.cc__0042_9573632363235121D150-if00
- /dev/ttyS0
- /dev/ttyACM0

mysensor.json - automatically generated:

{}

I’m currently using Hassio 0.97.2 on UnRaid (docker) - I’ve tried on Raspberry Pi, Ubuntu Server (docker) and same think, nothing is showing up in mysensor.json

Any idea how to make it work?

When I’m pushing button on the switch I’m getting those lines in the log:

2019-08-26 18:15:18 WARNING (MainThread) [mysensors] Node 0 is unknown
2019-08-26 18:15:18 INFO (MainThread) [mysensors] Requesting new presentation for node 0

Is your Node ID hardcoded in your switch? The push button on the Gateway?

Why this definition you don’t have any nodes in the config yet? maybe remove for now but I don’t think it will fix anything.

How many sensors have you created?

I’ve deleted that part as I was getting error in config check - I’ve forgot to edit that before posting
I’ve tried to define node ID as 1 and auto - still same logs.

when arduino is connecting to HA I’m getting this:

2019-08-26 18:33:09 INFO (MainThread) [mysensors] Connected to Serial<id=0x14800ea0cf90, open=True>(port='/dev/ttyACM0', baudrate=38400, bytesize=8, parity='N', stopbits=1, timeout=0, xonxoff=False, rtscts=False, dsrdtr=False)
2019-08-26 18:33:10 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:Starting gateway (R-NGA-, 2.0.0)
2019-08-26 18:33:10 INFO (MainThread) [mysensors.handler] n:0 c:255 t:3 s:14 p:Gateway startup complete.
2019-08-26 18:33:10 DEBUG (MainThread) [homeassistant.components.mysensors.gateway] Node update: node 0 child 255
2019-08-26 18:33:15 DEBUG (SyncWorker_12) [mysensors.persistence] Saving sensors to persistence file /config/mysensors_test.json
2019-08-26 18:33:20 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:No registration required
2019-08-26 18:33:20 DEBUG (MainThread) [mysensors.handler] n:0 c:255 t:3 s:9 p:Init complete, id=0, parent=0, distance=0, registration=1

Did you change this in your config??

Name of file changed?

How many sensors do you have? It is only seeing the Gateway

Persistence name has been changed as I was testing if writing new file would help. I was deleting mysensor1.json as well but to be sure I’ve changed name.
Baud changed in arduino and config as well for same reason as above.

Originally I’ll be connecting 2 arduinos with around 24 switches + 24 relays on first and 10 same config as first plus few extra switches on second one plus around 20 sensors.
At first I want to make switches and relays work as one of arduino will be responsible just for light.

And yes, HA is only seeing gateway.

Once I made it work partially - HA seen buttons but it was working one way, I could only activate switch to turn off the relay if it was on but I couldn’t turn it back on by HA GUI, only mechanical switches were working