一.硬件方案
本设计以STM32单片机作为主控芯片,实现对农作物大棚环境的温度、湿度、光强度和土壤湿度进行监测,并根据具体情况进行相应的补水和补光。
主要由STM32F103C8T6单片机最小系统+wifi模块+温湿度模块+1602液晶显示模块+光敏模块+土壤湿度检测模块+继电器模块+水泵电路+补光电路+按键电路+蜂鸣器报警+电源电路组成;如图:
二.设计功能
(1)采用LCD1602显示屏进行显示,实时显示测量到的土壤湿度,空气温湿度,光照值。
(2)可使用Wifi模块实时将采集到的数据传递给手机app。手机app可设置浇水和光照阀值,低于阀值自动浇水和补光;同时也可通过手机app发送指令,直接进行补光和浇水。
(3)当任何一检测值低于阈值,蜂鸣器自动报警。
(4)可通过按键控制浇水补光;
三.设计原理图
原理图主要采用AD软件进行设计,如图:
四.软件设计
(1)程序流程图
(2)主程序源码
int main(void)
{
u16 adcx;
// float temp;
delay_init(); //延时函数初始化
uart_init(115200); //串口初始化为115200
TIM3_Int_Init(4999,5999);//50ms
Adc_Init(); //ADC初始化
Beep_Init();
Relay_Init();
Led_Init();
key_Init();
Lcd_Init(); //初始化lcd屏幕
delay_ms(20);
Lcd_Puts(0,0,"Crop shed design"); //初始化显示
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
while(1)
{
if(ReadDhtFlag==1)//定时读取温湿度标志
{
sprintf(tab,"Temp:%02d DH:%02d",(u16)DHT11.Tem_H,(u16)DHT11.Hum_H);
Lcd_Puts(0,0,(unsigned char *)tab); //初始化显示
}
if(readFlag == 1)// 定时读取标志
{
readFlag = 0;
soil =99 - (unsigned int)adcx*99/4096;// 浓度按照100算*100
sprintf(tab,"soil:%02d",(unsigned int)soil);
Lcd_Puts(0,1,(unsigned char *)tab); //显示
}
if(readDhtFlag == 1)// 定时读取标志
{
readDhtFlag = 0;
ligh_max = adcx*50.1/255;
if(ligh_max>460)
ligh_max=460;
ligh =475-ligh_max;// 浓度按照100算*100
sprintf(tab,"ligh:%03d",(unsigned int)ligh);
Lcd_Puts(8,1,(unsigned char *)tab); //显示
}
if(Key3==0)
{
delay_ms(10);
if(Key3==0)
{
HAS_Flag = !HAS_Flag;
Led=Led_before;
Relay=Relay_before;
while(!Key3);
}
}
if(DHT11.Tem_H>TEMP_Alarm||DHT11.Hum_H>HUMI_Alarm||soil>soilmax_Alarm||ligh>ligh_max_Alarm)
{
Beep=1;
}
else
{
Beep=0;
}
if(HAS_Flag==1)
{
sprintf(tab,"M:%01d",(u8)HAS_Flag);
Lcd_Puts(13,0,(unsigned char *)tab); //初始化显示
if(Key2==0)
{
delay_ms(10);
if(Key2==0)
{
Relay =!Relay;
while(!Key2);
}
}
if(Key1==0)
{
delay_ms(10);
if(Key1==0)
{
Led =!Led;
while(!Key1);
}
}
}
else
{
sprintf(tab,"M:%01d",(u8)HAS_Flag);
Lcd_Puts(13,0,(unsigned char *)tab); //初始化显示
if(soil<soil_Alarm)
{
Relay =1;
}
else
{
Relay =0;
}
if(ligh<ligh_min_Alarm)
{
Led =1;
}
else
{
Led =0;
}
Led_before=Led;
Relay_before=Relay;
}
}
}
资料链接:https://pan.baidu.com/s/15inUdIiOtDP7sORLxI_JEQ?pwd=8ga2
提取码:8ga2