mirror of
https://git.beihong.wang/wangbeihong/iot-bedroom-environment-controller.git
synced 2026-04-23 18:03:04 +08:00
- 环境监测:温湿度/光照/空气质量传感器采集 - 智能控制:时间段/降温/通风三种自动模式 - 闹钟系统:3个闹钟+温和唤醒功能 - 远程控制:MQTT双向通信 - 本地显示:LVGL图形界面 - 双MCU架构,FreeRTOS 10任务并行 - 完整的1250行README文档
48 lines
1.5 KiB
C
48 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include "serial_mcu.h"
|
|
#include "esp_log.h"
|
|
#include "driver/uart.h"
|
|
#include "esp_err.h"
|
|
#include "string.h"
|
|
#include "driver/gpio.h"
|
|
// 这里是 serial_mcu.c 的实现代码
|
|
|
|
static const char *TAG = "serial_mcu";
|
|
|
|
|
|
#define TXD_PIN (GPIO_NUM_21) // TXD 引脚
|
|
#define RXD_PIN (GPIO_NUM_20) // RXD 引脚
|
|
#define UART_PORT_NUM UART_NUM_1 // 使用 UART1
|
|
|
|
static const int RX_BUF_SIZE = 1024; // 接收缓冲区大小
|
|
|
|
// 初始化串口
|
|
void serial_mcu_init(void)
|
|
{
|
|
// TODO: 初始化串口
|
|
ESP_LOGD(TAG, "Serial MCU initialized");
|
|
|
|
const uart_config_t uart_config = {
|
|
.baud_rate = 230400, // 波特率
|
|
.data_bits = UART_DATA_8_BITS, // 数据位
|
|
.parity = UART_PARITY_DISABLE, // 校验位
|
|
.stop_bits = UART_STOP_BITS_1, // 停止位
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, // 流控
|
|
.source_clk = UART_SCLK_DEFAULT, // 时钟源
|
|
};
|
|
|
|
// 配置串口
|
|
uart_driver_install(UART_PORT_NUM, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
|
|
uart_param_config(UART_PORT_NUM, &uart_config);
|
|
uart_set_pin(UART_PORT_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); // 设置引脚
|
|
// 日志
|
|
ESP_LOGI(TAG, "UART driver installed");}
|
|
|
|
int sendControlFrame(uint8_t cmd, uint8_t data)
|
|
{
|
|
uint8_t buf[3] = {0x55, cmd, data};
|
|
int txBytes = uart_write_bytes(UART_NUM_1, (const char*)buf, 3);
|
|
ESP_LOGI("TX", "Sent %d bytes CMD=0x%02X DATA=%d", txBytes, cmd, data);
|
|
return txBytes;
|
|
}
|