Arduino or multiple Pi's for 120 buttons, 40 relays, 90 DHT22, 20 Alarm sensors

Hi

I have recently had my house rewired, whilst this work was done I added a lot of data and alarm cables to my house. As the title says we have:

120 No. MK Momentary/Push to make buttons (to control 30 zwave modules, multiple sonos & relay + scenes)
40 No. 240v Relays for windows, blinds, outlets, fans, MVHR, heating
30 No. DHT22 for internal temps and humidity
60 No DHT22 to monitor the house construction (vapour permeability project)
20No. Alarm sensors

There are three places in then house where these cables terminate. Initially I was thinking of just having 3 HA instances on separate PI’s and connecting arduino’s via standard firmata. Just wondering if anyone has anything similar and has a better/different setup? MQTT, MQTT with Master & Slaves, having multiple buttons on a single pin.

Also considering a move to a Nuc for my main instance.

Thanks for reading

1 Like

This MQTT solution supports pi io expansion boards:

I use two pi’s and custom made interface boards. They report to HA on a mini pc.

https://community.home-assistant.io/t/custom-raspi-gpio-interfcae-pcb-project/88269

I hasn’t missed a beat in over a year. Though all my DHT22s are connected to ESP boards around the house.

That is 270 nodes which is quite a lot.

My understanding is that you can only attach one arduino to each home assistant machine. My understanding comes from a thread on here, and the situation may have changed. That would limit you to the max number of arduino pins firmata supports - I don’t know what that number is, but this refers to 20 pins (14 digital and 6 analogue). https://www.home-assistant.io/components/arduino/

@tom_l that looks good.

Truth is that most home automation stuff is geared to being wireless (whether that is wifi, z-wave, zigbee, lora or whatever). Running a wire during building seems sensible, but doesn’t always fit in as well as the wireless options.

Of everything you listed, the only issue looks to be the DHT22 sensors. Primarily because they have specific timing requirements to communicate and the DHT libraries for Arduino tie each sensor to one pin. So you’d need 90 data pins for 90 sensors, which sucks.

Are you wedded to the DHT22 sensor or willing to use a different sensor? The BME280 is a better sensor and has both I2C and SPI connection options. You could connect many sensors to one Arduino using an I2C multiplexor, or put many on an SPI bus and use a multiplexor to select the CS of the sensor you wan to read. From the datasheet, the BME280 can have two different I2C addresses so you could put 2 on each I2C bus.

The rest are just inputs and outputs. And in a way very similar to what I am doing. In my home, each room uses one or more room controller boards. It’s my own custom board containing an Arduino Nano and W5100 Ethernet, 8 opto-isolated inputs for buttons/switches/contacts, and 8 outputs for relays. Each board speaks to HA via MQTT. Can have as many of those boards as you want, running as many sensors/buttons/relays. With multiplexors, one board would only be limited by available memory the number of devices connected. Right now I’m actually redesigning using a different processor because I’d like to hang other things off the board like RFID, DHT, run the whole thing with POE, etc.

So it seems you are comfortable enough with Arduino that you could do something similar. Eliminate multiple Pis and fermata, use Ethernet and MQTT. SeedStudio has an Arduino Uno with built in Ethernet which does a bunch of the work for you. Hard to find because they call it an “Ethernet network module development board for Arduino”. eBay 132739019637. Superhouse.tv has the EtherTen and EtherMega which do the same, and POE.

Awesome info Aaron, I’m stuck with the 60 DHT22’s as they are within the construction of my building and are inaccessible. I’m running an experiment on vapour permeability, insulation and cavity air changes. I could look at changing the internal ones as they are for automation of my heating, MVHR and windows. I more than happy to use MQTT and EtherMega’s, I ran in masses of cat6a S/FTP and have POE.

I haven’t done anything with multiplexors, I’ll have spend some time reading about them.

Thanks for taking the time to reply. This looks to be a good fit for my project. I hope everything goes well with your new boards.

Cheers for your reply Tom, I’ll look into these modules over the weekend.

