MySensors multiple relays (without button) + reed switches problem

Hi everyone. Please help I’m struggling with this problem for days…

I want to create a sketch for Adruino to control 10 reed switches + 20 relays. I know how to create 1 relay and 1 switch (based on Mysensors documentation) and this works perfectly. When I try to add more relays - no new sensors appears in HA :frowning:
I have json file to store data and I delete this file every time something changes in Mysensors config)

Please take a look at the code,

I’m pretty sure I do something wrong in
void receive(const MyMessage &message)
or
void loop

// Enable debug prints to serial monitor
#define MY_DEBUG

// Enable serial gateway
#define MY_GATEWAY_SERIAL

// Define a lower baud rate for Arduino’s running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE

// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP

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

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

// Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_PIN_1A 22
#define RELAY_PIN_1B 23

// Arduino Digital I/O pin for button/reed switch
#define BUTTON_PIN2 24

#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 CHILD_ID 1
#define CHILD_ID2 2
#define CHILD_ID3 3

void before()
{
for (int sensor=1, pin=RELAY_PIN_1A; 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(); // Reed
Bounce debouncer3 = Bounce();
// for reed switch
int oldValue=-1;

bool state = false;
bool initialValueSent = false;

MyMessage msg(CHILD_ID, V_STATUS);
MyMessage msg2(CHILD_ID2, V_TRIPPED);
MyMessage msg3(CHILD_ID3, V_STATUS);

void setup()
{

debouncer2.attach(BUTTON_PIN2);
debouncer2.interval(5);

// Setup the button
pinMode(BUTTON_PIN2,INPUT);
digitalWrite(BUTTON_PIN2,HIGH);

// Make sure relays are off when starting up
pinMode(RELAY_PIN_1A, OUTPUT);
digitalWrite(RELAY_PIN_1A, RELAY_OFF);
pinMode(RELAY_PIN_1B, OUTPUT);
digitalWrite(RELAY_PIN_1B, RELAY_OFF);
}

void presentation() {
sendSketchInfo(“Relay”, “1.0”);
present(CHILD_ID, S_BINARY);
present(CHILD_ID2, S_DOOR);
present(CHILD_ID3, S_BINARY);
}

void loop()
{
if (!initialValueSent) {
request(CHILD_ID, V_STATUS);
request(CHILD_ID2, V_TRIPPED);
request(CHILD_ID3, V_STATUS);
wait(2000, 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);
}
}
if (debouncer3.update()) {
if (debouncer3.read()==LOW) {
state = !state;
// Send new state and request ack back
send(msg3.set(state?RELAY_ON:RELAY_OFF), true);
}
}

if (debouncer2.update()) {
// reed
// Get the update value
int value = debouncer2.read();
if (value != oldValue) {
// Send in the new value
send(msg2.set(value==HIGH ? 1 : 0));
oldValue = value;
}
}

}

void receive(const MyMessage &message) {
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();

if (message.sensor == CHILD_ID) {
  digitalWrite(RELAY_PIN_1A, state?RELAY_ON:RELAY_OFF);
  send(msg.set(state?RELAY_ON:RELAY_OFF));
}

else if (message.sensor == CHILD_ID3) {
digitalWrite(RELAY_PIN_1B, state?RELAY_ON:RELAY_OFF);
send(msg3.set(state?RELAY_ON:RELAY_OFF));
}

//else {
//}

}
}

UPDATE

The code don’t show any sensors in HA righ now…

to get something shown in HA i need to use

void receive(const MyMessage &message) {
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_1A, state?RELAY_ON:RELAY_OFF);
  send(msg.set(state?RELAY_ON:RELAY_OFF));      

}
}

… but then I can control 1 relay only :frowning:

Hello, ich have a similar problem did you found any solution?:slight_smile:

This code should work. I took relevant parts from code I am running for years now. Code not fully mine, sources unknown.

Attention: not tested with Home Assistant. I’m just moving from fhem.de to Home Assistant. Not much knowledge yet, e.g. I have no clue, how to toggle the relays from the UI. In fhem it works well. If someone has any hints, I would appreciate hints :slight_smile:

/*
  Multi Relay Node 
  Relays: D5,D6,D7,D8					--> 5v
*/

// Enable and select radio type attached
#define MY_RADIO_RF24
#define MY_RF24_PA_LEVEL RF24_PA_MAX

#define MY_NODE_ID 2

#define MY_SPLASH_SCREEN_DISABLED
#define MY_TRANSPORT_WAIT_READY_MS (30000) // Don't stay awake for more than 30s if communication is broken

// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE

#define SKETCH_NAME "multi relay node"
#define SKETCH_VERSION "2.3.1"

#define CHILD_ID_RELAY1 1 // 2,3,4

#include <MySensors.h>

// Wait times
#define LONG_WAIT 500
#define SHORT_WAIT 50


// RELAY ///////////////////////////////////////////////////////////////////////
#define RELAY_1  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 4 // Total number of attached relays
#define RELAY_ON    LOW  // GPIO value to write to turn on attached relay
#define RELAY_OFF   HIGH // GPIO value to write to turn off attached relay

// /////// /////////////////////////////////////////////////////////////////////
// Initialize message types
// https://www.mysensors.org/download/serial_api_20#variable-types
MyMessage msgRelay(CHILD_ID_RELAY1, V_STATUS); //V_TRIPPED);

void presentation() {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo(SKETCH_NAME, SKETCH_VERSION " (" __DATE__ ")");
  wait(LONG_WAIT);

  // 4 channel relay
  for (int relays = 1, pin = RELAY_1; relays <= NUMBER_OF_RELAYS; relays++, pin++) {
    // Register all sensors to gateway (they will be created as child devices)
    present(relays, S_BINARY, "Relais (1/0)", ack);
    wait(SHORT_WAIT);
  }
}

void setup() {
  for (int relays = 1, pin = RELAY_1; relays <= NUMBER_OF_RELAYS; relays++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    // digitalWrite(pin, loadState(relays)?RELAY_ON:RELAY_OFF);
    digitalWrite(pin, 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) {
    // Change relay state
    digitalWrite(message.sensor - 1 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
  }
}

void loop() {}