*2.2. Software Design*

According to the hardware frame in Figure 1, the software of monitoring system includes three parts: (i) the sink node software. Receiving the data sent by each sensor node, starting the positioning module to obtain the positioning information, configuring the cloud platform, and compressing and sending the received data and so on must be completed by the software of the sink node. Before the sink node sends data, the cloud platform must be configured according to the actual situation. Then the data sent by the sink node can be stored in the cloud platform. The cloud platforms of different NB-IoT companies are different. People need to refer to the user manual for configuration. (ii) the sensor node software. The main task of sensor node software is to start each sensor on the node to make them work normally, then collect their data and send them to the sink node. (iii) the monitoring center software. The software of the monitoring center needs to download temperature, humidity and positioning information from the cloud platform and store them in the database. In addition, it also needs to display the location information on the map and draw the temperature and humidity curve.

#### 2.2.1. Software Design of Sink Nodes

The software design of sink node mainly focuses on four subprograms: NB-IoT subprogram, RFID subprogram, BDS/GPS subprogram and data compression subprogram. The main program flow chart of sink node is shown in Figure 4. After the initialization of each module, the main program will be looped in each module subprogram to deal with each module in real time. The watchdog is added to the main program to reset the program to prevent the program from getting stuck or running away.

OneNET platform is a NB-IoT cloud platform developed by China Mobile Communication Company. It can communicate with multiple sink nodes and can read data from multiple sink nodes at the same time by multithreading. We should login in the platform "https://open.iot.10086.cn (accessed on 20 January 2022)" to register the device name. Then add the objects for each device, as well as the number of points and properties of each object. In our experiment, there are three objects in each device, namely temperature, humidity and position information. Finally, the data type of each object should be described. For example, the data type of temperature is 31 bits floating point. According to the Internet Protocol for Smart Objects (IPSO) Alliance Technical Guideline, the longitude, the object ID of latitude, humidity and temperature is 3300, 3303 and 3304 separately. The instruction

format is described detailly in the manual [53]. After the NB-IoT module is connected to the OneNET platform, the platform will record the life cycle of the sink device (the life cycle is configured 3600 s in the initialization). When the life cycle expires, the OneNET platform will issue a life cycle update request, and the sink node can update the life cycle. Or the sink node can actively update the life cycle before the life cycle expires. In this paper, the life cycle is automatically updated, and the life cycle update flag is activated by setting a certain time through the timer. During the life time, the NB-IoT and cloud platform can communicate. The NB-IoT subprogram is shown in Figure 5. At first, the timer will judge whether it exceeds 3600. If yes, the NB-IoT module will be initialized. If no, it continues to judge whether the receive flag bit is 1. If it is 1, it means that the reception is completed, and the data needs to be sent and the receive flag should be cleared and over. Otherwise, the life cycle flag will be judged, if it is 1, it means the time is over and the new request of update the life cycle should be sent. The two flags are active in timer interrupt and UART interrupt respectively.

**Figure 4.** Main program flow chart of sink node.

**Figure 5.** The Flowchart of NB-IoT subprogram.

One sink node should receive data from several sensor nodes. How to receive data from multiple sensor nodes efficiently and successfully? Here, a polling mechanism is proposed. As Figure 6 shown, the sink node sends 'read request' to sensor node1, sensor node2, and sensor node3 in turn. If sensor node1 send 'answer request' in time, then the data of sensor node1 will be allowed to send, the sink node will receive the data from sensor node1. Then next sensor node. The RFID module cc1101 in sink node accesses a sensor node every second in turn. The response timeout mechanism of the sub node is set up to avoid the data transmission errors due to the fact that the sub node is not in the transmission range and does not respond when the sink node sends a request.

**Figure 6.** The flowchart of RFID subprogram of sink node.

RFID initialization mainly refers to the function configuration of cc1101 chip in RFID module. Specifically, through SPI communication mode, we can read and write the internal register of cc1101, so as to complete the setting of fundamental frequency, modulation and demodulation mode, baud rate, packet length and other related parameters. For one of the two general digital output pins with settable functions contained in cc1101, the level jump from low to high occurs when receiving data. The data can be received and read by configuring the rising edge of the I/O port of MCU to trigger interrupt. The flowchart of RFID subprogram is shown Figure 6.

