Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display mqtt raw message data ,present messy code!!!!!!!!! (IDFGH-14089) #19

Closed
3 tasks done
learnerzcz opened this issue Nov 17, 2024 · 6 comments
Closed
3 tasks done

Comments

@learnerzcz
Copy link

Checklist

  • Checked the issue tracker for similar issues to ensure this is not a duplicate
  • Read the documentation to confirm the issue is not addressed there and your configuration is set correctly
  • Tested with the latest version to ensure the issue hasn't been fixed

How often does this bug occurs?

always

Expected behavior

only display the receive message

Actual behavior (suspected bug)

i use mqtt client to subscribe a topic ,and when i receicve the publish message, i will print the message raw data.

Error logs or terminal output

I (627) wifi:config nano formating: disabled
I (627) wifi:Init data frame dynamic rx buffer num: 32
I (627) wifi:Init static rx mgmt buffer num: 5
I (637) wifi:Init management short buffer num: 32
I (637) wifi:Init dynamic tx buffer num: 32
I (647) wifi:Init static rx buffer size: 1600
I (647) wifi:Init static rx buffer num: 10
I (647) wifi:Init dynamic rx buffer num: 32
I (657) wifi_init: rx ba win: 6
I (657) wifi_init: tcpip mbox: 32
I (667) wifi_init: udp mbox: 6
I (667) wifi_init: tcp mbox: 6
I (667) wifi_init: tcp tx win: 5760
I (677) wifi_init: tcp rx win: 5760
I (677) wifi_init: tcp mss: 1440
I (687) wifi_init: WiFi IRAM OP enabled
I (687) wifi_init: WiFi RX IRAM OP enabled
I (697) phy_init: phy_version 4791,2c4672b,Dec 20 2023,16:06:06
I (787) wifi:mode : sta (ec:64:c9:90:8c:40)
I (787) wifi:enable tsf
wifi event id =28wifi event id =2wifi start
I (877) wifi:new:<13,0>, old:<1,0>, ap:<255,255>, sta:<13,0>, prof:1
I (877) wifi:state: init -> auth (b0)
wifi event id =28I (927) wifi:state: auth -> assoc (0)
I (1927) wifi:state: assoc -> init (2700)
I (1937) wifi:new:<13,0>, old:<13,0>, ap:<255,255>, sta:<13,0>, prof:1
wifi event id =5wifi event id =5I (8447) wifi:new:<13,0>, old:<13,0>, ap:<255,255>, sta:<13,0>, prof:1
I (8447) wifi:state: init -> auth (b0)
I (8487) wifi:state: auth -> assoc (0)
I (8507) wifi:state: assoc -> run (10)
I (8537) wifi:connected with 小学生, aid = 1, channel 13, BW20, bssid = ae:2d:44:71:18:7f
I (8547) wifi:security: WPA2-PSK, phy: bgn, rssi: -34
I (8547) wifi:pm start, type: 1

I (8547) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (8557) wifi:dp: 2, bi: 102400, li: 4, scale listen interval from 307200 us to 409600 us
I (8567) wifi:AP's beacon interval = 102400 us, DTIM period = 2
wifi event id =4WIFI_EVENT_STA_CONNECTED OK
I (9577) esp_netif_handlers: sta ip: 192.168.13.117, mask: 255.255.255.0, gw: 192.168.13.23
ip event id =0got ip ok
I (9577) main_task: Returned from app_main()
I (9607) wifi:<ba-add>idx:0 (ifx:0, ae:2d:44:71:18:7f), tid:0, ssn:4, winSize:64
I (10907) wifi:<ba-add>idx:1 (ifx:0, ae:2d:44:71:18:7f), tid:6, ssn:4, winSize:64
I (10957) MQTT: mqtt connected

I (11347) MQTT: mqtt subscribed ack

test
data->topic=/TEST/SUB11E        ��= q4Z�I��w�/��mdc����V����ò��ğ�C�ߚ�_���Dy��;�O���gp7E>="a^T���VXu��f��ǟ76m!�
                                                                                                              �������x#KOZ�ɽ���!s�d�����ӹ��r�w4�N�Ӑb�5V��m�- |�}�6e���
▒▒|��V�����B��H*7��
�|���0-��B!/Ts���/�6�|�:���&M���9���h_1=�Մ�jۜ����w^tx����+��Riv��g?HƝ)D�w�q.<#��@��ȹ��▒▒�t��q^.�QʁE

Steps to reproduce the behavior

only flash the firmware

Project release version

idf version v5.2-dirty , espidf monitor ver 1.5.0

System architecture

Intel/AMD 64-bit (modern PC, older Mac)

Operating system

Linux

Operating system version

ubuntu 20.04

Shell

CMD

Additional context

no

@github-actions github-actions bot changed the title display mqtt raw message data ,present messy code!!!!!!!!! display mqtt raw message data ,present messy code!!!!!!!!! (IDFGH-14089) Nov 17, 2024
@learnerzcz
Copy link
Author

Uploading image.png…

@learnerzcz
Copy link
Author

#include <stdio.h>
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_err.h"
#include "mqtt_client.h"

#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"

#define TAG "MQTT"
#define MQTT_HOST "broker.emqx.io"

#define MQTT_SUB_TOPIC "/TEST/SUB11"
#define MQTT_PUB_TOPIC "/TEST/PUB12"

#define PASSWORD "12345678"
#define SSID "小学生"