I would go with a single or 2 x Pi’s, Bosch BME280’s instead of DHT22’s, IO multiplexors, and use I2C for everything. That way you need far less wires, and I2C only needs 3 wires if you exclude power.

From your description, just wondering how far the DHT22s are from the device that will read them? Keep in mind that these sensors require short leads to the microcontroller. 6" or less in my experience. Any longer may, and probably still work, but the sensor will tend to freeze up, requiring a power cycle. I have this problem with sensors on 12" leads.

1 Like

Some are up to 20m. I ran a test system on the longest runs for about a month from a mega, I didn’t get any problems. I think I had 12 connected for the test.

I’m actually kind of amazed that 20M was working at all. Just make sure that you have some way to physically reset the sensor (automatically, relay). What I tend to see is the sensor runs reliably for a period of time, up to a month or two, then one day just stops returning a value.

[
] low consumption & long transmission distance(20m) enable DHT22 to be suited [
]

from:

:wink:

Hi,

So I used MQTT and an arduino.

I have 30 buttons, 1 LED and a dht22 connected to my arduino for a test.

I set each of the buttons up as a sensor in HA.

When I press the button, the arduino publishes it and and the sensor changes to “pressed”.

I set a switch in HA to publish LED on, arduino switches the LED on/off.

DHT22 publishes data to HA.

All seems ok, however the arduino script is very long and repetitive. I merged a few examples I found online. I’m sure I’ve made it much longer/sloppy than it needs to be but heres my sketch. I include bits needed for HA at the top.

Everything seems to be stable, I have added functions to most of these buttons in HA and I have been using the for best part of a month. I have had no real issues. I’m using MK grid switches on long lengths of stranded alarm cable. I’m powering the arduino through my pc and my PC usb power output is always on. As I add the rest of the arduinos I’ll add a powered usb hub, this way if I need to make changes to any of them I can.

