Hi,
I just can't seem to find a lot on internet dealing with the issue i have.
So i hope some here can help me out.
When i publish a state command to de MQTT broker i want it to have the flag retain=true.
But i can't seem to get it to work.
Now every time my device (wemos) or mqtt broker (HASSIO) get rebooted the default state gets to "OPEN". and this opens my front door :o
So it is very important to get the retain flag to true from the wemos to HASSIO.
On internet i found that this should work:
client.publish("ha/voordeur/state", "open",true);
But it doesn't. I still get retain=false when using HASSIO-> MQTT developer tab listening tool.
This is my whole sketch:
/////////////////////////////WIFI MANAGER///////////////////////////////////////////////////////
#include <FS.h> //this needs to be first, or it all crashes and burns...
#if defined(ESP8266)
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#else
#include <WiFi.h> //https://github.com/esp8266/Arduino
#endif
//needed for library
#include <DNSServer.h>
#if defined(ESP8266)
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#endif
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
////////////////////////////////////////////////////////BLINK INTERNAL LED///////////////////////////////
const int INTERNALledPin = D4;
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long interval = 200; // interval at which to blink (milliseconds)
//////////////////////////////////////////////////////////RESET WIFI BUTTON//////////////////////////////
#define BUTTON_PIN D7 //> button pin on esp8266
int ButtonState;
///////////////////////////////////////////////////////////////DOOR//////////////////////////////
boolean OpenDoorProcedure = false;
////////////////////////////////////////////////////////////MQTT///////////////////////////////////////////
#include <PubSubClient.h>
const char* mqtt_server = "192.168.2.12";
WiFiClient espClient;
PubSubClient client(espClient);
String Door_open;
String strTopic;
String strPayload;
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
strTopic = String((char*)topic);
if (strTopic == "ha/voordeur/set")
{
Door_open = String((char*)payload);
Serial.println(Door_open);
if (Door_open == "OPEN")
{
OpenDoorProcedure = true;
//client.publish("ha/voordeur/state", "OPEN");
}
else
{
Serial.println("CLOSE_123");
//OpenDoorProcedure = false;
}
}
}
long lastReconnectAttempt = 0;
boolean reconnect() {
if (client.connect("ESP8266_voordeur", "admin", "admin")) {
// Once connected, publish an announcement...
Serial.println("MQTT connected");
//client.publish("ha/voordeur/state", "closed");
//client.publish("ha/voordeur/set", "CLOSE");
// ... and resubscribe
client.subscribe("ha/#");
//client.publish("ha/voordeur/state", "closed");
}
return client.connected();
}
void setup(void) {
delay(1000);
Serial.begin(9600);
Serial.println();
delay(1000);
OpenDoorProcedure = false;
///////////////////////////////////////PINMODES///////////////////////////////////////
pinMode(D3, OUTPUT);//Doorlock
pinMode(INTERNALledPin, OUTPUT);// internal led
digitalWrite(D3, LOW);
digitalWrite(INTERNALledPin, LOW);
pinMode(BUTTON_PIN, INPUT_PULLUP);
//////////////////////////////////////////WiFiManager////////////////////////////////////////////////
WiFiManager wifiManager;
wifiManager.setBreakAfterConfig(true);
//////////////////////////////RESET WIFI//////////////////////
ButtonState = digitalRead(BUTTON_PIN);
Serial.println(ButtonState);
if ( ButtonState == LOW ) {
Serial.print("Reset");
wifiManager.resetSettings();
delay(2000);
}
//////////////////////////WifiManager part 2////////////////
if (!wifiManager.autoConnect("AutoConnectAP")) {
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.restart();
delay(5000);
}
Serial.println("connected...yeey :)");
Serial.println("local ip");
Serial.println(WiFi.localIP());
/////////////////////////////////////////MQTT SETUP/////////////////////////////////
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
lastReconnectAttempt = 0;
void loop(void)
{
///////////////////////////////MQTT///////////////////////////
if (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
// Attempt to reconnect
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {
//client.publish("ha/voordeur/state", "closed");
// Client connected
client.loop();
}
///////////////////////////////////////NEXTION////////////////////
if (isPwdSShow == 1)
{
isPwdSShow = 0;
key_pressed_count = 0;
delay(3000);
//digitalWrite(D3, LOW);
}
nexLoop(nex_listen_list);
///////////////////////////////////////////////////
//interal_led ();
//resetwifi ();
OpenDoorProcedureVoid ();
}
void OpenDoorProcedureVoid () {
// Serial.println(OpenDoorProcedure);
if (OpenDoorProcedure == true) {
Serial.println("de deur aan het openen");
client.publish("ha/voordeur/state", "open",true);
digitalWrite(D3, HIGH);
delay (5000);
digitalWrite(D3, LOW);
Serial.println("de deur aan het sluiten");
client.publish("ha/voordeur/state", "closed", true);
//client.publish("ha/voordeur/set", "CLOSE");
OpenDoorProcedure = false;
} else {
OpenDoorProcedure = false;
}
}
void interal_led () {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(INTERNALledPin, ledState);
}
}
void resetwifi () {
ButtonState = digitalRead(BUTTON_PIN);
//Serial.println(ButtonState);
if ( ButtonState == LOW ) {
// Serial.print("Restart WEMOS");
delay(1000);
ESP.restart();
delay(1000);
}
}