Compare commits
14 Commits
638ad0238f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b8d6128259 | |||
| f4f1772db6 | |||
| dbfdce8ec9 | |||
| c976c93aa5 | |||
| d85cdffa6f | |||
| e280004ddd | |||
| bc65530c00 | |||
| f4bd30a4dc | |||
| 60542be965 | |||
| f729f2268b | |||
| c0e3e0addd | |||
| 0d9424bf2b | |||
| 826e1aefd5 | |||
| 76036bcee7 |
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,9 +1,9 @@
|
||||
[submodule "SOFTWARE/Lib/pico-onewire"]
|
||||
path = SOFTWARE/Lib/pico-onewire
|
||||
url = https://github.com/adamboardman/pico-onewire.git
|
||||
[submodule "SOFTWARE/Server/libconf"]
|
||||
path = SOFTWARE/Server/libconf
|
||||
url = https://git.aixiao.me/aixiao/libconf.git
|
||||
[submodule "SOFTWARE-FreeRTOS/FreeRTOS-Kernel"]
|
||||
path = SOFTWARE-FreeRTOS/FreeRTOS-Kernel
|
||||
url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git
|
||||
[submodule "SERVER/libconf"]
|
||||
path = SERVER/libconf
|
||||
url = https://git.aixiao.me/aixiao/libconf.git
|
||||
|
||||
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
@@ -1,4 +1,12 @@
|
||||
{
|
||||
"cmake.sourceDirectory": "/mnt/c/Users/niuyuling/Desktop/raspberry-pico/Danger-alarm/SOFTWARE",
|
||||
"C_Cpp.errorSquiggles": "disabled"
|
||||
"C_Cpp.errorSquiggles": "disabled",
|
||||
"files.associations": {
|
||||
"cstring": "cpp",
|
||||
"random": "cpp",
|
||||
"system_error": "c",
|
||||
"*.tcc": "cpp",
|
||||
"streambuf": "cpp"
|
||||
},
|
||||
"cmake.ignoreCMakeListsMissing": true
|
||||
}
|
||||
26
README.md
26
README.md
@@ -1,4 +1,4 @@
|
||||
# 基于 Raspberry Pico / Pico W 的厨房危险(火灾)报警 (Pico W WiFi传输暂时不玩)
|
||||
# 基于 Raspberry Pico / Pico W (RP2040 FreeRTOS) 的厨房危险(火灾)报警 (Pico W WiFi传输暂时不玩)
|
||||
|
||||
* 使用 DS18B20温度传感器
|
||||
* 使用 CH4 N55A甲烷气体传感器(进口) (弃用)
|
||||
@@ -11,14 +11,20 @@
|
||||
## 源码构建
|
||||
|
||||
```bash
|
||||
# 使用WSL Debian GNU/Linux 12 (bookworm) 构建
|
||||
# 使用WSL Debian GNU/Linux 12 (bookworm) 构建,或者 Operating System: Ubuntu 23.10
|
||||
# 确保Pico-SDK环境变量
|
||||
export PICO_SDK_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry-pico/SDK/pico-sdk
|
||||
export PICO_EXTRAS_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry-pico/SDK/pico-extras
|
||||
# 使用官方工具 https://github.com/raspberrypi/pico-setup 下载并设置 SDK 环境变量
|
||||
export PICO_SDK_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry/pico-setup/pico/pico-sdk
|
||||
export PICO_EXAMPLES_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry/pico-setup/pico/pico-examples
|
||||
export PICO_EXTRAS_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry/pico-setup/pico/pico-extras
|
||||
export PICO_PLAYGROUND_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry/pico-setup/pico/pico-playground
|
||||
export PICOTOOL_FETCH_FROM_GIT_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry/pico-setup/pico/picotool
|
||||
|
||||
apt install cmake gcc-arm-none-eabi gcc g++
|
||||
apt install gdb-multiarch automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev
|
||||
|
||||
# RP2040 SDK 版本构建
|
||||
cd ~
|
||||
git clone https://git.aixiao.me/aixiao/Danger-alarm.git
|
||||
cd Danger-alarm
|
||||
git submodule init
|
||||
@@ -29,15 +35,21 @@
|
||||
cmake -DPICO_BOARD=pico_w ..
|
||||
make
|
||||
|
||||
# FreeRTOS 版本构建
|
||||
cd ~/Danger-alarm/SOFTWARE-FreeRTOS
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -DPICO_BOARD=pico_w .. && make -j4
|
||||
|
||||
# 树莓派Zero W 433MHZ HC-12接收服务端构建
|
||||
|
||||
# 树莓派Zero W 433MHZ HC-12接收服务端构建(需要自己开启 Raspberry Pi 串口, 并连接好硬件 HC-12)
|
||||
apt install libmysqlclient-dev
|
||||
cd ~
|
||||
git clone https://github.com/WiringPi/WiringPi.git
|
||||
cd WiringPi
|
||||
./build
|
||||
cd ~/Danger-alarm/SOFTWARE/Server/hc-12
|
||||
make clean; make
|
||||
cd ~/Danger-alarm/SERVER/libconf && make clean; make
|
||||
cd ~/Danger-alarm/SERVER/hc-12 && make clean; make
|
||||
./hc-12
|
||||
|
||||
```
|
||||
|
||||
@@ -17,5 +17,5 @@ hc-12: hc-12.o ../libconf/libconf.o mysql.o
|
||||
$(CC) $(CFLAGS) $(MYSQL_CFLAGS) -c $< $@
|
||||
|
||||
clean:
|
||||
rm hc-12 hc-12.o ../libconf/libconf.o mysql.o
|
||||
rm hc-12 *.o
|
||||
|
||||
@@ -189,6 +189,7 @@ int loop(int fd, CONF *conf, DATA *DATA)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// CO
|
||||
if (strstr(receivedString, "CO")) {
|
||||
|
||||
@@ -246,6 +247,14 @@ int loop(int fd, CONF *conf, DATA *DATA)
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
// Boot Time
|
||||
if (strstr(receivedString, "Boot Time")) {
|
||||
if (1 == (sscanf(receivedString, "Boot Time: %llu seconds\n", &DATA->boot_time_))) {
|
||||
printf("Boot Time: %llu seconds\n", DATA->boot_time_);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
receivedString[0] = '\0';
|
||||
@@ -360,6 +369,7 @@ int main()
|
||||
data->co_1st = 1;
|
||||
data->co2_1st = 1;
|
||||
data->_time = 180;
|
||||
data->boot_time_ = 0;
|
||||
|
||||
redirect_stdout_to_file(logfd, "hc-12.log");
|
||||
pthread_mutex_init(&mutex, NULL); // 初始化互斥锁
|
||||
@@ -48,6 +48,9 @@ typedef struct DATA {
|
||||
float co2;
|
||||
float ch4_;
|
||||
|
||||
// boot time
|
||||
long long unsigned int boot_time_;
|
||||
|
||||
// 超过阈值次数
|
||||
int ds18b20_num;
|
||||
int ch4_num;
|
||||
@@ -11,6 +11,14 @@ int _mysql(char *sql, char *MYSQL_HOST, char *MYSQL_PORT_, char *MYSQL_USRT, cha
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
int timeout = 7; // 设置超时时间(秒)
|
||||
|
||||
// 设置连接超时时间
|
||||
mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
|
||||
|
||||
// 设置读、写超时时间(影响查询)
|
||||
mysql_options(&mysql, MYSQL_OPT_READ_TIMEOUT, &timeout);
|
||||
mysql_options(&mysql, MYSQL_OPT_WRITE_TIMEOUT, &timeout);
|
||||
|
||||
if (mysql_real_connect(&mysql, MYSQL_HOST, MYSQL_USRT, MYSQL_PASSWORD, "mysql", atoi(MYSQL_PORT_), NULL, 0)) {
|
||||
if (0 != mysql_set_character_set(&mysql, "utf8")) {
|
||||
@@ -41,6 +41,8 @@ target_Sources(main PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/ZC13.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/ZE07CO.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/MHZ14B.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/HC-12.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/boot_time.cpp
|
||||
)
|
||||
|
||||
target_include_directories(main PRIVATE
|
||||
@@ -75,6 +77,7 @@ target_link_libraries(
|
||||
hardware_pwm
|
||||
hardware_adc
|
||||
hardware_pio
|
||||
hardware_timer
|
||||
)
|
||||
|
||||
pico_enable_stdio_uart(main 1)
|
||||
|
||||
@@ -1,144 +1,58 @@
|
||||
#include "DS18B20.hpp"
|
||||
#include "HC-12.hpp"
|
||||
#include "common.hpp"
|
||||
|
||||
QueueHandle_t xQueue;
|
||||
|
||||
|
||||
#define MAX_TIMINGS 85
|
||||
|
||||
void ds18b20_init() {
|
||||
gpio_init(DS18B20_PIN);
|
||||
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
|
||||
gpio_put(DS18B20_PIN, 1); // 上拉电阻
|
||||
}
|
||||
|
||||
void ds18b20_write_bit(uint8_t bit) {
|
||||
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
|
||||
gpio_put(DS18B20_PIN, 0);
|
||||
sleep_us(bit ? 10 : 60);
|
||||
gpio_put(DS18B20_PIN, 1);
|
||||
sleep_us(bit ? 55 : 5);
|
||||
}
|
||||
|
||||
uint8_t ds18b20_read_bit() {
|
||||
uint8_t bit = 0;
|
||||
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
|
||||
gpio_put(DS18B20_PIN, 0);
|
||||
sleep_us(3);
|
||||
gpio_set_dir(DS18B20_PIN, GPIO_IN);
|
||||
sleep_us(10);
|
||||
bit = gpio_get(DS18B20_PIN);
|
||||
sleep_us(50);
|
||||
return bit;
|
||||
}
|
||||
|
||||
void ds18b20_write_byte(uint8_t byte) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ds18b20_write_bit(byte & 0x01);
|
||||
byte >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ds18b20_read_byte() {
|
||||
uint8_t byte = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
byte >>= 1;
|
||||
if (ds18b20_read_bit()) {
|
||||
byte |= 0x80;
|
||||
}
|
||||
}
|
||||
return byte;
|
||||
}
|
||||
|
||||
int ds18b20_reset() {
|
||||
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
|
||||
gpio_put(DS18B20_PIN, 0);
|
||||
sleep_us(480);
|
||||
gpio_put(DS18B20_PIN, 1);
|
||||
gpio_set_dir(DS18B20_PIN, GPIO_IN);
|
||||
sleep_us(70);
|
||||
int presence = !gpio_get(DS18B20_PIN);
|
||||
sleep_us(410);
|
||||
return presence;
|
||||
}
|
||||
|
||||
float ds18b20_read_temperature() {
|
||||
if (!ds18b20_reset()) {
|
||||
printf("DS18B20 not found\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ds18b20_write_byte(0xCC); // 跳过ROM指令
|
||||
ds18b20_write_byte(0x44); // 启动温度转换
|
||||
|
||||
sleep_ms(750); // 温度转换时间,具体时间请参考传感器数据手册
|
||||
|
||||
if (!ds18b20_reset()) {
|
||||
printf("DS18B20 not found\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ds18b20_write_byte(0xCC); // 跳过ROM指令
|
||||
ds18b20_write_byte(0xBE); // 读取暂存器
|
||||
|
||||
uint8_t lsb = ds18b20_read_byte();
|
||||
uint8_t msb = ds18b20_read_byte();
|
||||
int16_t temp = (msb << 8) | lsb;
|
||||
|
||||
// 检查传感器数据有效性
|
||||
if (temp == 0xFFFF) {
|
||||
printf("Invalid temperature data\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return temp / 16.0; // 转换为摄氏度
|
||||
}
|
||||
|
||||
|
||||
// 温度传感器
|
||||
void DS18B20(void *pvParameters)
|
||||
{
|
||||
|
||||
// One_wire 第三方库方法
|
||||
float TEMPERATURE = -1;
|
||||
|
||||
char TEMPERATURE_TEMP[BUFER] = { 0 };
|
||||
One_wire one_wire(DS18B20_PIN);
|
||||
one_wire.init();
|
||||
rom_address_t address {
|
||||
|
||||
};
|
||||
|
||||
|
||||
_printTaskStackHighWaterMark("DS18B20");
|
||||
|
||||
while (1) {
|
||||
|
||||
|
||||
one_wire.single_device_read_rom(address);
|
||||
one_wire.convert_temperature(address, true, false);
|
||||
int delay_time = one_wire.convert_temperature(address, true, false);
|
||||
//printf("Conversion delay time: %d ms\n", delay_time);
|
||||
|
||||
TEMPERATURE = one_wire.temperature(address);
|
||||
|
||||
printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x DS18B20 Temperature: %3.1f°C\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
|
||||
|
||||
printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x DS18B20 Temperature: %3.1f°C\n", \
|
||||
address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], \
|
||||
address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
|
||||
|
||||
if (TEMPERATURE == -1000) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (TEMPERATURE != 85) {
|
||||
|
||||
sprintf(TEMPERATURE_TEMP, "Temperature: %.3f°C\n", TEMPERATURE);
|
||||
_HC_12(TEMPERATURE_TEMP);
|
||||
memset(TEMPERATURE_TEMP, 0, BUFER);
|
||||
TEMPERATURE = -1;
|
||||
}
|
||||
/*
|
||||
// 发送数据到队列
|
||||
xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
|
||||
*/
|
||||
//_printTaskStackHighWaterMark("DS18B20");
|
||||
watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
//watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(5000)); // 非阻塞延时
|
||||
}
|
||||
|
||||
/*
|
||||
ds18b20_init();
|
||||
|
||||
while (1) {
|
||||
float temperature = ds18b20_read_temperature();
|
||||
if (temperature != -1) {
|
||||
printf("DS18B20 Temperature: %.2f°C\n", temperature);
|
||||
} else {
|
||||
printf("Failed to read temperature\n");
|
||||
}
|
||||
|
||||
watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
}
|
||||
*/
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "semphr.h"
|
||||
#include "queue.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <stdbool.h>
|
||||
#include "pico/stdlib.h"
|
||||
@@ -15,6 +16,7 @@
|
||||
#include "../../SOFTWARE/Lib/pico-onewire/api/one_wire.h"
|
||||
|
||||
#define DS18B20_PIN 15 // DS18B20 引脚
|
||||
#define BUFER 256
|
||||
|
||||
extern QueueHandle_t xQueue;
|
||||
extern void DS18B20(void *pvParameters);
|
||||
|
||||
@@ -11,7 +11,7 @@ void _HC_12_INIT()
|
||||
return;
|
||||
}
|
||||
|
||||
void _HC_12(const char *string)
|
||||
void _HC_12(char *string)
|
||||
{
|
||||
uart_tx_program_puts(HC_12_PIO, HC_12_PIO_SM_TX, string);
|
||||
sleep_ms(100);
|
||||
|
||||
@@ -14,6 +14,6 @@
|
||||
#define HC_12_PIO_SERIAL_BAUD 9600
|
||||
|
||||
extern void _HC_12_INIT();
|
||||
extern void _HC_12(const char *string);
|
||||
extern void _HC_12(char *string);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "MHZ14B.hpp"
|
||||
#include "common.hpp"
|
||||
#include "HC-12.hpp"
|
||||
|
||||
void MH_Z14B_INIT()
|
||||
{
|
||||
@@ -9,16 +10,18 @@ void MH_Z14B_INIT()
|
||||
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
|
||||
uart_set_hw_flow(UART1, false, false);
|
||||
uart_set_format(UART1, DATA_BITS, STOP_BITS, PARITY);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
// CO2
|
||||
static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
|
||||
{
|
||||
|
||||
// 0x86 读气体浓度值
|
||||
uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
|
||||
uart_write_blocking(UART1, CMD, 9);
|
||||
sleep_ms(200);
|
||||
//sleep_ms(200);
|
||||
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
|
||||
|
||||
// 读取
|
||||
uint8_t CO2_DATA[9] = { 0 };
|
||||
@@ -28,46 +31,64 @@ static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
|
||||
uint16_t CO2_CONC = (256 * CO2_DATA[2]) + CO2_DATA[3];
|
||||
|
||||
// 校验
|
||||
uint8_t CHECKSUM = (0xFF - (CO2_DATA[1] + CO2_DATA[2] + CO2_DATA[3] + CO2_DATA[4] + CO2_DATA[5] + CO2_DATA[6] + CO2_DATA[7])) + 1;
|
||||
uint8_t CHECKSUM = (0xFF - (CO2_DATA[1] + CO2_DATA[2] + CO2_DATA[3] + CO2_DATA[4] + \
|
||||
CO2_DATA[5] + CO2_DATA[6] + CO2_DATA[7])) + 1;
|
||||
|
||||
if (CO2_DATA[8] == CHECKSUM && CO2_DATA[1] == 0x86) {
|
||||
//printf("CHECKSUM: %X = %X\n", CO2_DATA[8], CHECKSUM);
|
||||
//printf("CO2 Concentration: %d ppm\n", CO2_CONC);
|
||||
*MH_Z14B_DATA_IS_OK = 1;
|
||||
} else {
|
||||
}
|
||||
/*
|
||||
else {
|
||||
// 校准传感器 零点 (ZERO)
|
||||
uint8_t ZERO[] = { 0XFF, 0X01, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00, 0X78 };
|
||||
uart_write_blocking(UART1, ZERO, 9);
|
||||
sleep_ms(200);
|
||||
//sleep_ms(200);
|
||||
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
|
||||
|
||||
// 校准传感器 跨度点 (SPAN)
|
||||
uint8_t SPAN[] = { 0XFF, 0X01, 0X88, 0X07, 0XD0, 0X00, 0X00, 0X00, 0XA0 };
|
||||
uart_write_blocking(UART1, SPAN, 9);
|
||||
sleep_ms(200);
|
||||
//sleep_ms(200);
|
||||
vTaskDelay(pdMS_TO_TICKS(200)); // 非阻塞延时
|
||||
|
||||
*MH_Z14B_DATA_IS_OK = 0;
|
||||
printf("CO2 concentration reading failed!\n");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
return CO2_CONC;
|
||||
}
|
||||
|
||||
void CO2(void *pvParameters)
|
||||
{
|
||||
uint16_t CO2_DATA = -1;
|
||||
uint16_t CO2_DATA = 0;
|
||||
int MH_Z14B_DATA_IS_OK = 0;
|
||||
char CO2_DATA_TEMP[BUFER] = { 0 };
|
||||
MH_Z14B_INIT();
|
||||
_printTaskStackHighWaterMark("CO2");
|
||||
|
||||
while (1) {
|
||||
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
|
||||
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
|
||||
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
|
||||
printf("CO2 Concentration: %d ppm\n", CO2_DATA);
|
||||
|
||||
snprintf(CO2_DATA_TEMP, BUFER, "CO2 Concentration: %d ppm\n", CO2_DATA);
|
||||
_HC_12(CO2_DATA_TEMP);
|
||||
memset(CO2_DATA_TEMP, 0, BUFER);
|
||||
} else {
|
||||
printf("CO2 reading failed!!!");
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
}
|
||||
|
||||
//_printTaskStackHighWaterMark("CO2");
|
||||
watchdog_update(); // 喂狗
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
vTaskDelay(pdMS_TO_TICKS(5000)); // 非阻塞延时
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "semphr.h"
|
||||
#include "queue.h"
|
||||
|
||||
#include <cstring>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/clocks.h"
|
||||
#include "hardware/watchdog.h"
|
||||
@@ -26,6 +27,7 @@
|
||||
#define UART1_TX_PIN 5 // MH-Z14B T
|
||||
#define UART1_RX_PIN 4 // MH-Z14B R
|
||||
|
||||
#define BUFER 256
|
||||
extern void CO2(void *pvParameters);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ZC13.hpp"
|
||||
#include "common.hpp"
|
||||
#include "HC-12.hpp"
|
||||
|
||||
int ZC13_INIT()
|
||||
{
|
||||
@@ -33,7 +34,7 @@ int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
|
||||
if (uart_rx_program_available(pio, sm)) {
|
||||
c = uart_rx_program_getc(pio, sm);
|
||||
DATA[received_count++] = c;
|
||||
//printf("0x%X ", c);
|
||||
printf("0x%X ", c);
|
||||
if (c == '\n') {
|
||||
// 接收到换行符,停止接收数据
|
||||
break;
|
||||
@@ -66,8 +67,8 @@ int ZC13(const char *model)
|
||||
// 接收指令
|
||||
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
|
||||
|
||||
//printf("\n");
|
||||
// printf("%d\n", CH4_DATA_LEN);
|
||||
printf("\n");
|
||||
printf("%d\n", CH4_DATA_LEN);
|
||||
|
||||
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
|
||||
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
|
||||
@@ -104,13 +105,20 @@ int ZC13(const char *model)
|
||||
void CH4(void *pvParameters)
|
||||
{
|
||||
ZC13_INIT();
|
||||
_printTaskStackHighWaterMark("ZC13");
|
||||
char CH4_DATA[BUFER] = { 0 };
|
||||
_printTaskStackHighWaterMark("CH4");
|
||||
|
||||
while (1) {
|
||||
|
||||
ZC13("ZC05");
|
||||
//_printTaskStackHighWaterMark("ZC13");
|
||||
watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
while (1)
|
||||
{
|
||||
|
||||
snprintf(CH4_DATA, BUFER, "CH4 Concentration: %d\n", ZC13("ZC05"));
|
||||
_HC_12(CH4_DATA);
|
||||
memset(CH4_DATA, 0, BUFER);
|
||||
|
||||
//_printTaskStackHighWaterMark("CH4");
|
||||
//watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(5000)); // 非阻塞延时
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "semphr.h"
|
||||
#include "queue.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include "pico/stdlib.h"
|
||||
@@ -22,6 +23,7 @@
|
||||
#define ZC13_PIO_SM_RX 1
|
||||
#define ZC13_PIO_SERIAL_BAUD 9600
|
||||
|
||||
#define BUFER 256
|
||||
extern void CH4(void *pvParameters);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "ZE07CO.hpp"
|
||||
#include "HC-12.hpp"
|
||||
#include "common.hpp"
|
||||
|
||||
void ZE07CO_INIT()
|
||||
@@ -20,6 +21,7 @@ static uint16_t ZE07CO(int ANSWER, int *ZE07_CO_DATA_IS_OK)
|
||||
static uint8_t _ANSWER[9] = { 0xFF, 0x01, 0x78, 0x41, 0x00, 0x00, 0x00, 0x00, 0x46 };
|
||||
uart_write_blocking(UART0, _ANSWER, 9);
|
||||
sleep_ms(100);
|
||||
|
||||
}
|
||||
|
||||
if (ANSWER == 2) {
|
||||
@@ -71,10 +73,16 @@ void CO(void *pvParameters)
|
||||
CO_DATA = ZE07CO(2, &ZE07_CO_DATA_IS_OK);
|
||||
if (CO_DATA != -1 && ZE07_CO_DATA_IS_OK == 1) {
|
||||
printf("CO Concentration: %d ppm\n", CO_DATA);
|
||||
|
||||
char CO_DATA_TEMP[BUFER] = { 0 };
|
||||
sprintf(CO_DATA_TEMP, "CO Concentration: %d ppm\n", CO_DATA);
|
||||
_HC_12(CO_DATA_TEMP);
|
||||
|
||||
}
|
||||
|
||||
//_printTaskStackHighWaterMark("CO");
|
||||
watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
//watchdog_update(); // 喂狗
|
||||
vTaskDelay(pdMS_TO_TICKS(5000)); // 非阻塞延时
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#define UART0_TX_PIN 1 // T
|
||||
#define UART0_RX_PIN 0 // R
|
||||
|
||||
#define BUFER 256
|
||||
extern void CO(void *pvParameters);
|
||||
|
||||
#endif
|
||||
|
||||
39
SOFTWARE-FreeRTOS/Source/boot_time.cpp
Normal file
39
SOFTWARE-FreeRTOS/Source/boot_time.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
#include "common.hpp"
|
||||
#include "HC-12.hpp"
|
||||
#include "boot_time.hpp"
|
||||
|
||||
|
||||
void BOOT_TIME(void *pvParameters)
|
||||
{
|
||||
(void)pvParameters;
|
||||
|
||||
_printTaskStackHighWaterMark("BOOT_TIME");
|
||||
|
||||
int _HC_12_SEND_NUM = 0;
|
||||
char BOOT_TIME_TEMP[BUFER] = { 0 };
|
||||
|
||||
|
||||
while (1) {
|
||||
// 获取自开机以来的微秒数
|
||||
uint64_t uptime_us = time_us_64();
|
||||
uint64_t total_uptime_sec = uptime_us / 1000000;
|
||||
|
||||
// 打印开机秒数
|
||||
printf("Boot Time: %llu seconds\n", total_uptime_sec);
|
||||
|
||||
{
|
||||
_HC_12_SEND_NUM++;
|
||||
if (_HC_12_SEND_NUM > 5) {
|
||||
sprintf(BOOT_TIME_TEMP, "Boot Time: %llu seconds\n", total_uptime_sec);
|
||||
_HC_12(BOOT_TIME_TEMP);
|
||||
memset(BOOT_TIME_TEMP, 0, BUFER);
|
||||
_HC_12_SEND_NUM = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
_printTaskStackHighWaterMark("BOOT_TIME");
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); // 每秒延迟
|
||||
}
|
||||
}
|
||||
17
SOFTWARE-FreeRTOS/Source/boot_time.hpp
Normal file
17
SOFTWARE-FreeRTOS/Source/boot_time.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef BOOT_TIME_H
|
||||
#define BOOT_TIME_H
|
||||
|
||||
/* Scheduler include files. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#include "queue.h"
|
||||
|
||||
#include <cstring>
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#define BUFER 256
|
||||
|
||||
extern void BOOT_TIME(void *pvParameters);
|
||||
|
||||
#endif
|
||||
@@ -14,14 +14,16 @@
|
||||
#include "main.hpp"
|
||||
#include "lwipopts.h"
|
||||
#include "common.hpp"
|
||||
#include "ds18b20.hpp"
|
||||
#include "DS18B20.hpp"
|
||||
#include "ZC13.hpp"
|
||||
#include "ZE07CO.hpp"
|
||||
#include "MHZ14B.hpp"
|
||||
#include "HC-12.hpp"
|
||||
#include "boot_time.hpp"
|
||||
|
||||
#ifndef PICO_DEFAULT_LED_PIN
|
||||
#warning pio/hello_pio example requires a board with a regular LED
|
||||
#define PICO_DEFAULT_LED_PIN 25
|
||||
#warning pio/hello_pio example requires a board with a regular LED
|
||||
#define PICO_DEFAULT_LED_PIN 25
|
||||
#endif
|
||||
|
||||
void Led_Blinky(void *pvParameters)
|
||||
@@ -48,7 +50,7 @@ void Led_Blinky(void *pvParameters)
|
||||
// 处理接收到的数据
|
||||
printf("%f\n", TEMPERATURE);
|
||||
}
|
||||
//_printTaskStackHighWaterMark("Led_Blinky");
|
||||
_printTaskStackHighWaterMark("Led_Blinky");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +62,7 @@ void CPU(void *pvParameters)
|
||||
adc_set_temp_sensor_enabled(true);
|
||||
adc_select_input(4); // Input 4 is the onboard temperature sensor.
|
||||
|
||||
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
|
||||
_printTaskStackHighWaterMark("CPU_");
|
||||
|
||||
while (1) {
|
||||
const float conversionFactor = 3.3f / (1 << 12);
|
||||
@@ -69,14 +71,19 @@ void CPU(void *pvParameters)
|
||||
float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
|
||||
|
||||
printf("CPU temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
|
||||
|
||||
|
||||
char ONBOARD_TEMPERATURE_TEMP[BUFER] = { 0 };
|
||||
sprintf(ONBOARD_TEMPERATURE_TEMP, "Onboard temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
|
||||
_HC_12(ONBOARD_TEMPERATURE_TEMP);
|
||||
|
||||
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
|
||||
_printTaskStackHighWaterMark("CPU_");
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
stdio_init_all();
|
||||
sleep_ms(3000);
|
||||
@@ -88,11 +95,13 @@ int main(void)
|
||||
printf("Clean boot\n");
|
||||
}
|
||||
watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒)
|
||||
watchdog_start_tick(12);
|
||||
//watchdog_start_tick(12);
|
||||
|
||||
// 创建队列
|
||||
xQueue = xQueueCreate(10, sizeof(long));
|
||||
|
||||
_HC_12_INIT();
|
||||
|
||||
// 创建任务
|
||||
BaseType_t xReturned;
|
||||
TaskHandle_t CPU_xHandle = NULL;
|
||||
@@ -101,7 +110,8 @@ int main(void)
|
||||
TaskHandle_t CH4_xHandle = NULL;
|
||||
TaskHandle_t CO_xHandle = NULL;
|
||||
TaskHandle_t CO2_xHandle = NULL;
|
||||
|
||||
TaskHandle_t BOOT_TIME_xHandle = NULL;
|
||||
|
||||
|
||||
// 板载CPU温度
|
||||
xReturned = xTaskCreate(CPU, "CPU task", 512, NULL, tskIDLE_PRIORITY, &CPU_xHandle);
|
||||
@@ -122,23 +132,30 @@ int main(void)
|
||||
}
|
||||
|
||||
// CH4
|
||||
xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
|
||||
xReturned = xTaskCreate(CH4, "CH4 task", 2048, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
|
||||
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
|
||||
printf("CH4() Task Error!");
|
||||
}
|
||||
|
||||
|
||||
// CO
|
||||
xReturned = xTaskCreate(CO, "CO task", 2048, NULL, tskIDLE_PRIORITY, &CO_xHandle);
|
||||
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
|
||||
printf("CO() Task Error!");
|
||||
}
|
||||
|
||||
|
||||
// CO2
|
||||
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
|
||||
xReturned = xTaskCreate(CO2, "CO2 task", 2048, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
|
||||
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
|
||||
printf("CO2() Task Error!");
|
||||
}
|
||||
|
||||
// Boot Time
|
||||
xReturned = xTaskCreate(BOOT_TIME, "BOOT_TIME task", 512, NULL, tskIDLE_PRIORITY, &BOOT_TIME_xHandle);
|
||||
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
|
||||
printf("CO2() Task Error!");
|
||||
}
|
||||
|
||||
|
||||
vTaskStartScheduler();
|
||||
while (1) {};
|
||||
return 0;
|
||||
|
||||
@@ -14,4 +14,6 @@
|
||||
#include "hardware/pwm.h"
|
||||
#include "hardware/adc.h"
|
||||
|
||||
#define BUFER 256
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,12 +17,13 @@ pico_generate_pio_header(Danger-alarm ${CMAKE_CURRENT_LIST_DIR}/Source/uart_tx.p
|
||||
pico_generate_pio_header(Danger-alarm ${CMAKE_CURRENT_LIST_DIR}/Source/uart_rx.pio)
|
||||
|
||||
target_sources(Danger-alarm PRIVATE
|
||||
lib/radio-switch.cc
|
||||
Lib/radio-switch.cc
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/common.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/MAIN.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/DTH11.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/HC-12.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/ZC13.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/Source/WIFI.c
|
||||
)
|
||||
|
||||
pico_enable_stdio_uart(Danger-alarm 1)
|
||||
|
||||
@@ -16,10 +16,14 @@
|
||||
#include "HC-12.hpp"
|
||||
#include "ZC13.hpp"
|
||||
|
||||
#ifndef PICO_DEFAULT_LED_PIN
|
||||
#warning pio/hello_pio example requires a board with a regular LED
|
||||
#define PICO_DEFAULT_LED_PIN 25
|
||||
extern "C" {
|
||||
#include "WIFI.h"
|
||||
}
|
||||
|
||||
|
||||
#ifndef PICO_DEFAULT_LED_PIN
|
||||
#warning pio/hello_pio example requires a board with a regular LED
|
||||
#define PICO_DEFAULT_LED_PIN 25
|
||||
#endif
|
||||
|
||||
static inline bool uart_rx_program_available(PIO pio, uint sm)
|
||||
@@ -150,7 +154,9 @@ static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
|
||||
//printf("CHECKSUM: %X = %X\n", CO2_DATA[8], CHECKSUM);
|
||||
//printf("CO2 Concentration: %d ppm\n", CO2_CONC);
|
||||
*MH_Z14B_DATA_IS_OK = 1;
|
||||
} else {
|
||||
}
|
||||
/*
|
||||
else {
|
||||
// 校准传感器 零点 (ZERO)
|
||||
uint8_t ZERO[] = { 0XFF, 0X01, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00, 0X78 };
|
||||
uart_write_blocking(UART1, ZERO, 9);
|
||||
@@ -164,6 +170,7 @@ static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
|
||||
*MH_Z14B_DATA_IS_OK = 0;
|
||||
printf("CO2 concentration reading failed!\n");
|
||||
}
|
||||
*/
|
||||
|
||||
return CO2_CONC;
|
||||
}
|
||||
@@ -251,7 +258,101 @@ static void core1_main()
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
|
||||
#define TARGET_SSID "cpyfb-01" // 替换为你要连接的 SSID
|
||||
#define TARGET_PASSWORD "aa1122334" // 替换为你的 Wi-Fi 密码
|
||||
static bool is_target_ssid_found = false; // 标志:目标 SSID 是否被找到
|
||||
|
||||
// 扫描结果处理函数
|
||||
static int scan_result(void *env, const cyw43_ev_scan_result_t *result) {
|
||||
if (result) {
|
||||
// 输出每个扫描结果的信息
|
||||
printf("ssid: %-32s rssi: %4d chan: %3d mac: %02x:%02x:%02x:%02x:%02x:%02x sec: %u\n",
|
||||
result->ssid, result->rssi, result->channel,
|
||||
result->bssid[0], result->bssid[1], result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5],
|
||||
result->auth_mode);
|
||||
|
||||
// 检查是否是目标 SSID
|
||||
if (!is_target_ssid_found && strcmp((const char*)result->ssid, TARGET_SSID) == 0) {
|
||||
printf("Found target SSID: %s\n", TARGET_SSID);
|
||||
is_target_ssid_found = true;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 执行 Wi-Fi 扫描
|
||||
int wifi_scan() {
|
||||
cyw43_wifi_scan_options_t scan_options = {0};
|
||||
int err = cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, scan_result);
|
||||
if (err != 0) {
|
||||
printf("Failed to start Wi-Fi scan: %d\n", err);
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 连接 Wi-Fi
|
||||
int wifi_connect() {
|
||||
printf("Connecting to Wi-Fi...\n");
|
||||
if (cyw43_arch_wifi_connect_timeout_ms(TARGET_SSID, TARGET_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
|
||||
printf("Wi-Fi connection failed.\n");
|
||||
return -1;
|
||||
} else {
|
||||
printf("Wi-Fi connected successfully.\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// 主程序
|
||||
int main(int argc, char *argv[]) {
|
||||
stdio_init_all();
|
||||
sleep_ms(3000);
|
||||
|
||||
// 初始化 CYW43 模块
|
||||
if (cyw43_arch_init()) {
|
||||
printf("CYW43 initialization failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
// 执行扫描并等待找到目标 SSID
|
||||
while (!is_target_ssid_found) {
|
||||
int scan_result_code = wifi_scan();
|
||||
if (scan_result_code != 0) {
|
||||
cyw43_arch_deinit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
sleep_ms(7000); // 每隔 7 秒重新扫描一次
|
||||
}
|
||||
|
||||
// 目标 Wi-Fi 已找到,连接 Wi-Fi
|
||||
if (wifi_connect() != 0) {
|
||||
cyw43_arch_deinit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 连接成功,执行 TCP 客户端测试
|
||||
while (1) {
|
||||
float ONBOARD_TEMPERATURE = read_onboard_temperature();
|
||||
printf("Onboard temperature %.02f°C %.02f°F\n", ONBOARD_TEMPERATURE, (ONBOARD_TEMPERATURE * 9 / 5 + 32));
|
||||
|
||||
char ONBOARD_TEMPERATURE_TEMP[BUFER] = { 0 };
|
||||
sprintf(ONBOARD_TEMPERATURE_TEMP, "PICO_W CPU temperature %.02f°C %.02f°F\n", ONBOARD_TEMPERATURE, (ONBOARD_TEMPERATURE * 9 / 5 + 32));
|
||||
|
||||
run_tcp_client_test(ONBOARD_TEMPERATURE_TEMP);
|
||||
sleep_ms(3000);
|
||||
}
|
||||
|
||||
cyw43_arch_deinit();
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
stdio_init_all();
|
||||
sleep_ms(1000);
|
||||
@@ -307,14 +408,6 @@ int main()
|
||||
}
|
||||
watchdog_update(); // 喂狗
|
||||
|
||||
/*
|
||||
if (1 == CH4()) {
|
||||
printf("Kitchen danger (fire) alarm detects CH4!!!\n");
|
||||
light_flashing();
|
||||
_HC_12("Kitchen danger (fire) alarm detects CH4!!!\n");
|
||||
}
|
||||
watchdog_update(); // 喂狗
|
||||
*/
|
||||
|
||||
// DS18B20
|
||||
TEMPERATURE = DS18B20();
|
||||
@@ -370,9 +463,9 @@ int main()
|
||||
printf("\r\n");
|
||||
|
||||
watchdog_update(); // 喂狗
|
||||
sleep_ms(3000);
|
||||
sleep_ms(5000);
|
||||
watchdog_update(); // 喂狗
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@
|
||||
#include <cstdio>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/gpio.h"
|
||||
#include "lib/pico-onewire/api/one_wire.h"
|
||||
#include "lib/radio-switch.h"
|
||||
#include "Lib/pico-onewire/api/one_wire.h"
|
||||
#include "Lib/radio-switch.h"
|
||||
|
||||
#include "hardware/clocks.h"
|
||||
#include "hardware/watchdog.h"
|
||||
@@ -17,8 +17,13 @@
|
||||
#include "hardware/adc.h"
|
||||
|
||||
#include "pico/cyw43_arch.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/ip_addr.h" // 包含 IP 地址结构体定义
|
||||
|
||||
#include "hardware/vreg.h"
|
||||
#include "hardware/clocks.h"
|
||||
|
||||
#define BUFER 1024
|
||||
#define DS18B20_PIN 15 // DS18B20 引脚
|
||||
|
||||
251
SOFTWARE/Source/WIFI.c
Normal file
251
SOFTWARE/Source/WIFI.c
Normal file
@@ -0,0 +1,251 @@
|
||||
#include "WIFI.h"
|
||||
|
||||
#if 0
|
||||
static void dump_bytes(const uint8_t *bptr, uint32_t len)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
|
||||
printf("dump_bytes %d", len);
|
||||
for (i = 0; i < len;) {
|
||||
if ((i & 0x0f) == 0) {
|
||||
printf("\n");
|
||||
} else if ((i & 0x07) == 0) {
|
||||
printf(" ");
|
||||
}
|
||||
printf("%02x ", bptr[i++]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#define DUMP_BYTES dump_bytes // 调试用的字节转储函数
|
||||
#else
|
||||
#define DUMP_BYTES(A,B)
|
||||
#endif
|
||||
|
||||
typedef struct TCP_CLIENT_T_ {
|
||||
struct tcp_pcb *tcp_pcb; // TCP控制块指针
|
||||
ip_addr_t remote_addr; // 远程地址
|
||||
uint8_t buffer[BUF_SIZE]; // 接收缓冲区
|
||||
int buffer_len; // 缓冲区当前长度
|
||||
int sent_len; // 已发送的数据长度
|
||||
bool complete; // 是否完成测试
|
||||
int run_count; // 运行次数
|
||||
bool connected; // 是否已连接
|
||||
} TCP_CLIENT_T;
|
||||
|
||||
static err_t tcp_client_close(void *arg)
|
||||
{
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||
err_t err = ERR_OK;
|
||||
if (state->tcp_pcb != NULL) {
|
||||
tcp_arg(state->tcp_pcb, NULL); // 清除回调参数
|
||||
tcp_poll(state->tcp_pcb, NULL, 0); // 清除轮询回调
|
||||
tcp_sent(state->tcp_pcb, NULL); // 清除发送回调
|
||||
tcp_recv(state->tcp_pcb, NULL); // 清除接收回调
|
||||
tcp_err(state->tcp_pcb, NULL); // 清除错误回调
|
||||
err = tcp_close(state->tcp_pcb); // 关闭TCP连接
|
||||
if (err != ERR_OK) {
|
||||
DEBUG_printf("close failed %d, calling abort\n", err); // 如果关闭失败,调用abort
|
||||
tcp_abort(state->tcp_pcb);
|
||||
err = ERR_ABRT;
|
||||
}
|
||||
state->tcp_pcb = NULL; // 清除TCP控制块指针
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
// 操作结果回调
|
||||
static err_t tcp_result(void *arg, int status)
|
||||
{
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||
if (status == 0) {
|
||||
DEBUG_printf("test success\n"); // 测试成功
|
||||
} else {
|
||||
DEBUG_printf("test failed %d\n", status); // 测试失败
|
||||
}
|
||||
state->complete = true; // 标记测试完成
|
||||
return tcp_client_close(arg); // 关闭连接
|
||||
}
|
||||
|
||||
// 轮询回调
|
||||
static err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb)
|
||||
{
|
||||
DEBUG_printf("tcp_client_poll\n");
|
||||
return tcp_result(arg, -1); // 无响应视为错误
|
||||
}
|
||||
|
||||
// 错误处理回调
|
||||
static void tcp_client_err(void *arg, err_t err)
|
||||
{
|
||||
if (err != ERR_ABRT) {
|
||||
DEBUG_printf("tcp_client_err %d\n", err); // 打印错误信息
|
||||
tcp_result(arg, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 连接建立回调
|
||||
static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
|
||||
{
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||
if (err != ERR_OK) {
|
||||
printf("connect failed %d\n", err); // 连接失败
|
||||
return tcp_result(arg, err);
|
||||
}
|
||||
state->connected = true; // 标记已连接
|
||||
DEBUG_printf("连接成功,准备发送消息!!!\n"); // 连接成功,准备发送消息
|
||||
|
||||
// 发送字符串到服务器
|
||||
err = tcp_write(tpcb, state->buffer, state->buffer_len, TCP_WRITE_FLAG_COPY);
|
||||
if (err != ERR_OK) {
|
||||
DEBUG_printf("发送失败: %d\n", err); // 发送失败
|
||||
return tcp_result(arg, -1);
|
||||
}
|
||||
DEBUG_printf("发送数据: %s\n", state->buffer); // 打印发送的消息
|
||||
|
||||
memset(state->buffer, 0, state->buffer_len);
|
||||
state->buffer_len = 0;
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
{
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||
if (!p) {
|
||||
DEBUG_printf("接收数据失败,p 为 NULL\n"); // 添加调试信息
|
||||
return tcp_result(arg, -1);
|
||||
}
|
||||
// this method is callback from lwIP, so cyw43_arch_lwip_begin is not required, however you
|
||||
// can use this method to cause an assertion in debug mode, if this method is called when
|
||||
// cyw43_arch_lwip_begin IS needed
|
||||
cyw43_arch_lwip_check();
|
||||
if (p->tot_len > 0) {
|
||||
DEBUG_printf("recv %d err %d\n", p->tot_len, err);
|
||||
for (struct pbuf * q = p; q != NULL; q = q->next) {
|
||||
DUMP_BYTES(q->payload, q->len);
|
||||
}
|
||||
// Receive the buffer
|
||||
const uint16_t buffer_left = BUF_SIZE - state->buffer_len;
|
||||
state->buffer_len += pbuf_copy_partial(p, state->buffer + state->buffer_len, p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
|
||||
tcp_recved(tpcb, p->tot_len);
|
||||
}
|
||||
|
||||
// 打印当前缓冲区内容
|
||||
printf("接收数据: %s", state->buffer);
|
||||
|
||||
pbuf_free(p);
|
||||
|
||||
memset(state->buffer, 0, state->buffer_len);
|
||||
state->buffer_len = 0;
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// 发送数据回调
|
||||
static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
|
||||
{
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||
DEBUG_printf("tcp_client_sent %u\n", len); // 打印发送的数据长度
|
||||
state->sent_len += len; // 更新已发送的数据长度
|
||||
|
||||
if (state->sent_len >= BUF_SIZE) {
|
||||
state->run_count++; // 增加运行次数
|
||||
if (state->run_count >= TEST_ITERATIONS) {
|
||||
tcp_result(arg, 0); // 达到迭代次数,测试成功
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// 准备接收新的数据缓冲区
|
||||
state->buffer_len = 0;
|
||||
state->sent_len = 0;
|
||||
DEBUG_printf("Waiting for buffer from server\n");
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// 打开TCP连接
|
||||
static bool tcp_client_open(void *arg)
|
||||
{
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T *) arg;
|
||||
DEBUG_printf("Connecting to %s port %u\n", ip4addr_ntoa(&state->remote_addr), TCP_PORT);
|
||||
state->tcp_pcb = tcp_new_ip_type(IP_GET_TYPE(&state->remote_addr)); // 创建TCP控制块
|
||||
if (!state->tcp_pcb) {
|
||||
DEBUG_printf("failed to create pcb\n"); // 创建失败
|
||||
return false;
|
||||
}
|
||||
|
||||
tcp_arg(state->tcp_pcb, state); // 设置回调参数
|
||||
tcp_poll(state->tcp_pcb, tcp_client_poll, POLL_TIME_S * 2); // 设置轮询回调
|
||||
tcp_sent(state->tcp_pcb, tcp_client_sent); // 设置发送回调
|
||||
tcp_recv(state->tcp_pcb, tcp_client_recv); // 设置接收回调
|
||||
tcp_err(state->tcp_pcb, tcp_client_err); // 设置错误回调
|
||||
|
||||
//state->buffer_len = 0; // 初始化缓冲区长度
|
||||
|
||||
// 使用cyw43_arch_lwip_begin/end确保正确锁定
|
||||
cyw43_arch_lwip_begin();
|
||||
err_t err = tcp_connect(state->tcp_pcb, &state->remote_addr, TCP_PORT, tcp_client_connected); // 尝试连接
|
||||
cyw43_arch_lwip_end();
|
||||
|
||||
return err == ERR_OK;
|
||||
}
|
||||
|
||||
// 初始化TCP客户端
|
||||
static TCP_CLIENT_T *tcp_client_init(void)
|
||||
{
|
||||
TCP_CLIENT_T *state = calloc(1, sizeof(TCP_CLIENT_T)); // 分配内存
|
||||
if (!state) {
|
||||
DEBUG_printf("failed to allocate state\n"); // 分配失败
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 如果 TEST_TCP_SERVER_IP 是 IP 地址,直接解析为 IP 地址
|
||||
if (ip4addr_aton(TEST_TCP_SERVER_IP, &state->remote_addr) == 0) {
|
||||
// 如果解析失败,尝试作为域名解析
|
||||
DEBUG_printf("Failed to parse IP address, trying DNS resolution\n");
|
||||
|
||||
err_t err = dns_gethostbyname(TEST_TCP_SERVER_IP, &state->remote_addr, NULL, NULL);
|
||||
if (err != ERR_OK) {
|
||||
DEBUG_printf("DNS resolution failed\n");
|
||||
free(state);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
// 运行TCP客户端测试
|
||||
void run_tcp_client_test(char *s)
|
||||
{
|
||||
TCP_CLIENT_T *state = tcp_client_init(); // 初始化客户端
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
if (!tcp_client_open(state)) { // 打开连接
|
||||
tcp_result(state, -1); // 连接失败
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
strcpy(state->buffer, s);
|
||||
state->buffer_len = strlen(state->buffer);
|
||||
|
||||
while (!state->complete) { // 循环等待测试完成
|
||||
#if PICO_CYW43_ARCH_POLL
|
||||
// 如果使用pico_cyw43_arch_poll,需要定期调用cyw43_arch_poll
|
||||
cyw43_arch_poll();
|
||||
// 可以选择休眠直到有工作需要做
|
||||
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||
#else
|
||||
//cyw43_arch_poll();
|
||||
// 可以选择休眠直到有工作需要做
|
||||
//cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
|
||||
|
||||
// 如果不使用pico_cyw43_arch_poll,可以通过中断在后台处理
|
||||
//sleep_ms(1000); // 示例中的阻塞操作
|
||||
#endif
|
||||
}
|
||||
free(state); // 释放内存
|
||||
}
|
||||
25
SOFTWARE/Source/WIFI.h
Normal file
25
SOFTWARE/Source/WIFI.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef WIFI_H
|
||||
#define WIFI_H
|
||||
|
||||
#include <string.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/dns.h"
|
||||
|
||||
#define TEST_TCP_SERVER_IP "192.168.31.17"
|
||||
#define TCP_PORT 91
|
||||
|
||||
#define BUF_SIZE 2048
|
||||
#define TEST_ITERATIONS 10
|
||||
#define POLL_TIME_S 5
|
||||
#define DEBUG_printf printf
|
||||
|
||||
void run_tcp_client_test(char *s);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user