I use the following code for an MQTT switch on an Arduino UNO. After a few toggles the device no longer responds
#include <Ethernet.h>
#include <ArduinoHA.h>
#define OPENER_PIN 9
#define RING_INPUT_PIN 8
#define BROKER_ADDR IPAddress(192,168,178,55)
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
EthernetClient client;
HADevice device(mac, sizeof(mac));
HAMqtt mqtt(client, device);
HASwitch switchDoorOpener("switchDoorOpener");
void onSwitchCommand(bool state, HASwitch* sender) {
digitalWrite(OPENER_PIN, (state ? HIGH : LOW));
sender -> setState(state); // report state back to the Home Assistant
}
void setup() {
// setup pins
pinMode(OPENER_PIN, OUTPUT);
// initialize pins
digitalWrite(OPENER_PIN, LOW);
// you don't need to verify return status
Ethernet.begin(mac);
// enable last will
//device.enableSharedAvailability();
//device.enableLastWill();
// optional device's details
device.setName("Haustür");
device.setSoftwareVersion("1.0.0");
// optional properties
switchDoorOpener.setIcon("mdi:door-open");
switchDoorOpener.setName("Türöffner");
// callbacks
switchDoorOpener.onCommand(onSwitchCommand);
mqtt.begin(BROKER_ADDR);
}
void loop() {
Ethernet.maintain();
mqtt.loop();
}
I use the following code for an MQTT switch on an Arduino UNO. After a few toggles the device no longer responds