paulino / ha-mqtt-entities

Easy Home Assistant MQTT Integration Library
Apache License 2.0
6 stars 1 forks source link

Problems with connection to broker #4

Open proasnet opened 3 months ago

proasnet commented 3 months ago

Dear @paulino , during testing I found that there are occasional problems connecting to the broker. I am sending you a piece of the program based on your examples and a console listing. There is a small timing adjustment to not delay the program, and the connection data constants are replaced by reading this data from the EEPROM. The problem with this could be that it randomly doesn't read the connection data correctly, maybe it would help to add debug to the library to be sure what the problem is.

In loop

if ( millis() - mqtt_try_connection_interval >= 1000 && !HAMQTT.connected() && !HAMQTT.connect( exchange_data.serial_number.c_str() , EEPROM.readString( EEPROM_MQTT_USERNAME ).c_str() , EEPROM.readString( EEPROM_MQTT_PASSWORD ).c_str() )) {
  debug.println( "MQTT connecting..." );
  mqtt_connected = false;
  mqtt_try_connection_interval = millis();
} else if ( !mqtt_connected ) {
  debug.println( "MQTT connected..." );
  mqtt_connected = true;
  mqtt_try_connection_interval = millis();
}

Console - cyclic to next restart esp

06:31:07.957 -> MQTT connecting...
06:31:08.190 -> MQTT connected...
06:31:09.269 -> MQTT connecting...
06:31:09.503 -> MQTT connected...
06:31:10.578 -> MQTT connecting...
06:31:11.279 -> MQTT connected...
06:31:12.357 -> MQTT connecting...
06:31:12.590 -> MQTT connected...
06:31:13.665 -> MQTT connecting...
06:31:13.851 -> MQTT connected...
06:31:14.972 -> MQTT connecting...
06:31:15.671 -> MQTT connected...
06:31:16.743 -> MQTT connecting...
06:31:16.977 -> MQTT connected...
06:31:18.051 -> MQTT connecting...
...
proasnet commented 3 months ago

I even managed to simulate the situation. If the esp is connected to HA and everything works, and I disconnect and connect the ethernet, or turn the wifi off and on, depending on what it's connected to, it retries and always fails to connect.

proasnet commented 3 months ago

I've tried automatic restart if it doesn't connect 10x before we figure out the problem. So that it is not cyclical and the device works autonomously, the restart is performed only 2 times. If a connection is established, these memories are reset. It works now, but it's not an ideal solution.


mqtt_connected = HAMQTT.connected();

if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  debug.println( F("MQTT connecting...") );
  if ( !HAMQTT.connect( exchange_data.serial_number.c_str() , EEPROM.readString( EEPROM_MQTT_USERNAME ).c_str() , EEPROM.readString( EEPROM_MQTT_PASSWORD ).c_str() ) ) debug.println( "Problem with MQTT connection..." );
  pb_mqtt = false;
  mqtt_connection_count++;
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connected && !pb_mqtt ) {
  debug.println( F("MQTT connected...") );
  pb_mqtt = true;
  mqtt_connection_count = 0;
  EEPROM.write( EEPROM_MQTT_DISABLE_RST , false );
  EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , 0 );
  EEPROM.commit();
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connection_count >= 10 && !EEPROM.read( EEPROM_MQTT_DISABLE_RST )) {
  byte count = EEPROM.read( EEPROM_MQTT_RST_COUNT );
  count++;
  EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , count );
  debug.println( "RST COUNTER: " + String( count ));
  EEPROM.commit();
  delay( 1000 );
  ESP.restart();
}

if ( EEPROM.readByte( EEPROM_MQTT_RST_COUNT ) >= 2 && !EEPROM.read( EEPROM_MQTT_DISABLE_RST ) ) {
  EEPROM.write( EEPROM_MQTT_DISABLE_RST , true );
  debug.println( F("MQTT connection auto restart disabled...") );
  EEPROM.commit();
}
paulino commented 3 months ago

Ok, I'll try to reproduce it

proasnet commented 3 months ago

Thank you very much @paulino . The second method for reproduce it is STOP MQTT broker in HA, and START again. Then ESP must be restarted for fine working.

paulino commented 3 months ago

I updated the dev branch to fix the bug in the library loop() method. I think now the mqtt reconnections are working right.