static SemaphoreHandle_t x_wifi_con_sem;
esp_mqtt_client_handle_t client;
void wifi_event_handle(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id,void* event_data)
{
if(event_base == WIFI_EVENT)
{
printf("wifi event id =%lx",event_id);
switch (event_id)
{
case WIFI_EVENT_STA_START:
printf("wifi start\n");
esp_wifi_connect();
break;
case WIFI_EVENT_STA_CONNECTED:
printf("WIFI_EVENT_STA_CONNECTED OK\n");
break;
case WIFI_EVENT_STA_DISCONNECTED:
vTaskDelay(pdMS_TO_TICKS(2000));
esp_wifi_connect();
break;
case WIFI_EVENT_STA_BSS_RSSI_LOW:
printf("rssi low\n");
break;
default:
break;
}

    }else if(event_base == IP_EVENT)
    {
         printf("ip event id =%lx",event_id);
        switch (event_id)
        {
            case IP_EVENT_STA_GOT_IP:
                xSemaphoreGive(x_wifi_con_sem);
                printf("got ip ok\n");
            

            default:
                break;
        }


    }

}

void esp_mqtt_event_handler(void* event_handler_arg,
esp_event_base_t event_base,
int32_t event_id,
void* event_data)
{
static uint8_t count=0;
char data_buf[15];
memset(data_buf,0,sizeof(data_buf));
esp_mqtt_event_handle_t event = event_data;
switch (event_id)
{
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG,"mqtt connected\n");
esp_mqtt_client_subscribe_single(client,MQTT_SUB_TOPIC,1);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG,"mqtt disconnected\n");
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG,"mqtt published ack\n");
break;

    case MQTT_EVENT_SUBSCRIBED:
        ESP_LOGI(TAG,"mqtt subscribed ack\n");
    break;

    case MQTT_EVENT_DATA:
        count++;
        printf("test\n");
        printf("data->topic=%s\n",event->topic);
        printf("%d\n",event->topic_len);
        memcpy(data_buf,event->topic,event->topic_len);
        printf("data->topic=%s\n",data_buf);
        //         printf("payload->%s\n",data->data);
        // ESP_LOGI(TAG,"top->%s\n",data->topic);
        // ESP_LOGI(TAG,"payload->%s\n",data->data);
        break;
    default:
        break;
}

}

void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

esp_netif_create_default_wifi_sta();

wifi_init_config_t wifi_cfg=WIFI_INIT_CONFIG_DEFAULT();

ESP_ERROR_CHECK(esp_wifi_init(&wifi_cfg));

esp_event_handler_register(WIFI_EVENT,ESP_EVENT_ANY_ID,wifi_event_handle,NULL);

esp_event_handler_register(IP_EVENT,IP_EVENT_STA_GOT_IP,wifi_event_handle,NULL);

wifi_config_t wifi_config={
    .sta.threshold.authmode=WIFI_AUTH_WPA2_PSK,
    .sta.pmf_cfg.capable=true,
    .sta.pmf_cfg.required=false,
    .sta.ssid=SSID,
    .sta.password=PASSWORD,
};


x_wifi_con_sem=xSemaphoreCreateBinary();


ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA,&wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());


esp_mqtt_client_config_t mqtt_client_cfg={
    .broker.address.hostname=MQTT_HOST,
    .broker.address.transport=MQTT_TRANSPORT_OVER_TCP,
    .broker.address.port=1883,
};

client=esp_mqtt_client_init(&mqtt_client_cfg);
esp_mqtt_client_register_event(client,ESP_EVENT_ANY_ID,esp_mqtt_event_handler,NULL);

xSemaphoreTake(x_wifi_con_sem,portMAX_DELAY);

esp_mqtt_client_start(client);

return ;

}

@learnerzcz learnerzcz reopened this Nov 18, 2024
@peterdragun
Copy link
Collaborator

Hi @learnerzcz, can you please try following the example in ESP-IDF, e.g. this one? This does not seem like an issue with IDF Monitor to me.

@learnerzcz
Copy link
Author

@peterdragun ,i use the demo i learned ,it doesn't present the issue ,i think i miss some steps. it result the issue

@igrr
Copy link
Member

igrr commented Nov 19, 2024

The problem is that you are not null-terminating your string buffer, so printf function doesn't stop printing the MQTT topic name where it ends, and proceeds printing random garbage data beyond the end of the buffer.

        memcpy(data_buf,event->topic,event->topic_len);
        printf("data->topic=%s\n",data_buf);

If you fill topic_len bytes in data_buf with data, you have to write 0 into data_buf[topic_len].

        memcpy(data_buf,event->topic,event->topic_len);
        data_buf[event->topic_len] = 0;
        printf("data->topic=%s\n",data_buf);

(You also need to check that event->topic_len is not larger than the size of your buffer, otherwise you may corrupt other data on the stack.)

Alternatively, you can use printf's %.*s syntax, which allows specifying the number of bytes to print

        printf("data->topic=%.*s\n",event->topic_len, event->topic);

like it is done in the example:
https://github.com/espressif/esp-idf/blob/f420609c332fbd2d2f7f188c6579d046c9560e42/examples/protocols/mqtt/tcp/main/app_main.c#L82-L83

@learnerzcz
Copy link
Author

@igrr thanks, like you said , i copy the topic to the bufffer,and make the null-terminating ,the display is ok.

but the reason that refuse me ,is that log "--- Warning: Failed to decode multiple lines in a row. Try checking the baud rate and XTAL frequency setting in menuconfig"

�$;�W��0(d�Dfi: top->/test/topic2434xsUn��]�bj�a G���$\�
M�=�gr�dw�/.��_�1�{0�|k�?׍

�$;�W��0(d�Dfi: payload->434xsUn��]�bj�a G���$\�
--- Warning: Failed to decode multiple lines in a row. Try checking the baud rate and XTAL frequency setting in menuconfig
M�=�gr�dw�/.��_�1�{0�|k�?׍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants