|
|
|
#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; // 烟雾
|
|
|
|
float PoisonousGas; // 有毒气体
|
|
|
|
int smog_threshold;
|
|
|
|
int PoisonousGas_threshold;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UserInfo userInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description: 报警
|
|
|
|
* @param {*}
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
void Warning(){
|
|
|
|
BEEP = 0;
|
|
|
|
delay_ms(100);
|
|
|
|
BEEP = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = ADC_Get_Data();
|
|
|
|
userInfo.smog = (float)adcx*(100.0/4096);
|
|
|
|
adcx = ADC_Get_Data1();
|
|
|
|
userInfo.PoisonousGas = (float)adcx*(100.0/4096);
|
|
|
|
if(getResult()) Warning();
|
|
|
|
else BEEP = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 C,temp_threshold:%dC,smog:%0.2f%%,smog_threshold:%d%%, PoisonousGas:%0.2f%%, PoisonousGas_threshold:%d%%\r\n", \
|
|
|
|
userInfo.temp_value,userInfo.temp_threshold, userInfo.smog, userInfo.smog_threshold,userInfo.PoisonousGas,userInfo.PoisonousGas_threshold);
|
|
|
|
}
|
|
|
|
if(strstr((const char*)USART_RX_BUF, "setTemp") != NULL){
|
|
|
|
userInfo.temp_threshold = getValue();
|
|
|
|
printf("setTemp OK\r\n");
|
|
|
|
}
|
|
|
|
if(strstr((const char*)USART_RX_BUF, "setSmog") != NULL){
|
|
|
|
userInfo.smog_threshold = getValue();
|
|
|
|
printf("setSmog OK\r\n");
|
|
|
|
}
|
|
|
|
if(strstr((const char*)USART_RX_BUF, "setPoisonousGas") != NULL){
|
|
|
|
userInfo.PoisonousGas_threshold = getValue();
|
|
|
|
printf("setPoisonousGas OK\r\n");
|
|
|
|
}
|
|
|
|
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_MY();
|
|
|
|
|
|
|
|
userInfo.temp_threshold = 28;
|
|
|
|
userInfo.smog_threshold = 28;
|
|
|
|
userInfo.PoisonousGas_threshold = 28;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
Sys_Init();
|
|
|
|
BEEP = 0;
|
|
|
|
delay_ms(1000);
|
|
|
|
BEEP = 1;
|
|
|
|
printf("start\r\n");
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
Data_Deal();
|
|
|
|
Receive_data();
|
|
|
|
delay_ms(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|