You can do a git pull on the dev branch to get new changes and test them.

proasnet commented 3 months ago

Dear @paulino , I tested it, but no effect. I tried both tests.

  1. stop and start the broker
  2. disconnect ethernet or wifi, depending on what esp was connected and connect again.

I set the number of attempts to restart to 100, even so it connected only after the restart.

paulino commented 3 months ago

Hi again,

I have detected the problem. My library uses a singleton instance of HAMQTT and only supports one pubsubclient. If you call begin( ... ) twice, the first call has no effect.

The solution is to create a second instance of HAMQTTController but the class is written to avoid this. I need to rewrite HAMQTTController code and it will affect to all examples

To do this will take some time. I never think of a use case where two connections were used.

Maybe open a new enhancement issue for a new release and put the test code in a different branch. But I need to keep the current behavior since the library is running in some nodes and this is a breaking change.

proasnet commented 3 months ago

Do you mean that I have 2x HAMQTT.begin() in my program? One for Wifi and the second for ethernet? If so, in my setup program, HAMQTT.begin() will only run once, because at the time of the call the connection type is already known, and other begin is not called

WiFiClient wificlient;
EthernetClient ethernetclient;

PubSubClient wifi_mqtt_client( wificlient );
PubSubClient ethernet_mqtt_client( ethernetclient );
#define ENTITIES_COUNT 50

...

if ( wifi_connected ) {
  wifi_mqtt_client.setServer( mqtt_adress.c_str() , MQTT_PORT );
  delay( 100 );
  HAMQTT.begin( wifi_mqtt_client, ENTITIES_COUNT );
}
if ( ethernet_connected ) {
  ethernet_mqtt_client.setServer( mqtt_adress.c_str() , MQTT_PORT );
  delay( 100 );
  HAMQTT.begin( ethernet_mqtt_client, ENTITIES_COUNT );
}
proasnet commented 3 months ago

Hi I solved the problem with connecting to MQTT with an autorestart. The last edit on connecting didn't help me.

paulino commented 2 months ago

@proasnet, What kind of board are you using? I only have wifi boards (wroom/S2/C3), they don't have ethernet and can't test your code.

proasnet commented 2 months ago

I have ESP32 DEVKIT V1 https://allegro.sk/ponuka/modul-esp32-devkit-v1-30-pin-esp-wroom32-cp2102-13352067239?utm_feed=547ce2b2-2e28-4773-9707-58218068540e&utm_source=google&utm_medium=cpc&utm_campaign=SK%3EElectro%3EHome-electro%3E3P%3EPMAX&ev_campaign_id=21066407971&gad_source=1&gclid=Cj0KCQjw0Oq2BhCCARIsAA5hubWvXVkPNg7Mw0zLR4YCfKF5nPSoF3ax_bL_A4fuR2Ya2Hr7NOeOVisaAlz7EALw_wcB

And ethernet Wiznet W5500.

My program in ESP is only as comunicator. Is connected via UART to Atmel ATMEGA 2560, that is a main device CPU. All connection parameters is stored via UI in atmel eeprom. If is ESP32 powered by atmel command, at setup routine in esp has connection asks. Atmel answer, if esp will be connected via ethernet or wifi, if is dhcp enabled and all ip adresses are sended. After this procedure, esp will connect to network.

paulino commented 2 months ago

After some testing I rewrote the HAMQTTController::loop(). I think, the reconnect problem came from:

  1. When the broker is restarted, the data sent will be lost if HA is not connected first.
  2. It is necessary to delay the first state send, HA need some time to process MQTT discovery and enable the entities before sending states

I've tested the new implementation by stopping and restarting the broker. I also tested restarting HA and in my environment it works fine.

Two parameters are added to hamqttcontroller.cpp file : DELAY_RECONNECT (set to 5000). This is the time to wait for HA to connect. The second is DELAY_SEND_STATES (set to 3000)

They are not configurable yet, but I think it is a good idea to add them as optional parameters in the begin function.

The new implementation is in dev branch and you can test it by downloading or switching to the dev branch.

proasnet commented 2 months ago

Dear @paulino I tested your change today.

  1. The ESP was connected to the MQTT broker
  2. I restarted the MQTT broker, or I restarted the HA HOST PC
  3. ESP reconect only after restart ESP
paulino commented 2 months ago

After the last change in devbranch I tested with the attached file. I am uploading a screenshot stopping and starting wifi and mosquitto. Please, update your dev branch "git pull" and, can you verify if the behavior is different in your environment?

The code is:

#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <HaMqttEntities.h>

#define WIFI_SSID "MyWifi"
#define WIFI_PASSWORD "MyPassword"
#define MQTT_SERVER "192.168.1.X"
#define MQTT_PORT 1883
#define MQTT_USER "MyBrokerUser"
#define MQTT_PASSWORD "MyBrokerPassword"

WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);

// HA Parts
#define ENTITIES_COUNT 3
// This example optimizes the memory usage by using PSTR to store the strings
// in flash memory
#define SW_VERSION PSTR("1.0.0")
#define IDENTIFIER PSTR("test00")
#define DEVICE_NAME PSTR("Example Github")

HADevice ha_device = HADevice(IDENTIFIER, DEVICE_NAME, SW_VERSION);
HAText ha_text = HAText(PSTR("text"), PSTR("Input text"), ha_device, 100);
HASwitch ha_switch = HASwitch(PSTR("switch"), PSTR("Test availability"), ha_device);

void setup()
{
  Serial.begin(115200);
  mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);

  // Initialize entities
  HAMQTT.begin(mqtt_client, ENTITIES_COUNT);
  HAMQTT.addEntity(ha_text);
  HAMQTT.addEntity(ha_switch);

  // Availability
  ha_device.addFeature(HA_FEATURE_AVAILABILITY);

  // Initial states
  ha_text.setState("Hello HomeAssistant");

  // start wifi
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.printf("\nHA-MQTT VERSION: %s\n\n", HA_MQTT_VERSION_S);
}

void loop()
{
  static int mqtt_retries = 0;
  if (WiFi.status() != WL_CONNECTED) {
      Serial.printf("WiFi disconnected, status: %d\n", WiFi.status());
      delay(5000);
  }
  if (!HAMQTT.connected())
  {
    Serial.printf("MQTT disconnected\n");
  }

  if (WiFi.status() == WL_CONNECTED && !HAMQTT.connected())
    {
        if (HAMQTT.connect("examples", MQTT_USER, MQTT_PASSWORD))
        {
            Serial.printf("Connected to MQTT\n");
            mqtt_retries = 0;
        }
        else
        {
            Serial.printf("Failed to connect to MQTT. Retries %d\n", mqtt_retries++);
            delay(5000);
        }
    }
    HAMQTT.loop();
    delay(50);
}

https://github.com/user-attachments/assets/b46663fb-5bf2-4cce-bc7b-45c47b629817

proasnet commented 2 months ago

Dear @paulino , I am trying a dev branch today. I looked at your code and somehow it doesn't differ in function from mine. It still doesn't work for me like it did for you. Please see my program, the connection section. When I stopped the MQTT broker and started it again, the ESP did not connect, there was an automatic restart again after 200 connection attempts.

Maybe I have a mistake somewhere that I can't see. My HA is a clean install with only the necessary components. Nothing more.

mqtt_connected = HAMQTT.connected();

if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  debug.println( F("MQTT connecting...") );
  if ( !HAMQTT.connect( exchange_data.serial_number.c_str() , EEPROM.readString( EEPROM_MQTT_USERNAME ).c_str() , EEPROM.readString( EEPROM_MQTT_PASSWORD ).c_str() ) ) debug.println( "Problem with MQTT connection..." );
  pb_mqtt = false;
  mqtt_connection_count++;
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connected && !pb_mqtt ) {
  debug.println( F("MQTT connected...") );
  pb_mqtt = true;
  mqtt_connection_count = 0;
  EEPROM.write( EEPROM_MQTT_DISABLE_RST , false );
  EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , 0 );
  EEPROM.commit();
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connection_count >= 200 && !EEPROM.read( EEPROM_MQTT_DISABLE_RST )) {
  byte count = EEPROM.read( EEPROM_MQTT_RST_COUNT );
  count++;
  EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , count );
  debug.println( "RST COUNTER: " + String( count ));
  EEPROM.commit();
  delay( 1000 );
  ESP.restart();
}
paulino commented 2 months ago

Hi again @proasnet, testing your code with small changes it works fine.

The EEPROM vars are removed and at the end HAMQTT.loop(); is necessary, otherwise the connection is lost after some seconds.

Could it be a problem with the "EEPROM.readString" function? Or a memory heap overflow?

Here is the pasted code:

void loop()
{
static unsigned long mqtt_try_connection_interval = millis();
static bool pb_mqtt = false;
static int mqtt_connection_count = 0;
static int EEPROM_MQTT_DISABLE_RST, EEPROM_MQTT_RST_COUNT;

bool mqtt_connected = HAMQTT.connected();

if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  debug.println( F("MQTT connecting...") );
  if ( !HAMQTT.connect( "test", MQTT_USER , MQTT_PASSWORD ))
    debug.println( "Problem with MQTT connection..." );
  pb_mqtt = false;
  mqtt_connection_count++;
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connected && !pb_mqtt ) {
  debug.println( F("MQTT connected...") );
  pb_mqtt = true;
  mqtt_connection_count = 0;
  EEPROM_MQTT_DISABLE_RST = false;
  //EEPROM.write( EEPROM_MQTT_DISABLE_RST , false );
  EEPROM_MQTT_RST_COUNT = 0;
  //EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , 0 );
  //EEPROM.commit();
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connection_count >= 200 && !EEPROM_MQTT_DISABLE_RST) {
  //byte count = EEPROM.read( EEPROM_MQTT_RST_COUNT );
  //EEPROM_MQTT_RST_COUNT
  //EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , count );
  debug.println( "Reseting ...");
  //EEPROM.commit();
  delay( 1000 );
  ESP.restart();
}
HAMQTT.loop();

}
proasnet commented 2 months ago

Dear @paulino , thank you for your time. I had to use the EEPROM for the initial configuration of the device via the USB port, because the source code of the project will not be accessible to the workers, they will have to upload the BIN program, and then configure the ESP via the terminal. It was also possible to use the ESP file system and set the connection parameters in a file. But here was the problem that the Arduino IDE and source code are needed to upload files, I couldn't find an application that would standalone upload files to ESP. Therefore, the use of EEPROM seemed to me to be the ideal way, the worker sets the connection parameters via the console.

I don't know that there could be a problem with reading the strings from the EEPROM, because after a restart it always connects without a problem and from the data from the EEPROM. The problem only occurs when the program is running. But I can't completely rule it out.

proasnet commented 2 months ago

I tried replacing the EEPROM.readString() function, which is part of the EEPROM.h library, with the following functions:

//------------------------------------------------------------------------------------------------------------------------------------
// Zápis stringu do EEPROM
//------------------------------------------------------------------------------------------------------------------------------------

void writeStringToEEPROM( int addrOffset, const String & strToWrite ) {
  byte len = strToWrite.length();
  EEPROM.write( addrOffset , len );
  for ( int i = 0; i < len; i++ )
  {
    EEPROM.write( addrOffset + 1 + i, strToWrite[i] );
  }
}

//------------------------------------------------------------------------------------------------------------------------------------
// Čtení stringu z EEPROM
//------------------------------------------------------------------------------------------------------------------------------------

String readStringFromEEPROM( int addrOffset )
{
  int newStrLen = EEPROM.read( addrOffset );
  char data[ newStrLen + 1 ];
  for ( int i = 0; i < newStrLen; i++ )
  {
    data[i] = EEPROM.read( addrOffset + 1 + i );
  }
  data[ newStrLen ] = '\0';
  return String( data );
}

But the resulting test turned out the same as the previous ones. After boot ESP, mqtt connected. After stop and start broker, without reconnect.

paulino commented 2 months ago

Test this part to see if the buffers are overwritten

Is there enough space between EEPROM_MQTT_USERNAME and EEPROM_MQTT_PASSWORD?


if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  debug.println( F("MQTT connecting...") );

  String user;
  String pass;
  user=EEPROM.readString(EEPROM_MQTT_USERNAME);
  pass=EEPROM.readString(EEPROM_MQTT_PASSWORD);
  Serial.println(user);
  Serial.println(pass);

  if ( !HAMQTT.connect( "test", user.c_str() , pass.c_str() ))
    debug.println( "Problem with MQTT connection..." );
proasnet commented 2 months ago

