khoih-prog / AsyncMQTT_Generic

Arduino Arduino Library for ESP8266, ESP32, Portenta_H7, STM32 and RP2040W asynchronous MQTT client implementation. This library, ported to support ESP32, WT32_ETH01 (ESP32 + LAN8720), ESP8266, Portenta_H7 (Ethernet or WiFi) and STM32 (LAN8742A or LAN8720 Ethernet), Teensy 4.1 using QNEthernet, RASPBERRY_PI_PICO_W with CYW43439 WiFi. Currently supporting TLS/SSL for ESP32 only
MIT License
64 stars 10 forks source link

[OBSERVED] disconnection after router reboot ESP8266 : with 512 ESP MIN MEMORY paramter #26

Closed ajinkya-open closed 1 year ago

ajinkya-open commented 1 year ago

Describe the bug

A clear and concise description of what the bug is. my observation: following configuration with 512 helps mqtt to get re-connect again when router is restarted and net gets online, but when i put 4096 , it dont reconnect automatically in file : AsyncMqttClient_Generic.hpp

ifndef MQTT_MIN_FREE_MEMORY

// #define MQTT_MIN_FREE_MEMORY 4096

define MQTT_MIN_FREE_MEMORY 512

endif

Steps to Reproduce

to reproduce the error following is the hardware

Chip is ESP8266EX Features: WiFi Crystal is 26MHz

Uploading stub... Running stub... Stub running... Changing baud rate to 460800 Changed. Configuring flash size...

Expected behavior

expected behaviour should be it should autoconnect with 4096 value [ i may be not aware of that #define parameter, please enlighten is i am being blind to any obvious thing, thanks ]

Actual behavior

behaviour should be autoconected after router it rebooted , you can check this scene by running library on esp8266 as above and then reboot router from web ui control panel, and observe the disconnection in serial logs

Debug and AT-command log (if applicable)

[ logs when 512 size connect logs ]

12:58:55:641] Connecting to MQTT...␍␊
[12:58:55:666] <removed ip address here intentionally>␍␊
[12:58:55:823] Connected to MQTT broker: [MAIN] Free heap: 38248 bytes␊
[12:59:02:691] [MAIN] Free heap: 38656 bytes␊
[12:59:07:692] [MAIN] Free heap: 38656 bytes␊

[ log when 4096] first it connected automtically but when in restart it it show freq disconnection

[13:11:04:773] Connecting to MQTT...␍␊
[13:11:04:797] <broker hidden>
[13:11:04:825] Connected to MQTT broker: [MAIN] Free heap: 40656 bytes␊
[13:11:14:435] [MAIN] Free heap: 40656 bytes␊
[13:11:19:437] [MAIN] Free heap: 40656 bytes␊
[13:11:24:437] [MAIN] Free heap: 40656 bytes␊
[13:11:29:439] [MAIN] Free heap: 37456 bytes␊
[13:11:34:440] [MAIN] Free heap: 37456 bytes␊
[13:11:38:925] Disconnected from MQTT.␍␊
[13:11:39:441] [MAIN] Free heap: 41816 bytes␊
[13:11:44:442] [MAIN] Free heap: 40144 bytes␊
[13:11:49:590] [MAIN] Free heap: 40144 bytes␊
[13:11:52:490] 192.168.0.102␍␊
[13:11:52:507] Connecting to MQTT...␍␊
[13:11:52:531] <broker hidden>
[13:11:54:591] [MAIN] Free heap: 37224 bytes␊
[13:11:59:515] Disconnected from MQTT.␍␊
[13:11:59:592] [MAIN] Free heap: 38448 bytes␊
[13:12:01:515] Connecting to MQTT...␍␊
[13:12:01:541] <broker hidden>
[13:12:04:593] [MAIN] Free heap: 38448 bytes␊
[13:12:08:515] Disconnected from MQTT.␍␊
[13:12:09:594] [MAIN] Free heap: 38448 bytes␊
[13:12:10:515] Connecting to MQTT...␍␊
[13:12:10:546] <broker hidden>
[13:12:14:596] [MAIN] Free heap: 38448 bytes␊

Screenshots

NA

Information

Please ensure to specify the following:

my included libraries:

