From 4434e1492d34b0c3303dcdf2341520d598007ef0 Mon Sep 17 00:00:00 2001 From: dgjames <1943357252@qq.com> Date: Sat, 15 Mar 2025 07:18:52 +0800 Subject: [PATCH] =?UTF-8?q?wifi=E8=87=AA=E5=8A=A8=E8=BF=9E=E6=8E=A5=20?= =?UTF-8?q?=E4=B8=8Esensor=E6=94=B9=E5=90=8D=EF=BC=8C=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E5=8D=B1=E9=99=A9=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++- applications/init.c | 10 ++--- applications/my_func.c | 2 +- applications/myproject.c | 90 +++++++++++++++++++++++----------------- applications/sensor.c | 49 +++++++++++++++------- applications/sensor.h | 6 ++- applications/status.h | 1 + 7 files changed, 103 insertions(+), 60 deletions(-) create mode 100644 applications/status.h diff --git a/README.md b/README.md index ffcc479..bfc0859 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,10 @@ 语音助手 TX:PD8 RX:PD9 - +ADC传感器 +PA1 pm25 +PA5 空气质量 +PA6 压力 # 红外遥控贪吃蛇/显示+上传温度等数据 diff --git a/applications/init.c b/applications/init.c index 24d82f2..f76fa11 100644 --- a/applications/init.c +++ b/applications/init.c @@ -63,11 +63,11 @@ int main_init(void) wla_init(); - if (wlan_connected == RT_EOK) - { - last_stop = 1; - mqt_init(); - } + // if (wlan_connected == RT_EOK) + // { + // last_stop = 1; + // mqt_init(); + // } ap3_init(); diff --git a/applications/my_func.c b/applications/my_func.c index 308d9fe..da0c54d 100644 --- a/applications/my_func.c +++ b/applications/my_func.c @@ -10,7 +10,7 @@ //串口 rt_device_t serial; char str[] = "forward\n"; -int wlan_connected=-RT_ERROR; +int wlan_connected=RT_FALSE; extern char tmp[]; void lcd_black(int x, int y) diff --git a/applications/myproject.c b/applications/myproject.c index ab9927a..81ae4ba 100644 --- a/applications/myproject.c +++ b/applications/myproject.c @@ -16,6 +16,7 @@ #include #include "my_func.h" #include "indicator_led.h" +#include "sensor.h" #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 4096 #define THREAD_TIMESLICE 5 @@ -44,8 +45,8 @@ int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]); uint64_t HAL_UptimeMs(void); int HAL_Snprintf(char *str, const int len, const char *fmt, ...); -#define WLAN_CONNECTED 1; -#define WLAN_DISCONNECTED 0; +#define WLAN_NAME "as" +#define WLAN_PASSWORD "07691234" // 定义接受文件内容的缓冲区 char buffer[1026] = {}; @@ -261,41 +262,41 @@ void tmp_payload(void) my_round(20); page_first = 0; } - if (!mqtt_enable) + if (!wlan_connected) { - lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12, 32, "disable"); - if (last_stop) - { - last_stop = 0; - IOT_MQTT_Destroy(&pclient); - rt_thread_delete(MQTT_Thread); - } + lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12, 32, "disconnect"); + // if (last_stop) + // { + // last_stop = 0; + // IOT_MQTT_Destroy(&pclient); + // rt_thread_delete(MQTT_Thread); + // } } else { - lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12, 32, "enable "); - if (mqtt_enable && !last_stop && wlan_connected == RT_EOK) - { - last_stop = 1; - mqt_init(); - } - if (wlan_connected == RT_EOK) - { - lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12 + 32, 32, "connect"); - // if (last_connect_status != RT_EOK) - // { - // mqt_init(); - // } - } - else - { - lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12 + 32, 32, "disconnect"); - // if (last_connect_status == RT_EOK) - // { - // IOT_MQTT_Destroy(&pclient); - // rt_thread_delete(MQTT_Thread); - // } - } + lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12, 32, "connected "); + // if (mqtt_enable && !last_stop && wlan_connected == RT_EOK) + // { + // last_stop = 1; + // mqt_init(); + // } + // if (wlan_connected == RT_EOK) + // { + // lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12 + 32, 32, "connect"); + // // if (last_connect_status != RT_EOK) + // // { + // // mqt_init(); + // // } + // } + // else + // { + // lcd_show_string(240 / 2 - 24 * 2, 240 / 2 + 12 + 32, 32, "disconnect"); + // // if (last_connect_status == RT_EOK) + // // { + // // IOT_MQTT_Destroy(&pclient); + // // rt_thread_delete(MQTT_Thread); + // // } + // } // last_connect_status = wlan_connected; } } @@ -309,7 +310,7 @@ void tmp_payload(void) page_chosen = (page_chosen % PAGE_MAX) + 1; page_first = 1; } - rt_sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f,\"LightLux\":%.2f,\"Psdata\":%d,\"Snakelen\":%d}}", Temp, Humi, brightness, ps_data, snake_len); + rt_sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f,\"LightLux\":%.2f,\"Psdata\":%d,\"Snakelen\":%d,\"Air\":%.2f,\"Pressure\":%.2f,\"PM25\":%.2f}}", Temp, Humi, brightness, ps_data, snake_len,ADC_air,ADC_pressure,ADC_PM25); return; } void test_lcd() @@ -433,13 +434,28 @@ void cdc_entry(void *parameter) void wlan_connect_thread(void *parameter) { (void *)parameter; - + rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD); + rt_wlan_config_autoreconnect(RT_TRUE); + wlan_connected=RT_FALSE; while (1) { - if (wlan_connected != RT_EOK) + if (rt_wlan_is_connected() != RT_TRUE) { - wlan_connected = rt_wlan_connect("as", "07691234"); + if(!wlan_connected) + { + wlan_connected = RT_TRUE; + mqt_init(); + } } + else + { + if(wlan_connected) + { + wlan_connected = RT_FALSE; + IOT_MQTT_Destroy(&pclient); + rt_thread_delete(MQTT_Thread); + } + } rt_thread_mdelay(2000); } } diff --git a/applications/sensor.c b/applications/sensor.c index 6cbfd88..9f575bc 100644 --- a/applications/sensor.c +++ b/applications/sensor.c @@ -1,12 +1,13 @@ #define ADC_DEV_NAME "adc1" /* ADC 设备名称 */ -#define ADC_DEV_CHANNEL 5 /* ADC 通道 */ -#define ADC_DEV_CHANNEL1 6 /* ADC 通道 */ -#define ADC_DEV_CHANNEL2 7 /* ADC 通道 */ +#define ADC_CHANNEL_AIR 5 /* ADC 通道 */ +#define ADC_CHANNEL_PM25 1 /* ADC 通道 */ +#define ADC_CHANNEL_PRESS 6 /* ADC 通道 */ #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/ #define CONVERT_BITS (1 << 12) /* 转换位数为12位 */ #include #include #include +#include "status.h" rt_thread_t Sensor_Thread = RT_NULL; @@ -34,42 +35,42 @@ float ADC_air; float ADC_PM25; float ADC_pressure; -float ADC_Read() +float Air_Read() { rt_adc_device_t adc_dev; /* ADC 设备句柄 */ rt_uint32_t value, vol; /* 查找设备 */ adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME); /* 使能设备 */ - rt_adc_enable(adc_dev, ADC_DEV_CHANNEL); + rt_adc_enable(adc_dev, ADC_CHANNEL_AIR); /* 读取采样值 */ - value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL); + value = rt_adc_read(adc_dev, ADC_CHANNEL_AIR); return value; } -float ADC1_Read() +float PM25_Read() { rt_adc_device_t adc_dev; /* ADC 设备句柄 */ rt_uint32_t value, vol; /* 查找设备 */ adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME); /* 使能设备 */ - rt_adc_enable(adc_dev, ADC_DEV_CHANNEL1); + rt_adc_enable(adc_dev, ADC_CHANNEL_PM25); /* 读取采样值 */ - value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL1); + value = rt_adc_read(adc_dev, ADC_CHANNEL_PM25); return value; } -float ADC2_Read() +float Pressure_Read() { rt_adc_device_t adc_dev; /* ADC 设备句柄 */ rt_uint32_t value, vol; /* 查找设备 */ adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME); /* 使能设备 */ - rt_adc_enable(adc_dev, ADC_DEV_CHANNEL2); + rt_adc_enable(adc_dev, ADC_CHANNEL_PRESS); /* 读取采样值 */ - value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL2); + value = rt_adc_read(adc_dev, ADC_CHANNEL_PRESS); return value; } @@ -82,7 +83,7 @@ float PM25_GetData(void) rt_pin_write(GPIO_PIN, PIN_HIGH);//置1 开启内部LED rt_hw_us_delay(280); - ADCVal = ADC1_Read(); + ADCVal = PM25_Read(); rt_hw_us_delay(25); rt_pin_write(GPIO_PIN, PIN_LOW); //置0 关闭内部LED rt_hw_us_delay(9680); //需要脉宽比0.32ms/10ms的PWM信号驱动传感器中的LED @@ -118,14 +119,32 @@ float Get_PM25_Average_Data(void) temp_val/=PM25_READ_TIMES;//得到平均值 return temp_val;//返回算出的ADC平均值 } + +void warning_range(char* str,float value, float min, float max) +{ + if (value < min) + { + LOG5("%s's value:%f is too low\n",str, value); + danger_status(); + } + else if (value > max) + { + LOG5("%s's value:%f is too high\n",str, value); + danger_status(); + } +} + static void sensor_thread(void *parameter) { while (1) { - ADC_air = ADC_Read(); // 空气质量传感器数值 0-4095 表示从差到优秀 + ADC_air = Air_Read(); // 空气质量传感器数值 0-4095 表示从差到优秀 ADC_PM25 = Get_PM25_Average_Data(); // PM2.5传感器数值0-500 表示从优秀到查差 - ADC_pressure = ADC2_Read(); // 压力传感器数值 0-4095 表示从差到优秀 + ADC_pressure = Pressure_Read(); // 压力传感器数值 0-4095 表示从差到优秀 LOG5("ADC_air:%f,ADC_PM25:%f,ADC_pressure:%f", ADC_air, ADC_PM25, ADC_pressure); + warning_range("air", ADC_air, 0, 4095); + warning_range("PM25", ADC_PM25, 0, 500); + warning_range("pressure", ADC_pressure, 0, 4095); rt_thread_mdelay(1000); } } diff --git a/applications/sensor.h b/applications/sensor.h index 11127b7..fd02f87 100644 --- a/applications/sensor.h +++ b/applications/sensor.h @@ -1 +1,5 @@ -void sensor_init(void); \ No newline at end of file +void sensor_init(void); + +extern float ADC_air; +extern float ADC_PM25; +extern float ADC_pressure; \ No newline at end of file diff --git a/applications/status.h b/applications/status.h new file mode 100644 index 0000000..a48a7d3 --- /dev/null +++ b/applications/status.h @@ -0,0 +1 @@ +void danger_status(void); \ No newline at end of file