OJAppS  Vol.11 No.9 , September 2021
Hardware Implementation of STM32 Microcontroller-Based Indoor Environment Monitoring System
Abstract: Microcontroller is widely used in the intelligent life of modern society. Intelligent development based on Microcontroller to solve the actual needs of people’s life, work, study and other fields is the core of Microcontroller application. Therefore, it is a task for researchers to understand the structure and performance of microcontroller, develop software, and be familiar with the method and process of intelligent development based on microcontroller. And with that in mind, this paper designs and produces a physical hardware system for indoor environment detection based on STM32 microcontroller. The system can detect the light intensity, temperature and humidity, and CO gas concentration in the indoor environment; and the data is integrated and processed by the STM32 microcontroller to display the current parameter values of each quantity in the indoor environment on a 3.5-inch resistive screen; at the same time, the PC can also log in to the OneNET cloud platform through the web page, and display the light intensity, temperature and humidity, and CO gas concentration values in the indoor environment in real time in the device created by OneNET for real-time viewing. The system can also display the light intensity, temperature and humidity, and CO gas concentration values in the indoor environment in real time. The hardware system has been tested and tested to achieve its function.

1. Introduction

The intensity of light, temperature and humidity, and CO gas concentration in indoor environment become several important elements that affect human physical and mental health, and their changes will also counteract human mood and emotion. Especially, high CO gas concentration in indoor air can seriously affect human life. There are many ways to make the real-time monitoring system for indoor environment, but the function of display and alarm of the monitoring system is relatively single. Therefore, it is a key problem to solve in the design and production to realize multiple display and alarm functions of real-time indoor environment detection information based on single chip microcomputer, especially OneNET platform display and alarm or mobile phone display and alarm. This is also to provide the basis for intelligent management of living quarters. This designed indoor detection system can effectively detect the current indoor environment, and through real-time understanding of the indoor environment, it can prevent the occurrence of a series of situations that are harmful to human safety, such as excessive indoor temperature and humidity, CO gas poisoning, etc., and provide OneNET platform display, instrument display and other information display. With the development of modern technology and the improvement of people’s living standards, home intelligence is becoming more and more common [1], and in the modern era of Internet of everything [2], the application of indoor environment intelligent detection system will enter every household.

2. Hardware Design Implementation

2.1. STM32 Indoor Detection System Design Ideas

Based on STM32F103 series microcontroller development board for indoor detection system design ideas.

1) The data of indoor environment temperature and humidity collected by temperature and humidity sensor module in real time is transmitted to STM32 microcontroller, integrated and processed to display the current environment temperature and humidity on 3.5 inch resistive screen [3]; and the microcontroller is connected to OneNET platform through WIFI module ESP8266, the measured temperature and humidity data can be uploaded to PC or cell phone in real time to understand the environment in real time [4]; if the collected indoor temperature is too high and exceeds the set threshold value, the buzzer will alarm.

2) The data of light intensity in the indoor environment is collected in real time with the photoresistor light-sensing module and transmitted to the microcontroller. After the data is processed by the microcontroller, it is converted into light intensity figures and transmitted to the OneNET cloud platform through the ESP8266 module, and the light intensity in the current environment can be viewed remotely in real time in the device established by the OneNET cloud platform.

3) The voltage value of CO gas concentration in the indoor environment is collected in real time with the CO sensor module and transmitted to the microcontroller, after the microcontroller converts the voltage value into CO concentration value between 0 and 100, and the data is uploaded to the OneNET cloud platform using the ESP8266 module, and the CO concentration in the current environment can be viewed in the device established by the OneNET platform at any time, and if the air CO concentration exceeds the set threshold, the buzzer alarms and sends alarm information to the user.

4) Using the WIFI module ESP8266 module, the data collected by the sensor is handed over to the MCU, and according to the EDP protocol of OneNET platform. It is encapsulated into data packets using json format and uploaded to OneNET platform, which parses the data packets and displays them to the web side using line graphs and dials to present them to the users intuitively and conveniently [5].

2.2. Hardware Design of STM32 Indoor Detection System

