I modified the code, so the relay status is sent to the HA only at the beginning. I also changed the child id numbers (those for relays now correspond to the pin numbers).
The good news is the sensor part for the pushbuttons is now working, but the relays still not. They do not register to the HA in any way.
// 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
// Set LOW transmit power level as default, if you have an amplified NRF-module and
// power your radio separately with a good regulator you can turn up PA level.
//#define MY_RF24_PA_LEVEL RF24_PA_LOW
// Enable serial gateway
#define MY_GATEWAY_SERIAL
// Define a lower baud rate for Arduinos 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
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Inverses the behavior of leds
//#define MY_WITH_LEDS_BLINKING_INVERSE
// Flash leds on rx/tx/err
// 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>
#define RELAY_22 22 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 16 // Total number of attached relays: 4
// Opto Relay Module I was using Active Low - Low (0):ON, High (1): OFF
#define RELAY_ON 0 // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay
#define FIRST_PIR_ID 1
#define MAX_PIRS 16
const uint8_t pirPin[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15}; // switch around pins to your desire
Bounce debouncer[MAX_PIRS];
MyMessage pirMsg(0, V_TRIPPED);
bool oldPir[MAX_PIRS] = {false};
bool initialValueSent = false;
//Init MyMessage for Each Child ID
MyMessage msg22(22, V_LIGHT);
MyMessage msg23(23, V_LIGHT);
MyMessage msg24(24, V_LIGHT);
MyMessage msg25(25, V_LIGHT);
MyMessage msg26(26, V_LIGHT);
MyMessage msg27(27, V_LIGHT);
MyMessage msg28(28, V_LIGHT);
MyMessage msg29(29, V_LIGHT);
MyMessage msg30(30, V_LIGHT);
MyMessage msg31(31, V_LIGHT);
MyMessage msg32(32, V_LIGHT);
MyMessage msg33(33, V_LIGHT);
MyMessage msg34(34, V_LIGHT);
MyMessage msg35(35, V_LIGHT);
MyMessage msg36(36, V_LIGHT);
MyMessage msg37(37, V_LIGHT);
void before() {
for (int sensor=22, pin=RELAY_22; 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);
}
}
void setup()
{
for (uint8_t i = 0; i < MAX_PIRS; i++) {
debouncer[i] = Bounce(); // initialize debouncer
debouncer[i].attach(pirPin[i], INPUT_PULLUP);
debouncer[i].interval(5);
oldPir[i] = debouncer[i].read();
}
wait(5000);
Serial.println("Sending initial value");
send(msg22.set(loadState(22)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg23.set(loadState(23)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg24.set(loadState(24)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg25.set(loadState(25)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg26.set(loadState(26)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg27.set(loadState(27)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg28.set(loadState(28)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg29.set(loadState(29)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg30.set(loadState(30)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg31.set(loadState(31)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg32.set(loadState(32)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg33.set(loadState(33)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg34.set(loadState(34)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg35.set(loadState(35)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg36.set(loadState(36)?RELAY_OFF:RELAY_ON),true);
wait(1000);
send(msg37.set(loadState(37)?RELAY_OFF:RELAY_ON),true);
wait(1000);
Serial.println("Sending initial value: Completed");
wait(5000);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Combo_22-37_i_A0-A15", "1.0");
for (int sensor=22, pin=RELAY_22; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
present(sensor, S_LIGHT);
for (int i = 0; i < MAX_PIRS; i++) { //i < numSensors &&
present(FIRST_PIR_ID + i, S_MOTION);
}
}
}
void loop() {
bool pir[MAX_PIRS];
for (uint8_t i = 0; i < MAX_PIRS; i++) {
debouncer[i].update();
pir[i] = debouncer[i].read();
if (pir[i] != oldPir[i]) {
send(pirMsg.setSensor(FIRST_PIR_ID + i).set( pir[i])); // Send tripped value to gw
oldPir[i] = pir[i];
}
}
}
void receive(const MyMessage &message) {
Serial.println("=============== Receive Start =======================");
if (message.isAck()) {
Serial.println(">>>>> ACK <<<<<");
Serial.println("This is an ack from gateway");
Serial.println("<<<<<< ACK >>>>>>");
}
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
Serial.println(">>>>> V_LIGHT <<<<<");
if (!initialValueSent) {
Serial.println("Receiving initial value from controller");
initialValueSent = true;
}
// Update relay state to HA
digitalWrite(message.sensor-1+RELAY_22, message.getBool()?RELAY_ON:RELAY_OFF);
switch (message.sensor) {
case 22:
Serial.print("Incoming change for sensor 1");
send(msg22.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 23:
Serial.print("Incoming change for sensor 2");
send(msg23.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 24:
Serial.print("Incoming change for sensor 3");
send(msg24.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 25:
Serial.print("Incoming change for sensor 4");
send(msg25.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 26:
Serial.print("Incoming change for sensor 5");
send(msg26.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 27:
Serial.print("Incoming change for sensor 6");
send(msg27.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 28:
Serial.print("Incoming change for sensor 7");
send(msg28.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 29:
Serial.print("Incoming change for sensor 8");
send(msg29.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 30:
Serial.print("Incoming change for sensor 9");
send(msg30.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 31:
Serial.print("Incoming change for sensor 10");
send(msg31.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 32:
Serial.print("Incoming change for sensor 11");
send(msg32.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 33:
Serial.print("Incoming change for sensor 12");
send(msg33.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 34:
Serial.print("Incoming change for sensor 13");
send(msg34.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 35:
Serial.print("Incoming change for sensor 14");
send(msg35.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 36:
Serial.print("Incoming change for sensor 15");
send(msg36.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
case 37:
Serial.print("Incoming change for sensor 16");
send(msg37.set(message.getBool()?RELAY_OFF:RELAY_ON));
break;
default:
Serial.println("Default Case: Receiving Other Sensor Child ID");
break;
}
// Store state in Arduino eeprom
saveState(message.sensor, message.getBool());
Serial.print("Saved State for sensor: ");
Serial.print( message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
Serial.println("<<<<<< V_LIGHT >>>>>>");
}
Serial.println("=============== Receive END =======================");
}
No error logs in HA, not a sign of relay entities in the json file.