knolleary / pubsubclient

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

ESP8266 frequent mqtt connection break and reconnecting problems. #825

Open bogus105 opened 3 years ago

bogus105 commented 3 years ago

Hi, as in the topic. My broker is Mosquitto in docker, run on RB Pi4 on latest IOTstack (main blocks Mosquitto, Node-Red, influxDB, Grafana). The client is ESP01 which, apart from wifi/mqtt job talks over i2c to MCP23017 (16-bit) port expander, ADS115 (4-ch, 16-bit) ADC and BME280 (temp, press, hum sensor). Port expander control two HP4067 MUXes so i can read 32 voltages from MCP23017/HP4067/ADS1115 combo and environmental data from BME280. I need to publish these data periodically do the broker. So far i'm doing it "on purpose" - node-red publish request which esp01 is subscribed to. After studying many similar threads here about reliability problems i have modified my code so all the i2c operations are processed in the main loop, no unnecessary calculation, just raw values to be sent (calculations will be performed in node-red). Serial monitor show data very quickly (250000bps serial link speed). Wifi connection is quite fast(?) - within 3 seconds, then mqtt broker almost instantaneously. I have no publish code in callback function. Just in reconnect function, and main loop when particular data is requested from the broker. The only jobs in callback function are receive topic and payload, change payload to string (String (capital S) object), and few comparisons with preset, expected topics/payloads and setting few global boolean flags accordingly, to trigger data reading and publishing in the main loop. Publish frequency i tested was 1s to 1 minute (periodic publishing from node-red). At the beginning, after reset, all is ok. After some minutes i can see in the serial console more and more frequent mqtt connection drops with rc=-2. i set a variable that counts there events starting from zero on esp01 reset. Withing, say 30 minutes, i can "achieve" like 200 mqtt connection breaks and more retries (i've set mqtt reconnection period 2 seconds). Tried playing with KeepAlive as well - from 5 seconds, 10s, 15s and 60s. I don't see improvement. I've schecked PubSubClient.cpp line #122 - my library has "yield();" already.

I'm thinking to change logic now: node-red to publish "data update interval" value on first connection so esp01 will take it, publish all the readings, disconnect and reconnect after the "data update interval" time elapsed. That way i can still manage how often to receive data (and maybe what data i need, i.e. BME280 data more often, 32channels voltage less). But it's not wha i want. I'm interested in knowing the issue with esp/library/sketch. Wifi connection is stable, haven't seen a single break down so far. All the workload not related to wifi/pubsubclient is not elaborative at all. I know esp01 uses serial uart which i'm using to debug in serial monitor (i'll try to make a version with no serial at all to check if it change something). I2c transmission is stable (Wire.h lib). I don't have reset problems, i'm using "yield();" method quite frequently as well as "client.loop();" in the main loop. WHAT ELSE can i do to make it reliable. Desperate:/

Thank you in advance for input. bogi

bogus105 commented 3 years ago

Additional info: I have only one esp01 in the system as the only client so it is not about clientId conflict.

hutje commented 3 years ago

Duplicate of #795