/* MQTT Arduino Client
Functions:

Publish’s:
DHT22 Information
Momentary Button Press’s

Subscribe’s:
LED High/Low (Relay Control)

Libraries installed (I had some issues with newer versions of the sensor library’s)
PubSubClient, Version 2.7.0
Ethernet, Version 2.0
DHT sensor, Version 1.0.0
Adafruit Unified Sensor, Version 1.0.3

Used with Home Assistant Mosquito Broker
Install addon, set passwords, create ACL files
New HA user: USERNAME, Password: PASSWORD

MQTT configuration.yaml ------------------------------------------------

mqtt:
broker: 192.168.178.40
port: 1883
username: USERNAME
password: PASSWORD
discovery: true

sensor 1:
platform: mqtt
name: ButtonUp1
state_topic: “arduino_up/button_1”
qos: 0

sensor 2:
platform: mqtt
name: ButtonUp2
state_topic: “arduino_up/button_2”
qos: 0

sensor 3:
platform: mqtt
name: ButtonUp3
state_topic: “arduino_up/button_3”
qos: 0

sensor 4:
platform: mqtt
name: ButtonUp4
state_topic: “arduino_up/button_4”
qos: 0

sensor 5:
platform: mqtt
name: ButtonUp5
state_topic: “arduino_up/button_5”
qos: 0

sensor 6:
platform: mqtt
name: ButtonUp6
state_topic: “arduino_up/button_6”
qos: 0

sensor 7:
platform: mqtt
name: ButtonUp7
state_topic: “arduino_up/button_7”
qos: 0

sensor 8:
platform: mqtt
name: ButtonUp8
state_topic: “arduino_up/button_8”
qos: 0

sensor 9:
platform: mqtt
name: ButtonUp9
state_topic: “arduino_up/button_9”
qos: 0

sensor 10:
platform: mqtt
name: ButtonUp10
state_topic: “arduino_up/button_10”
qos: 0

sensor 11:
platform: mqtt
name: ButtonUp11
state_topic: “arduino_up/button_11”
qos: 0

sensor 12:
platform: mqtt
name: ButtonUp12
state_topic: “arduino_up/button_12”
qos: 0

sensor 13:
platform: mqtt
name: ButtonUp13
state_topic: “arduino_up/button_13”
qos: 0

sensor 14:
platform: mqtt
name: ButtonUp14
state_topic: “arduino_up/button_14”
qos: 0

sensor 15:
platform: mqtt
name: ButtonUp15
state_topic: “arduino_up/button_15”
qos: 0

sensor 16:
platform: mqtt
name: ButtonUp16
state_topic: “arduino_up/button_16”
qos: 0

sensor 17:
platform: mqtt
name: ButtonUp17
state_topic: “arduino_up/button_17”
qos: 0

sensor 18:
platform: mqtt
name: ButtonUp18
state_topic: “arduino_up/button_18”
qos: 0

sensor 19:
platform: mqtt
name: ButtonUp19
state_topic: “arduino_up/button_19”
qos: 0

sensor 20:
platform: mqtt
name: ButtonUp20
state_topic: “arduino_up/button_20”
qos:

sensor 21:
platform: mqtt
name: ButtonUp21
state_topic: “arduino_up/button_21”
qos: 0

sensor 22:
platform: mqtt
name: ButtonUp22
state_topic: “arduino_up/button_22”
qos: 0

sensor 23:
platform: mqtt
name: ButtonUp23
state_topic: “arduino_up/button_23”
qos: 0

sensor 24:
platform: mqtt
name: ButtonUp24
state_topic: “arduino_up/button_24”
qos: 0

sensor 25:
platform: mqtt
name: ButtonUp25
state_topic: “arduino_up/button_25”
qos: 0

sensor 26:
platform: mqtt
name: ButtonUp26
state_topic: “arduino_up/button_26”
qos: 0

sensor 27:
platform: mqtt
name: ButtonUp27
state_topic: “arduino_up/button_27”
qos: 0

sensor 28:
platform: mqtt
name: ButtonUp28
state_topic: “arduino_up/button_28”
qos: 0

sensor 29:
platform: mqtt
name: ButtonUp29
state_topic: “arduino_up/button_29”
qos: 0

sensor 30:
platform: mqtt
name: ButtonUp30
state_topic: “arduino_up/button_30”
qos: 0

sensor:31
platform: mqtt
name: “Temperature”
state_topic: “arduino_up/temp_1”
qos: 0
unit_of_measurement: “ÂșC”

sensor 32:
platform: mqtt
name: “Humidity”
state_topic: “arduino_up/hum_1”
qos: 0
unit_of_measurement: “%”

switch:
platform: mqtt
name: Test LED
command_topic: “arduino_up/led”
payload_on: “on”
payload_off: “off”
optimistic: false
qos: 0

MQTT configuration.yaml Finished -------------------------------------------

MQTT automation.yaml EXAMPLE -------------------------------------------------------

id: ‘1570079268345’
alias: MQTT Button
trigger:

  • entity_id: sensor.buttonUp1
    from: Waiting
    platform: state
    to: Pressed
    condition: []
    action:
  • data:
    entity_id: light.fibaro_system_fgrgbwm441_rgbw_controller_level_6
    service: light.toggle

MQTT automation.yaml Finished ----------------------------------------------

*/

#include “Ethernet.h”
#include “PubSubClient.h”
#include “DHT.h”

#define CLIENT_ID “ArduinoUp”, “USERNAME”, “PASSWORD”
#define AT1 “arduino_up/temp_1”
#define AH1 “arduino_up/hum_1”
#define AB1 “arduino_up/button_1”
#define AB2 “arduino_up/button_2”
#define AB3 “arduino_up/button_3”
#define AB4 “arduino_up/button_4”
#define AB5 “arduino_up/button_5”
#define AB6 “arduino_up/button_6”
#define AB7 “arduino_up/button_7”
#define AB8 “arduino_up/button_8”
#define AB9 “arduino_up/button_9”
#define AB10 “arduino_up/button_10”
#define AB11 “arduino_up/button_11”
#define AB12 “arduino_up/button_12”
#define AB13 “arduino_up/button_13”
#define AB14 “arduino_up/button_14”
#define AB15 “arduino_up/button_15”
#define AB16 “arduino_up/button_16”
#define AB17 “arduino_up/button_17”
#define AB18 “arduino_up/button_18”
#define AB19 “arduino_up/button_19”
#define AB20 “arduino_up/button_20”
#define AB21 “arduino_up/button_21”
#define AB22 “arduino_up/button_22”
#define AB23 “arduino_up/button_23”
#define AB24 “arduino_up/button_24”
#define AB25 “arduino_up/button_25”
#define AB26 “arduino_up/button_26”
#define AB27 “arduino_up/button_27”
#define AB28 “arduino_up/button_28”
#define AB29 “arduino_up/button_29”
#define AB30 “arduino_up/button_30”

