系统功能:
系统分为手动和自动模式,上电默认为自动模式,自动模式下系统根据采集到的传感器值
自动控制,温度过低后自动开启加热,湿度过高后自动开启通风,光照过低后自动开启补
光,水位过低后自动开启水泵补水。手动模式下可以手动控制加热/除湿/补光/补水。
主要功能模块原理图:
传感器采集:
系统主要功能模块代码
初始化代码:
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
InitVar();
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
//在HAL库初始化串口1后调用
HAL_Delay(100);
SetUart1RxToInput();
HAL_Delay(50);
//uart1打开接收中断
HAL_UART_Receive_IT(&huart1, (uint8_t*)&uart1RecBuff, 1);
//uart2打开接收中断
HAL_UART_Receive_IT(&huart2, (uint8_t*)&uart2RecBuff, 1);
InitOled();
InitOledDisp();
ResetDHT11();
//初始化ESP8266
ESP8266_Init();
/* USER CODE END 2 */
ESP8266接入阿里云代码:
//硬件复位ESP8266
HAL_GPIO_WritePin(ESP8266RST_GPIO_Port, ESP8266RST_Pin, GPIO_PIN_RESET);
HAL_Delay(250);
HAL_GPIO_WritePin(ESP8266RST_GPIO_Port, ESP8266RST_Pin, GPIO_PIN_SET);
HAL_Delay(500);
//关闭板载LED
HAL_GPIO_WritePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin, GPIO_PIN_SET);
ESP8266_Clear();
//测试AT指令
printf("AT Testing\r\n");
while(ESP8266_SendCmd("AT\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//设置为Station
printf("set CWMODE\r\n");
while(ESP8266_SendCmd("AT+CWMODE=1\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//上电不自动连接AP
printf("set CWAUTOCONN\r\n");
while(ESP8266_SendCmd("AT+CWAUTOCONN=0\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//关闭回显
printf("set ATE0\r\n");
while(ESP8266_SendCmd("ATE0\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//配置WIFI
printf("set Wifi\r\n");
while(ESP8266_SendCmd("AT+CWJAP=\""WIFI_SSID"\",\""WIFI_PASSWD"\"\r\n", "OK", 500))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//MQTT USER CONFIG
printf("mqtt config\r\n");
while(ESP8266_SendCmd("AT+MQTTUSERCFG=0,1,\""MQTT_CLIENT_ID"\",\""MQTT_USER_NAME"\",\""MQTT_PASSWD"\",0,0,\"\"\r\n", "OK", 500))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(1000);
//MQTT连接服务器
printf("mqtt broker\r\n");
while(ESP8266_SendCmd("AT+MQTTCONN=0,\""BROKER_ASDDRESS"\",1883,0\r\n", "OK", 500))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
读传感器数据:
//读传感器值,AD值
void ReadSensorVal(void)
{
getTempVal = DHT11_Data.temp_int;
getHumiVal = DHT11_Data.humi_int;
getLightVal = ReadADCChannel(ADC_CHANNEL_0);
getWaterVal = ReadADCChannel(ADC_CHANNEL_4);
//系统模式为自动模式
if(sysMode==AUTO_MODE)
{
//获取到的温度小于设定温度则开启加热
if(getTempVal<setTempVal)
{
heatState = true;
HEAT_ON();
}
else
{
heatState = false;
HEAT_OFF();
}
//获取到的湿度大于设定湿度则开启通风
if(getHumiVal>setHumiVal)
{
fanState = true;
FAN_ON();
}
else
{
fanState = false;
FAN_OFF();
}
//获取到的光照值大于设定光照值则开启补光
if(getLightVal>setLightVal)
{
lightState = true;
LED_ON();
}
else
{
lightState = false;
LED_OFF();
}
//获取到的水位值大于设定水位值则开启水泵
if(getWaterVal>setWaterVal)
{
pumpState = true;
PUMP_ON();
}
else
{
pumpState = false;
PUMP_OFF();
}
}
}
此设计提供的资料包含原理图PCB(包含PDF,AD,立创EDA三个版本),源代码,系统框图,主程序流程图,物料清单(包含使用到的器件手册),功能操作说明等。
资料获取地址