I tried putting all the strings read from the EEPROM (address, user, password) into the functions as absolute strings "string" and everything works as it should. So the problem is not in your the library. The problem is that the your library has a problem with strings from EEPROM. It doesn't matter if I use the EEPROM.readString or readStringFromEEPROM functions, the result is always negative. Please do you have any idea what could be the problem if I use EEPROM strings? Thank you very much.

proasnet commented 2 months ago

Test this part to see if the buffers are overwritten

Is there enough space between EEPROM_MQTT_USERNAME and EEPROM_MQTT_PASSWORD?

if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  debug.println( F("MQTT connecting...") );

  String user;
  String pass;
  user=EEPROM.readString(EEPROM_MQTT_USERNAME);
  pass=EEPROM.readString(EEPROM_MQTT_PASSWORD);
  Serial.println(user);
  Serial.println(pass);

  if ( !HAMQTT.connect( "test", user.c_str() , pass.c_str() ))
    debug.println( "Problem with MQTT connection..." );

Yes, I have defined address as:

#define EEPROM_MQTT_ADRESS 0
#define EEPROM_MQTT_USERNAME 127
#define EEPROM_MQTT_PASSWORD 159
#define EEPROM_MQTT_DISABLE_RST 255
#define EEPROM_MQTT_RST_COUNT 256
paulino commented 2 months ago

Ok,

In my projects I also use EEPROM ... but not to store user/pass

give me some time to test EEPROM strings ... I will write you later

proasnet commented 2 months ago

Thank you @paulino , you are friend

paulino commented 2 months ago

I test your code with EEPROM and it works. Maybe your code has an overflow on some array in the global vars.

It is also possible some overflow in my library, but by now with 30 entities in some of my projects everything works fine.

The full tested codes is the pasted below. The EEPROM was pre-written in the setup at the first run time (the code is commented). The full code is similar to your loop, you can test it. Also the version of libraries and ESP is shown at the end of the screen capture video.


#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <HaMqttEntities.h>
#include <EEPROM.h>

// This file is not included in the repository only used for local testing
#include "secrets.h"

WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);

// HA Parts
#define ENTITIES_COUNT 3
// This example optimizes the memory usage by using PSTR to store the strings
// in flash memory
#define SW_VERSION PSTR("1.0.0")
#define IDENTIFIER PSTR("test00")
#define DEVICE_NAME PSTR("Example Github")

HADevice ha_device = HADevice(IDENTIFIER, DEVICE_NAME, SW_VERSION);
HAText ha_text = HAText(PSTR("text"), PSTR("Input text"), ha_device, 100);
HASwitch ha_switch = HASwitch(PSTR("switch"), PSTR("Test availability"), ha_device);

#define EEPROM_MQTT_ADRESS 0
#define EEPROM_MQTT_USERNAME 127
#define EEPROM_MQTT_PASSWORD 159
#define EEPROM_MQTT_DISABLE_RST 255
#define EEPROM_MQTT_RST_COUNT 256

void setup()
{
  Serial.begin(115200);
  mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);

  // Initialize entities
  HAMQTT.begin(mqtt_client, ENTITIES_COUNT);
  HAMQTT.addEntity(ha_text);
  HAMQTT.addEntity(ha_switch);

  // Availability
  ha_device.addFeature(HA_FEATURE_AVAILABILITY);

  // Initial states
  ha_text.setState("Hello HomeAssistant");

  // start wifi
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.printf("\nHA-MQTT VERSION: %s\n\n", HA_MQTT_VERSION_S);

  EEPROM.begin(512);
  // EEPROM.writeString(EEPROM_MQTT_USERNAME, MQTT_USER);
  // EEPROM.writeString(EEPROM_MQTT_PASSWORD, MQTT_PASSWORD);
  // EEPROM.commit();

}

void loop()
{
static unsigned long mqtt_try_connection_interval = millis();
static bool pb_mqtt = false;
static int mqtt_connection_count = 0;
static bool mqtt_connected;

mqtt_connected = HAMQTT.connected();

if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  Serial.println( F("MQTT connecting...") );
  if ( !HAMQTT.connect("prueba" , EEPROM.readString( EEPROM_MQTT_USERNAME ).c_str() , EEPROM.readString( EEPROM_MQTT_PASSWORD ).c_str() ) )
    Serial.println( "Problem with MQTT connection..." );
  pb_mqtt = false;
  mqtt_connection_count++;
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connected && !pb_mqtt ) {
  Serial.println( F("MQTT connected...") );
  pb_mqtt = true;
  mqtt_connection_count = 0;
  EEPROM.write( EEPROM_MQTT_DISABLE_RST , false );
  EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , 0 );
  EEPROM.commit();
  mqtt_try_connection_interval = millis();
}