System hardware selection and connection: The core controller of the system is selected STM32F103 microcontroller development board (MCU), whose hardware circuit is shown in Figure 1, it is the main chip is STM32F103ZET6, equipped with 512 KB flash memory and 16 MB extended SPI flash memory, contains a power supply circuit, control circuit, download circuit, crystal circuit and reset circuit, can use a variety of interfaces Development of various experiments and design of a development board [6]. Photoresistor sensor module, hardware circuit as shown in Figure 2, the wiring of the photoreceptor module, VCC connected to the positive 3.3 V power supply, GND connected to the negative power supply, the output analog AO interface and the main control chip MCU PF6 pin connection. The integrated digital humidity and temperature sensor DHT11, hardware circuit as shown in Figure 3, its connection, VCC pin connected to the 3.3 V power supply, GND connected to the negative side of the power supply, data output interface and the main chip MCU PG11 pin connection for data transmission.CO gas sensor MQ-7, hardware circuit as shown in Figure 4, this sensor has a very sensitive sense of CO gas concentration, also This sensor is very sensitive to CO gas concentration and is also suitable for smoke and hazardous gas detection, its wiring, VCC connected to 5 V power supply positive, GND connected to power supply negative, output analog AO port and the main chip MCU PA7 pin connection. 3.5 inch resistive screen TFT-LCD, hardware circuit as shown in Figure 5, its resolution 480 * 320, size: 97.4 * 56.4, has 16-bit 8080 parallel port, its wiring, TFT-LCD pin and MCU corresponding pin connection. LCD pins are connected to the corresponding pins of MCU. There is also WIFI module ATK-ESP8266, hardware circuit as shown in Figure 6, its wiring VCC connected to 3.3 V power supply positive, GND ground, ATK-ESP8266 serial output RXD pins and serial input TXD pins connected with MCU serial input TXD pins and serial output RXD pins. The relay module, wired in such a way that VCC is connected to the positive terminal of 5 V power supply, GND is connected to the negative terminal of power supply, and the IN pin is connected to the PG10 pin of MCU.

Figure 1. STM32 MCU.

Figure 2. Light-sensitive module.

Figure 3. DHT11.

Figure 4. Sensor MQ-7.

Figure 5. TFT-LCD.

Figure 6. ATK-ESP8266.

3. Software Programming

The process of hardware design of the detection system not only considers the connection of the hardware, but also requires consideration of the control program writing. After the realization of the system hardware, it must be controlled by the program, and each part must be coordinated to achieve its set function. The more complex and complicated the function of the system, the more difficult it is to write the internal control code, therefore. The more complex the system, the more difficult it is to write the internal control code. Therefore, the control program should be written in a logical and rational manner, and the program should be more concise and logical under the condition of ensuring the normal operation of the system. The software part of this system design process is: the choice of programming language, the design of the main function code, serial port design, interrupt design, display part design, temperature and humidity part, light part, WIFI module part, gas concentration part and alarm part design. C language was chosen for this program programming.

3.1. Main Program Design

The main program, as the beginning of the STM32 microcontroller program operation, is written to contain mainly the initialization of each part of the module and the call of the function functions of various sensors. The main program control completes the process as follows: Step 1, after the microcontroller is powered on and started, it initializes the timer, peripheral hardware devices, serial port interrupt, LCD LCD, photosensitive sensor, buzzer port, etc., accessing the OneNET platform; Step 2, collecting data such as temperature and humidity, light intensity, CO concentration, etc.; Step 3, it calls the display program, serial port program and the program of the OneNET platform to display on the LCD LCD After collecting the data of temperature and humidity, light intensity, CO concentration, etc., the display program, the serial program and the program of OneNET platform are called to display the collected data on the LCD LCD screen; Step 4, the microcontroller uploads the data to OneNET cloud platform through ESP8266 module, and the data is displayed in real time in the device established by OneNET cloud platform; Step 5, the dead loop while (1) function is executed at the end of the program, which prevents the program from running away after executing the main program and ensures that the program always runs within the main function.

The design flow of the main program is shown in Figure 7.

3.2. Subroutine Design

