本文主要是介绍【单片机毕业设计选题24018】-基于STM32和阿里云的农业大棚系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
系统功能:
系统分为手动和自动模式,上电默认为自动模式,自动模式下系统根据采集到的传感器值
自动控制,温度过低后自动开启加热,湿度过高后自动开启通风,光照过低后自动开启补
光,水位过低后自动开启水泵补水。手动模式下可以手动控制加热/除湿/补光/补水。
主要功能模块原理图:
传感器采集:
系统主要功能模块代码
初始化代码:
/* 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();//初始化ESP8266ESP8266_Init(); /* USER CODE END 2 */
ESP8266接入阿里云代码:
//硬件复位ESP8266HAL_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);//关闭板载LEDHAL_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);//上电不自动连接APprintf("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);//配置WIFIprintf("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 CONFIGprintf("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三个版本),源代码,系统框图,主程序流程图,物料清单(包含使用到的器件手册),功能操作说明等。
资料获取地址
这篇关于【单片机毕业设计选题24018】-基于STM32和阿里云的农业大棚系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!