功能介绍:
0.本系统采用STC89C52作为单片机
1.系统实时监测水族箱温度及水质,并定时向WIFI串口发送监测信息
2.当水质超过设定阈值时,启动换水继电器
3.按键可设定阈值,可手动换水,喂食,补氧,每次执行5s左右
4.WIFI可发送命令实现换水,喂食,补氧,每次执行5s左右
*W# 换水 *O# 补氧 *F# 喂食
5.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB:
主程序:
#include "main.h"/*******************变量定义*********************/enum _MODE_DF_ dispMode;uchar setIndex = 0; //设置位置int tempBuf = 0;float temperature; //温度uint qualityMax = 50;uint waterQuality;uint waterDelay = 0;uint O2Delay = 0;uint feedDelay = 0;bit dispFlag = 0;bit sendFlag = 0;char dis[32];/********************************************************函数名称:void mian()函数作用:主函数参数说明:********************************************************/void main(){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 (waterQuality > qualityMax) //水质差{waterDelay = 10; //启动换水计时}if (waterDelay > 0){waterDelay--;RELAY_WATER = ON; //启动换水继电器}else{RELAY_WATER = OFF;}if (O2Delay > 0){O2Delay--;RELAY_O2 = ON; //启动补氧继电器}else{RELAY_O2 = OFF;}if (feedDelay > 0){feedDelay--;RELAY_FEED = ON; //启动喂食继电器}else{RELAY_FEED = 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,31\r\n", 17); //发送数据DelayMs(100);sprintf(dis, "Quality: %4dppm Temp:%5.1f'C\r\n", waterQuality, temperature); //串口发送UART_SendStr(dis, 31); //发送数据DelayMs(100);}void DispNormal(void){TR0 = 0;DS18B20_Start();DS18B20_GetTemp(&tempBuf);temperature = 0.0625 * tempBuf;sprintf(dis, "Temp:%5.1f", temperature);LCD_DispStr(0, 0, dis);LCD_DispOneChar(10, 0, 0xdf);LCD_DispOneChar(11, 0, 'C');waterQuality = ReadADC(AIN0_GND);waterQuality = 1000 * ((float)waterQuality / 118);TR0 = 1;sprintf(dis, "Quality: %4dppm", waterQuality);LCD_DispStr(0, 1, dis);}void DispSetWaterQiality(unsigned char setIndex){LCD_DispStr(0, 0, "Set WaterQuality");sprintf(dis, " Max:%3dppm ", qualityMax);LCD_DispStr(0, 1, dis);switch (setIndex){case 1: LCD_SetCursor(10, 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 //串行中断服务程序{static unsigned char i = 0;static unsigned char firstBit = 0;static unsigned char R_buf[3];if (RI)//判断是接收中断产生{RI = 0; //标志位清零if (SBUF == '*'){firstBit = 1; //接收标志成功i = 0;R_buf[1] = 0;R_buf[2] = 0;}if (firstBit == 1){R_buf[i] = SBUF;SBUF = SBUF;i++;if (i == 3){i = 0;if (R_buf[0] == '*' && R_buf[2] == '#'){if (R_buf[1] == 'W') //换水命令{waterDelay = 10;}else if (R_buf[1] == 'O') //补氧命令{O2Delay = 10;}else if (R_buf[1] == 'F') //喂食命令{feedDelay = 10;}}firstBit = 0;}}}}
仿真演示视频:
/video/BV1KF411j7vs/
实物演示视频:
/video/BV1cr4y1t7xj/