Skip to content

Triggering switch causes disconnect #296

@mintdaniel42

Description

@mintdaniel42

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions