#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 #include #include #include "motor.h" #include "DS1302.h" u8 humi_value,temp_value,temp_pig; u16 smog_value,beam_value; int UpperThreshold = 30; int LowerThreshold = -10; u8 temp_threshold = 10, humi_threshold = 10; u8 cleanFlag = 0x00, feedFlag = 0x00; // 保存当前时间的结构体 struct timing nowTime; // 切换模式 0 自动模式, 1 手动模式 int model = 0; // 用户数据的结构体 struct UserInfo{ u8 humi_value; // 湿度 u8 temp_value; // 温度 float temp_pig; // 猪温度 u8 conditioner; // 空调 u8 nappe; // 水帘 u8 water_gun; // 水枪 }; u8 Settime[6]={0x22,0x04,0x13,0x01,0x17,0x00}; // 设置时间 22年 03月 27日 22:34:00 struct UserInfo userInfo; void SendUserInfo(); void SendTime(); int getTempResult(){ static int flag = 0, time_min = 0; if(flag = 0){ // 温度 15 - 20 风扇待机 if(userInfo.temp_value > 15 && userInfo.temp_value <= 20){ LED_CONDITIONER = 0; FAN = 1; }else if(userInfo.temp_value < 15){ // 风扇停, 空调亮灯 LED_CONDITIONER = 1; FAN = 0; }else if(userInfo.temp_value > 20 && userInfo.temp_value <= 28){// 风扇转 LED_CONDITIONER = 0; FAN = 1; }else if( userInfo.temp_value > 28){ time_min = nowTime.min; time_min += 3; if(time_min > 59) time_min = time_min - 59; flag = 1; LED_CONDITIONER = 0; LED_NAPPE = 1; FAN = 1; } }else{ if(time_min == nowTime.min){ LED_CONDITIONER = 0; LED_NAPPE = 0; FAN = 0; flag = 0; }else{ LED_CONDITIONER = 0; LED_NAPPE = 1; FAN = 1; } } if(userInfo.temp_pig > 40){ BEEP = ~BEEP; LED_WATER_GUN = 1; delay_ms(100); }else{ BEEP = 0; LED_WATER_GUN = 0; } return 0; } int getHumiResult(){ // 湿度 75% - 80% 风扇待机, 不亮 if(userInfo.humi_value > 75 && userInfo.humi_value <= 80){ LED_NAPPE = 0; FAN = 0; }else if(userInfo.humi_value < 50){ // 水帘亮灯 LED_NAPPE = 1; FAN = 0; }else if(userInfo.humi_value > 80){// 风扇转动 LED_NAPPE = 0; FAN = 1; } return 0; } /** * @description: 获取数据 * @param {*} * @return {*} */ void getData() { userInfo.nappe = LED_NAPPE; userInfo.water_gun = LED_WATER_GUN; userInfo.conditioner = LED_CONDITIONER; } /** * @description: 数据处理 * @param {*} * @return {*} */ void Data_Deal() { // 获取数据 if(model == 0){ DHT11_Read_Data(&(userInfo.temp_value),&(userInfo.humi_value)); userInfo.temp_pig = DS18B20_Get_Temp()/10; } if(model != 2){ getTempResult(); getHumiResult(); } getData(); } /** * @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, "model") != NULL){ model++; if(model >= 3) model = 0; if(model == 0) printf("automatic mode\r\n"); else if(model == 1) printf("manual1 mode\r\n"); else if(model == 2) printf("manual2 mode\r\n"); } if(strstr((const char*)USART_RX_BUF, "getInfo") != NULL){ SendUserInfo(); } if(strstr((const char*)USART_RX_BUF, "getTime") != NULL){ SendTime(); } if(strstr((const char*)USART_RX_BUF, "feed") != NULL){ if(model == 0 || model == 1) printf("Please switch to manual2 mode\r\n"); else FAN = ~FAN; } if(strstr((const char*)USART_RX_BUF, "clean") != NULL){ if(model == 0 || model == 1) printf("Please switch to manual2 mode\r\n"); else CLEAN = ~CLEAN; } if(strstr((const char*)USART_RX_BUF, "setTemp") != NULL){ if(model == 0) printf("Please switch to manual1 mode\r\n"); else { userInfo.temp_value = getValue(); printf("set temp_value OK\r\n"); } } if(strstr((const char*)USART_RX_BUF, "setTemp_pig") != NULL){ if(model == 0) printf("Please switch to manual1 mode\r\n"); else { userInfo.temp_pig = getValue(); printf("set temp_pig OK\r\n"); } } if(strstr((const char*)USART_RX_BUF, "setHump") != NULL){ if(model == 0) printf("Please switch to manual1 mode\r\n"); else { userInfo.humi_value = getValue(); printf("set humi_value OK\r\n"); } } memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF)); USART_RX_STA = 0x00; } } /** * @description: 定时任务 * @param {*} * @return {*} */ void Crontab() { // 时间范围早上六点 到 晚上10点 投喂三次 if(nowTime.hour == 6 || nowTime.hour == 14 || nowTime.hour == 22 ){ // 投喂一分钟 if(nowTime.min == 0){ LED_WATER_GUN = 1; CLEAN = 1; } else { LED_WATER_GUN = 0; CLEAN = 0; } } } void Sys_Init(void) { delay_init(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); uart_init(115200); LED_Init(); DS1302_init(Settime); //DS1302_SetTime(Settime); DHT11_Init(); DS18B20_Init(); motor_Init(); } /** * @description: 发送用户数据 * @param {*} * @return {*} */ void SendUserInfo() { printf("temp:%d, humi:%d, temp_pig:%0.2f, conditioner:%d, nappe:%d, water_gun:%d \r\n",userInfo.temp_value, userInfo.humi_value, \ userInfo.temp_pig , userInfo.conditioner,userInfo.nappe,userInfo.water_gun); } /** * @description: 发送时间 * @param {*} * @return {*} */ void SendTime() { printf("20%d-%d-%d %d:%d:%d\r\n", nowTime.year, nowTime.mon, nowTime.day, nowTime.hour, nowTime.min, nowTime.sec); } int main(void) { Sys_Init(); delay_ms(1000); DS1302_SetTime(Settime); BEEP = 0; while(1) { Data_Deal(); Receive_data(); DS1302_Readtime(); delay_ms(200); } }