乐鑫技术交流
直播中

李伟

7年用户 1464经验值
私信 关注
[问答]

esp32-c3 mqtt遗嘱实验没成功的原因?

esp32-c3 mqtt遗嘱实验没成功,问题请教!

问题描述:

目的:为了实现esp32-c3断开mqtt网后(不能发送mqtt public主题)。服务器能发送提示消息给订阅者,告知订阅者esp32-c3断网。

使用的芯片为esp32-c3内置4m的乐鑫芯片模块。esp32-c3-mimi-1

使用的sdk为esp-idf  4.4

修改内容:

static void mqtt_app_start(void)

{

    esp_mqtt_client_config_t mqtt_cfg = {

        .uri = CONFIG_BROKER_URL,

    };

    mqtt_cfg.uri="mqtt://xxx.xxx.xxx.xxx";

    mqtt_cfg.port = 1883;                        

    mqtt_cfg.username ="myuser1";                  

    mqtt_cfg.password  ="123456";  

    mqtt_cfg.client_id ="abcdef123456789";

    mqtt_cfg.lwt_topic ="/test";

    static int i=0;

    mqtt_cfg.lwt_msg ="noconnectnd!!!%s",i++;

    mqtt_cfg.lwt_msg_len =sizeof(mqtt_cfg.lwt_msg);

    mqtt_cfg.lwt_qos =0;

    mqtt_cfg.lwt_retain =1;




//#if CONFIG_BROKER_URL_FROM_STDIN

//    char line[128];

//

//    if (strcmp(mqtt_cfg.uri, "FROM_STDIN") == 0) {

//        int count = 0;

//        printf("Please enter url of mqtt brokern");

//        while (count < 128) {

//            int c = fgetc(stdin);

//            if (c == 'n') {

//                line[count] = '';

//                break;

//            } else if (c > 0 && c < 127) {

//                line[count] = c;

//                ++count;

//            }

//            vTaskDelay(10 / portTICK_PERIOD_MS);

//        }

//        mqtt_cfg.uri = line;

//        printf("Broker url: %sn", line);

//    } else {

//        ESP_LOGE(TAG, "Configuration mismatch: wrong broker url");

//        abort();

//    }

//#endif /* CONFIG_BROKER_URL_FROM_STDIN */



    client = esp_mqtt_client_init(&mqtt_cfg);

    /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */

    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);

    esp_mqtt_client_start(client);

}

但测试发现没有遗嘱出现。不知道是什么情况。请指教。还有哪里需要修改?

反到是订阅的客户端断开后重连上时出现"/test" topic 的消息 消息内容cono 好像去掉了后面一部分。

后来改变esp32-c3(发布者)的内容都还是订阅的客户端这个topic出现。

mqtt.fx1.71 两台电脑可以监视到 两个之间的遗嘱。 但mqtt.fx1.71 电脑检测不到esp32-c3模块的。esp32-c3模块发布和订阅消息都没有问题。

还有 遗愿标识(Will Flag) esp32-idf sdk 在哪里设置?

更多回帖

发帖
×
20
完善资料,
赚取积分