【IoT Graduation Project】Gizwits AIoT+STM32+Remote Environment Real-time Monitoring
[Copy link]
Abstract: Aiming at the problem of remote environmental monitoring needs, a remote environmental real-time monitoring system based on STM32 is designed. The system consists of three subsystems: information acquisition, electrical control and coordinator. Each subsystem uses STM32F103C8T6 microcontroller as the main control chip, and uses LoRa wireless communication technology to form a star network to complete the transmission of environmental information and control instructions.
The information collection subsystem collects information such as temperature, humidity, smoke, carbon monoxide, air quality and light intensity in the environment in real time through the sensor network; the electrical control subsystem completes the control function through relays and motors; the coordinator subsystem connects the system to the cloud platform through the Wi-Fi module, uses the mobile phone APP to remotely monitor the environmental information, and remotely control the system at the same time. The test results show that the system can stably complete the functions of real-time collection, remote monitoring and remote control.
At present, existing environmental monitoring systems, such as the use of ZigBee communication technology, can achieve real-time monitoring of alarm buttons, door magnets, window magnets and infrared sensing modules, but the system coverage is small [5-6]; the detection of indoor carbon dioxide concentration is achieved through wireless sensor networks, but the detection information is single [7]; the global system for mobile communications (GSM) module only remotely alarms abnormal environmental information in the greenhouse, and cannot remotely monitor the real-time environment of the greenhouse [8]; in wireless sensor networks, Wi-Fi technology is used to transmit multi-hop data of environmental parameters, and the network complexity is high [9].
In view of the above problems, a remote environment real-time monitoring system based on STM32 is designed to remotely monitor the indoor environment. The system first uses the LoRa technology [10-11] with long communication distance and low power consumption to form a star network for each subsystem, and then connects the system to the Internet of Things cloud platform through the Wi-Fi module , in order to realize remote real-time monitoring of environmental information and remote control of the system.
1 System overall design
The remote environment real-time monitoring system based on STM32 is mainly composed of information acquisition subsystem, coordinator subsystem, electrical control subsystem and Internet of Things monitoring and management platform. The subsystems are connected in a star network through LoRa wireless communication technology. The overall design of the system is shown in Figure 1.
Figure 1 System overall design
1.1 Information Collection Subsystem
As a node, the information collection subsystem collects information such as light, temperature, humidity, smoke, carbon monoxide and air quality in the environment through the sensor network and sends the data to the coordinator subsystem.
1.2 Coordinator Subsystem
As a gateway, the coordinator subsystem connects to the wireless router through the Wi-Fi module to access the Internet, uploads system information to the Internet of Things cloud platform on the Internet, and obtains control instructions from the Internet of Things cloud platform. When the environmental information is abnormal, the buzzer will sound an alarm to avoid accidents such as fire or carbon monoxide poisoning from causing harm to people. In addition, the working mode of the coordinator subsystem is divided into automatic mode and manual mode. When in automatic mode, the system compares the actual environmental information with the set threshold, decides the control instructions for electrical appliances, and provides intelligent control services. When in manual mode, electrical appliances are controlled through mobile phone APP or buttons. After the coordinator subsystem decides the control instructions according to the working mode, it sends the control instructions to the electrical control subsystem.
1.3 Electrical control subsystem
As a node, the electrical control subsystem is responsible for parsing the control instruction data packets and controlling the electrical appliances. The control of electrical appliances includes the control of relays and the driving of stepper motors.
1.4 IoT Monitoring and Management Platform
The IoT monitoring and management platform is responsible for remote monitoring of environmental information and remote control of the system. It consists of the mobile APP and Web end of the Gizwits IoT cloud platform. The Gizwits IoT cloud platform is a third-party cloud server on the Internet. The mobile APP communicates with the cloud platform through the mobile network to view the information uploaded by the system. At the same time, the control instructions are sent to the cloud platform through the mobile network to modify the system parameters and control the electrical appliances. The historical data uploaded by the coordinator subsystem is queried through the communication log on the Web end, and the data is counted and analyzed.
2 Hardware Design
The three subsystems are controlled and data processed by the STM32 single-chip microcomputer minimum system. The STM32 single-chip microcomputer minimum system consists of an STM32F103C8T6 single-chip microcomputer [12-13], a power supply circuit, a crystal oscillator circuit, a reset circuit, and a Boot circuit. The subsystems communicate with each other through the LoRa wireless communication module, which uses the SX1268 chip.
2.1 Information Collection Subsystem Hardware Design
The hardware part of the information acquisition subsystem mainly includes three parts: STM32 microcontroller minimum system, sensor circuit and wireless communication module. The hardware structure is shown in Figure 2.
Figure 2 Hardware structure of information acquisition subsystem
The sensor circuit includes temperature and humidity sensor, light sensor, smoke sensor, carbon monoxide sensor and air quality sensor. The temperature and humidity sensor uses DHT11 module, and communicates with the microcontroller via single bus. Its humidity accuracy is ±5%RH, temperature accuracy is ±2℃, humidity range is 20%~90%RH, and temperature range is 0~52℃. The light sensor, smoke sensor, carbon monoxide sensor and air quality sensor use photoresistor sensor and MQ-2, MQ-7, MQ-135 gas sensors respectively. All four sensors output analog signals, which are connected to the analog to digital converter (ADC) pin of the microcontroller. The output signal is converted into A/D to obtain environmental data.
2.2 Coordinator Subsystem Hardware Design
The hardware of the coordinator subsystem mainly includes six parts: STM32 microcontroller minimum system, wireless communication module, key circuit, display module, Wi-Fi module and alarm module. The hardware structure is shown in Figure 3.
Figure 3 Coordinator subsystem hardware structure
The key circuit is responsible for switching the screen display content, turning on and off electrical appliances, modifying the system working mode and threshold information, etc. The display module uses a 0.96-inch organic light-emitting diode (OLED) screen to display environmental information and system parameters, and communicates with the microcontroller using the IIC (inter-integrated circuit) communication protocol; the Wi-Fi module uses ESP8266 [14] to upload environmental information to the Gizwits IoT cloud platform and communicate with the microcontroller through a serial port; the alarm module uses an active buzzer to alarm abnormal situations.
2.3 Electrical control subsystem hardware design
The hardware of the electrical control subsystem mainly consists of four parts: STM32 microcontroller minimum system, wireless communication module, stepper motor control drive circuit and relay. The hardware structure is shown in Figure 4.
Figure 4 Hardware structure of electrical control subsystem
The stepper motor control drive circuit uses the ULN2003 chip [15] to drive the stepper motor by amplifying the current. The clockwise and counterclockwise rotation of the stepper motor can control the opening and closing of the curtain. The relay uses a low-level trigger relay to control the lighting system.
3. Software Design
3.1 Information Collection Subsystem Software Design
The information collection subsystem mainly completes the collection and packaging of information such as light, temperature, humidity, carbon monoxide and air quality in the environment, and sends it to the coordinator subsystem. The specific work flow is shown in Figure 5. In order to reduce power consumption, the main control chip is set to sleep mode, and the main control chip is awakened by timer interrupt. After the system is powered on and initialized, the main control chip enters sleep mode and waits for the timer interrupt to wake up. When a 5s timer interrupt occurs, the microcontroller executes the timer interrupt function, collects and packages information such as temperature, humidity, light, smoke, carbon monoxide and air quality, and finally sends the environmental information data packet to the coordinator subsystem. After sending, the main control chip enters sleep mode again and waits for the timer interrupt to occur.
Figure 5 Information collection subsystem engineering process
3.2 Coordinator Subsystem Software Design
The coordinator subsystem is mainly responsible for receiving, parsing and displaying environmental information data packets, abnormal environmental information alarms, control command decisions, system parameter modifications, communication with the cloud platform, and sending control commands to the electrical control subsystem. The specific work flow is shown in Figure 6.
Figure 6 Coordinator subsystem workflow
After the system is powered on and initialized, it is determined whether serial port 2 has received an environmental information data packet. If a data packet is received, the data packet is parsed according to the frame format to obtain environmental information and display it on the OLED screen. When the environmental information is abnormal, an alarm is sounded through the buzzer. When the system is in automatic mode, if the light intensity is greater than the threshold, the curtains are opened and the lights are turned off; when the light intensity is less than the threshold, the curtains are closed and the lights are turned on. Then the key scanning program is entered, and the system parameters can be modified by pressing the keys, and the lights and curtains can also be controlled. When communicating with the cloud platform, the environmental information and system parameters are uploaded to the Gizwits IoT cloud platform, and control instructions are obtained from the cloud platform. Finally, the control instructions are packaged and sent to the electrical control subsystem.
3.3 Electrical control subsystem software design
The electrical control subsystem is mainly responsible for receiving and parsing control instruction data packets, and driving relays and stepper motors according to the control information. The specific work flow is shown in Figure 7.
Figure 7 Electrical control subsystem workflow
After the system is powered on and initialized, it waits for serial port 2 to receive the control information data packet, then parses the control information, drives the stepper motor and controls the relay according to the control information.
3.4 Internet of Things Monitoring and Management Platform Access
The IoT monitoring and management platform consists of the mobile APP and Web end of the Gizwits IoT cloud platform. After registering and creating a product on the Gizwits official website, create data points corresponding to the product. The data points abstract the actual functions of the device and describe the parameters and functions of the product. The created data points are shown in Table 1. The data points correspond to the functions of the device one by one, and the cloud platform communicates with the device and the mobile APP in a certain data format.
After the product is created, the Gizwits official website generates protocol code based on the data points established by the product. This part of the code can be transplanted into the MCU project of the coordinator subsystem to complete the device access. The Gizwits protocol code mainly includes 4 files, as shown in Table 2.
The gizwits_product.c and gizwits_protocol.c files contain key functions used in uploading data to the cloud platform and issuing control instructions from the cloud platform.
The specific steps for the coordinator subsystem to upload data to the cloud platform are as follows.
Step 1: Call the userHandle function to assign the information to be uploaded to the data point.
Step 2 calls the gizCheckReport function to determine whether the data reported this time is the same as the data reported last time. If they are the same, no report is made; if they are different, proceed to step 3.
Step 3 calls the gizDataPoints2ReportData function to convert the uploaded data into report data that can be parsed by the cloud.
Step 4 calls the gizReportData function to send the converted data to the Wi-Fi module through the serial port, and the Wi-Fi module then uploads the data to the cloud platform.
The specific steps for the cloud platform to issue control instructions to control the system are as follows.
Step 1: After the coordinator subsystem receives the information sent by the cloud platform, it calls the gizProtocolGetOnePacket function to obtain a data packet from the receiving buffer of the ring buffer. If the data packet is a control data packet, it goes to step 2.
Step 2 calls the gizDataPoint2Event function to generate a control event according to the protocol.
Step 3 calls the gizwitsEventProcess function to perform corresponding processing according to the generated control event.
4. System Testing
In order to verify the feasibility of the system, the Internet of Things monitoring and management platform functions, working stability, communication distance, environmental information abnormality alarm function and automatic control function were tested respectively.
4.1 Functional testing of IoT monitoring and management platform
The OLED screen of the coordinator subsystem can display environmental information, the status of curtains and lights, system working mode and threshold information, as shown in Figure 8. At this time, the content displayed on the mobile phone APP is shown in Figure 9. Compare the information displayed on the mobile phone APP with the information displayed on the OLED screen, and the two are consistent. If the actual environmental information is artificially changed, the information displayed on the OLED screen and the information displayed on the mobile phone APP will be updated synchronously.
The lights and curtains can be controlled through the mobile phone APP, and the relays and stepper motors of the electrical control subsystem will respond in real time. The threshold information and working mode can be adjusted through the mobile phone APP, and the information displayed on the OLED screen will also be updated. The test results show that the system can realize the functions of real-time collection of environmental information, remote monitoring and remote control of the system.
Figure 8 OLED display information
Figure 9 Mobile APP display information
4.2 System stability test
Check the communication log of the coordinator subsystem uploaded information on the Gizwits Cloud official website, and collect statistics on the 12-bit ADC sampling values of light, smoke, carbon monoxide, air quality and other information uploaded by the system within 1 hour in a normal environment, as well as the temperature and humidity values. The results are shown in Table 3.
As can be seen from Table 3, the temperature and humidity information uploaded by the device within 1 hour varies within a very small range, the temperature is maintained at around 30°C, and the humidity is maintained at around 45%, both of which remain basically stable, which is in line with the actual situation of the environment. The ADC sampling values of environmental parameters such as light intensity, smoke, carbon monoxide and air quality uploaded by the device within 1 hour all vary within a small range, reflecting that the environment is relatively stable. The data uploaded by the system remains basically stable, indicating that the system's functions of data collection, transmission and uploading are stable.
4.3 Communication distance test
The packet loss rate of point-to-point communication at 0-0.8 km was tested under the condition of wall obstruction and line of sight. The transmission power of the LoRa wireless communication module was 20 dBm, the air rate was 2.4 Kbps, and the antenna gain was 3 dBi. 500 data packets were sent and received at each test distance, each packet had 26 bytes, and even parity was used. The relationship between the communication packet loss rate and the transmission distance is shown in Figure 10.
As can be seen from Figure 10, the communication packet loss rate is higher in the case of obstruction than in the case of line of sight. In the case of obstruction, when the communication distance is less than 0.1 km, the communication packet loss rate is less than 3%, which can meet the indoor communication requirements in general scenarios. High-gain antennas can be used to increase the transmission distance without increasing power consumption.
Figure 10 Relationship between transmission distance and communication packet loss rate
4.4 Test of abnormal environmental information alarm function
When the smoke threshold is modified to be lower than the actual smoke concentration in the environment, the buzzer of the coordinator subsystem will sound an alarm; when the smoke threshold is modified to be higher than the actual smoke concentration in the environment, the buzzer of the coordinator will stop sounding. Alarms will be issued when the smoke concentration, carbon monoxide concentration and air quality in the environment are changed to abnormal states. Through repeated tests, the system alarm function is stable.
4.5 Automatic control function test
After setting the working mode of the coordinator subsystem to automatic mode, reduce the light intensity of the environment to below the light threshold to simulate the night environment. At this time, the light controlled by the relay in the electrical control subsystem will automatically turn on, and the stepper motor will rotate counterclockwise to control the curtains to close. Increase the light intensity in the environment to above the light threshold to simulate the daytime environment. At this time, the light of the electrical control subsystem will automatically turn off, and the stepper motor will rotate clockwise to control the curtains to open. The test results show that the system has completed the function of automatically controlling lights and curtains when the light intensity in the environment changes.
5 Conclusion
The remote environment real-time monitoring system based on STM32 uses a sensor network to collect information such as temperature, humidity, smoke, carbon monoxide, air quality and light intensity in the environment, and the monitoring information is rich. The subsystems are networked in a star shape through LoRa wireless communication technology, with low network complexity and large system coverage. The environmental information is uploaded to the Gizwits IoT cloud platform through the WiFi module, realizing remote monitoring of the environment.
The test results show that the system can stably collect, transmit and upload environmental information. The mobile phone APP can remotely view environmental information and control the system through the mobile network. When the environmental information is abnormal, the buzzer can automatically alarm. The communication distance between subsystems can reach more than 0.5km. When the system is in automatic mode, it can automatically control electrical appliances according to the changes in the light intensity in the environment, and the control method is intelligent. The system architecture with separated functions is also conducive to the maintenance and upgrade of the system. The system meets the needs of remote environmental monitoring.
|