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