Yet another MQTT doorbell - an ESP-01 with an annoying issue

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?

Yes, I am not sure why you would set retain to be true for a momentary thing like a doorbell. I think re-flashing is your answer. Include some OTA code while you are there :slight_smile:

Thanks! I’ve learned a lot about MQTT since I did this…

I didn’t know anything about OTA flashing and only just knew it was a thing when I started mucking around with Sonoff basics. The doorbell is all soldered up on perfboard so I might just make another and swap it out if there’s no solution with a script that I could cobble together - again I’ve a lot to learn in this respect too.

I’m just looking at OTA flashing - can you do this with an ESP-01 (Ver2.0) 1M Flash?

I am not sure.

Did you fix your problem in the end? I automated my doorbell the other day and went with Frenck’s solution using ESPHOME. However, I ended up modifying the board and code to use I different GPIO than the one he recommended.