knolleary / pubsubclient

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

[ESP32-S3] callback function sometimes fail within a day or a week or a month #1021

Open TONYCHOU81905 opened 11 months ago

TONYCHOU81905 commented 11 months ago

Hi everyone,

I faced a strange problem. my callback function fail sometimes within a day and sometimes a week or a month. I checked Wi-Fi, and it worked fine. In my loop, I checked the Wi-Fi status and the mqtt connection status, but when this happens, the debug view shows nothing, which means it doesn't notice that the mqtt is not connected. So I have no idea how to solve this problem.

if anyone faced this problem before, please give me a hand. Thank you in advance!

------loop function------ `void loop() {

if (init_statce == 1) { checkButton(); if (millis() - time2setwifi > 2000) { Serial.println("WAIT_TO_SET_FreeHeap : " + String(ESP.getFreeHeap())); checkButton(); time2setwifi = millis(); } delay(500); } else {

if ((WiFi.status() != WL_CONNECTED) || (!mqtt.connected())) {
  Serial.println("open AP mode");
  WiFi.softAP("xxxxxxxxxx", "bgddsdfgedg");
  delay(1000);
  WiFi.softAP(apssid, appassword);
  Serial.println("熱點信息");
  Serial.print("wifi_ssid:");
  Serial.println(apssid);
  Serial.print("wifi_password:");
  Serial.println(appassword);
  Serial.print("wifi中斷\n");
  delay(1000);
  WiFi.disconnect();
  delay(1000);
  WiFi.begin(sd, pd);
  delay(1000);
  Serial.println("LOOP_WIFI_UNC_FreeHeap : " + String(ESP.getFreeHeap()));
  checkButton();
  net_led(false);
  time2waitwifi = millis();
  WiFi.begin(sd, pd);
  while (WiFi.status() != WL_CONNECTED) {
    delay(2000);
    Serial.print(".");
    checkButton();
    if (millis() - time2waitwifi > 4000) {
      WiFi.disconnect(true);
      delay(1000);
      WiFi.begin(sd, pd);
      delay(1000);
      Serial.println("LOOP_WIFI_UNC_FreeHeap : " + String(ESP.getFreeHeap()));
      Serial.print(".*");
      time2waitwifi = millis();
    }
  }
  esp_task_wdt_reset();
  connectAWS();
  connectFirebase();
}
}

if (millis() - time2ping > 1000 * 60 * 1) {
  checkButton();
  timeClient.update();
  time_t epochTime = timeClient.getEpochTime();
  struct tm* ptm = gmtime((time_t*)&epochTime);
  int monthDay = ptm->tm_mday;
  int currentMonth = ptm->tm_mon + 1;
  int currentYear = ptm->tm_year + 1900;
  String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay) + " " + String(timeClient.getFormattedTime());
  Serial.print("time2ping: ");
  Serial.println(currentDate);
  Serial.println("time2ping_request : " + String(mqtt.connect(THINGNAME.c_str())));
  Serial.println("LOOP_time2ping_FreeHeap : " + String(ESP.getFreeHeap()));
  mqtt.connect(THINGNAME.c_str());
  time2ping = millis();
}

mqtt.loop();
Firebase.ready();
checkButton();
esp_task_wdt_reset();

} } ` -----connect AWS function-----

void connectAWS() {

  client.setCACert(AWS_CERT_CA);
  client.setCertificate(AWS_CERT_CRT);
  client.setPrivateKey(AWS_CERT_PRIVATE);

  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  mqtt.setServer(MQTT_HOST, 8883);

  // Create a message handler
  mqtt.setCallback(callback);
  mqtt.setKeepAlive(60);
  Serial.println("Connecting to AWS IOT");

  while (!mqtt.connected()) {
    Serial.println("Connecting to AWS IoT...");
    Serial.println("AWS2_FreeHeap : " + String(ESP.getFreeHeap()));
    if (mqtt.connect(THINGNAME.c_str())) {
      Serial.println("Connected to AWS IoT!");
      Serial.println("Topic:");
      Serial.println(THINGNAME);
    } else {
      Serial.print("Failed to connect to AWS IoT, rc=");
      Serial.println(mqtt.state());
      delay(2000);
    }
  }

  WiFi.softAPdisconnect(true);
  Serial.println("連線成功 關閉AP模式");

  // Subscribe to a topic
  mqtt.subscribe(AWS_IOT_SUBSCRIBE_TOPIC.c_str());
  net_led(true);
  Serial.println("AWS IoT Connected!");
}
davidhq commented 11 months ago

I'm also experiencing this .... for two years... but now debugging more properly..

it looks like sometimes callback is not invoked but then it is on subsequent messages... so basically messages are lost...

but it also happens that from some point on no messages arrive anymore... days, weeks or months to happen and then reboot of ESP device is needed.

davidhq commented 11 months ago

PS: also check this https://github.com/knolleary/pubsubclient/issues/922 (Callback function stop working after some time on ESP8266 at least.)