#define SUB_LED “arduino_up/led”
#define PUBLISH_DELAY 8000

#define DHTPIN 3
#define DHTTYPE DHT22

#define ledPin 5

//Button section
bool statusBtn1 = HIGH; // Sets statusBtn1 High as True and Low as False
bool stateBtn1 = 0; // Sets state (used for button) Low as True and High as False
bool statusBtn2 = HIGH;
bool stateBtn2 = 0;
bool statusBtn3 = HIGH;
bool stateBtn3 = 0;
bool statusBtn4 = HIGH;
bool stateBtn4 = 0;
bool statusBtn5 = HIGH;
bool stateBtn5 = 0;
bool statusBtn6 = HIGH;
bool stateBtn6 = 0;
bool statusBtn7 = HIGH;
bool stateBtn7 = 0;
bool statusBtn8 = HIGH;
bool stateBtn8 = 0;
bool statusBtn9 = HIGH;
bool stateBtn9 = 0;
bool statusBtn10 = HIGH;
bool stateBtn10 = 0;
bool statusBtn11 = HIGH;
bool stateBtn11 = 0;
bool statusBtn12 = HIGH;
bool stateBtn12 = 0;
bool statusBtn13 = HIGH;
bool stateBtn13 = 0;
bool statusBtn14 = HIGH;
bool stateBtn14 = 0;
bool statusBtn15 = HIGH;
bool stateBtn15 = 0;
bool statusBtn16 = HIGH;
bool stateBtn16 = 0;
bool statusBtn17 = HIGH;
bool stateBtn17 = 0;
bool statusBtn18 = HIGH;
bool stateBtn18 = 0;
bool statusBtn19 = HIGH;
bool stateBtn19 = 0;
bool statusBtn20 = HIGH;
bool stateBtn20 = 0;
bool statusBtn21 = HIGH;
bool stateBtn21 = 0;
bool statusBtn22 = HIGH;
bool stateBtn22 = 0;
bool statusBtn23 = HIGH;
bool stateBtn23 = 0;
bool statusBtn24 = HIGH;
bool stateBtn24 = 0;
bool statusBtn25 = HIGH;
bool stateBtn25 = 0;
bool statusBtn26 = HIGH;
bool stateBtn26 = 0;
bool statusBtn27 = HIGH;
bool stateBtn27 = 0;
bool statusBtn28 = HIGH;
bool stateBtn28 = 0;
bool statusBtn29 = HIGH;
bool stateBtn29 = 0;
bool statusBtn30 = HIGH;
bool stateBtn30 = 0;

int btn1 = 22;
int btn2 = 23;
int btn3 = 24;
int btn4 = 25;
int btn5 = 26;
int btn6 = 27;
int btn7 = 28;
int btn8 = 29;
int btn9 = 30;
int btn10 = 31;
int btn11 = 32;
int btn12 = 33;
int btn13 = 34;
int btn14 = 35;
int btn15 = 36;
int btn16 = 37;
int btn17 = 38;
int btn18 = 39;
int btn19 = 40;
int btn20 = 41;
int btn21 = 42;
int btn22 = 43;
int btn23 = 44;
int btn24 = 45;
int btn25 = 46;
int btn26 = 47;
int btn27 = 48;
int btn28 = 49;
int btn29 = 50;
int btn30 = 51;

