wifi自动连接 与sensor改名,加入危险范围

This commit is contained in:
dgjames 2025-03-15 07:18:52 +08:00
parent 66d8e6fdbb
commit 4434e1492d
7 changed files with 103 additions and 60 deletions

View File

@ -2,7 +2,10 @@
语音助手
TX:PD8
RX:PD9
ADC传感器
PA1 pm25
PA5 空气质量
PA6 压力
# 红外遥控贪吃蛇/显示+上传温度等数据

View File

@ -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();

View File

@ -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)

View File

@ -16,6 +16,7 @@
#include <ulog.h>
#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);
}
}
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)
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, "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,12 +434,27 @@ 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);
}

View File

@ -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 <rtthread.h>
#include <rtdevice.h>
#include <drv_gpio.h>
#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);
}
}

View File

@ -1 +1,5 @@
void sensor_init(void);
extern float ADC_air;
extern float ADC_PM25;
extern float ADC_pressure;

1
applications/status.h Normal file
View File

@ -0,0 +1 @@
void danger_status(void);