程序的入口函数是 void app_main()

void app_main()
{
    ESP_ERROR_CHECK(nvs_flash_init());    //初始化默认的NVS分区
    ESP_ERROR_CHECK(esp_netif_init());    //初始化基础的TCP/IP堆栈
    ESP_ERROR_CHECK(esp_event_loop_create_default());    //创建默认事件循环

    ESP_ERROR_CHECK(example_connect());    //连接WiFi

    ESP_LOGI(TAG, "[APP] Startup..");
    ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());    //打印内存信息
    ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());    //打印idf版本号信息

    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
    esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
    esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
    esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
    esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);

    mqtt_app_start();
}

可以看到在 app_main() 函数中完成里一些必要的初始化
app_main() 中调用 example_connect() 函数完成WiFi连接
app_main() 中调用 mqtt_app_start() 函数完成 MQTT 服务器的配置、MQTT事件回调函数函数等的设置

static void mqtt_app_start(void)
{
    esp_mqtt_client_config_t mqtt_cfg = {
        .event_handle = mqtt_event_handler,    //MQTT事件回调函数
        .host="161.118.69.27",                 //MQTT服务器地址
        .username="esp8266",                   //MQTT账号
        .password="000000",                    //MQTT密码
        .port=1883,                            //MQTT端口
        .client_id="esp8266",                  //ID
    };
    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);    //MQTT客户点初始化
    esp_mqtt_client_start(client);             //启动MQTT客户端
}

在 void mqtt_app_start(void)函数可以看到,定义了一个 mqtt_cfg 结构体,通过该结构体配置MQTT的相关设置。
通过 esp_mqtt_client_init(const esp_mqtt_client_config_t *config) 初始化 MQTT 客户端
然后使用 esp_mqtt_client_start(esp_mqtt_client_handle_t client) 启动 MQTT客户端

MQTT客户端启动后通过MQTT事件回调函数完成MQTT相关事件的处理(MQTT事件回调函数,在上文的mqtt_cfg结构体中设置)

static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
{
    esp_mqtt_client_handle_t client = event->client;
    int msg_id;
    // your_context_t *context = event->context;
    switch (event->event_id) {
        case MQTT_EVENT_CONNECTED:    //连接事件
            ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
            msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);    //发布消息
            ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);

            msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);    //订阅主题 /topic/qos0 消息质量:0
            ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);

            msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);    //订阅主题 /topic/qos1 消息质量:1
            ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);

            msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");     //取消主题 /topic/qos1 的订阅
            ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
            break;
        case MQTT_EVENT_DISCONNECTED:    //断开连接事件
            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
            break;

        case MQTT_EVENT_SUBSCRIBED:    //订阅事件
            ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
            msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
            ESP_LOGI(TAG, "sent publish successful, msg_id=%d", 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:    //接收到消息
            ESP_LOGI(TAG, "MQTT_EVENT_DATA");
            printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
            printf("DATA=%.*s\r\n", event->data_len, event->data);
            break;
        case MQTT_EVENT_ERROR:    //错误事件
            ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
            break;
    }
    return ESP_OK;
}