#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqtt_Generic.h>
#include <Hash.h>

#include <ESP8266WiFiMulti.h>
#include <ESPAsyncWebServer.h>
#include "fauxmoESP.h"
#include <DNSServer.h>
#include <PubSubClient.h>
#include <FS.h>
#include <ArduinoJson.h>

platform ini file as below

#platform = ${common.esp8266_platform_2_4_0}
platform = ${common.esp8266_platform_2_7_0}
# platform = ${common.esp8266_platform_2_4_1}
board = esp01_1m
#board = esp01
#board = esp12e
#board = esp01
#board_build.f_cpu = 80000000L

#for BTX type ic Z logo ic
board_build.flash_mode = dout 
build_flags = 
    -g 
    #-D DEBUG_FAUXMO=Serial 
    #-D  DEBUG_FAUXMO_VERBOSE_TCP=0 
    #-D  DEBUG_FAUXMO_VERBOSE_UDP=0 
    -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH
    #-D  PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH
    #-D PUYA_SUPPORT=1
    #-D XMC_SUPPORT=1
lib_deps =   
    vintlabs/FauxmoESP @ 3.4
    bblanchon/ArduinoJson @ 6.12
    knolleary/PubSubClient @ 2.8
    me-no-dev/ESP Async WebServer @ 1.2.2
    ; me-no-dev/ESPAsyncTCP @ 1.2.2
    me-no-dev/AsyncTCP @ 1.1.1
    khoih-prog/AsyncMQTT_Generic @ 1.8.0

code snippets from my code

void onMqttConnect(bool sessionPresent)
{
    String node = "client" + String(ESP.getChipId());
    String nodeid = node + " " + WiFi.localIP().toString();

    node = String(ESP.getChipId());

    String topicx = getParameterFromJson("topicname", "/config.json");
    String nodex = "{\"node\":\"" + node + "\",\"topic\":\"" + topicx + "\",\"client\":\"" + nodeid + "\"}";
    mqttClient.publish("modelStatus", 0, false, nodex.c_str());

    mqttClient.subscribe(topicx.c_str(), 2);

    Serial.print("Connected to MQTT broker: ");
}

void connectToMqtt()
{
    Serial.println("Connecting to MQTT...");
    // mqttClient.disconnect();
    mqttClient.connect();
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
    (void)reason;

    Serial.println("Disconnected from MQTT.");

    if (WiFi.isConnected())
    {
        mqttReconnectTimer.once(2, connectToMqtt);
    }
}

//in setup>>

wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
// mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// String broker = getParameterFromJson("broker","/config.json");
// uint16_t port = 1883;
// Serial.println(broker);
mqttClient.setServer("brokerdomain_name", 1883);
// mqttClient.setServer("192.168.0.110", 1883);

connectToWifi();
khoih-prog commented 1 year ago

There is some issue with your code, but I won't have time to figure out why

The real issue is you don;t have enough memory to run your code

Carefully look at the library code for ESP8266

https://github.com/khoih-prog/AsyncMQTT_Generic/blob/fdf0c2a138c66f9ac4343454b9f4b3a1c55ea969/src/AsyncMqttClient_Generic.hpp#L235

https://github.com/khoih-prog/AsyncMQTT_Generic/blob/fdf0c2a138c66f9ac4343454b9f4b3a1c55ea969/src/AsyncMqttClient_Generic_Impl.h#L1331-L1332

from

uint16_t AsyncMqttClient::publish(const char* topic, uint8_t qos, bool retain, const char* payload, size_t length,
                                  bool dup, uint16_t message_id)
{
  (void) dup;
  (void) message_id;

  if (_state != CONNECTED || GET_FREE_MEMORY() < MQTT_MIN_FREE_MEMORY)
    return 0;

  AMQTT_LOGINFO("PUBLISH");

  AsyncMqttClientInternals::OutPacket* msg = new AsyncMqttClientInternals::PublishOutPacket(topic, qos, retain, payload,
                                                                                            length);
  _addBack(msg);

  return msg->packetId();
}

If you don't have enough Free Memory =>GET_FREE_MEMORY() < 4096 will happen in extreme cases while GET_FREE_MEMORY() < 512 might not happen.

Try using ESP32 with larger memory size.

Good Luck,