Closed sebashb closed 2 years ago
So I will send you configuration its work for me check it i hope that will help you
#include <user_interface.h> <----- important !
#define WM_ASYNC
#define WM_MDNS
#define MQTT_TASK_PRIO 1
#define MQTT_TASK_QUEUE_SIZE 16
#define MQTT_BUF_SIZE 256
#define QUEUE_BUFFER_SIZE 512
const byte DNS_PORT = 53;
DNSServer dns;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "2.de.pool.ntp.org", 3600, 60000); //7200
char _tpm[15];
void setup(){
EEPROM.begin(64); <------ important cause needed in some librarys with WM
if(digitalRead(WPSBUTTON) == LOW)
{
Serial.println("WPS Pairing...");
Serial.println(WiFi.beginWPSConfig() ? "Success" : "Failed");
delay(1000);
}
////Serial.println();
if(digitalRead(WPSBUTTON) == HIGH)
{
wm.setScanDispPerc(true);
wm.setClass("invert"); // dark theme
wm.autoConnect(_tpm);
}
wm.setAPCallback(configModeCallback);
wm.setSaveConfigCallback(saveConfigCallback);
Serial.println(_tpm);
Serial.println("ChipID:");
Serial.println(ESP.getChipId());
if(WiFi.status() == WL_CONNECTED){
if(MDNS.begin(_tpm)){ //Start mDNS with name esp8266
////Serial.println("MDNS started");
MDNS.addService("http", "tcp", 80);
////Serial.println("MDNS Service Started");
MDNS.update();
}
}
WiFi.begin();
MQTT_server_start(1883, 30, 30);
MySimonServer.publish("#", Message);
MySimonServer.publish("inTopic", Message);
MySimonServer.subscribe("outTopic");
MySimonServer.subscribe("/Security/");
MySimonServer.subscribe("/Normal/");
}
I am using my custom librarys thats why its written MySimonServer its should be client or mqttclient. Dont forget about dns server and the best UDP time. Somethimes i got cases ESP dont wanna connect with router cause of not actual hour. Thats happen on new TP_link routers.
And this is onData function for mqtt
virtual void onData(String topic, const char *data, uint32_t length)
{
char data_str[length + 1];
os_memcpy(data_str, data, length);
data_str[length] = '\0';
// Serial.println("received topic '" + topic + "' with data '" + (String)data_str + "'");
if(topic == "/Security/"){
TriggerSecurity = true;
}
if(topic == "/Normal/"){
TriggerSecurity = false;
}
}
// Sample for the usage of the client info methods
also reduce timming for message sending :
char _value [3];
if ((millis() - TimerOffTLast) > TimerOffTNow)
{
MySimonServer.publish("/Topic/", (String)_value, 1, 1);
TimerOffTLast = millis();
}
After updating to version 2.8 my code is working. I'm also using custom libraries that's why I can't share the code.
My code is failing to reconnect to the broker after the AP is rebooted. There must be something different in rebooting the AP and manually blocking the ESP32 MAC Address on the router, because with blocking the code is able to properly reconnect.
The number in the output is the result of
WiFi.status();
which indicates that the ESP32 has successfully reconnected with the WiFi AP.I'm on PubSubClient version 2.7