espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.73k stars 7.43k forks source link

ESP-MQTT Intermittent MQTTS Connection Failures #10541

Closed vicatcu closed 3 weeks ago

vicatcu commented 3 weeks ago

Board

ESP32 Dev Module

Device Description

Custom PCBA

Hardware Configuration

N/A

Version

latest master (checkout manually)

IDE Name

Arduino IDE

Operating System

Ubuntu 22.04

Flash frequency

40MHz

PSRAM enabled

no

Upload speed

115200

Description

When connecting to my MQTT Server (self-hosted, Mosquitto), I intermittently experience the following outcome in console output.

E (15049) mqtt_client: esp_mqtt_handle_transport_read_error: transport_read(): EOF
E (15050) mqtt_client: esp_mqtt_handle_transport_read_error: transport_read() error: errno=119
[MQTT EVENT] reported from esp-tls (0x8008)
[MQTT EVENT] Last errno string (Success)
E (15065) mqtt_client: esp_mqtt_connect: mqtt_message_receive() returned -2
E (15068) mqtt_client: MQTT connect failed

Sketch

My code effectively looks like this:

  static esp_mqtt_client_config_t mqtt_cfg;
  mqtt_cfg.broker.address.hostname = settings->server;
  mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_SSL;
  mqtt_cfg.broker.address.port = 8883;
  mqtt_cfg.credentials.username = settings->username;
  mqtt_cfg.credentials.client_id = settings->identity;
  mqtt_cfg.credentials.authentication.password = settings->password;
  mqtt_cfg.broker.verification.certificate = root_ca;
  esp_mqtt_client_handle_t _client = esp_mqtt_client_init(&mqtt_cfg);
  esp_mqtt_client_register_event(_client, (esp_mqtt_event_id_t) ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
  const esp_err_t ret = esp_mqtt_client_start(_client);
  boolean connected = false;
  if (ESP_OK == ret) {
      connected = waitForMqttConnected();
      // polls for global _mqttIsConnected global variable set in mqtt event handler to become true
      // case MQTT_EVENT_CONNECTED sets it to true
      // case MQTT_EVENT_DISCONNECTED sets it to false
      // case MQTT_EVENT_ERROR sets it to false 
      // or times out after 15 seconds
  }

  ... event handler code that is printing out the [MQTT EVENT] log messages looks like this:
    case MQTT_EVENT_ERROR:
      lastMqttEventId = MQTT_EVENT_ERROR;
      _mqttIsError = true;
      _mqttIsConnected = false;
      esp_tls_stack_err = 0;
      if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
        if (event->error_handle->esp_tls_last_esp_err  != 0) {
          Serial.printf("[MQTT EVENT] reported from esp-tls (0x%x)\r\n", event->error_handle->esp_tls_last_esp_err);
        }
        if (event->error_handle->esp_tls_stack_err != 0) {
          esp_tls_stack_err = event->error_handle->esp_tls_stack_err;
          Serial.printf("[MQTT EVENT] reported from tls stack (0x%x)\r\n", event->error_handle->esp_tls_stack_err);
        }
        if (event->error_handle->esp_transport_sock_errno != 0) {
          Serial.printf("[MQTT EVENT] captured as transport's socket errno (0x%x)\r\n", event->error_handle->esp_transport_sock_errno);
        }
        Serial.printf("[MQTT EVENT] Last errno string (%s)\r\n", strerror(event->error_handle->esp_transport_sock_errno));
      }
      break;

Debug Message

N/A

Other Steps to Reproduce

Try running that code and see what happens over several iterations of it

I have checked existing issues, online documentation and the Troubleshooting Guide

me-no-dev commented 3 weeks ago

I suggest you ask here instead :)

VojtechBartoska commented 3 weeks ago

Hello, closing this as it is tracked in MQTT repository.

Thanks for creating a ticket there @vicatcu.