if ( mqtt_connection_count >= 200 && !EEPROM.read( EEPROM_MQTT_DISABLE_RST )) {
  byte count = EEPROM.read( EEPROM_MQTT_RST_COUNT );
  count++;
  EEPROM.writeByte( EEPROM_MQTT_RST_COUNT , count );
  Serial.println( "RST COUNTER: " + String( count ));
  EEPROM.commit();
  delay( 1000 );
  ESP.restart();
}
HAMQTT.loop();
}

https://github.com/user-attachments/assets/d0d75b7b-de5a-410a-a0dd-085079efe745

proasnet commented 2 months ago

Dear @paulino I tried load global strings with address and credentials to MQTT in a setup and trim() strings, as you see:

mqtt_adress = EEPROM.readString( EEPROM_MQTT_ADRESS );
mqtt_user = EEPROM.readString( EEPROM_MQTT_USERNAME );
mqtt_password = EEPROM.readString( EEPROM_MQTT_PASSWORD );
mqtt_adress.trim();
mqtt_user.trim();
mqtt_password.trim();

and in a loop as:

if ( millis() - mqtt_try_connection_interval >= 1000 && !mqtt_connected ) {
  debug.println( F("MQTT connecting...") );
if ( !HAMQTT.connect( exchange_data.serial_number.c_str() , mqtt_user.c_str() , mqtt_password.c_str() ) ) debug.println( "Problem with MQTT connection..." );
  pb_mqtt = false;
  mqtt_connection_count++;
  mqtt_try_connection_interval = millis();
}

It seems that the problem is with the termination character at the end of the strings, which your library does not accept. Now it working fine. I can stop and run broker, and library reconnect fine. In your example, the data was written to the EEPROM without a terminating character, but in my project it is written from the console, probably with a terminating character.

proasnet commented 1 month ago

Dear @paulino , I'm still having trouble disconnecting from the Broker. Without any changes on HA side ( disable/enable broker, restart HA etc.. )

14:53:11.142 ->   MQTT connecting...
14:53:13.883 ->   MQTT connected...
14:53:33.743 ->   MQTT connecting...
14:53:36.711 -> Problem with MQTT connection...
14:53:38.203 ->   MQTT connecting...
14:53:39.133 ->   MQTT connected...
14:53:58.230 ->   MQTT connecting...
14:54:01.251 -> Problem with MQTT connection...
14:54:02.835 ->   MQTT connecting...
14:54:03.719 ->   MQTT connected...
paulino commented 1 month ago

Hi again, thought the problem was solved. I had some changes pending to commit, and I committed them today in the dev branch.

I'm not sure if these changes will solve your problem. You can test them

If the problem persists we can share an arduino sketch that has the problem, so I can test it and try to fix the bug ...

proasnet commented 1 month ago

Dear @paulino today I tested new dev version, but still reconnecting..

13:17:13.267 ->   MQTT connecting...
13:17:15.919 ->   MQTT connected...
13:17:33.321 ->   MQTT connecting...
13:17:33.321 -> Problem with MQTT connection...
13:17:34.434 ->   MQTT connecting...
13:17:37.460 -> Problem with MQTT connection...
13:17:38.438 ->   MQTT connecting...
13:17:38.577 ->   MQTT connected...
13:17:52.248 ->   MQTT connecting...
13:17:52.248 -> Problem with MQTT connection...
13:17:53.265 ->   MQTT connecting...
13:17:56.242 -> Problem with MQTT connection...
13:17:57.407 ->   MQTT connecting...
13:17:57.546 ->   MQTT connected...
13:18:06.097 ->   MQTT connecting...
13:18:06.097 -> Problem with MQTT connection...
13:18:07.165 ->   MQTT connecting...
13:18:10.135 -> Problem with MQTT connection...
13:18:11.205 ->   MQTT connecting...
13:18:11.342 ->   MQTT connected...

