Hi all,
A project I did some time ago and borrows heavily from bitluni’s smallest IOT button (details here). It has one annoying issue which is that the doorbell goes off on a HA restart or when I save any automation. The code on the ESP-01 is as follows:
#include <ESP8266WiFi.h>;
#include <PubSubClient.h>;
#define wifi_ssid "SSIDName";
#define wifi_password "Password";
#define mqtt_server "IP_ADDRESS";
#define mqtt_user "mqttUsername";
#define mqtt_password "mqttPassword";
#define clientID "Doorbell";
#define doorbell_topic "home/outdoors/doorbell";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
if (client.connect(clientID, mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish topic...
client.publish(doorbell_topic, "on", true);
Serial.println("Sent doorbell topic");
Serial.println("Waiting 10s...");
delay(10000);
Serial.println("Going to sleep unless I'm woken up");
//sleep until reset
ESP.deepSleep(0);
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address:");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
}
I think I see the problem in the code as the retain is set to true.
Would this cause this issue? Reflashing the ESP-01 is a real pain so wonder if there’s some script I can run after a restart or saving an automation to stop this behaviour? Any ideas?