// Networking details
byte mac[] = { 0xB8, 0x27, 0xEB, 0x03, 0x9A, 0x25 }; // Ethernet shield (W5100) MAC address
IPAddress ip(192, 168, 178, 30); // Ethernet shield (W5100) IP address
IPAddress server(192, 168, 178, 40); // MTTQ server IP address
EthernetClient ethClient; // EthernetClient shortened to ethClient
PubSubClient mqttClient; //PubSubClient shortened to mqttClient

DHT dht(DHTPIN, DHTTYPE); //Temp and Humidity Sensor on DHTPIN(40 as defined), DHTTYPE(DHT22 as defined)
long previousMillis = 0; //Sets previousMillis as a long variable (0 not relevant, but setsit initially as 0)

void setup() {
pinMode(btn1, INPUT_PULLUP); //turns on internal pull up resistor
pinMode(btn2, INPUT_PULLUP);
pinMode(btn3, INPUT_PULLUP);
pinMode(btn4, INPUT_PULLUP);
pinMode(btn5, INPUT_PULLUP);
pinMode(btn6, INPUT_PULLUP);
pinMode(btn7, INPUT_PULLUP);
pinMode(btn8, INPUT_PULLUP);
pinMode(btn9, INPUT_PULLUP);
pinMode(btn10, INPUT_PULLUP);
pinMode(btn11, INPUT_PULLUP);
pinMode(btn12, INPUT_PULLUP);
pinMode(btn13, INPUT_PULLUP);
pinMode(btn14, INPUT_PULLUP);
pinMode(btn15, INPUT_PULLUP);
pinMode(btn16, INPUT_PULLUP);
pinMode(btn17, INPUT_PULLUP);
pinMode(btn18, INPUT_PULLUP);
pinMode(btn19, INPUT_PULLUP);
pinMode(btn20, INPUT_PULLUP);
pinMode(btn21, INPUT_PULLUP);
pinMode(btn22, INPUT_PULLUP);
pinMode(btn23, INPUT_PULLUP);
pinMode(btn24, INPUT_PULLUP);
pinMode(btn25, INPUT_PULLUP);
pinMode(btn26, INPUT_PULLUP);
pinMode(btn27, INPUT_PULLUP);
pinMode(btn28, INPUT_PULLUP);
pinMode(btn29, INPUT_PULLUP);
pinMode(btn30, INPUT_PULLUP);

pinMode(ledPin, OUTPUT); //sets pin as an output
digitalWrite(ledPin, HIGH); //These 3 lines flash
delay (1000); //the led on and
digitalWrite(ledPin, LOW); //off at start up

// setup serial communication
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println(F(“MQTT Arduino Up Serial Connected”)); // prints text to serial
Serial.println(); //leaves a gap on serial print

// Ethernet shield configuration
Ethernet.begin(mac, ip); //Start ethernet using defined “mac” and “ip”

// setup mqtt client
mqttClient.setClient(ethClient); //tells mqtt to set ethernetclient as client
mqttClient.setServer(server, 1883); //tells mqtt to set defined “server” as server on port 1883
mqttClient.setCallback(callback); //sets up the callback block for use

// setup DHT sensor
dht.begin(); //tells the DHT sensor to start

Serial.println(“Client IP”);
Serial.println(ip);
Serial.println();
Serial.println(“Server IP”);
Serial.println(server);
Serial.println();
previousMillis = millis(); //long variable “previousMillis” = millis (time)
}

