当前位置:网站首页>[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system

[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system

2022-07-01 13:51:00 gizwits_ csdn

Following last article , Content contains 1 System content ,2, Component selection ,3, Please refer to the system hardware design : Prompt information - Wit cloud

4   System software design
4.1 SCM programming
4.1.1 SCM program development environment

The main development environment of SCM is Keil5,Keil C51 yes 51 Series compatible with single chip microcomputer C Language software development system . The chip used in the laboratory safety monitoring system is STM32, The language used is C Language , therefore Keil5 Just ready to use . At the same time, it has many advantages , For example, it has a very powerful system , The readability is relatively strong , Debugging is very convenient ……

Keil5 It is a good development platform , It can go through C Language 、 Assembly language and other programming , You can also debug the program by setting breakpoints , When compiling, you can remind of writing errors 、 Writing nonstandard behavior , And for different languages , It is compatible , Many people can pass Keil5 This platform uses different languages to develop software .

4.1.2 Monolithic integrated circuit program overall flow chart

The data uplink flow chart of single chip microcomputer is shown in Figure 4.1 Shown . First initialize the functions you need , Then initialize the required pins . Measure temperature and humidity by calling various functions 、 Smoke concentration 、 Propane concentration , Then judge whether the measured data exceeds the threshold , If it exceeds the buzzer, it will alarm . Then send the data received by the Bluetooth host and the measured data to the server .



When cell phone APP When there are instructions to execute , For example, the command to turn on the fan , The instruction is sent to the server , The server sends the instruction to the lower computer . When the lower computer receives the instruction , Set the corresponding pin high or low , The flow chart is shown in Figure 4.2 Shown .


4.1.3 Temperature and humidity acquisition program design

When it is necessary to collect temperature and humidity , Just call the program . If SCM detects DHT11 The existence of , Just go back to 0 Give the conditions for single chip microcomputer to judge , Otherwise, do not proceed to the next step , When detected DHT11 After the existence of , Start reading DHT11 Uploaded temperature and humidity , Pass verification 、 decode , Finally get the correct temperature and humidity . The primary data reading is 5 byte , front 2 Bytes are the integer sum of humidity 、 Decimal sum , Again 2 Bytes are the integer sum of temperature 、 Decimal sum , final 1 Bytes are checksums . The flow chart is shown in Figure 4.3 Shown .




4.1.4 Gas concentration acquisition procedure

First, measure the voltage value of the smoke sensor, that is, the analog output pin of the smoke sensor , And in order to reduce the error , Take multiple measurements to obtain the average value of the voltage of the smoke sensor . Then convert the voltage into 3.3V Under the 4096 Share , namely 12 Bit accuracy ADC, Finally, the body resistance of the smoke sensor is calculated by formula Rs、 resistance R0. Finally, calibrate the execution time of the system , Use the formula to calculate the smoke concentration in the Laboratory . The concentration of propane gas is collected based on the same principle . The flow chart is shown in Figure 4.4 Shown .




chart 4.4 Flow chart of smoke concentration acquisition procedure

MQ6 The same is true of , The flow chart is shown in Figure 4.5 Shown .




chart 4.5 Propane concentration acquisition program flow chart

4.1.5 Human body induction module acquisition program

When the human body sensing module senses signs of human activity within the sensing range , It will output high level . Therefore, it is necessary to read the level of the input pin of the human body induction module connected to the MCU , If this pin is high , It proves that someone exists , If it is low , It proves that no one exists . The flow chart is shown in Figure 4.6 Shown .



chart 4.6 Human body sensor acquisition program flow chart

4.1.6 Water immersion sensor acquisition program

When the probe of the water immersion sensor detects the presence of water , That is, there is water leakage , Through the conversion circuit , Read voltage . Therefore, you only need to read the output pin of the conversion circuit of the water immersion sensor connected with the single chip microcomputer . The flow chart is shown in Figure 4.7 Shown .





chart 4.7 Flow chart of water immersion sensor acquisition program


4.1.7 Bluetooth slave sending program


The Bluetooth slave needs to invade the doors and windows collected by the auxiliary control system 、 Report the ground water leakage to the master control system . Use different data to represent different situations and send them to the main control system , When there are people , Just send the number 1; When there's no one , There are numbers 2; When there is no water leakage on the ground , Just send the letters C; When the ground leaks , Just send the letters D. The flow chart is shown in Figure 4.8 Shown .



chart 4.8 Bluetooth slave sending program flowchart

4.1.8 Bluetooth host receiving program

When the Bluetooth master and slave are successfully paired , The slave sends the collected data to the host for processing . The single chip computer stores the received data , Then read again , Use different numbers and letters , Judge the intrusion of doors and windows 、 Water leakage on the ground . The flow chart is shown in Figure 4.9 Shown .




chart 4.9 Bluetooth host receiving program flowchart


4.2 mobile phone APP Programming
4.2.1 Timer timing program


The data sent to the server should be sent regularly , Otherwise, the server cannot receive the data normally because the data is sent too fast , New data will overwrite old data too soon . If the data is sent too slowly, it is not conducive to the mobile phone APP Reading data . Therefore, the timing time is 6 second , Every time 6 Upload data to the server once every second . The flow chart is shown in Figure 4.10 Shown .




chart 4.10 Timer timing program flow chart


4.2.2 Data uploading program

The data sent to the server is not only the information collected by the main control system , And the information collected by the auxiliary control system . When serial port 1 Received the information sent by the auxiliary control system , Judge this and send it to the server , Different numbers 、 Letters represent different meanings , For example, Bluetooth sends numbers from the machine 1, It indicates that the doors and windows are invaded . Then call various functions , Read the temperature and humidity 、 Smoke concentration 、 Propane concentration , Send this information to the server . The flow chart is shown in Figure 4.11 Shown .




chart 4.11 Flow chart of data upload procedure


4.2.3 Data downlink program

When cell phone APP When an instruction is received , The instruction is sent to the server , The server then sends the instruction to the main control system , The main control system makes a judgment , Whether to operate the instruction . For example, you want to turn on the smoke exhaust fan remotely , Just on the phone APP Click to turn on the fan , The instruction will be sent to the server , The server sends this information to the MCU , The single chip microcomputer executes the instruction , Turn on the fan . The flow chart is shown in Figure 4.12 Shown .




chart 4.12 Flow chart of data downlink procedure


5   mobile phone APP The design of the
5.1  Wit cloud AIoT The access process of


For smart cloud , Developers need to develop some functions for the next machine to access the machine intelligence cloud . First, register in the developer center , Choose to be an individual developer or an enterprise developer , Then create products according to requirements, such as the way to access the machine intelligence cloud 、 The development of equipment 、 Development and application , Finally enter the product debugging . After a process ,APP You can debug and use .






chart 5.1 Smart cloud access process

5.2 Product creation


For mobile phone APP In terms of design , The first thing to set is to display in APP Interface on , That is, the data uploaded to the server . According to the laboratory safety monitoring system , You need to set multiple data points .


First, create new products in the developer center of smart cloud . First the “ Product classification ” Choose Customize for others , Fill in the name of the product ,“ Technical solution ” The choice is Wi-Fi/ Mobile network solution , choice “ Communication mode ” For Mobile Networks ,“ Data transmission mode ” To get longer ,“ Power consumption type ” It's normal , Save it at last . such , A new product is created . When a new product is created , Smart cloud will allocate two keys , These two keys are connected to the lower computer 4G modular , A connected mobile phone APP. They are unique , When the connection is , Smart cloud will automatically distinguish 、 Connect . Pictured 5.2 Shown .






chart 5.2 Basic information of the product


After creating the product , You need to set the data in the product . In the created product , Select new data point , Pictured 5.3 Shown . What is a data point ? Data points are data in products , Data you want to upload to the server , These data in the server and in the code are one-to-one correspondence , Therefore, avoid data errors . The definition of these data includes the identification name , Is the name to be displayed in the product , Including reading and writing types 、 data type , Include comments . These data can be changed , After the change , You also need to change the content in the code , Otherwise, the data format will be incorrect , There will be problems in the communication between the cloud platform and the lower computer . The specific description is shown in table 5.1 Shown .





therefore , According to the requirements of the laboratory safety monitoring system , Analyze the requirements of hardware development , Set the data points , The configuration of data points is APP The most important thing in design , As shown in the table 5.2 Shown .


5.3 MCU Development

For smart cloud , Its advantage is that there is no need for developers to write APP Code for , There won't be many wrong problems , You can use his automatic code generation service , Convenient and quick . These generated codes usually include communication protocols 、 Conversion logic of communication data, etc . Add corresponding development measures to the code , On the cell phone APP After sending instructions , The instruction will become an event and be sent to the server , The server occurs to the lower computer , Drive the lower computer to realize the corresponding functions . Choose here “ Hardware solution ” What we need is independence MUC programme ,“ Hardware platform ” Choose other platforms , Then put the product Product Secret Fill in the corresponding position with parameters , Then generate the code package , Pictured 5.4 Shown . Finally, transplant the generated code package into the code of the lower computer .






chart 5.4 MCU SDK Development


5.4 Mobile app settings

First, create a new mobile application in the developer center of smart cloud , Fill in the application name , namely APP Name ,“ App package name ” Choose a unique name , It should not be the same as the name of other products .“ type ” Just choose smart home ,“ platform ” Just choose ios and android, Pictured 5.5 Shown .






chart 5.5 New mobile application interface


After creating a new mobile app , Click to enter the mobile application . In its personalization options , Select related products , Click on “+” Number , Associate the previously created product with the currently created mobile application , You can also choose your favorite theme , Change icons, etc .
After the associated number product , Configure the application certificate of the product . Under the option of building applications , Select application certificate , Then you can configure the certificate . There is only Android certificate , Pictured 5.6 Shown .




chart 5.6 Android Certificate configuration


Under the same option , Select application build , Click build beta , wait for APP The construction of . When APP After building , A QR code is generated , Scan with your mobile phone's built-in browser , You can download the built APP, It can be installed normally .


5.5 Connection of upper and lower computers


When downloading good APP after , We need to APP Connect with the lower computer . Use software GAgent_Debugger Configure mobile phone APP Contact with the lower computer . In this device , choice “ newly added ”, We choose “ Server platform ” For domestic servers , The of the product Product Key and Product Secret Fill in the parameters , choice “ Device type ” It is a single product , choice “ communication mode ” For Mobile Networks . After successfully configuring the server , Just like the picture 5.7 Shown .






5.7 Configuration success interface


In the tool options , Select the device QR code , take Product Key Fill in the parameters , as well as 4G Modular IMEI Number is also filled in , choice “ Server domain name ” For domestic servers , And then click “ QR code generation ”, A two-dimensional code will be generated , Pictured 5.8 Shown . Mobile phone APP open , Click the QR code of the scanning device , Scanning device QR code , The mobile phone APP Bind with the lower computer . What we should pay attention to here is ,APP When binding, the lower computer must be connected to the machine intelligence cloud . When the lower computer works normally ,APP You can view all kinds of information .






chart 5.8 Generate device QR code

5.6 mobile phone APP Push of

The first push function used is based on the machine intelligence cloud platform , Therefore, first apply to the machine intelligence cloud for opening . The first application is the function of rule engine . When the application is submitted , Wait for the smart cloud platform to open , Then you can use its function .


After successfully opening the rule engine , In the created product , Under service options , More D3 Engine choice .
Click on D3 Engine, Enter the configuration interface , Click Edit push platform , Pictured 5.9 Shown .
 

chart 5.9 Edit the push platform
The push platform chosen here is Baidu , The application platform is Android , Will apply on Baidu platform API Key and Secret Key Fill in the corresponding position , Pictured 5.10 Shown .



 

chart 5.10 Push platform binding
It's also in D3 Engine Under the function , Select item list , New projects . In the new project , Build the functions you need , The block diagram on the left is the selected data 、 Trigger action, etc , On the right is the created editing area . Pictured 5.11 Shown .







chart 5.11 Edit push function


First, drag the device data to the editing area , Edit the triggering method as equipment reporting status , Pictured 5.12 Shown . Then choose the writing of logical rules , Compare parameters 1 Is the data you want to alarm , Compare parameters 2 Is the set threshold , The comparison symbol is the relationship between these two parameters , Pictured 5.13 Shown . Finally, edit the email push , Fill in the title of the email 、 Content and push mailbox , Pictured 5.14 Shown .  






chart 5.14 Edit the email push function


6   System debugging

6.1 Configuration and communication of master-slave Bluetooth
For Bluetooth , Its setting is through AT Command to configure the name of Bluetooth 、 Baud rate 、 Binding address, etc . Therefore, we should first understand the commonly used Bluetooth AT Instructions , As shown in the table 6.1 Shown .
surface 6.1 Bluetooth is commonly used AT Instructions






Before the master and slave of Bluetooth are formally configured , We need to be prepared first , Connect Bluetooth with USB turn TTL Connect , Set the enable end of Bluetooth to high level , Then power on the module , So Bluetooth enters AT Pattern , The baud rate is fixed to 38400, It can be sent directly AT Instructions , The enable end is set high once without sending an instruction . Get into AT After instruction mode , First restore the two Bluetooth to the default factory settings , Get their Bluetooth address at the same time , This is the machine MAC Address . Send each one AT At the time of instruction , Remember to add a carriage return after the instruction , If you don't enter ,AT The command will fail to send . When a message is successfully sent AT At the time of instruction , Bluetooth test software will reply OK, If there is no reply OK, It indicates that the instruction was not sent successfully .


When setting up the host , First set the name of the host to Y1( The name of Bluetooth can be whatever ), Then set it to host mode , Configure pairing password 7890( Pairing passwords can be arbitrary ), Bind the address of Bluetooth slave , Set the baud rate to 9600, Change the connection mode of Bluetooth to 0, That is, specify the Bluetooth address connection mode . The final result is shown in the figure 6.1 Shown .






chart 6.1 Host settings configuration


Set slave , First set the name of the slave to Y1, Then set it to slave mode , Configure pairing password 7890, Bind the address of the Bluetooth host , Set the baud rate to 9600, Change the connection mode of Bluetooth to 0, That is, specify the Bluetooth address connection mode . The final result is shown in the figure 6.2 Shown . What we should pay attention to here is , Bluetooth Host 、 The name of the slave 、 Pairing password 、 Baud rate must be consistent , Otherwise, the setting will not succeed . If the Bluetooth connection mode is not set to specified , Instead, set it to other modes , But only Bluetooth is not sent one-to-one , Therefore, it is best to set it to the specified mode .






chart 6.2 Slave setting configuration
After Bluetooth is successfully configured , Before work , They will automatically pair , When the indicator light of Bluetooth changes from fast flash to slow flash, it means successful pairing . After successful pairing , Slave Slave serial port 1 send data , Host slave serial port 1 Receive the data and print it out , Use the serial port debugging assistant to view the data sent by the slave , If the printed data is consistent with the sent data, Bluetooth communication is normal . When sending data , Try not to use Chinese characters , Prevent garbled code .


6.2 Receiving and sending cloud data

How to make sure that the server and the lower computer are successfully connected ? Will give 4G Module power supply , Do not contact with the main control system 5V Voltage phase connection , But give it alone 4G Module power supply , Because of the main control system 5V Can't drive 4G Module operation . take 4G Module and serial port 3 Connect ,TXD Connect the serial port 3 Of RXD,RXD Connect the serial port 3 Of TXD. At the same time, don't forget to put the lithium battery on the ground 、4G The ground of the module is connected with the ground of the MCU , otherwise 4G The module does not work properly . When 4G When the module works successfully , Its power indicator is on , Connection cloud indicator flashes . meanwhile , You can view it in the cloud 4G Whether the module is successfully connected to the cloud , If in the device log of smart cloud , You can find the device IMEI Number 、 equipment ID And whether it is online , Pictured 6.3 Shown .






chart 6.3 Connect to the cloud interface


When the master system is successfully connected to the server , Whether the uploaded data is successful , You can observe through the device log of the server . The server will actively record the data uploaded through the lower computer , While uploading data, these data are transmitted through the serial port 1 Print out , Connect the data received by the cloud with the serial port 1 Compare the printed data , If the data is normal, the communication is successful .


When the data is successfully uploaded to the cloud , How to show the successful sending of cloud data . Using smart cloud DEMO Software and self created mobile phones APP Open at the same time , Also open the serial port debugging assistant to check the serial port 1 The data of , Compare these three together , If the data is consistent, it indicates that the cloud data is sent normally , No packet loss occurred . When uploading data , Be sure to remember to send regularly , This time can't be too fast , Soon, the later data will cover the previous data , Too slow leads to abnormal data update , Easy to empty . So it's best to schedule 6 second , Every time 6 Send data to the cloud once every second , The data received by the cloud is shown in the figure 6.4 Shown .






chart 6.4 Data received in the cloud

7   parameter measurement
7.1 Gas concentration measurement

Pictured 7.1 Shown , This circuit is the basic circuit of gas sensor , The circuit only needs to apply VH and VC. It can be seen from this circuit




among RL take 4.7K ohm ,VC Is the circuit voltage ,VRL by AO Port output voltage ,RS Is the resistance value of the sensor in the gas .



chart 7.1 Basic circuit


Pictured 7.2 Shown , The abscissa of the characteristic curve is the gas concentration , Unit is ppm, namely 1 Cubic centimeter /1 Cubic meters , Ordinate for RS/R0. From this curve, we can know the relationship between the two , List tables 7.1, And draw a scatter diagram , Simulate the relationship between points in the scatter diagram , Get the formula y=33607x^(-2.998), among y by ppm,x by RS/R0. Use this formula to calculate ppm, theory ppm And calculation ppm The error between can be calculated . As shown in the table 7.1 Sum graph 7.3 Shown .






chart 7.2 MQ2 Sensitivity characteristic curve
surface 7.1 RS/R0 Relationship with smoke concentration




chart 7.3 Smoke concentration scatter diagram and calculation formula
Similarly, the calculation of propane concentration can be obtained , Pictured 7.4、 surface 7.2 Sum graph 7.5 Shown .








chart 7.5 Propane concentration scatter diagram and calculation formula

原网站

版权声明
本文为[gizwits_ csdn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011346109030.html