BeiDou System is a positioning and navigation system independently developed by China. The positioning accuracy of BDS can reach 2.5 m in the Asia Pacific region and 10 m in the world. The test accuracy is 0.2 m/s; Timing accuracy is 10 ns. It also has its unique short message communication function. GPS is Global Positioning System. It is well known in the world and developed by the United States. We use BDS/GPS dual system for positioning to obtain higher positioning accuracy. The positioning principle is as follows:

$$\begin{array}{l} \rho\_B^{(m)} = r\_B^{(m)} + E\_B^{(m)} + i \ast \delta\_{\mathfrak{t}\_B} + j \ast \delta\_{\mathfrak{t}\_G} - \mathfrak{r}\_B^m + I\_B^m + T\_B^m + \mathfrak{e}\_B^m\\ \rho\_G^{(n)} = r\_G^{(n)} + E\_G^{(n)} + j \ast \delta\_{\mathfrak{t}\_G} + i \ast \delta\_{\mathfrak{t}\_B} - \mathfrak{r}\_G^n + I\_G^n + T\_G^n + \mathfrak{e}\_G^n \end{array} \tag{1}$$

where, *ρ*, *r*, *E*, *δt,τ*, *I*, *T* respectively represent the pseudo range measurement value of the receiver to a star, the real distance, ephemeris error, receiver clock error, satellite clock error, ionospheric delay, tropospheric delay and pseudo range measurement noise. Superscript *m*, *n* denotes different satellites. Subscripts *B* and *G* indicate different satellite systems. *B* represents Beidou and *G* represents GPS. *i* = 1, *j* = 0. If (*x*, *y*, *z*) is used to represent the

position coordinates of the unknown receiver, and (*x(n)*, *y(n)*, *z(n)*) is used to represent the position of satellite *n*, then *r*(*n*) is equal to the following expression.

$$r^{(n)} = \sqrt{\left(\mathbf{x} - \mathbf{x}^{(n)}\right)^2 + \left(y - y^{(n)}\right)^2 + \left(z - z^{(n)}\right)^2} \tag{2}$$

In Expressions (1) and (2), *ρ*, *τ*, *I*, *T* and the position of satellite can be calculated by original observation, navigation message and corresponding model. If the pseudo range measurement noise is ignored, five unknown parameters need to be solved, namely, the position of the receiver, the BDS clock difference of the receiver and the GPS clock difference ((*x*, *y*, *z*)> *δtB ,δtG* ). Define error correction pseudo range measurements *ρ* (*n*) *<sup>c</sup>* is as Expression (3) shown.

$$
\rho\_{\varepsilon}^{(m)} = \rho^{(n)} + \tau^{(n)} - I^{(n)} - T^{(n)} \tag{3}
$$

Then, the pseudo range observation equation of BDS/GPS dual system can be expressed as Expression (4):

$$\begin{aligned} \rho\_{c,B}^{(m)} &= \sqrt{\left(\mathbf{x} - \mathbf{x}\_{B}^{(m)}\right)^{2} + \left(y - y\_{B}^{(m)}\right)^{2} + \left(z - z\_{B}^{(m)}\right)^{2}} + i \ast \delta\_{t\_{B}} + j \ast \delta\_{t\_{G}} \\ \rho\_{c,G}^{(n)} &= \sqrt{\left(\mathbf{x} - \mathbf{x}\_{G}^{(n)}\right)^{2} + \left(y - y\_{G}^{(n)}\right)^{2} + \left(z - z\_{G}^{(n)}\right)^{2}} + j \ast \delta\_{t\_{B}} + i \ast \delta\_{t\_{G}} \end{aligned} \tag{4}$$

Linearize Expression (4) through the first-order Taylor expansion to obtain the linearized matrix equation as Expression (5) shown.

$$G \begin{bmatrix} \Delta x \\ \Delta y \\ \Delta z \\ \Delta \delta\_{t\_B} \\ \Delta \delta\_{t\_C} \end{bmatrix} \tag{5}$$

where,

$$G = \begin{bmatrix} -I\_{B,K-1}^{(1)} & -p\_{B,K-1}^{(1)} & -q\_{B,K-1}^{(1)} & 1 & 0 \\ -I\_{B,K-1}^{(2)} & -p\_{B,K-1}^{(2)} & -q\_{B,K-1}^{(2)} & 1 & 0 \\ & \dots & \dots & \dots & \dots & \dots \\ -I\_{B,K-1}^{(m)} & -p\_{B,K-1}^{(m)} & -q\_{B,K-1}^{(m)} & 1 & 0 \\ -I\_{G,K-1}^{(m+1)} & -p\_{G,K-1}^{(m+1)} & -q\_{G,K-1}^{(m+1)} & 0 & 1 \\ & \dots & \dots & \dots & \dots & \dots \\ -I\_{G,k-1}^{(m+n)} & -p\_{G,k-1}^{(m+n)} & -q\_{G,k-1}^{(m+n)} & 0 & 1 \end{bmatrix} \tag{6}$$

