第一次提交:完成了网关的单路485数据采集,还有以太网链接和MQTT配置,实现数据上报和命令下发,差一个断网储存
This commit is contained in:
4
main/CMakeLists.txt
Executable file
4
main/CMakeLists.txt
Executable file
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES nvs_flash STATUS_LED RS-485-SP3485EEN ETH_CH390H MQTT_ESP SNTP_ESP
|
||||
)
|
||||
18
main/idf_component.yml
Normal file
18
main/idf_component.yml
Normal file
@@ -0,0 +1,18 @@
|
||||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
## Required IDF version
|
||||
idf:
|
||||
version: '>=4.1.0'
|
||||
# # Put list of dependencies here
|
||||
# # For components maintained by Espressif:
|
||||
# component: "~1.0.0"
|
||||
# # For 3rd party components:
|
||||
# username/component: ">=1.0.0,<2.0.0"
|
||||
# username2/component2:
|
||||
# version: "~1.0.0"
|
||||
# # For transient dependencies `public` flag can be set.
|
||||
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||
# # All dependencies of `main` are public by default.
|
||||
# public: true
|
||||
|
||||
espressif/mqtt: ^1.0.0
|
||||
81
main/main.c
Executable file
81
main/main.c
Executable file
@@ -0,0 +1,81 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user