You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
8.0 KiB
390 lines
8.0 KiB
#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" |
|
#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; |
|
|
|
|
|
// 记录时间 |
|
int recordDate = 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(); |
|
|
|
|
|
|
|
/** |
|
* @description: 判断是否为闰年 |
|
* @param {int} year |
|
* @return {*} |
|
*/ |
|
int isYear(int year){ |
|
return (year%4==0)&&( year %400==0 || year %100!= 0) ? 1:0; |
|
} |
|
|
|
/** |
|
* @description: 获取月份最高天数 |
|
* @param {int} year |
|
* @param {int} month |
|
* @return {*} |
|
*/ |
|
int getDayOfMonth(int year , int month) { |
|
int dayArr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; |
|
return (month == 2 && isYear(year)==1) ? dayArr[month]+1 : dayArr[month] ; |
|
} |
|
|
|
/** |
|
* @description: 报警 |
|
* @param {*} |
|
* @return {*} |
|
*/ |
|
void Warning(){ |
|
BEEP = 1; |
|
delay_ms(100); |
|
BEEP = 0; |
|
} |
|
|
|
/** |
|
* @description: 获取清洁的时间 |
|
* @param {int} data |
|
* @return {*} |
|
*/ |
|
int getCleanTime(int data){ |
|
int max; |
|
data += 3; |
|
if(data < 27) return data; |
|
max = getDayOfMonth(nowTime.year, nowTime.mon); |
|
if(data <= max) return data; |
|
data = max - data; |
|
return data; |
|
} |
|
|
|
/** |
|
* @description: 获取温度的结果 |
|
* @param {*} |
|
* @return {*} |
|
*/ |
|
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 = 0; |
|
if(userInfo.humi_value < 50) LED_NAPPE = 1; |
|
else LED_NAPPE = 0; |
|
}else if(userInfo.temp_value < 15){ // 风扇停, 空调亮灯 |
|
LED_CONDITIONER = 1; |
|
FAN = 0; |
|
if(userInfo.humi_value < 50) LED_NAPPE = 1; |
|
else LED_NAPPE = 0; |
|
}else if(userInfo.temp_value > 20 && userInfo.temp_value <= 28){// 风扇转 |
|
LED_CONDITIONER = 0; |
|
FAN = 1; |
|
if(userInfo.humi_value < 50) LED_NAPPE = 1; |
|
else LED_NAPPE = 0; |
|
}else if( userInfo.temp_value > 28){ |
|
Warning(); |
|
time_min = nowTime.min; |
|
// 设定的洒水时间 3分钟 |
|
time_min += 3; |
|
|
|
if(time_min > 59) time_min = time_min - 59; |
|
flag = 1; |
|
LED_CONDITIONER = 0; |
|
LED_NAPPE = 1; |
|
FAN = 1; |
|
printf("watering\r\n"); |
|
} |
|
}else{ |
|
if(time_min == nowTime.min){ |
|
LED_CONDITIONER = 0; |
|
FAN = 0; |
|
flag = 0; |
|
Warning(); |
|
printf("Stop the water\r\n"); |
|
if(userInfo.humi_value < 50) LED_NAPPE = 1; |
|
else LED_NAPPE = 0; |
|
}else{ |
|
LED_CONDITIONER = 0; |
|
LED_NAPPE = 1; |
|
FAN = 1; |
|
} |
|
} |
|
if(userInfo.temp_pig > 40){ |
|
BEEP = 1; |
|
LED_WATER_GUN = 1; |
|
delay_ms(100); |
|
BEEP = 0; |
|
}else{ |
|
BEEP = 0; |
|
LED_WATER_GUN = 0; |
|
} |
|
return 0; |
|
} |
|
|
|
/** |
|
* @description: 获取湿度的结果 |
|
* @param {*} |
|
* @return {*} |
|
*/ |
|
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){ |
|
getHumiResult(); |
|
getTempResult(); |
|
} |
|
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, "fan") != NULL){ |
|
if(model == 0 || model == 1) printf("Please switch to manual2 mode\r\n"); |
|
else FAN = ~FAN; |
|
} |
|
|
|
if(strstr((const char*)USART_RX_BUF, "feed") != NULL){ |
|
if(model == 0 || model == 1) printf("Please switch to manual2 mode\r\n"); |
|
else MOTOR = ~MOTOR; |
|
} |
|
if(strstr((const char*)USART_RX_BUF, "clean") != NULL){ |
|
if(model == 0 || model == 1) printf("Please switch to manual2 mode\r\n"); |
|
else{ |
|
LED_WATER_GUN = ~LED_WATER_GUN; |
|
MOTOR = LED_WATER_GUN; |
|
} |
|
} |
|
|
|
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, "setPigTemp") != 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.sec == 0 || nowTime.sec == 59) Warning(); |
|
// 投喂一分钟 |
|
if(nowTime.min == 0){ |
|
MOTOR = 1; |
|
} |
|
else { |
|
MOTOR = 0; |
|
} |
|
} |
|
|
|
if(recordDate == nowTime.day && nowTime.hour == 0){ |
|
// 开始结束各报警一次 |
|
if(nowTime.sec == 0 || nowTime.sec == 59) Warning(); |
|
// 清洁一分钟 |
|
if(nowTime.min == 0){ |
|
MOTOR = 1; |
|
LED_WATER_GUN = 1; |
|
} |
|
else{ |
|
MOTOR = 0; |
|
LED_WATER_GUN = 0; |
|
// 记录三天后清洁的日期 |
|
recordDate = getCleanTime(nowTime.day); |
|
} |
|
} |
|
} |
|
|
|
|
|
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(); |
|
MOTOR = 0; |
|
DS1302_Readtime(); |
|
recordDate = getCleanTime(nowTime.day); |
|
} |
|
|
|
/** |
|
* @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(); |
|
BEEP = 1; |
|
delay_ms(1000); |
|
DS1302_SetTime(Settime); |
|
BEEP = 0; |
|
while(1) |
|
{ |
|
Data_Deal(); |
|
Receive_data(); |
|
DS1302_Readtime(); |
|
Crontab(); |
|
delay_ms(200); |
|
} |
|
} |
|
|
|
|
|
|