$$b = \begin{bmatrix} \rho\_{c,B}^{(1)} - r\_{B,k-1}^{(1)} - i \ast \delta\_{l,B,k-1} - j \ast \delta\_{l,G,k-1} \\ \rho\_{c,B}^{(2)} - r\_{B,k-1}^{(2)} - i \ast \delta\_{l,B,k-1} - j \ast \delta\_{l,G,k-1} \\ \dots \\ \rho\_{c,B}^{(m)} - r\_{B,k-1}^{(m)} - i \ast \delta\_{l,B,k-1} - j \ast \delta\_{l,G,k-1} \\ \rho\_{c,G}^{(m+1)} - r\_{G,k-1}^{(m+1)} - j \ast \delta\_{b,k-1} - i \ast \delta\_{l,G,k-1} \\ \dots \\ \rho\_{c,G}^{(m+n)} - r\_{G,k-1}^{(m+n)} - j \ast \delta\_{b,k-1} - i \ast \delta\_{l,G,k-1} \end{bmatrix}\_{0} \tag{7}$$

 −*I* (*m*) *<sup>B</sup>*,*k*−1, <sup>−</sup>*<sup>p</sup>* (*m*) *<sup>B</sup>*,*k*−1, <sup>−</sup>*<sup>q</sup>* (*m*) *B*,*k*−1 is the directional cosine of the observation vector from the receiver to satellite m. Using the principle of weighted least squares to solve Expression (5), continue to use Newton iterative algorithm to solve the location result.

In order to reduce the utilization rate of CPU, the positioning data of BDS/GPS is transmitted to memory of MCU by UART port through DMA mode every second. The UART idle interrupt is triggered after all the positioning data are sent successfully, and the MCU responds to the interrupt to read the data in DMA for subsequent processing. The process of interrupt response program is similar to NB-IoT. It is not described in detail. The subprogram flowchart of BDS/GPS is given in Figure 7. After the BDS/GPS module parameters are set successfully, we can use the serial port assistant to read the module positioning information.

**Figure 7.** The flowchart of BDS/GPS subprogram.

Data compression subprogram is used to compress data redundancy, reduce power consumption of sink node and improve data transmission efficiency. According to the characteristics of small amount of data transmission and good local time correlation of the system studied in this paper, the wireless remote data compression system designed includes sequence correlation packet processing and Adaptive Optimal Zero Suppression (AOZS) compression. Correlation grouping processing refers to the grouping rearrangement of the original sequence according to the transmission data format to obtain *n* groups of incremental subsequences with good time correlation. Then AOZS compression is performed on each subsequence to eliminate the time redundancy in the sequence.

Adaptive optimal zero suppression (AOZS) compression algorithm is improved from differential code compression algorithm, which is suitable for the compression of incrementally sorted data sequences. AOZS algorithm reduces the number of coded data and removes the time redundancy in the original sequence by eliminating zeros and encoding the zero eliminational factor, and selects the best coding bits by calculating the compressed data length to shorten the length of the final coding. Assume that the data sequence collected by the sink node in an upload cycle can be expressed as *D*.

$$D = \begin{bmatrix} d\_1 \\ d\_2 \\ \vdots \\ \vdots \\ d\_m \end{bmatrix} = \begin{bmatrix} v\_{11} & \cdots & v\_{1n} \\ \vdots & \ddots & \vdots \\ v\_{m1} & \cdots & v\_{mn} \end{bmatrix} \tag{8}$$

where, *m* is the acquisition times of sensor terminal in an upload cycle; *di* is the data sequence value collected for the *i*-th time; *n* is the number of measurement parameters; *vjk* is the value of the *k*-th measurement parameter in the *j*-th data acquisition. There are *N* data that appear only once, and the minimum data is recorded as α, the maximum difference between adjacent data is recorded as *β*. When in data compression, the relevant bit factor {*r*1,*r*2,...,*rm*} of the original data sequence should be recorded at first:

