室内环境检测系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

139 lines
2.7 KiB

#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "wifi.h"
#include "dht11.h"
#include "lcd1602.h"
#include "adc.h"
#include "timer.h"
#include "string.h"
#include "ds18b20.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "motor.h"
// 用户数据的结构体
struct UserInfo{
u8 temp_value; // 温度
u8 temp_threshold;
float smog; // 烟雾
int PoisonousGas; // 有毒气体
float smog_threshold;
int PoisonousGas_threshold;
};
struct UserInfo userInfo;
/**
* @description: 报警
* @param {*}
* @return {*}
*/
void Warning(){
BEEP = 1;
delay_ms(100);
BEEP = 0;
}
int getResult(){
int result = 0;
if(userInfo.temp_value > userInfo.temp_threshold) result = 1;
if(userInfo.smog > userInfo.smog_threshold) result = 1;
if(userInfo.PoisonousGas > userInfo.PoisonousGas_threshold) result = 1;
return result;
}
/**
* @description: 数据处理
* @param {*}
* @return {*}
*/
void Data_Deal()
{
u16 adcx;
userInfo.temp_value = DS18B20_Get_Temp() / 10;
adcx = Get_Adc_Average(ADC_Channel_1,10);
userInfo.smog = (float)adcx*(3.3/4096);
adcx = Get_Adc_Average(ADC_Channel_0,10);
userInfo.PoisonousGas = (float)adcx*(3.3/4096);
if(getResult()) Warning();
else BEEP = 0;
}
/**
* @description: 解析数据
* @param {*}
* @return {*}
*/
int getValue()
{
int target1 = 0, target2 = 0,i = 0;
int result = 0;
target1 = strstr((const char*)USART_RX_BUF, "(") - USART_RX_BUF;
target2 = strstr((const char*)USART_RX_BUF, ")") - USART_RX_BUF;
result = 0;
for(i = target1 + 1; i < target2; i++){
result = result * 10 + (*(USART_RX_BUF + i) - 0x30);
}
return result;
}
void Receive_data(void)
{
// 接受到数据
if(USART_RX_STA & 0x8000){
// 模式切换
if(strstr((const char*)USART_RX_BUF, "getInfo") != NULL){
printf("temp:%d, temp_threshold:%d,smog:%0.2f, PoisonousGas%0.2f, smog_threshold:%d, PoisonousGas_threshold:%d", \
userInfo.temp_value,userInfo.temp_threshold, userInfo.smog, userInfo.PoisonousGas, userInfo.smog_threshold, userInfo.PoisonousGas_threshold);
}
if(strstr((const char*)USART_RX_BUF, "setTemp") != NULL){
userInfo.temp_threshold = getValue();
}
if(strstr((const char*)USART_RX_BUF, "setSmog") != NULL){
userInfo.smog_threshold = getValue();
}
if(strstr((const char*)USART_RX_BUF, "setPoisonousGas") != NULL){
userInfo.PoisonousGas_threshold = getValue();
}
memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF));
USART_RX_STA = 0x00;
}
}
void Sys_Init(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
DS18B20_Init();
Adc_Init();
}
int main(void)
{
Sys_Init();
BEEP = 1;
delay_ms(1000);
BEEP = 0;
while(1)
{
Data_Deal();
Receive_data();
delay_ms(200);
}
}