ESP32 verbindet sich in setup(), aber client.connected() ist in loop() einige Sekunden später false
Von English mit KI übersetzt ·
Uni-Labor, ESP32 DevKit, PubSubClient, WiFiClientSecure. In setup() verbinde ich mich mit dem WLAN und mit Hermetry, es gibt „MQTT connected“ aus, und der erste Messwert kommt an. Danach liest der loop() einen BME280 aus, veröffentlicht den Wert und ruft delay(30000) auf. Ab dem zweiten Durchlauf ist client.connected() false und client.state() liefert -3. Mein Reconnect-Code verbindet sich erneut, ein weiterer Messwert kommt durch, und das wiederholt sich. Ich bekomme also Messwerte, aber auf der Geräteseite wechselt das Gerät ständig zwischen online und offline.
void loop() {
if (!client.connected()) reconnect();
float t = bme.readTemperature();
snprintf(buf, sizeof buf, "{"temperature":%.2f}", t);
client.publish("v1/telemetry", buf);
delay(30000);
}
Keepalive ist der Standardwert. Ich weiß nicht, was ich falsch mache, es sieht aus wie in jedem Tutorial.
Uni lab, ESP32 DevKit, PubSubClient, WiFiClientSecure. In setup() I connect to Wi-Fi and to Hermetry, it prints „MQTT connected“, and the first reading arrives. Then in loop() I read a BME280, publish, and delay(30000). From the second loop on client.connected() is false and client.state() is -3. My reconnect code connects again, one more reading gets through, and it repeats. So I get readings but the device keeps flipping online/offline on the device page.
void loop() {
if (!client.connected()) reconnect();
float t = bme.readTemperature();
snprintf(buf, sizeof buf, "{"temperature":%.2f}", t);
client.publish("v1/telemetry", buf);
delay(30000);
}
Keepalive is the default. I’m not sure what I’m doing wrong, it looks like every tutorial.
Von English mit KI übersetzt ·
delay(30000) ohne jegliche Verarbeitung anderer Aufgaben. Das ist dein Problem.
delay(30000) with nothing serviced. That’s your problem.
Von English mit KI übersetzt ·
Prüfen Sie außerdem im Log-Tab des Geräts die Gründe für die Verbindungstrennung. Wenn Sie „Taken over by a new session“ sehen, verwendet eine andere Platine dasselbe Token.
Also check the device’s Log tab for the disconnect reasons. If you see „Taken over by a new session“, another board is using the same token.
Von English mit KI übersetzt ·
Tyler hat in einem Satz recht, hier ist die längere Version. PubSubClient liest nur vom Socket und sendet Pings innerhalb von client.loop(). Das Standard-Keepalive beträgt 15 s. Sie schlafen 30 s, also wird kein Ping gesendet, der Broker trennt die Verbindung nach keepalive_timeout, und Status -3 bedeutet „connection lost“.
Blockieren Sie niemals in loop(), verwenden Sie stattdessen einen Timer:
unsigned long last = 0;
void setup() {
...
client.setKeepAlive(60);
client.setBufferSize(512);
}
void loop() {
if (!client.connected()) reconnect(); // with a back-off, not a tight loop
client.loop(); // every pass
if (millis() - last >= 30000) {
last = millis();
snprintf(buf, sizeof buf, "{"temperature":%.2f}", bme.readTemperature());
client.publish("v1/telemetry", buf);
}
}
Ein Keepalive zwischen 30 und 300 s wird laut Dokumentation empfohlen. Und tun Sie, was Niall gesagt hat, der Log-Tab nennt den Grund für jede Trennung.
Tyler’s right in one line, here’s the longer version. PubSubClient only reads from the socket and sends pings inside client.loop(). Its default keepalive is 15 s. You sleep 30 s, so no ping goes out, the broker drops you for keepalive_timeout, and state -3 is „connection lost“.
Never block in loop(), use a timer instead:
unsigned long last = 0;
void setup() {
...
client.setKeepAlive(60);
client.setBufferSize(512);
}
void loop() {
if (!client.connected()) reconnect(); // with a back-off, not a tight loop
client.loop(); // every pass
if (millis() - last >= 30000) {
last = millis();
snprintf(buf, sizeof buf, "{"temperature":%.2f}", bme.readTemperature());
client.publish("v1/telemetry", buf);
}
}
Keepalive between 30 and 300 s is what the docs recommend. And do what Niall said, the Log tab names the reason for every disconnect.
Von English mit KI übersetzt ·
Behoben, aber es waren zwei Dinge! Im Log-Tab stand keepalive_timeout UND „Taken over by a new session“. Mein erster Breadboard-Prototyp war zu Hause noch mit demselben Token an einem USB-Ladegerät angeschlossen. Abgesteckt, auf die millis()-Version umgestellt, und das Gerät ist seit 6 Stunden online.
Fixed, but it was two things! Log tab showed keepalive_timeout AND „Taken over by a new session“. My first breadboard prototype was still plugged into a USB charger at home with the same token. Unplugged it, switched to the millis() version, and the device has been online for 6 hours.
Von English mit KI übersetzt ·
Der Log-Tab erspart einem so viel Raterei. +1
The Log tab saves so much guessing. +1
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

