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