功能介绍:
0.本系统采用STC89C52作为单片机
1.系统实时监测当前的有害气体状态和温湿度,并定时向WIFI串口发送监测信息
2.当温湿度超过阈值或有害气体浓度过高时,控制对应继电器,同时蜂鸣器报警
3.按键可设定阈值
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB:
主程序:
#include "main.h"/*******************变量定义*********************/
enum _MODE_DF_ dispMode;
bit alarmFlag = 0; //报警标志
bit alarmFlag1 = 0; //报警标志1
bit alarmFlag2 = 0; //报警标志2
uchar setIndex = 0; //设置位置
uint humidity; //湿度
uint temperature; //温度uint hmMin= 35;
uint hmMax= 70;uint tempMin = 10;
uint tempMax = 40;bit dispFlag = 0;
bit sendFlag = 0;char dis[32];/********************************************************
函数名称:void mian()
函数作用:主函数
参数说明:
********************************************************/
void main()
{BUZZER = OFF;RELAY_HEAT = OFF;RELAY_COOL = OFF;RELAY_HM = OFF;RELAY_DHM = OFF;RELAY_FAN = OFF;Timer0_Init(); //初始化定时器0UART_Init(); //初始化串口LCD_Init(); //初始化液晶DelayMs(200); //延时有助于稳定LCD_DispStr(4, 0, "Welcome!");UART_SendStr("AT+CIPMUX=1\r\n", 13); //打开多连接DelayS(1);UART_SendStr("AT+CIPSERVER=1,8080\r\n", 21); //建立服务 端口号为8080DelayS(1);LCD_Clear(); //清屏while (1) //死循环{if (dispFlag == 1){dispFlag = 0;if (dispMode == NORMAL){DispNormal();}}if (sendFlag == 1){sendFlag = 0;SendData();}if (PIN_MQ2 == 0) //检测到有害气体浓度较高{DelayMs(200);if (PIN_MQ2 == 0) //确认检测到有毒气体{RELAY_FAN = ON; //打开排气扇alarmFlag = 1;}}else{RELAY_FAN = OFF; //关闭排气扇alarmFlag = 0;}if (humidity > hmMax){RELAY_DHM = ON; //打开除湿继电器RELAY_HM = OFF; //关闭加湿继电器alarmFlag1 = 1;}else if (humidity < hmMin){RELAY_DHM = OFF; //关闭除湿继电器RELAY_HM = ON; //打开加湿继电器alarmFlag1 = 1;}else{RELAY_DHM = OFF; //关闭除湿继电器RELAY_HM = OFF; //关闭加湿继电器alarmFlag1 = 0;}if (temperature > tempMax){RELAY_COOL = ON; //打开降温继电器RELAY_HEAT = OFF; //关闭加热继电器alarmFlag2 = 1;}else if (temperature < tempMin){RELAY_COOL = OFF; //关闭降温继电器RELAY_HEAT = ON; //打开加热继电器alarmFlag2 = 1;}else{RELAY_COOL = OFF; //关闭降温继电器RELAY_HEAT = OFF; //关闭加热继电器alarmFlag2 = 0;}if (alarmFlag == 1 || alarmFlag1 == 1 || alarmFlag2 == 1){BUZZER = ON;}else{BUZZER = OFF;}KeyProcess();}
}/*------------------------------------------------定时器初始化子程序
------------------------------------------------*/
void Timer0_Init(void)
{TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响TH0 = (65536 - 9216) / 256; //重新赋值 10msTL0 = (65536 - 9216) % 256;EA = 1; //总中断打开ET0 = 1; //定时器中断打开TR0 = 1; //定时器开关打开
}
/*------------------------------------------------定时器中断子程序
------------------------------------------------*/
void Timer0_Interrupt(void) interrupt 1
{static unsigned int time10ms = 0;TH0 = (65536 - 9216) / 256; //重新赋值 10msTL0 = (65536 - 9216) % 256;time10ms++;if (time10ms % 50 == 0){dispFlag = 1; //显示标志}if (time10ms > 500){time10ms = 0;sendFlag = 1; //WIFI发送信息标志}}void SendData(void)
{UART_SendStr("AT+CIPSEND=0,18\r\n", 17); //发送数据DelayMs(150);sprintf(dis, "Hm:%2d%% Temp:%3d'C\r\n", humidity, temperature); //串口发送UART_SendStr(dis, 18); //发送数据DelayMs(200);DelayMs(200);UART_SendStr("AT+CIPSEND=0,18\r\n", 17); //发送数据DelayMs(150);if (PIN_MQ2 == 0){UART_SendStr("Gas: Warning!!! \r\n", 18);}else{UART_SendStr("Gas: Normal \r\n", 18);}
}void DispNormal(void)
{DHT11_0_ReadData();//读取温湿度temperature = U8T_data_H;humidity = U8RH_data_H; sprintf(dis, "Hm:%2d%% Temp:%2d", humidity, temperature);LCD_DispStr(0, 0, dis);LCD_DispOneChar(14, 0, 0xdf);LCD_DispOneChar(15, 0, 'C');if (PIN_MQ2 == 0){LCD_DispStr(0, 1, "Gas: Warning!!! ");}else{LCD_DispStr(0, 1, "Gas: Normal ");}}void DispSetHm(unsigned char setIndex)
{LCD_DispStr(0, 0, " Set Humidity ");sprintf(dis, " H:%2d%% L:%2d%% ", hmMax, hmMin);LCD_DispStr(0, 1, dis);switch (setIndex){case 1: LCD_SetCursor(6, 1, 1); break;case 2: LCD_SetCursor(12, 1, 1); break;default:;}
}void DispSetTemp(unsigned char setIndex)
{LCD_DispStr(0, 0, "Set Temperature ");sprintf(dis, " H:%3d L:%3d ", tempMax, tempMin);LCD_DispStr(0, 1, dis);switch (setIndex){case 1: LCD_SetCursor(7, 1, 1); break;case 2: LCD_SetCursor(13, 1, 1); break;default:;}
}void UART_Init(void)
{SCON = 0x50;TH2 = 0xFF;TL2 = 0xFD;RCAP2H = 0xFF; //(65536-(FOSC/32/BAUD)) BAUD = 115200 FOSC = 11059200RCAP2L = 0xFD;/*****************/TCLK = 1;RCLK = 1;C_T2 = 0;EXEN2 = 0;/*****************/TR2 = 1;ES = 1; //打开串口中断EA = 1; //打开总中断}void UART_SendByte(unsigned char dat) //串口发送单字节数据
{unsigned char time_out;time_out = 0;SBUF = dat; //将数据放入SBUF中while ((!TI) && (time_out < 100)) //检测是否发送出去{time_out++;DelayUs10x(2);} //未发送出去 进行短暂延时TI = 0; //清除ti标志
}void UART_SendStr(unsigned char *s, unsigned char length) //发送定长度字符串
{unsigned char num;num = 0x00;while (num < length) //发送长度对比{UART_SendByte(*s); //放松单字节数据s++; //指针++num++; //下一个++}
}void UART_Interrupt(void) interrupt 4 //串行中断服务程序
{if (RI)//判断是接收中断产生{RI = 0; //标志位清零}
}
仿真演示视频:
https://www.bilibili.com/video/BV1X5411Q7cx/
实物演示视频:
https://www.bilibili.com/video/BV1zr4y147ZA/