Open ebivan opened 6 years ago
client.loop()
too quickly - try adding a delay(100)
in your main loopclient.loop()
at all. This prevents the client from doing its keep-alive handling so the default keepalive timeout of 15 seconds will expire and disconnect the client.It is not conclusive, but I have had better longevity using:
if(client.state != MQTT_CONNECTED)
rather than:
if(!client.connected())
I say not conclusive, as my disconnects were few and far between (few hours) and haven't put in the time to investigate.
Something new on that topic ? i have the same issue even when this is the one and only client connected to the broker.
Also tried delay, and the connection detection change..
Even this code brings up the reconnects
void loop() {
// Read pin values and check for changes
//if (!mqttClient.connected()) {
if (mqttClient.state() != MQTT_CONNECTED) {
connectToMqtt();
}
mqttClient.loop();
delay(100);
}
Broker logs that :
1678195174: Client MEGA2560_004 closed its connection. 1678195190: New connection from 192.168.1.4:49156 on port 1883. 1678195190: New client connected from 192.168.1.4:49156 as MEGA2560_004 (p2, c1, k15).
Hey there, i'm using pubsubclient to publish sensor values from a BME 280 sensor on an esp8266 board. Everything works fine, but I keep getting disconnects, I think I have messed up my code somewhere.
I thought either my Mosquitto server or pubsubclient would run into timeout, so I addes the last line to send an "online" message every 4.9 seconds but this didn't help. The sensor transmits data, and reconnects, so its kind of working, but its keeps getting disconnectet and reconnect every minute or so, which is bad because Home Assistant expects the "state" to be "online".