想要更多项目私wo!!!
一、电路设计
室内环境智能监测系统
主要功能:
1.检测空气温湿度
2.检测光照强度
3.检测烟雾浓度
4.数据显示在手机端和设备OLED屏幕上
5.当空气温度高于设定的阈值时,风扇开启
6.当空气湿度低于设定的阈值时,加湿器开启
7.当光照强度低于设定的阈值时,灯开启
8.当烟雾浓度高于设定的阈值时,风扇开启蜂鸣器开启
9.以上5~8条的阈值可以在手机端控制更改或者设备的按键进行更改
10.设备具有手动和自动两种模式,可以通过手机或设备端更改
11.可以用手机远程控制设备
手动模式下可以控制风扇,加湿器,灯,蜂鸣器的开关
自动模式下就是以上5~8条的阈值自动控制
二、运行结果
三、部分代码
#include "sys.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
#include "ADC.h"
#include "usart3.h"
#include "key.h"
#include "oled.h"
#include "math.h"
#include "dht11.h"
#include "gizwits_product.h"
#include "gizwits_protocol.h"
#include "bh1750.h"
#include "math.h"
int wifi_linking; //wifi连接状态,如果连上,值为1 如果未连接,值为0
int maxmode=0; //手动/自动模式标识符
void key_scanf(void); //WiFi连接控制,按下即可配网
//显示的时间
int display_hour;
int display_minute;
int display_second;
int display_day;
u32 STM32_xx0=0X4E4C4A;
u32 STM32_xx1=0X364B1322;
u32 STM32_xx2=0X132D13 ;
#define FLASH_SAVE_ADDR 0x08010000
u8 buff[30];//OLED显示缓存数组
u8 count;
dataPoint_t currentDataPoint;//云端数据点
u8 display_contrl=0;
//以下几个EXTERN的是定义在机智云产品.c文件里面的传来的参数
extern u16 TEMPYUZHI;
extern u16 HUMYUZHI;
extern u16 M2YUZHI;
extern u16 GZYUZHI;
//从光照的配置文件传来
extern int lux;
//从温湿度的配置文件传来
extern u8 DHT11_Temp,DHT11_Hum; //温湿度
//外设采集来的数值
u16 gz_value; //光照检测值
u16 m2_value; //烟雾检测值
//初始阈值
u16 A_DHT11_Temp=35; //温度阈值
u16 A_DHT11_Hum=10; //湿度阈值
u16 A_gz_value=60; //光照强度阈值
u16 A_m2_value=1000; //烟雾阈值
//此处表示上一次从手机发送来的数值,如果和上一次发来的不同,代表手机端又下发了新的阈值,新的阈值会生效,lastXXXXXX会变成新的
u16 lastTEMPYUZHI;
u16 lastHUMYUZHI;
u16 lastM2YUZHI;
u16 lastGZYUZHI;
//当前数值超标值为1,不超标值为0
int flag_temp;
int flag_mq2;
//风扇,蜂鸣器的工作标志位
int flag;
int flag_2;
int times;
//协议初始化
void Gizwits_Init(void)
{
TIM3_Int_Init(9,7199);//1MS系统定时
usart3_init(9600);//WIFI初始化
memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));//设备状态结构体初始化
gizwitsInit();//缓冲区初始化
}
//数据上传
void userHandle(void)
{
currentDataPoint.valuetemp = DHT11_Temp ;
currentDataPoint.valuehumi = DHT11_Hum;
currentDataPoint.valueLight = gz_value;
currentDataPoint.valueMQ2 = m2_value;
}
void Get_Data(u16 count); //获取传感器数据
void WIFI_Contection(u8 key);//WiFi连接控制
void Canshu_Change(u8 key); //系统参数调节
void Mode_Change(u8 key); //模式切换
int main(void)
{
u8 t=0;
int key_value; //按键值
uart_init(115200); //串口初始化为115200
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
Adc1_Channe_Init(); //ADC通道初始化
KEY_Init(); //按键引脚初始化
Gizwits_Init(); //机智云初始化
while(DHT11_Init()) //DHT11模块初始化
{
delay_ms(600);
}; //等待DHT11的初始化
LED_Init(); //LED初始化
bh1750_iic_init(); //光照传感器初始化
RELAY_Init(); //继电器初始化
BEEP=0;
delay_ms(300);
BEEP=1;//全部都初始化完成,蜂鸣器响一下
OLED_Init(); //OLED初始化
OLED_Clear(); //OLED清屏
gizwitsSetMode(WIFI_AIRLINK_MODE); //Air-link模式接入
while(1)
{
if(wifi_linking)
{
times++;
if(times>=2)
{
times=0;
gizwitsGetNTP();//获取网络时间
delay_ms(100);
}
}
Get_Data(0);
if(TEMPYUZHI!=lastTEMPYUZHI) //阈值的获取,如果和上次一样,就代表没变化,如果变化了,新收到的阈值会直接生效
{
A_DHT11_Temp=TEMPYUZHI;
lastTEMPYUZHI=TEMPYUZHI;
}
if(HUMYUZHI!=lastHUMYUZHI)
{
A_DHT11_Hum=HUMYUZHI;
lastHUMYUZHI=HUMYUZHI;
}
if(M2YUZHI!=lastM2YUZHI)
{
A_m2_value=M2YUZHI;
lastM2YUZHI=M2YUZHI;
}
if(GZYUZHI!=lastGZYUZHI)
{
A_gz_value=GZYUZHI;
lastGZYUZHI=GZYUZHI;
}
if(maxmode==0)//如果优先级标志位为0 那么就是自动模式
{
//跑马灯
LED1=~LED1;
delay_ms(100);
//如果当前环境太黑 开灯
if(gz_value<=A_gz_value)
{
LED0=1;
}else
if(gz_value>A_gz_value)
{
LED0=0;
}
//如果当前环境温度太高或者MQ2 MQ135 MQ7数值高于阈值 开风扇 因为是同一个风扇,所以设计了这种方法
if(m2_value<A_m2_value)
{
flag_mq2=0;
}
if(m2_value>=A_m2_value)
{
flag_mq2=1;
}
if(DHT11_Temp<A_DHT11_Temp)
{
flag_temp=0;
}
if(DHT11_Temp>=A_DHT11_Temp)
{
flag_temp=1;
}
flag=flag_temp+flag_mq2;
if(flag>=1)
{
FAN=FAN_ON;
}
if(flag==0)
{
FAN=FAN_OFF;
}
//如果当前环境三个监测的传感器检测到的数值过高 蜂鸣器会不停的警报至其含量低于设定的阈值
flag_2=flag_mq2;
if(flag_2>=1)
{
BEEP=BEEP_ON;
}
if(flag_2==0)
{
BEEP=BEEP_OFF;
}
//如果当前环境空气湿度低于设置的阈值,开启加湿器
if(DHT11_Hum<A_DHT11_Hum)
{
HUMIPLUS=HUMIPLUS_ON;
}
if(DHT11_Hum>=A_DHT11_Hum)
{
HUMIPLUS=HUMIPLUS_OFF;
}
}
key_value = KEY_Scan(0);
if(key_value==2)
{
display_contrl++;
OLED_Clear(); //OLED清屏
}
if(display_contrl%2==0&&t>=10)
{
OLED_ShowCHinese(0,0,0);
OLED_ShowCHinese(16,0,2);
sprintf((char*)buff,":%2d",DHT11_Temp);
OLED_ShowString(32,0,buff,16); //显示温度
OLED_ShowCHinese(64,0,21);
OLED_ShowCHinese(0,2,1);
OLED_ShowCHinese(16,2,2);
sprintf((char*)buff,":%2d ",DHT11_Hum);
OLED_ShowString(32,2,buff,16);//显示湿度
OLED_ShowCHinese(64,2,22);
OLED_ShowCHinese(0,4,5);
OLED_ShowCHinese(16,4,6);
OLED_ShowCHinese(32,4,7);
OLED_ShowCHinese(48,4,8);
sprintf((char*)buff,":%4dLx",gz_value);
OLED_ShowString(64,4,buff,16);//显示光照强度
OLED_ShowCHinese(0,6,13);
OLED_ShowCHinese(16,6,14);
OLED_ShowCHinese(32,6,15);
OLED_ShowCHinese(48,6,16);
sprintf((char*)buff,":%4dppm",m2_value);
OLED_ShowString(64,6,buff,16);//显示空气质量
}
else if(display_contrl%2==1&&t>=10)
{
OLED_ShowCHinese(0,0,0);
OLED_ShowCHinese(16,0,2);
sprintf((char*)buff,":%2d",DHT11_Temp);
OLED_ShowString(32,0,buff,16); //显示温度
OLED_ShowCHinese(64,0,21);
OLED_ShowCHinese(0,2,1);
OLED_ShowCHinese(16,2,2);
sprintf((char*)buff,":%2d ",DHT11_Hum);
OLED_ShowString(32,2,buff,16);//显示湿度
OLED_ShowCHinese(64,2,22);
OLED_ShowCHinese(0,4,5);
OLED_ShowCHinese(16,4,6);
OLED_ShowCHinese(32,4,7);
OLED_ShowCHinese(48,4,8);
sprintf((char*)buff,":%4dLx",gz_value);
OLED_ShowString(64,4,buff,16);//显示光照强度
OLED_ShowCHinese(0,6,13);
OLED_ShowCHinese(16,6,14);
OLED_ShowCHinese(32,6,15);
OLED_ShowCHinese(48,6,16);
sprintf((char*)buff,":%4dppm",m2_value);
OLED_ShowString(64,6,buff,16);//显示空气质量
}
userHandle(); //用户数据采集
key_scanf();
gizwitsHandle((dataPoint_t *)¤tDataPoint);//机智云协议处理
Canshu_Change(key_value);
t++;
delay_ms(100);
}
}
void key_scanf(void) //控制为自动
{
if(KEY4==RESET)
{
delay_ms(10);
if(KEY4==RESET)
{
maxmode=5;
LED0=LED0_OFF;
LED1=LED1_OFF;
BEEP=BEEP_OFF;
FAN=FAN_OFF;
HUMIPLUS=HUMIPLUS_OFF;
}
while(!KEY4);//松手检测
}
}
void Canshu_Change(u8 key)
{
u8 obj=0;
if(key==1)
{
OLED_Clear();
while(1)
{
key = KEY_Scan(0);
if(key==1)
{
obj++;
if(obj>=5)
{
obj=0;
}
}
sprintf((char *)buff," M_CHANGE");
OLED_ShowString(30,0,buff,12);
sprintf((char *)buff,"M_Temp :%4d",A_DHT11_Temp);
OLED_ShowString(8,1,buff,12);
sprintf((char *)buff,"M_Humi :%4d",A_DHT11_Hum);
OLED_ShowString(8,2,buff,12);
sprintf((char *)buff,"M_Light :%4d",A_gz_value);
OLED_ShowString(8,3,buff,12);
sprintf((char *)buff,"M_MQ2 :%4d",A_m2_value);
OLED_ShowString(8,4,buff,12);
sprintf((char *)buff,"M_mode :%4d",maxmode);
OLED_ShowString(8,5,buff,12);
if(obj==0)
{
OLED_ShowString(0,0," ",12);
OLED_ShowString(0,1,"*",12);
OLED_ShowString(0,2," ",12);
OLED_ShowString(0,3," ",12);
OLED_ShowString(0,4," ",12);
OLED_ShowString(0,5," ",12);
OLED_ShowString(0,6," ",12);
OLED_ShowString(0,7," ",12);
}
if(obj==1)
{
OLED_ShowString(0,0," ",12);
OLED_ShowString(0,1," ",12);
OLED_ShowString(0,2,"*",12);
OLED_ShowString(0,3," ",12);
OLED_ShowString(0,4," ",12);
OLED_ShowString(0,5," ",12);
OLED_ShowString(0,6," ",12);
OLED_ShowString(0,7," ",12);
}
if(obj==2)
{
OLED_ShowString(0,0," ",12);
OLED_ShowString(0,1," ",12);
OLED_ShowString(0,2," ",12);
OLED_ShowString(0,3,"*",12);
OLED_ShowString(0,4," ",12);
OLED_ShowString(0,5," ",12);
OLED_ShowString(0,6," ",12);
OLED_ShowString(0,7," ",12);
}
if(obj==3)
{
OLED_ShowString(0,0," ",12);
OLED_ShowString(0,1," ",12);
OLED_ShowString(0,2," ",12);
OLED_ShowString(0,3," ",12);
OLED_ShowString(0,4,"*",12);
OLED_ShowString(0,5," ",12);
OLED_ShowString(0,6," ",12);
OLED_ShowString(0,7," ",12);
}
if(obj==4)
{
OLED_ShowString(0,0," ",12);
OLED_ShowString(0,1," ",12);
OLED_ShowString(0,2," ",12);
OLED_ShowString(0,3," ",12);
OLED_ShowString(0,4," ",12);
OLED_ShowString(0,5,"*",12);
OLED_ShowString(0,6," ",12);
OLED_ShowString(0,7," ",12);
}
if(obj==0)
{
if(key==3)
{
A_DHT11_Temp+=1;
}
if(key==4)
{
A_DHT11_Temp-=1;
}
}
if(obj==1)
{
if(key==3)
{
A_DHT11_Hum+=1;
}
if(key==4)
{
A_DHT11_Hum-=1;
}
}
if(obj==2)
{
if(key==3)
{
A_gz_value+=2;
}
if(key==4)
{
A_gz_value-=2;
}
}
if(obj==3)
{
if(key==3)
{
A_m2_value+=5;
}
if(key==4)
{
A_m2_value-=5;
}
}
if(obj==4)
{
if(key==3)
{
maxmode=5;
}
if(key==4)
{
maxmode=0;
}
}
if(key==2)
{
OLED_Clear();
break;
}
}
}
}
void Get_Data(u16 count)//获取传感器数据
{
DHT11_Read_Data(&DHT11_Temp,&DHT11_Hum);
Conversion();/// bh1750 光照模块数据采集
gz_value = lux/10; //灯光;
m2_value = get_Adc_Value(0x06)/10;
}