knolleary / pubsubclient

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

LWT not working with Mosquitto #520

Open the-kenny opened 5 years ago

the-kenny commented 5 years ago

I'm writing a fairly simple data logger using a Wemos D1 with an ESP8266 on top.

The following (shortened) code works just fine, but it fails to deliver properly set up the LWT message: If I disconnect the power supply and wait for it to time out on my mosquitto server, no message is published:

#define MQTT_VERSION_3_1_1      // Also tried MQTT_VERSION_3_1

#include <PubSubClient.h>

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

char* ssid = ...;
char* passphrase = ...;
const char* MQTT_BROKER = "10.10.20.75";

PubSubClient mqtt;
WiFiClient espClient;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, passphrase);

  mqtt.setClient(espClient);
  mqtt.setServer(MQTT_BROKER, 1883);
}

const char* statusTopic = "/appliance/washing_machine/status";
const char* willTopic = "/appliance/washing_machine/availability";

void loop() {
  if (!mqtt.connected()) {
    String clientId = ...;
    if (mqtt.connect(clientId.c_str()), NULL, NULL, willTopic, 0, false, "offline", true) {
      mqtt.publish(willTopic, "online");
    }
  }

  mqtt.loop();
  mqtt.publish(statusTopic, ...);

  delay(1000*5);
}

Debug log from mosquitto (showing connect):

1541854587: New connection from 10.10.20.74 on port 1883.
1541854587: New client connected from 10.10.20.74 as washing-mashine (c1, k15).
1541854587: Sending CONNACK to washing-mashine (0, 0)
1541854587: Received PUBLISH from washing-mashine (d0, q0, r0, m0, '/appliance/washing_machine/availability', ... (6 bytes))

Disconnect after timeout:

1541854667: Client washing-mashine has exceeded timeout, disconnecting.
1541854667: Socket error on client washing-mashine, disconnecting.

Any idea why this isn't working?

tackelua commented 5 years ago

I hit a similar problem. Can not send the LWT when PubSubClient::connect() This problem only in version 2.7 The old version 2.6 work ok

djmaze commented 5 years ago

Works as intended for me. With NodeMCU V3, PubSubClient 2.7 and Mosquitto 1.5.

dobseh commented 5 years ago

Did anyone find a fix for this? I've got exactly the same problem as the above.

marcobrianza commented 5 years ago

It does not work for me too. I can see that mosquitto is sending the lastWill message for example killing a MQTT.fx client. @djmaze can you please share your code, maybee we are doing a mistake in using it.

marcobrianza commented 5 years ago

I have gone on in debugging using pubsubclient 2.7 and mosquitto 1.6.4 in mosquitto.conf I have added this configuration line: log_type all in the file /var/log/mosquitto/mosquitto.log we can see everything mosquitto is doing including pings.

with MQTT.fx when the connection is done I can see: New client connected from 172.20.10.2 as 52eb717743ac415ca688b83280dd5e9a (p2, c1, k30). 1565162993: Will message specified (6 bytes) (r0, q1). 1565162993: mqttfx

with esp8266: 1565163968: New client connected from 172.20.10.3 as ESP8266Client-1937 (p2, c1, k15). 1565163968: No will message specified.

I'm using the provided example for esp8266 with the connect line changed to: client.connect(clientId.c_str()), "", "", "endtopic", 1, false, "gone")

I hope this helps to locate the issue.

marcobrianza commented 5 years ago

ok I found my issue, a leftover parenthesys... with this correct line the example works if (client.connect(clientId.c_str(), "", "", "endtopic", 1, true, "gone")) {

I'm very surprised that the previous code could compile!