I can send you my program with description of parts, to email. I can not publish and share here.

EDIT: Some news So far I have noticed that reconnecting happens right after ESP programming. When I turned off the whole device from power, and after a few seconds turned on again, the esp connected and did not disconnect anymore.

paulino commented 1 month ago

ok, If you want to send me some code by email, my email is at my GitHub profile in the portfolio link.

proasnet commented 1 month ago

Dear @paulino I have currently found a problem on my device, and it seems to be related to the runtime of the program. My board's power is weak and the program is running intermittently, I'll fix my problem first and then see if the connection problem shows up as well. It seems to be having an effect.

proasnet commented 1 month ago

Now, I have a laboratory power source. The reconnecting remaining. I added to code loop time. You can see, that connecting going up to 20 second. After 2 minutes of trying connect, esp was connected permanently. Is possible add to library debuging? Thank you very much for your time

10:02:31.746 -> Loop time: 127 ms
10:02:31.887 -> Loop time: 162 ms
10:02:32.073 -> Loop time: 165 ms
10:02:32.210 -> Loop time: 170 ms
10:02:32.396 -> Loop time: 155 ms
10:02:32.533 -> Loop time: 161 ms
10:02:32.721 -> Loop time: 161 ms
10:02:32.860 ->   MQTT connecting...
10:02:35.332 -> Loop time: 2649 ms
10:02:35.378 ->   MQTT connected...
10:02:43.995 -> Loop time: 8654 ms
10:02:44.040 ->   MQTT connecting...
10:02:44.040 -> Problem with MQTT connection...
10:02:44.040 -> Loop time: 36 ms
10:02:44.180 -> Loop time: 157 ms
10:02:44.367 -> Loop time: 164 ms
10:02:44.505 -> Loop time: 163 ms
10:02:44.690 -> Loop time: 158 ms
10:02:44.829 -> Loop time: 161 ms
10:02:45.015 -> Loop time: 163 ms
10:02:45.153 ->   MQTT connecting...
10:02:48.183 -> Problem with MQTT connection...
10:02:48.183 -> Loop time: 3170 ms
10:02:48.183 -> Loop time: 4 ms
10:02:48.323 -> Loop time: 163 ms
10:02:48.508 -> Loop time: 163 ms
10:02:48.695 -> Loop time: 180 ms
10:02:48.883 -> Loop time: 181 ms
10:02:49.069 -> Loop time: 187 ms
10:02:49.256 ->   MQTT connecting...
10:02:49.256 -> Loop time: 204 ms
10:02:49.396 ->   MQTT connected...
10:03:09.813 -> Loop time: 20559 ms
10:03:09.861 ->   MQTT connecting...
10:03:09.861 -> Problem with MQTT connection...
10:03:09.861 -> Loop time: 31 ms
10:03:10.000 -> Loop time: 160 ms
10:03:10.184 -> Loop time: 169 ms
10:03:10.323 -> Loop time: 158 ms
10:03:10.510 -> Loop time: 158 ms
10:03:10.649 -> Loop time: 163 ms
10:03:10.836 -> Loop time: 166 ms
10:03:10.974 ->   MQTT connecting...
paulino commented 1 month ago

It is a strange behavior, the library has no debug code . You can test direct connect pubsubclient instead of HAMQTT connect to test the connection time


  if( mqttClient->connect(id,user,pass))
            Serial.println("Connected to MQTT");
         else
         {
            Serial.println("Failed to connect to MQTT");
            delay(5000);
         }

This code is not compatible with my library, but if it does not connect, then the problem is in the pubsubclient library ...

proasnet commented 1 month ago

I'm starting to suspect the connection method. If I connect via wifi these disconnects occur. When I connect via ethernet, it works.

Both connection methods are through the same router. Nothing else but esp is connected to it, and only one method in one time. Could it be related to the number of entities? I have 71 entities defined for your library in the project. It is a lot, but in reality fewer of them are transferred, because the entities are selected according to the type of device and the addEntity function is no longer for everyone. If, for example, some wifi buffer is not overflowing, but I don't know how to find out and where to reset it. I am using wifi library from ESP32 Core version 2.0.14. I can't update to the latest one because I get a lot of incompatibilities with the libraries used. I think there will be a problem somewhere.