The main program needs to call each subroutine when it is executed. The subroutines of this system mainly include temperature measurement subroutine, humidity measurement subroutine, light intensity detection subroutine, CO gas concentration measurement subroutine, relay subroutine, alarm system subroutine, threshold subroutine, display subroutine, serial port subroutine and interrupt

Figure 7. Flow chart of main program design.

subroutine. The design is completed with all subroutines, and the main subroutine design process is as follows.

1) Temperature and humidity measurement subroutine design

The sensor is initialized by the DHT11_init function, and the temperature and humidity values of the DHT11 are read according to the execution sequence of each bus. Use the dht11_read_data function to read the temperature and humidity values read from the DHT11 [7]. If the return value is zero it means that the reading was successful, if the return value is 1, the reading failed. The file DHT11.h contains mainly port parameters and function declarations, and the code is relatively simple [8]. After successful initialization, the data is collected at 100 ms intervals and uploaded to the OneNET cloud platform for data display.

2) Light intensity measurement subroutine design

Add the file lsens.c under the HARDWARE group and add the header file lsens.h to the header file’s include path. The photoresistor sensor is initialized using the Lsens_Init function, followed by the PF6 interface initialization bit analog input interface, followed by the channel ADC3 initialization using the Adc3_Init function.

The acquisition of light intensity in the current indoor environment is realized through the Lsens_Get_Val function. This function uses Get_Adc3 to obtain the voltage conversion value of ADC_CH6, and after simple processing by the microcontroller, the light intensity is presented in values from 0 to 100, with the light intensity value of 0 being the weakest and the light brightest when the value is 100. The light intensity values measured using the Lsens_Get_Val function are presented in the OneNET platform for data presentation. In the header file of lsens.h, it is mainly declarations and macros to define some functions. After the initialization is done, it enters the while (1) dead loop.

3) Measuring gas concentration subroutine design

The flow of CO gas concentration detection subroutine design is shown in Figure 8.

4. STM32 Indoor Detection System Hardware Debugging and Testing

Choose Keil 5 as the programming software, after installing MDK5, install the installation package that supports the STM32F1 series development board: Keil.STM32F1xx_DFP.1.0.5.pack, after the installation is complete, you can develop and run the program. The program will be run on the STM32 microcontroller, and the program will be modified in time for any errors, warnings and other problems that may occur during the program run, until the program can be compiled successfully on the Keil5 software without errors. The entire program can run normally in the microcontroller and the modules can cooperate with each other to achieve the expected functions of the system. Since the OneNET cloud platform is chosen to receive the detected data from the WIFI module on the PC side, the account of the OneNET cloud platform is logged in first

Figure 8. CO gas concentration detection subroutine design flow chart.

before use. The connection is made so that the device created in OneNET platform can be online and realize the data transfer between microcontroller and PC [9].

The modules are connected to the STM32 microcontroller, and care is taken not to connect the wrong pins when connecting them. The physical connection is shown in Figure 9, and after the control program is installed and debugged, the devices established on the OneNET platform are viewed online. The actual test is conducted indoors, and the measured data of temperature and humidity, light intensity, and CO gas concentration in the environment are displayed on a 3.5" resistive screen, as shown in Figure 10. Meanwhile, OneNET cloud platform can display the detected data in real time with both refraction graph and dial, and present the detected data with a line graph for temperature as shown in Figure 11, humidity as shown in Figure 12, CO gas concentration as shown in Figure 13, and light intensity as shown in Figure 14 [10]. Figure 11 shows the temperature change detected during the 6-minute period. Figure 12 shows the humidity change detected during the 6-minute period. Figure 13 shows the change of CO gas concentration detected during the period of 6 minutes. Figure 14 shows

Figure 9. STM32 indoor environment detection system hardware physical.

Figure 10. Display screen test data.

Figure 11. Ambient temperature change detection data.

Figure 12. Number of environmental humidity change detection.

Figure 13. Ambient CO gas concentration change detection data.

Figure 14. Ambient light intensity change detection data.

Figure 15. Indicator graph of data detected at a moment in time in the environment.

