Imroy / pubsubclient

A client library for the ESP8266 that provides support for MQTT
MIT License
434 stars 115 forks source link

ESP8266 lost MQTT connection, no TCP to reconnect #37

Open Pfannex opened 8 years ago

Pfannex commented 8 years ago

I'm trying to run the PubSubClient with my ESP8266-12e. I run the following Code on my ESP:

/*
 Basic MQTT example 

  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "MySecret.h"

// Update these with values suitable for your network.
IPAddress server(192, 168, 1, 203);
long lastMsg = 0;

void callback(const MQTT::Publish& pub) {
  // handle message arrived
}

WiFiClient wclient;
PubSubClient client(wclient, server);

void setup() {
  // Setup console
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();

  client.set_callback(callback);
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, password);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      Serial.println("MQTT-Client disconnected");
      if (client.connect("arduinoClient")) {
        Serial.println("MQTT-Client connected");
        //client.publish("outTopic","hello world");
        client.subscribe("inTopic");
      }
    }

    if (client.connected())
      client.loop();
  }

    long now = millis();
  if (now - lastMsg > 300000) {
    lastMsg = now;
    client.publish("/30/Status/MQTT","on");
  }

}

Generally the Code runs, but after a while it seams that the ESP lost his TCP-connection to the MQTT-Server.

At this point the debug :

Connecting to Pf@nne-NET...
WiFi connected
MQTT-Client disconnected
MQTT-Client connected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected

At this point the TCP-connection should be renewed, but no network activity will shown on my WireShark monitor!

Maybe, the TCP-Connection is always active. The WiFi-connection schould be stable.

Is the someone who has problems like this? I´m not a C expert, so maybe someone has fun to help me to set some more debug-informations to solve the problem.

The MQTT-broker runs on a Rapberry. Used version of the Mosquitto broker is 1.4.4.

Thaks for your Help.......

mtnbrit commented 8 years ago

Hi, I had an odd incident today where one of my sensors running arduino/esp8266 and lmroy pubsubclient was seemingly connected to the mosquitto server and sending publishes, in my loop I have if (client.connected()), but at the broker side, the last log entry at the time the sensor went down shows a socket error disconnect.

Im wondering how the pubsubclient could go on thinking it was connected and be making pubs for hours while in fact it was not "connected" at the broker side?

Is this the same issue as mentioned where we should really be checking the return value of client.loop() and reconnect if it fails? If so whats the point of client.connected()?