系统功能:
此设计采用STM32单片机采集环境温湿度,烟雾浓度和一氧化碳浓度显示在OLED上,并将这些信息上报至阿里云平台。
1. 上电连接手机热点后自动连接阿里云,可通过阿里云平台收到系统上报的温湿度,烟雾
浓度,一氧化碳数据以及系统状态信息,并可以通过阿里云下发风扇开关命令。
2. 硬件系统包含四个机械按键,第一个按键为系统显示界面切换,第二个和第三个按键
为当前显示界面的设定阈值增减,第四个按键为手动开启/关闭风扇。
3. 系统有三个显示界面,第一个界面显示温湿度/继电器状态/系统状态,第二个界面
显示烟雾传感器设定阈值及状态,第三个界面显示一氧化碳传感器设定阈值及状态。系统功能框图:
主要功能模块原理图
系统主要功能模块代码
初始化代码:
/* 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_USART2_UART_Init();MX_ADC1_Init();/* USER CODE BEGIN 2 *///uart2打开接收中断HAL_UART_Receive_IT(&huart2, (uint8_t*)&uart2RecBuff, 1);InitOled();InitOledDisp();ResetDHT11();//初始化ESP8266ESP8266_Init(); HAL_Delay(100);OledFill(0x00); //全屏熄灭
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;//获取烟雾传感器值getMQ2Val = ReadADCChannel(ADC_CHANNEL_4);//获取一氧化碳传感器值getMQ7Val = ReadADCChannel(ADC_CHANNEL_5);//烟雾和一氧化碳超标后需打开风扇,自动开风扇if((getMQ2Val>setMQ2Val)||(getMQ7Val>setMQ7Val)){relayState = true; RELAY_ON();}else{relayState = false;}//四个传感器值中任意一个超标都报警if((getMQ2Val>setMQ2Val)||(getMQ7Val>setMQ7Val)||(getTempVal>setTempVal)||(getHumiVal>setHumiVal)){sysAlarm = true;BUZZER_ON();}else{sysAlarm = false;BUZZER_OFF();}}
系统资料
此设计提供的资料包含原理图PCB(包含PDF,AD,立创EDA三个版本),源代码,系统框图,主程序流程图,物料清单(包含使用到的器件手册),功能操作说明等。