Hello everyone,
I think I have a basic problem and I miss some basic knowledge to solve it.
Situation:
Wemos D1 mini with ESP8266 connected to home assistant through MQTT wired with:
- DHT11 temperature and humidity sensor
- relay for light control
- Momentary button
I can operate the relay nicely through the hass interface and see the temp en humidity.
When I push the button it also turns on
but… when I push the button i also want the button on the interface to change.
What am I missing in my client.publish()?
Home Assistant config:
sensor:
- platform: mqtt
state_topic: 'home/midden/keuken/temp'
name: 'keuken temperatuur'
unit_of_measurement: '°C'
- platform: mqtt
state_topic: 'home/midden/keuken/humi'
name: 'keuken vochtigheid'
unit_of_measurement: '%'
switch:
- platform: mqtt
name: "keuken spots"
state_topic: "home/midden/keuken/spots"
command_topic: "home/midden/keuken/spots/set"
payload_off: 0
payload_on: 1
qos: 1
Wemos Arduino Code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN D7
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#define LED D3
#define buttonPin D2
int buttonState = 0;
int lightstate = 0;
// Update these with values suitable for your network.
const char* ssid = "Awesomeness";
const char* password = "Fakenews";
const char* mqtt_server = "192.168.178.172";
#define CLIENT_ID "keuken"
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid); //We don't want the ESP to act as an AP
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(125);
digitalWrite(LED_BUILTIN, LOW);
delay(125);
digitalWrite(LED_BUILTIN, HIGH);
delay(125);
digitalWrite(LED_BUILTIN, LOW);
delay(125);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the relay if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(LED, HIGH);
client.publish("home/midden/keuken/spots", "1", true);
int lightstate = 1;
Serial.print("lightstate = ");
Serial.println(lightstate);
} else {
digitalWrite(LED, LOW);
client.publish("home/midden/keuken/spots", "0", true);
int lightstate = 0;
Serial.print("lightstate = ");
Serial.println(lightstate);
}
}
void reconnect() {
// Loop until we're reconnected
digitalWrite(LED_BUILTIN, LOW);
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(CLIENT_ID)) {
Serial.println("connected");
client.subscribe("home/midden/keuken/spots/set");
digitalWrite(LED_BUILTIN, HIGH);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
}
}
void setup(){
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(LED, LOW);
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dht.begin();
}
void loop(){
if (!client.connected()) {
reconnect();
}
client.loop();
// DHT11 temperature and humidity
long now = millis(); // only check every x ms
if (now - lastMsg > 2000) {
lastMsg = now;
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(h) || isnan(t)) {
Serial.println("Impossible de lire la sonde DHT!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
client.publish("home/midden/keuken/temp", String(t).c_str(), false);
client.publish("home/midden/keuken/humi", String(h).c_str(), false);
}
// read button press
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (lightstate == 0){
digitalWrite(LED, HIGH);
client.publish("home/midden/keuken/spots", "1", true);
int lightstate = 1;
Serial.print("lightstate = ");
Serial.println(lightstate);
} else {
digitalWrite(LED, LOW);
client.publish("home/midden/keuken/spots", "0", true);
int lightstate = 0;
Serial.print("lightstate = ");
Serial.println(lightstate);
}
}
}