🚧 添加(MQTT APP):添加文件,未打通
This commit is contained in:
parent
ca46cd1fee
commit
ae73fc4bab
208
main/APP/app_task_mqtt/app_task_mqtt.c
Normal file
208
main/APP/app_task_mqtt/app_task_mqtt.c
Normal file
@ -0,0 +1,208 @@
|
||||
/**
|
||||
* @file app_task_mqtt.c
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-09-29
|
||||
* _ _
|
||||
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
|
||||
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
|
||||
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
|
||||
* /_/ /___/ /___/
|
||||
* @copyright Copyright (c) 2022 impressionyang
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-09-29 <td>v1.0 <td>impressionyang <td>内容
|
||||
* </table>
|
||||
*/
|
||||
#include "app_task_mqtt.h"
|
||||
#include "app_main.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/netdb.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "mqtt_client.h"
|
||||
|
||||
static const char *TAG = "MQTT_EXAMPLE";
|
||||
|
||||
|
||||
static void log_error_if_nonzero(const char *message, int error_code)
|
||||
{
|
||||
if (error_code != 0) {
|
||||
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Event handler registered to receive MQTT events
|
||||
*
|
||||
* This function is called by the MQTT client event loop.
|
||||
*
|
||||
* @param handler_args user data registered to the event.
|
||||
* @param base Event base for the handler(always MQTT Base in this example).
|
||||
* @param event_id The id for the received event.
|
||||
* @param event_data The data for the event, esp_mqtt_event_handle_t.
|
||||
*/
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
||||
esp_mqtt_event_handle_t event = event_data;
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
int msg_id;
|
||||
switch ((esp_mqtt_event_id_t)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);
|
||||
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||||
|
||||
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
|
||||
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||||
|
||||
msg_id = esp_mqtt_client_unsubscribe(client, "/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");
|
||||
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
|
||||
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
|
||||
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
|
||||
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
|
||||
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
|
||||
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.host = "mqtt://mqtt.impressionyang.top",
|
||||
.port = 21883,
|
||||
};
|
||||
#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 broker\n");
|
||||
while (count < 128) {
|
||||
int c = fgetc(stdin);
|
||||
if (c == '\n') {
|
||||
line[count] = '\0';
|
||||
break;
|
||||
} else if (c > 0 && c < 127) {
|
||||
line[count] = c;
|
||||
++count;
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
mqtt_cfg.uri = line;
|
||||
printf("Broker url: %s\n", line);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Configuration mismatch: wrong broker url");
|
||||
abort();
|
||||
}
|
||||
#endif /* CONFIG_BROKER_URL_FROM_STDIN */
|
||||
|
||||
esp_mqtt_client_handle_t 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);
|
||||
}
|
||||
|
||||
// void app_main(void)
|
||||
// {
|
||||
// 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());
|
||||
|
||||
// esp_log_level_set("*", ESP_LOG_INFO);
|
||||
// esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
|
||||
|
||||
// ESP_ERROR_CHECK(nvs_flash_init());
|
||||
// ESP_ERROR_CHECK(esp_netif_init());
|
||||
// ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
// /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||
// * Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||
// * examples/protocols/README.md for more information about this function.
|
||||
// */
|
||||
// ESP_ERROR_CHECK(example_connect());
|
||||
|
||||
// mqtt_app_start();
|
||||
// }
|
||||
|
||||
static void _app_task_mqtt_run(void* param)
|
||||
{
|
||||
while (!app_main_handle_get_nework_state()) {
|
||||
vTaskDelay(10);
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
mqtt_app_start();
|
||||
while (1) {
|
||||
vTaskDelay(10);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t app_task_mqtt_start()
|
||||
{
|
||||
vTaskDelay(10);
|
||||
xTaskCreatePinnedToCore(_app_task_mqtt_run, "mqtt app task", 4096, NULL, 5, NULL, tskNO_AFFINITY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t app_task_mqtt_stop()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
52
main/APP/app_task_mqtt/app_task_mqtt.h
Normal file
52
main/APP/app_task_mqtt/app_task_mqtt.h
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @file app_task_mqtt.h
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-09-29
|
||||
* _ _
|
||||
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
|
||||
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
|
||||
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
|
||||
* /_/ /___/ /___/
|
||||
* @copyright Copyright (c) 2022 impressionyang
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-09-29 <td>v1.0 <td>impressionyang <td>内容
|
||||
* </table>
|
||||
*/
|
||||
#ifndef __APP_TASK_MQTT_H__
|
||||
#define __APP_TASK_MQTT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @return uint8_t
|
||||
*
|
||||
* @details
|
||||
*/
|
||||
uint8_t app_task_mqtt_stop();
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @return uint8_t
|
||||
*
|
||||
* @details
|
||||
*/
|
||||
uint8_t app_task_mqtt_start();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif//__APP_TASK_MQTT_H__
|
||||
@ -116,11 +116,11 @@ static void fast_scan(void)
|
||||
static void _app_wifi_task_run(void *param)
|
||||
{
|
||||
fast_scan();
|
||||
sg_wifi_app_handle.is_wifi_conneted = 1;
|
||||
while(sg_wifi_app_handle.is_wifi_app_running) {
|
||||
printf("wifi info:\r\n");
|
||||
printf("is wifi conneted : %d\r\n", sg_wifi_app_handle.is_wifi_conneted);
|
||||
// printf("wifi info:\r\n");
|
||||
// printf("is wifi conneted : %d\r\n", sg_wifi_app_handle.is_wifi_conneted);
|
||||
vTaskDelay(100);
|
||||
sg_wifi_app_handle.is_wifi_conneted = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3,8 +3,10 @@ idf_component_register(
|
||||
"."
|
||||
"APP/app_task_hello"
|
||||
"APP/app_task_wifi"
|
||||
"APP/app_task_mqtt"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
"APP/app_task_hello"
|
||||
"APP/app_task_wifi"
|
||||
"APP/app_task_mqtt"
|
||||
)
|
||||
|
||||
@ -16,6 +16,11 @@
|
||||
|
||||
#include "app_task_hello.h"
|
||||
#include "app_task_wifi.h"
|
||||
#include "app_task_mqtt.h"
|
||||
|
||||
#include "app_main.h"
|
||||
|
||||
static app_main_handle_t sg_app_main_handle = {0};
|
||||
|
||||
static void _init_esp()
|
||||
{
|
||||
@ -30,13 +35,15 @@ static void _init_esp()
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
app_task_wifi_t wifi_info = {0};
|
||||
printf("Hello world!\n");
|
||||
|
||||
vTaskDelay(10);
|
||||
|
||||
_init_esp();
|
||||
app_hello_task_start();
|
||||
// app_hello_task_start();
|
||||
app_task_wifi_start();
|
||||
app_task_mqtt_start();
|
||||
|
||||
/* Print chip information */
|
||||
esp_chip_info_t chip_info;
|
||||
@ -56,9 +63,16 @@ void app_main(void)
|
||||
|
||||
for (int i = 10; ; ) {
|
||||
// printf("Restarting in %d seconds...\n", i);
|
||||
app_task_wifi_get_info(&wifi_info);
|
||||
sg_app_main_handle.have_network = wifi_info.is_wifi_conneted;
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
printf("Restarting now.\n");
|
||||
fflush(stdout);
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
uint8_t app_main_handle_get_nework_state()
|
||||
{
|
||||
return sg_app_main_handle.have_network;
|
||||
}
|
||||
48
main/app_main.h
Normal file
48
main/app_main.h
Normal file
@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @file app_main.h
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-09-29
|
||||
* _ _
|
||||
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
|
||||
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
|
||||
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
|
||||
* /_/ /___/ /___/
|
||||
* @copyright Copyright (c) 2022 impressionyang
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-09-29 <td>v1.0 <td>impressionyang <td>内容
|
||||
* </table>
|
||||
*/
|
||||
#ifndef __APP_MAIN_H__
|
||||
#define __APP_MAIN_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct _app_main_handle_
|
||||
{
|
||||
uint8_t have_network;
|
||||
} app_main_handle_t;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief 获取网络连接状态
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @return uint8_t
|
||||
*
|
||||
* @details
|
||||
*/
|
||||
uint8_t app_main_handle_get_nework_state();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif//__APP_MAIN_H__
|
||||
@ -132,12 +132,12 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
|
||||
#
|
||||
# Partition Table
|
||||
#
|
||||
# CONFIG_PARTITION_TABLE_SINGLE_APP is not set
|
||||
CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y
|
||||
CONFIG_PARTITION_TABLE_SINGLE_APP=y
|
||||
# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set
|
||||
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
|
||||
# CONFIG_PARTITION_TABLE_CUSTOM is not set
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp_large.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv"
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0x8000
|
||||
CONFIG_PARTITION_TABLE_MD5=y
|
||||
# end of Partition Table
|
||||
|
||||
Loading…
Reference in New Issue
Block a user