knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.79k stars 1.46k forks source link

Failed to reconnect to broker after AP Reboot (NO_AP_FOUND) #945

Closed sebashb closed 2 years ago

sebashb commented 2 years ago

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.

[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 5 - STA_DISCONNECTED
[W][WiFiGeneric.cpp:391] _eventCallback(): Reason: 201 - NO_AP_FOUND
Attempting MQTT connection...
[E][WiFiClient.cpp:232] connect(): connect on fd 62, errno: 118, "Host is unreachable"
failed, rc=-2
1
Attempting MQTT connection...
[E][WiFiClient.cpp:232] connect(): connect on fd 63, errno: 118, "Host is unreachable"
failed, rc=-2
1
Attempting MQTT connection...
[E][WiFiClient.cpp:232] connect(): connect on fd 54, errno: 118, "Host is unreachable"
failed, rc=-2
1
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:419] _eventCallback(): STA IP: 192.168.0.49, MASK: 255.255.255.0, GW: 192.168.0.1
Attempting MQTT connection...
[E][WiFiClient.cpp:258] connect(): socket error on fd 56, errno: 113, "Software caused connection abort"
failed, rc=-2
3
Attempting MQTT connection...
[E][WiFiClient.cpp:258] connect(): socket error on fd 57, errno: 113, "Software caused connection abort"
failed, rc=-2
3
Attempting MQTT connection...
[E][WiFiClient.cpp:258] connect(): socket error on fd 58, errno: 113, "Software caused connection abort"
failed, rc=-2
3

I'm on PubSubClient version 2.7

IronBamBam1990 commented 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();
  }
sebashb commented 2 years ago

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.