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

Client.connected() does not return false if MQTT Broker Pc disconnects from Internet or local network. #1051

Closed ozersenol closed 3 months ago

ozersenol commented 3 months ago

On ESP32 Dev Kit I can connect local pc mqtt broker if all connections are set and works well.

Normally and if I stop mqtt service on pc, loop goes to ESP.restart as it should be. But...

If broker pc disconnects from local network like switching off wifi, ESP32 deos not recognize broker connection is lost.

What could cause this ??

// wm.resetSettings();//hafızadaki wifi ayarlarını temizler
  WiFi.mode(WIFI_STA);  //varsa hafızadaki ağa bağlanır.
  //wm.setTimeout(5);         // OzER ekledi.
  wm.setConnectTimeout(5);  // OzER ekledi.
  wm.setConfigPortalTimeout(120);
  wm.setConnectRetries(5);        // Set the number of connection retries to 3
  wm.setWiFiAutoReconnect(true);  // Enable automatic reconnection

in Setup:

      client2.setServer(offline_mqtt_server, mqtt_port);
      client2.setCallback(LocalCallback);
      client2.setKeepAlive(300);
      client2.setSocketTimeout(5);

in loop:

 if (!client2.connected()) {
     ESP.restart();
      } else {
        client2.loop();
}

Any ideas and help apprecieted. Thanks.

ozersenol commented 3 months ago

it is Duplicate of #896 .