the change of light intensity detected during the period of 6 minutes. The feature of using the line graph is that the change of the detected data in the current environment can be viewed in real time. The dial is used to present the detected data as shown in Figure 15. Figure 15 shows the values of humidity, temperature, CO gas concentration and light intensity measured at a time point, and the dial display features real-time observation of the detected data at the current moment.

The test results show that the finished object can better implement the expected functions of the STM32 indoor detection system.

5. Conclusions

After the system test was completed, the actual indoor environment was measured with the system. The measured value of indoor temperature was 26˚C, the humidity value was 50%, the data displayed by OneNET platform was compared with the actual real-time weather data in the cell phone, the measured data was reasonable and the test result was good. The system uses the gas generated by the lighter ignition instead of CO gas when testing the CO concentration, and the CO gas concentration in the room appears to rise substantially when the lighter is ignited, and the test result is good. The system detects the indoor light intensity at night with a value of 30. When the photoresistor module is illuminated with a flashlight, the indoor light intensity value rises significantly to 84. When the light is too dark in the indoor environment, the relay is triggered and a small light is illuminated, and the test effect is ideal. This further verifies the feasibility of the designed hardware indoor environment detection system in practical application.

This design has solved the problem of indoor environmental testing data transmission and display through the network. This result can make good use of modern network technology to realize the intelligent management of family living environment, and further promote the realization of intelligent detection and control of community environment. It has a good social application value.

Secondly, this hardware system design process and methods for detailed production operation instructions, the STM32 microcontroller-based development to solve the actual needs of the real world of hardware design training for electronics students and microcontroller development enthusiasts application design training, advocate and guide students to design and produce such hardware physical system with realistic significance and solve the actual problems of society, so that students are familiar with the various resources of STM32 microcontroller development board, and they also improve students’ awareness of solving the practical needs of the real society, realize the theory to practical application, cultivate students’ independent innovation ability, well enhance students’ hands-on ability, and achieve the purpose of talent training.

Cite this paper: Ren, L. and Yu, X. (2021) Hardware Implementation of STM32 Microcontroller-Based Indoor Environment Monitoring System. Open Journal of Applied Sciences, 11, 997-1008. doi: 10.4236/ojapps.2021.119072.

[1]   Gu, X., Mo, H. and Liu, S. (2021) Design of Control System of Cleaning Machine for Exterior Wall of High-Rises Based on STM32. International Core Journal of Engineering, 7, 207-210.

[2]   Yang, F. and Wei, Q. (2021) Design of Smart Home Control System of Internet of Things Based on STM32. Journal of Physics: Conference Series, No. 1, 1-5.

[3]   Zhang, J.Q., et al. (2012) Temperature and Humidity Detection System Based on Power Line Communication. Applied Mechanics and Materials, 236-237, 242-246.

[4]   Yin, L. (2021) PID Control of STM32 Serial Port Based on Simulink. Scientific Journal of Intelligent Systems Research, 3.

[5]   Peng, D. (2021) Real Time Monitoring System for Environmental Parameters in Terminal Based on STM32. Computer Science and Application, 11, 1627-1636.

[6]   Ovalles, F.O., Bolivar, A.E. and Rodriguez, A.J. (2018) Use of an Embedded System with Wifi Technology for Domotic Control of Conventional Environments. Journal of Physics: Conference Series, 1126, Article ID: 012070.

[7]   Jin, L. and Zhao, T. (2013) Granary Temperature and Humidity Detection System Based on MCU. Advanced Materials Research, 605-607, 941-944.

[8]   Wang, X., Pan, H. and Gao, H.Y. (2013) Design of Warehouse Temperature and Humidity Detection System. Applied Mechanics and Materials, 341-342, 1005-1008.

[9]   Wang, J., Lin, C.T., Xie, J.X. and Feng, J.S. (2019) The Design of Data Acquisition and Management Based on the STM32 and WiFi. Science and Engineering Research Center. Proceedings of 2019 4th International Conference on Automation, Mechanical and Electrical Engineering (AMEE 2019), Xiamen, 27 October 2019, 352-356.

[10]   Ji, Z. and Bin, R. (2021) Weather Monitoring System Based on STM32 and OneNet Internet of Things Platform. Journal of Physics: Conference Series, 1820, Article ID: 012150.