I’m trying to automate my door bell in the same way some people did in this topic: MQTT Doorbell
I’m using a ESP8266 and this arduino sketch to send the message:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define wifi_ssid "lofoten"
#define wifi_password "mypass"
#define mqtt_server "192.168.2.8"
#define mqtt_user "homeassistant"
#define mqtt_password "mymqttpass"
#define doorbell_topic "hal/doorbell"
int doorbellState = 0;
const int doorbellPin = 2;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
pinMode(doorbellPin, INPUT_PULLUP);
WiFi.mode(WIFI_STA); //don't be a AP
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
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 you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
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() {
if (!client.connected()) {
reconnect();
}
client.loop();
doorbellState = digitalRead(doorbellPin);
if ( doorbellState == LOW ) {
// Put your code here. e.g. connect, send, disconnect.
client.publish(doorbell_topic, "on" , true);
Serial.println("Doorbell is pressed!");
delay( 5000 );
}
}
I see the right debug information in my arduino serial monitor:
Connecting to lofoten
...
WiFi connected
IP address:
192.168.2.28
Attempting MQTT connection...connected
Doorbell is pressed!
In my automations I have this configured:
- alias: Doorbell ringing
trigger:
platform: mqtt
topic: hal/doorbell
payload: 'on'
action:
service: notify.pushbullet
data:
message: "Deurbel!"
and in my configuration I have:
mqtt:
username: homeassistant
password: mymqttpass
But when I trigger the doorbell, I do see the right output in the arduino serial monitor, yet there doesn’t seem to arrive any message in home assistant.
Any idea what could be wrong?