void loop() {
if (!mqttClient.connected())
reconnect();

statusBtn1 = digitalRead (btn1);// Button1
statusBtn2 = digitalRead (btn2);
statusBtn3 = digitalRead (btn3);
statusBtn4 = digitalRead (btn4);
statusBtn5 = digitalRead (btn5);
statusBtn6 = digitalRead (btn6);
statusBtn7 = digitalRead (btn7);
statusBtn8 = digitalRead (btn8);
statusBtn9 = digitalRead (btn9);
statusBtn10 = digitalRead (btn10);
statusBtn11 = digitalRead (btn11);
statusBtn12 = digitalRead (btn12);
statusBtn13 = digitalRead (btn13);
statusBtn14 = digitalRead (btn14);
statusBtn15 = digitalRead (btn15);
statusBtn16 = digitalRead (btn16);
statusBtn17 = digitalRead (btn17);
statusBtn18 = digitalRead (btn18);
statusBtn19 = digitalRead (btn19);
statusBtn20 = digitalRead (btn20);
statusBtn21 = digitalRead (btn21);
statusBtn22 = digitalRead (btn22);
statusBtn23 = digitalRead (btn23);
statusBtn24 = digitalRead (btn24);
statusBtn25 = digitalRead (btn25);
statusBtn26 = digitalRead (btn26);
statusBtn27 = digitalRead (btn27);
statusBtn28 = digitalRead (btn28);
statusBtn29 = digitalRead (btn29);
statusBtn30 = digitalRead (btn30);

// send new data?
if (millis() - previousMillis > PUBLISH_DELAY) {
sendData();
previousMillis = millis();
}

mqttClient.loop();
// if the switch is being pressed
if (statusBtn1 == 0)
{
stateBtn1 = !stateBtn1; //toggle state
if (stateBtn1 == 1) // ON
{
mqttClient.publish(AB1, “Pressed”);
Serial.println((String)AB1 + " => Pressed");
Serial.println();
}

}

// if the switch is being pressed
if (statusBtn2 == 0)
{
stateBtn2 = !stateBtn2; //toggle state
if (stateBtn2 == 1) // ON
{
mqttClient.publish(AB2, “Pressed”);
Serial.println((String)AB2 + " => Pressed");
Serial.println();

}

}
// if the switch is being pressed
if (statusBtn3 == 0)
{
stateBtn3 = !stateBtn3; //toggle state
if (stateBtn3 == 1) // ON
{
mqttClient.publish(AB3, “Pressed”);
Serial.println((String)AB3 + " => Pressed");
delay (200); ////////////// Edited for Volume or hold type buttons
Serial.println();
mqttClient.publish(AB3, “Waiting”); ////////////// Edited for Volume or hold type buttons
}

}
// if the switch is being pressed
if (statusBtn4 == 0)
{
stateBtn4 = !stateBtn4; //toggle state
if (stateBtn4 == 1) // ON
{
mqttClient.publish(AB4, “Pressed”);
Serial.println((String)AB4 + " => Pressed");
delay (200); ////////////// Edited for Volume or hold type buttons
Serial.println();
mqttClient.publish(AB3, “Waiting”); ////////////// Edited for Volume or hold type buttons
}
}

// if the switch is being pressed
if (statusBtn5 == 0)
{
stateBtn5 = !stateBtn5; //toggle state
if (stateBtn5 == 1) // ON
{
mqttClient.publish(AB5, “Pressed”);
Serial.println((String)AB5 + " => Pressed");
Serial.println();
}
}

// if the switch is being pressed
if (statusBtn6 == 0)
{
stateBtn6 = !stateBtn6; //toggle state
if (stateBtn6 == 1) // ON
{
mqttClient.publish(AB6, “Pressed”);
Serial.println((String)AB6 + " => Pressed");
Serial.println();
}

}

// if the switch is being pressed
if (statusBtn7 == 0)
{
stateBtn7 = !stateBtn7; //toggle state
if (stateBtn7 == 1) // ON
{
mqttClient.publish(AB7, “Pressed”);
Serial.println((String)AB7 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn8 == 0)
{
stateBtn8 = !stateBtn8; //toggle state
if (stateBtn8 == 1) // ON
{
mqttClient.publish(AB8, “Pressed”);
Serial.println((String)AB8 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn9 == 0)
{
stateBtn9 = !stateBtn9; //toggle state
if (stateBtn9 == 1) // ON
{
mqttClient.publish(AB9, “Pressed”);
Serial.println((String)AB9 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn10 == 0)
{
stateBtn10 = !stateBtn10; //toggle state
if (stateBtn10 == 1) // ON
{
mqttClient.publish(AB10, “Pressed”);
Serial.println((String)AB10 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn11 == 0)
{
stateBtn11 = !stateBtn11; //toggle state
if (stateBtn11 == 1) // ON
{
mqttClient.publish(AB11, “Pressed”);
Serial.println((String)AB11 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn12 == 0)
{
stateBtn12 = !stateBtn12; //toggle state
if (stateBtn12 == 1) // ON
{
mqttClient.publish(AB12, “Pressed”);
Serial.println((String)AB12 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn13 == 0)
{
stateBtn13 = !stateBtn13; //toggle state
if (stateBtn13 == 1) // ON
{
mqttClient.publish(AB13, “Pressed”);
Serial.println((String)AB13 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn14 == 0)
{
stateBtn14 = !stateBtn14; //toggle state
if (stateBtn14 == 1) // ON
{
mqttClient.publish(AB14, “Pressed”);
Serial.println((String)AB14 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn15 == 0)
{
stateBtn15 = !stateBtn15; //toggle state
if (stateBtn15 == 1) // ON
{
mqttClient.publish(AB15, “Pressed”);
Serial.println((String)AB15 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn16 == 0)
{
stateBtn16 = !stateBtn16; //toggle state
if (stateBtn16 == 1) // ON
{
mqttClient.publish(AB16, “Pressed”);
Serial.println((String)AB16 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn17 == 0)
{
stateBtn17 = !stateBtn17; //toggle state
if (stateBtn17 == 1) // ON
{
mqttClient.publish(AB17, “Pressed”);
Serial.println((String)AB17 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn18 == 0)
{
stateBtn18 = !stateBtn18; //toggle state
if (stateBtn4 == 1) // ON
{
mqttClient.publish(AB18, “Pressed”);
Serial.println((String)AB16 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn19 == 0)
{
stateBtn19 = !stateBtn19; //toggle state
if (stateBtn19 == 1) // ON
{
mqttClient.publish(AB19, “Pressed”);
Serial.println((String)AB19 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn20 == 0)
{
stateBtn20 = !stateBtn20; //toggle state
if (stateBtn20 == 1) // ON
{
mqttClient.publish(AB20, “Pressed”);
Serial.println((String)AB20 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn21 == 0)
{
stateBtn21 = !stateBtn21; //toggle state
if (stateBtn21 == 1) // ON
{
mqttClient.publish(AB21, “Pressed”);
Serial.println((String)AB21 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn22 == 0)
{
stateBtn22 = !stateBtn22; //toggle state
if (stateBtn22 == 1) // ON
{
mqttClient.publish(AB22, “Pressed”);
Serial.println((String)AB22 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn23 == 0)
{
stateBtn23 = !stateBtn23; //toggle state
if (stateBtn23 == 1) // ON
{
mqttClient.publish(AB23, “Pressed”);
Serial.println((String)AB23 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn24 == 0)
{
stateBtn24 = !stateBtn24; //toggle state
if (stateBtn24 == 1) // ON
{
mqttClient.publish(AB24, “Pressed”);
Serial.println((String)AB24 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn25 == 0)
{
stateBtn25 = !stateBtn25; //toggle state
if (stateBtn25 == 1) // ON
{
mqttClient.publish(AB25, “Pressed”);
Serial.println((String)AB25 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn26 == 0)
{
stateBtn26 = !stateBtn26; //toggle state
if (stateBtn26 == 1) // ON
{
mqttClient.publish(AB26, “Pressed”);
Serial.println((String)AB26 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn27 == 0)
{
stateBtn27 = !stateBtn27; //toggle state
if (stateBtn27 == 1) // ON
{
mqttClient.publish(AB27, “Pressed”);
Serial.println((String)AB27 + " => Pressed");
Serial.println();
}
}
// if the switch is being pressed
if (statusBtn28 == 0)
{
stateBtn28 = !stateBtn28; //toggle state
if (stateBtn28 == 1) // ON
{
mqttClient.publish(AB28, “Pressed”);
Serial.println((String)AB28 + " => Pressed");
Serial.println();
}
}

// if the switch is being pressed
if (statusBtn29 == 0)
{
stateBtn29 = !stateBtn29; //toggle state
if (stateBtn29 == 1) // ON
{
mqttClient.publish(AB29, “Pressed”);
Serial.println((String)AB29 + " => Pressed");
Serial.println();
}
}

// if the switch is being pressed
if (statusBtn30 == 0)
{
stateBtn30 = !stateBtn30; //toggle state
if (stateBtn30 == 1) // ON
{
mqttClient.publish(AB30, “Pressed”);
Serial.println((String)AB30 + " => Pressed");
Serial.println();
}

}
}

void reconnect()
{
// Loop until reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection 
 ");
// Attempt to connect
if (mqttClient.connect(CLIENT_ID)) {
Serial.println(“connected”);
Serial.println();
// (re)subscribe
} else {
Serial.print(“Connection failed, state: “);
Serial.print(mqttClient.state());
Serial.println(”, retrying in 0.2 seconds”);
delay(200); // Wait 0.2 seconds before retrying
}
}
}

void sendData() {
char msgBuffer[20];
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Temperature ");
Serial.print(t);
Serial.println(“c”);
Serial.print(“Humidity “);
Serial.print(h);
Serial.println(”%”);
Serial.println();
if (mqttClient.connect(CLIENT_ID)) {
mqttClient.publish(AT1, dtostrf(t, 6, 2, msgBuffer));
mqttClient.publish(AH1, dtostrf(h, 6, 2, msgBuffer));
mqttClient.subscribe(SUB_LED);
mqttClient.publish(AB1, (statusBtn1 == HIGH) ? “Waiting” : “Pressed”); //Allows real time check on HA after pressing due to delay
mqttClient.publish(AB2, (statusBtn2 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB3, (statusBtn3 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB4, (statusBtn4 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB5, (statusBtn5 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB6, (statusBtn6 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB7, (statusBtn7 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB8, (statusBtn8 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB9, (statusBtn9 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB10, (statusBtn10 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB11, (statusBtn11 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB12, (statusBtn12 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB13, (statusBtn13 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB14, (statusBtn14 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB15, (statusBtn15 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB16, (statusBtn16 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB17, (statusBtn17 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB18, (statusBtn18 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB19, (statusBtn19 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB20, (statusBtn20 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB21, (statusBtn21 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB22, (statusBtn22 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB23, (statusBtn23 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB24, (statusBtn24 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB25, (statusBtn25 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB26, (statusBtn26 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB27, (statusBtn27 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB28, (statusBtn28 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB29, (statusBtn29 == HIGH) ? “Waiting” : “Pressed”);
mqttClient.publish(AB30, (statusBtn30 == HIGH) ? “Waiting” : “Pressed”);

}
}

void callback(char* topic, byte* payload, unsigned int length) {

//LED Bits
Serial.print("[sub: “);
Serial.print(topic);
Serial.print(”] ");
char message[length + 1] = “”;
for (int i = 0; i < length; i++)
message[i] = (char)payload[i];
message[length] = ‘\0’;
Serial.println(message);

// SUB_LED topic section
if (strcmp(topic, SUB_LED) == 0)
{
if (strcmp(message, “on”) == 0)
digitalWrite(ledPin, HIGH);
if (strcmp(message, “off”) == 0)
digitalWrite(ledPin, LOW);
}

}

I wonder how the sensor claims 20M distance when I see issues with 12" of wire? It may depend on supply voltage. All of the modules I use are 3.3V. 5V might make the difference.

For the code above, grab another Arduino and fire up the Arduino tutorials on Arduino.cc regarding arrays and loops.

Hi, I’ve created something similar too. Node MCU with 4-button ethernet port and 4 relays. Each button operates the corresponding relay. The problem is that when the server is powered MQTT everything goes perfectly. But if the MQTT server is turned off, the physical buttons will not work either. Can anyone help me correct the sketch!

if you are communicating via mqtt then the mqtt server needs to be running.

You could try esphome which is not reliant on mqtt.

If I send you the sketch, can you help me compile it? Remove MQTT and insert ESphome? I don’t know esphome very well

It sounds like your sketch is working. Just stop turning mqtt off.

I can’t take the risk that if you turn off the MQTT server for any reason, I can no longer close an awning. So I want to abandon MQTT in favor of EspHome

Ok, you’re still reliant on ha though.

I’ll help with your esphome. Tell us about the components and which gpios they connect to.