Files
DistributedCollectorGateway/main/main.c

82 lines
2.5 KiB
C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "RS-485-SP3485EEN.h"
#include "ETH_CH390H.h"
#include "MQTT_ESP.h"
#include "STATUS_LED.h"
#include "MODBUS_ESP.h"
#include "SNTP_ESP.h"
#define TAG "main"
void app_main(void)
{
// 1. 初始化LED最先初始化让用户知道设备已上电
status_led_init();
status_led_blink_mode(1, 1); // LED1 快闪:系统启动中
status_led_blink_mode(2, 0); // LED2 慢闪:等待初始化
ESP_ERROR_CHECK(nvs_flash_init());
// 初始化以太网,这里包含了 esp_netif_init();和esp_event_loop_create_default();
eth_init();
// 初始化RS485
init_specific_rs485_channel(0); // 初始化通道0
start_rs485_rx_task_for_channel(0, 5, 4096); // 为通道0启动接收任务
// 等待网络连接建立
ESP_LOGI(TAG, "Waiting for network connection...");
esp_netif_t *eth_netif = esp_netif_get_handle_from_ifkey("ETH_DEF"); // 获取默认以太网接口
// 循环等待直到获得IP地址
while (true)
{
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(eth_netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0)
{
ESP_LOGI(TAG, "Network connected with IP: " IPSTR, IP2STR(&ip_info.ip));
break;
}
ESP_LOGI(TAG, "Waiting for IP address...");
vTaskDelay(pdMS_TO_TICKS(1000)); // 等待1秒后重试
}
// 初始化SNTP时间同步服务
ESP_LOGI(TAG, "Initializing SNTP time synchronization...");
sntp_esp_init();
// 等待时间同步完成最长等待10秒
if (sntp_esp_wait_sync(10000)) {
ESP_LOGI(TAG, "Time synchronization completed successfully");
} else {
ESP_LOGW(TAG, "Time synchronization timeout, using local time");
}
ESP_LOGI(TAG, "Starting MQTT client...");
// 启动MQTT客户端
mqtt_app_start();
// 网络连接成功后
status_led_set(1, 1); // LED1 常亮:网络正常
// MQTT 启动后
status_led_blink_mode(2, 2); // LED2 心跳:系统运行正常
// ============================
// 启动设备状态上报任务每10秒上报一次
// ============================
if (mqtt_start_device_status_task(10000) == pdPASS) {
ESP_LOGI(TAG, "Device status report task started");
} else {
ESP_LOGE(TAG, "Failed to start device status report task");
}
ESP_LOGI(TAG, "Waiting for MODBUS poll command via MQTT...");
for (;;)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}