MQTT problem

edwi92
Posts: 6
Joined: Tue Jan 14, 2020 4:49 pm

MQTT problem

Postby edwi92 » Thu Feb 11, 2021 8:09 pm

Hi all,

I am having trouble running MQTT on my ESP-SOLO-1. It worked before, but not now. Can anyone suggest why?

What I got:

Code: Select all

D (4056) MQTT_CLIENT: MQTT client_id=ESP32_a89628
D (4056) event: created event loop 0x3ffbdaf0
D (4066) MQTT_CLIENT: Core selection disabled
I (4066) mqtt_event: Other event id:7
D (4266) TRANS_TCP: [sock=54],connecting to server IP:34.214.82.240,Port:8083...
D (4576) MQTT_CLIENT: Transport connected to mqtt://broker.emqx.io:8083
D (4586) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
E (9896) MQTT_CLIENT: mqtt_message_receive: transport_read() error: errno=128
I (9896) mqtt_event: Other event id:0
E (9896) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned -1
E (9906) MQTT_CLIENT: MQTT connect failed
D (9916) MQTT_CLIENT: Reconnect after 10000 ms
I (9916) mqtt_event: MQTT_EVENT_DISCONNECTED
D (19926) MQTT_CLIENT: Reconnecting...
I (19926) mqtt_event: Other event id:7
D (19926) TRANS_TCP: [sock=54],connecting to server IP:34.214.82.240,Port:8083...
D (20246) MQTT_CLIENT: Transport connected to mqtt://broker.emqx.io:8083
D (20246) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
E (25566) MQTT_CLIENT: mqtt_message_receive: transport_read() error: errno=128
I (25566) mqtt_event: Other event id:0
E (25566) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned -1
E (25576) MQTT_CLIENT: MQTT connect failed
D (25576) MQTT_CLIENT: Reconnect after 10000 ms
I (25586) mqtt_event: MQTT_EVENT_DISCONNECTED
D (35586) MQTT_CLIENT: Reconnecting...
I (35586) mqtt_event: Other event id:7
D (35586) TRANS_TCP: [sock=54],connecting to server IP:34.214.82.240,Port:8083...
D (35916) MQTT_CLIENT: Transport connected to mqtt://broker.emqx.io:8083
D (35916) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
E (41236) MQTT_CLIENT: mqtt_message_receive: transport_read() error: errno=128
I (41236) mqtt_event: Other event id:0
E (41236) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned -1
E (41246) MQTT_CLIENT: MQTT connect failed
D (41246) MQTT_CLIENT: Reconnect after 10000 ms
I (41246) mqtt_event: MQTT_EVENT_DISCONNECTED

Code: Select all

#include <stdio.h>
#include "esp_spi_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "mqtt_client.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "protocol_examples_common.h"

static EventGroupHandle_t mqtt_event_group;
const static int CONNECTED_BIT = BIT5;      
static bool mqtt_connected = false;
static esp_mqtt_client_handle_t global_client;

static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
{
    static const char *TAG = "mqtt_event";

    switch (event->event_id)
    {
    case MQTT_EVENT_CONNECTED:
        ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
        xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT);
        mqtt_connected = true;
        break;
    case MQTT_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
        mqtt_connected = false;
        break;
    case MQTT_EVENT_SUBSCRIBED:
        ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_UNSUBSCRIBED:
        ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_PUBLISHED:
        ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_DATA:
        printf("TOPIC=%d , %.*s\r\n", event->topic_len, event->topic_len, event->topic);
        printf("DATA=%d , %.*s\r\n", event->data_len, event->data_len, event->data);
        break;
    default:
        ESP_LOGI(TAG, "Other event id:%d", event->event_id);
        break;
    }

    return ESP_OK;
}


void mqtt_server_login()
{
        esp_mqtt_client_config_t mqtt_cfg = {   };
         mqtt_cfg.uri = "mqtt://broker.emqx.io";
         mqtt_cfg.port = 8083;
         mqtt_cfg.event_handle= mqtt_event_handler;
    global_client = esp_mqtt_client_init(&mqtt_cfg);
    esp_mqtt_client_start(global_client);
}

void app_main(void)
{
    WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(example_connect());

    mqtt_server_login();

    xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);

    while (1)
    {
        if(mqtt_connected == true)
            esp_mqtt_client_publish(global_client, "/test", "s", 1, 0, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

}

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: MQTT problem

Postby tommeyers » Fri Feb 12, 2021 1:37 am

What is happening on mqtt broker? You probably can run it as verbose to see more.
IT Professional, Maker
Santiago, Dominican Republic

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

Re: MQTT problem

Postby Zeni241 » Fri Feb 12, 2021 10:37 am

Never used "emqx.io" broker so cannot be sure. Just a guess, in
mqtt_cfg.uri = "mqtt://broker.emqx.io";
shouldn't it be mqtts:// instead of mqtt://.

Also where are credentials, username, password etc.?

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: MQTT problem

Postby PeterR » Fri Feb 12, 2021 11:49 am

There is a lot to go wrong from an MQTT client to a remote server!

Looks like you timeout, no response (CONACK) from server. As I remember it the 'transport_read() error' indicates no data returned. There were a couple of similar reports though so I modified the error report for clarity. If so then this suggests that the remote server is overloaded.

Hivemq is a good alternative. Would let you see if the issue is server related. I had bad days when using Hivemq though.
A Linux VM with Mosquitto would be simple to setup and should make for a solid test bench.

As a wild card - there are/were problems with the MQTT client over WS. There is an assumption of 1:1 MQTT packets to WS frame. As I remember it the issue is in the client code and so you might also see. When the issue showed I would see connection issue. Check my posts, there is an ESP patch & you will quickly see which files need modifying.
& I also believe that IDF CAN should be fixed.

edwi92
Posts: 6
Joined: Tue Jan 14, 2020 4:49 pm

Re: MQTT problem

Postby edwi92 » Fri Feb 12, 2021 3:05 pm

It was working 2 days ago but I changed someting and now it is not working... The code was the same, I did some changes in WiFI lib and sdkconfig file.

edwi92
Posts: 6
Joined: Tue Jan 14, 2020 4:49 pm

Re: MQTT problem

Postby edwi92 » Fri Feb 12, 2021 4:48 pm

Broker config:

Image

chegewara
Posts: 2306
Joined: Wed Jun 14, 2017 9:00 pm

Re: MQTT problem

Postby chegewara » Fri Feb 12, 2021 10:03 pm

This is TCP transport:

Code: Select all

mqtt://
but on screenshot you have websockets transport:

Code: Select all

ws://

Who is online

Users browsing this forum: Baidu [Spider], tharanilc and 241 guests