I use an MQTT to communicate with an ESP8266. I'm using cloudmqtt.com and PubSubClient. Sometimes the ESP disconnects from the MQTT and reconnects. I checked the log from cloud and I got this data
2019-02-18 20:11:18: New connection from 89.64.15.184 on port 18127.
2019-02-18 20:11:22: New connection from 89.64.15.184 on port 18127.
2019-02-18 20:11:35: New connection from 89.64.15.184 on port 18127.
2019-02-18 20:11:35: New client connected from 89.64.15.184 as ESP8266Client-80:7D:3A:69:6C:2E (c1, k15, u'userName').
2019-02-18 20:11:53: Client <unknown> has exceeded timeout, disconnecting.
2019-02-18 20:11:53: Socket error on client <unknown>, disconnecting.
2019-02-18 20:11:56: Saving in-memory database to /var/lib/mosquitto/18127/db.
2019-02-18 20:11:58: Client ESP8266Client-80:7D:3A:69:6C:2E has exceeded timeout, disconnecting.
in setup I have this method:
void setupMQTT() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
String clientId = "ESP8266Client-";
clientId += WiFi.macAddress();
if (client.connect(clientId.c_str(), mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("esp/blind/out", "hello world");
client.subscribe("esp/blind/in");
}
and in loop I have
client.loop();
without any delay.
I think that the ESP id in MQTT cloud is unique, because I use the MAC address.
delay(10)
or so inloop
for some ESP chips when using the latest (2.5.0dev) branch – Jaromanda X Feb 19 '19 at 0:42delay(10)
in your main loop or even ayield()
– Jaromanda X Feb 19 '19 at 9:26