CJ202传感器
esp32上的完整代码
主要有 驱动配置、数据解析
代码
app_uart.h
#ifndef _APP_UART_H_
#define _APP_UART_H_
void uart_init(void (*post_co2)(char*, double));
int sendData(const char* data, int len);
void rx_task(void *arg);
void uart_intr_handler(void *arg);
void (*fn_post_co2)(char*, double);
extern int uart_read_value;
#endif
app_uart.c
#include "app_uart.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#include "string.h"
#include <math.h>
const int RX_BUF_SIZE = 128;
#define TXD_PIN (GPIO_NUM_13)
#define RXD_PIN (GPIO_NUM_15)
#define UART_LOG_TAG "uart"
int uart_read_value = 10;
/**
* 初始化串口
*/
void uart_init(void (*fp)(char *, double)) {
fn_post_co2 = fp;
// 安装驱动,发送缓冲区设置为空
uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
const uart_config_t uart_config = {
.baud_rate = 9600,
// .baud_rate = 256000,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122,
.source_clk = UART_SCLK_APB,
};
// 设置参数
uart_param_config(UART_NUM_1, &uart_config);
// // 中断模式
// uart_isr_register(UART_NUM_1, uart_intr_handler, NULL, ESP_INTR_FLAG_IRAM,
// NULL); 设置引脚
uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE);
// 循环读取数值
xTaskCreate(rx_task, "uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES,
NULL);
}
/**
* 发送数据
*/
int sendData(const char *data, int len) {
const int txBytes = uart_write_bytes(UART_NUM_1, data, len);
ESP_LOGI(UART_LOG_TAG, "Wrote %d bytes: %s", txBytes, data);
return txBytes;
}
/**
* 接收数据任务
*/
void rx_task(void *arg) {
uint8_t header[] = {0x42, 0x4D};
int header_len = sizeof(header);
int one_frame_len = 16;
static const char *RX_TASK_TAG = "RX_TASK";
esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
uint8_t *data = (uint8_t *)malloc(RX_BUF_SIZE + 1);
while (1) {
int header_start_index = -1;
int rxBytes =
uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 1000 / portTICK_RATE_MS);
// 数据是否够长
if (rxBytes < one_frame_len) {
continue;
}
// 找到起始位
for (int i = 0; i < rxBytes - header_len; i++) {
if (memcmp(&data[i], header, header_len) == 0) {
header_start_index = i;
break;
}
}
if (header_start_index < 0) {
continue;
}
// 数据是否还够长
if (rxBytes - header_start_index < one_frame_len) {
continue;
}
uint8_t sum = 0;
for (int i = 0; i < one_frame_len - 1; i++) {
int index = header_start_index + i;
sum += data[index];
}
int last = header_start_index + 15;
ESP_LOGD(UART_LOG_TAG, "check sum:%02X-%d, byte15:%02X-%d, is equal: %d",
sum, sum, data[last], data[last], sum == data[last]);
for (int i = 0; i < one_frame_len; i++) {
if (i > 0 && i % 5 == 0) {
printf(" ");
}
int index = header_start_index + i;
printf("%02X", data[index]);
}
int value =
((int)data[header_start_index + 6] << 8) + data[header_start_index + 7];
printf("; %02X %02X value: %d \n", data[header_start_index + 6],
data[header_start_index + 7], value);
uart_read_value = value;
if (sum == data[last] && fn_post_co2 != NULL) {
(*fn_post_co2)("co2", value);
}
// vTaskDelay(3000 / portTICK_PERIOD_MS);
vTaskDelay(1);
}
free(data);
}