$$r = \begin{cases} 1, \, d\_i = d\_{i-1}, i = 2, 3, \dots, m \\ 0, \, d\_i > d\_{i-1}, i = 2, 3, \dots, m \end{cases} \tag{9}$$

The relevant bit factor records the repetition of sequence adjacent data. By default, the relevant bit factor *r*<sup>1</sup> of *d*<sup>1</sup> = 0. The coding bit factor *Cx* is determined by the minimum binary coding bit factor *C<sup>α</sup>* and the maximum difference binary coding bit factor *C<sup>β</sup>* of the sequence as Expression (10).

$$\mathbb{C}\_{\mathbf{x}} = \begin{cases} \mathbb{C}\_{\mathcal{S}\prime} & \mathbb{C}\_{\mathbf{a}} \le \mathbb{C}\_{\mathcal{S}} \\ \mathbb{C}\_{\gamma\prime} & \mathbb{C}\_{\mathbf{a}} > \mathbb{C}\_{\mathcal{S}} \end{cases} \tag{10}$$

Here, *<sup>C</sup><sup>γ</sup>* <sup>∈</sup> *Cβ*, *C<sup>α</sup>* . Then, the maximum value that can be represented by a set of *Cx* bit codes *dx* = <sup>2</sup>*Cx* − 1. 3-bit binary is used to record the encoded bit information in AOZS algorithm. 000, 001, ... , 111 means using 2, 3, ... , 9 bits binary to coding *di* respectively. The relationship of *Cx* and *di* is shown in Table 1. The relationship of coding length *Lx* and coding bit factor *Cx* is shown as Expression (11).

**Table 1.** The relationship of bit numbers, code, *Cx* and *di*.


$$L\_x = \left( \left[ \frac{a}{d\_x} \right] + N - 1 \right) \mathbf{C}\_x + M + 3 \tag{11}$$

where, [ ] means to take up as an integer. *M* is the numbers of {*d*1, *d*2,..., *dm*}, notes the minimum of *Lx* as *Lmin*, and its corresponding code bit factor *Cx* is the best code numbers, noted as *Coptimal.* Zero elimination operation refers to subtracting an integer value (recorded as zero elimination factor) from all data of the sequence, and finally making all data of the original sequence become 0. The zeroing factor *si* of the *i*-th order is recorded as Expression (12).

$$s\_i = \min\left\{d\_{\min}, d\_{\text{optimal}}\right\} \tag{12}$$

where, *dmin* is the minimum value of sequence.

After the above parameters are determined, the algorithm records the sequence coding information and related information, and uses *Coptimal* bit number binary to encode the all zero elimination factors. The flow chart is shown in Figure 8. Table 2 shows the implementation process of the AOZS algorithm.

**Figure 8.** The flowchart of data compress algorithm.


**Table 2.** An example of AOZS algorithm.

So, the compression code is 100,001001,1111,0100,0110,0010.

Compression Ratio (CR) is used to describe the efficiency of compression.

$$CR = \frac{S\_{CP}}{S\_{OR}}\tag{13}$$

Here, *SCP* is the amount of compressed data, *SOR* is the amount of original data. Obviously, the smaller the CR, the smaller the proportion of the compressed data to the original data, and the better the compression performance.

#### 2.2.2. Software Design of Sensor Nodes and Monitoring Center

Software of each sensor node is simpler than that of sink node. It includes two parts. One is to read the humidity data and temperature data of the DHT11. The other is to drive the Bluetooth module to send these data to the sink node. The flowchart is given in Figure 9. It needs to complete the initialization of MCU peripherals and related modules. Clock initialization is used to set the working frequency of the system. Timer initialization is used to read temperature and humidity sensor data. Because the single line communication mode of DHT11 does not have a standard communication format, it is necessary to use a timer to simulate the communication sequence to realize the reception and transmission of data. Finally, the cc1101 module should be initialized.

**Figure 9.** The flowchart of sensor nodes.

The monitoring center is developed with C++ language, which mainly realizes the following functions: (i) according to the longitude and latitude coordinates obtained from the cloud platform, it can display the location of the mobile carriage (sink node) in real time; (ii) it can display the real-time temperature and humidity in the carriage; (iii) it can dynamically draw the temperature and humidity change line chart; (iv) Using database to manage the collected data, it can save the historical data for data analysis. Figure 10 is the operation flowchart of monitoring center.

**Figure 10.** The operation flowchart of monitoring center.
