Remote monitoring system for livestock environmental information based on LoRa wireless ad hoc network technology

: The environmental quality of livestock houses is key to livestock breeding and directly affects the growth and health of animals. To target the characteristics of long application cycles and large coverage areas for environmental monitoring in large-scale livestock breeding, the current study designed a remote monitoring system to provide livestock environmental information based on LoRa wireless ad hoc network technology. The system consisted of collection terminals, control terminals, LoRa gateways, and Alibaba Elastic Compute Service. It realized real-time collection, wireless transmission, storage of multi-sensor node data, and remote control. The system was not limited by the selected time or region, because data interaction was achieved by accessing cloud servers using GPRS technology. Users could browse and obtain data from computers and a WeChat mini program from any location with network coverage. Additionally, the system used the improved receiver-based auto rate (RBAR) rate-adaptive algorithm in the LoRa wireless communication component. After application on a dairy farm, the results showed that the whole system collected 6140 sets of environmental data from four dairy houses. The packet loss rate was less than 1% within a communication distance of 604 m, and the communication success rate was greater than 99%. The control instructions were real-time and accurate, and the response time was less than 10 s, which met the remote control needs of large farms. The system provided powerful data and technical support for precision animal production.


Introduction 
China is a very active country in animal husbandry, and the development of this area is extremely important for the national economy [1] . According to the National Bureau of Statistics, the total output value of China's animal husbandry reached RMB 3046.117 billion Yuan in 2016, surpassing RMB 3 trillion Yuan for the first time. With the support of agricultural modernization technology in the future, the total output value will exceed 3.2 trillion Yuan in 2024 [2] .
However, an unsuitable breeding environment reduces economic benefits. For example, in northeastern China, the temperature is below 0°C for up to four or five months each year, and the relative humidity in barns is usually well above 70%. Such a low-temperature and high-humidity environment easily lead to a decrease in milk yields and dairy product quality [3] . And the mortality rate of calves is approximately 10%-25%, and the economic loss is as high as 9-15 billion Yuan in this case [2,4] . Therefore, how to quickly and accurately obtain environmental data and realize automatic control of the livestock environment has become an important research topic [5] .
The environmental monitoring of livestock houses is a complex and large project. With the development of information technology, the Internet of Things (IoT) technology has been effectively applied in this field [6][7][8] . Long et al. [9] designed a mobile intelligent detection platform based on Wi-Fi technology, which can realize environmental monitoring of livestock and poultry farms using a wireless positioning system and integrated sensors. Zhu et al. [10] adopted ZigBee technology to realize automatic control and remote real-time monitoring in nursery pig houses. Wang et al. [11] designed a comprehensive system for pig house environmental monitoring based on a wireless mesh network, which provided effective data for studying the changing rules of environmental parameters in pig houses. Although Wi-Fi and Zigbee technologies have been used in barn environmental monitoring systems, Wi-Fi technology has high power consumption and is relatively more suitable for the needs of mobile office users and the home field; Zigbee is ideal for long-range, wide-area sensor networking, but its cost is hardly less than $10 and is not suitable for cost-sensitive application such as dairy barns where a large number of nodes are required.
At present, the low-power wireless wide area network (WWAN) technology represented by LoRa has been widely used [12][13][14][15] . Compared with traditional wireless communication technology, LoRa technologies can connect and control a large number of sensors and have the advantages of low power consumption and low cost [16] . Sadowski et al. [17] applied three wireless technologies, Wi-Fi, ZigBee, and LoRa, to agricultural monitoring systems. The test results showed that when the equipment cost, power consumption, and network transmission performance were given priority, LoRa nodes were the most ideal choices for agricultural applications. In addition, Badreddine et al. [18] confirmed the potential of LoRa for smart livestock applications. According to the above literature and technological analysis, the current environmental monitoring of livestock barns faces the following problems: First, environmental monitoring requires the collection of more parameters, which can fully and accurately display situations. Second, the automatic regulation of fans and other control equipment cannot be separated from the stability of the control system. Third, the monitoring system should add a visual component to facilitate real-time viewing of data and equipment statuses.
Finally, with the continuous increase in the volume of input data, data transmission accuracy in the communication system should be ensured to reduce data transmission time and thus improve throughput.
In response to the above problems, the study developed a remote monitoring system for livestock environmental information based on LoRa wireless ad hoc network technology. This system has a detailed design including collection terminals, control terminals, LoRa gateways, and an Alibaba Elastic Compute Service (ECS). A star network topology with one master node and many slave nodes was used between the LoRa gateway and terminal nodes (collection and control terminals). Each collection terminal periodically gathers several types of environmental data for the LoRa gateway and then uploads the data to the server through the General packet radio service (GPRS) network for storage and visualization processing. Meanwhile, the LoRa gateway receives instruction information from the ECS and manages the corresponding control terminals in an orderly manner. In addition, the improved rate-adaptive algorithm was adopted in the LoRa communication process to ensure reliable packet transmission by adjusting the data transmission rate. And finally, the current study developed a WSN system with strong robustness and better network performance.

Overall design
The system in this study is composed of a perception layer, a network layer, and an application layer. The overall architecture is shown in Figure 1. 1) Perception layer: A collection terminal and control terminal are designed separately in this layer. Multiple environmental parameters (temperature, relative humidity, NH 3 , CO 2 , PM 2.5 , and illumination) can be obtained by the collection terminal and uploaded to the network layer through the LoRa module. The control terminal receives the control instructions issued by the network layer through the LoRa module and realizes the remote management of fans and other related control equipment in the livestock houses.
2) Network layer: This layer includes a LoRa gateway and a CoolMay touch screen. The LoRa gateway is the core of the network layer. Environmental data are displayed on the CoolMay touch screen, and a GPRS network is used to establish communication with the cloud network platform.
TCP/IP technology is used to package and process environmental data and upload it to the ECS.
3) Application layer: This layer is composed of the ECS, a visualization platform, and an intermediate link. The visualization platform includes a computer and a WeChat mini program, which send data requests to the ECS through the HTTP and HTTPS protocols, respectively.
The server feeds back response information after receiving each request. The collection terminal design is mainly divided into three parts: a main control board, a LoRa communication module, and a power module. STM32F407 (MCU) is the main control chip of the main control board. The MCU has a timer, a DMA controller, USART, several 12-bit fast ADCs, and RTS and GPIO modules.
The core is based on Corin-M4 with ultralow power consumption, which can minimize power loss and meet the functional requirements of the collection terminal. The MCU collects environmental parameters by serial communication and stores the data in its internal memory. When the LoRa module receives a data collection instruction, the MCU directly accesses its memory to complete data uploading in real-time through the DMA controller.
After the task is completed, the MCU enters sleep mode.
The LoRa communication module uses the E32-433T20DC, which is embedded with a high-speed, low-power MCU, and a high-performance radio-frequency (RF) chip (SX1278). The transmission current is 110 mA, and the receiving current is only 14 mA. The module contains four working modes: a general mode, wake mode, power saving mode, and deep sleep mode. As shown in Figure 2, U2 is the LoRa communication module, M0, M1, RXD, TXD, and AUX are common pins for the LoRa communication module, and S0, S1, USART2_TX, USART2_RX, PD5, and PD6 are common pins for MCU. Pins M0 and M1 in U2 are connected to the PE0 and PE1 interfaces of the MCU, and they cooperate to determine the working mode of the module. To save transmission power, this system adopts the general mode and sleep mode. USART2_TX and USART2_RX represent the input and output of the TTL serial port of the LoRa module, respectively. The PD5 output pin and PD6 input pin of the MCU are connected to realize data transmission. AUX is used for self-check and wireless transceiver buffer indications. When the module is powered on, AUX outputs "low level" immediately and conducts a hardware self-check. After completion, AUX outputs "high level" and begins normal operations according to the working mode output by M0 and M1. LED1 light flashing indicates that the LoRa module has started to work, and LED2 light flashing indicates that the LoRa module is in the general mode of sending/receiving data. The MCU controls SX1278 through an interrupt mode and enters sleep mode as soon as it completes the transmission process. The terminal is equipped with an antenna in the 433M band with a gain of 2.5 dBi to increase the communication distance of the system. Note: U2 is the LoRa communication module; M0, M1, RXD, TXD, and AUX are common pins for the LoRa communication module; S0, S1, USART2_TX, and USART2_RX are common pins for MCU; R1 and R2 are resistors; D1 and D2 are common symbols for LEDs. The power module adopts an AMS1117-3.3 step-down regulator to stabilize the voltage from 5 V to 3.3 V, thereby supplying power to the main control board and LoRa module. The regulator adjusts the reference voltage to within a 1.5% error rate and provides an output current of 1 A, minimizing the pressure incurred by overloading the regulator and power supply circuits. The collection terminal is powered by a 2000 mA battery. If collection is performed every 20 min, the ideal service time of the battery will be 3 years.
The hardware of the collection terminal is shown in Figure 3. To enable the terminal to work for long periods in the harsh environment of livestock houses, the equipment is encapsulated. The sensor node adopts the standardized interface and plug-and-pull mode. The sensors used in the collection terminal all have high sensitivity, high stability, and strong anti-interference ability, which can meet the requirements of environmental measurement in livestock houses. The parameters of each sensor are shown in Table 1. When it is required to replace a failed sensor, users can choose different types and numbers of sensors for installation to meet personalized needs.

Hardware design of the control terminal
The control terminal is also divided into the main control board, LoRa module, and power module. The main control board circuit uses an STM32F103, which has a smaller volume and better performance than those of an 8-bit microcontroller. As shown in Figure 4, the microcontroller triggers triode S8050 to drive the relay to complete the opening or closing of the connected fans and other control equipment. The resistor (R5) between the MCU and S8050 is 10 K/0.25 W, which ensures that the base current is at the MA level to realize the conduction and closure of the triode. The relay adopts an Omron G4A-1A-PE, and the maximum load current of the contact is 20 A. The two ends of the coil are reverse parallel, eliminating the harmful influence of induced electromotive forces. In addition, a fuse is added between the relay and the output end to prevent excessive current and protect the circuit.
Note: K1 is the relay switch; D17 is the diode; C8 is the capacitor; R5 and R17 are 10K resistors; NPN is the triode. The LoRa gateway is the protocol conversion center of the whole monitoring system. It includes the design of the main control board, LoRa module, network communication module, touch screen, and power module. Figure 6 shows the physical diagram of the LoRa gateway. The gateway uses STM32F407 as the MCU. It communicates with the LoRa module, network communication module M26, and CoolMay touch screen through serial ports USART1, USART2, and USART4, respectively. LED1 light flashing is used to indicate that the CPU is working normally. The power module adopts an LRS-75-12 power supply to provide a stable and reliable 12 V DC voltage to the main control board, and its working efficiency can reach 90%.
Note: M26 is the network communication module for the LoRa gateway; RS485 is the communication interface CoolMay touch screen.  In addition, M26 provides an RF welding plate interface for connection with a GSM external antenna interface with 50 Ω impedance features. The antenna gain is 3 dBi, which can increase the communication distance.

Software design
The software design of the system includes the collection terminal design, control terminal design, and LoRa gateway design. All microprocessors use Cubemx to build STM32 projects and generate code quickly for resource allocation. The integrated compilation environment is Keil5, and it is connected to SCM via an ST-LINK/V2 emulator. After compilation, the program was downloaded, simulated, and written to microprocessors to complete the development process.

Software design of the collection terminal
The design of collection terminal software mainly includes network processing tasks and data processing tasks. A network processing task uploads the network access request and network access information provided by the collection terminal. A data processing task realizes packet parsing and data information uploading.
The flow chart of the collection terminal program is shown in Figure 8. First, the LoRa gateway is turned on to receive the signal, and a random-access time delay is generated. Then, an access request is sent to the LoRa gateway according to the communication protocol of the terminal node access request in Table 2. The access interface is called and waited for. After successfully joining the network, the terminal node can communicate with the LoRa gateway and analyze the wireless packets sent by the LoRa gateway to determine whether they are data collection instructions. If so, data collection is carried out. Then, according to the packet protocol in Table 3, the environmental data are packaged and sent to the LoRa gateway on time. Otherwise, the network continues to wait for the LoRa gateway to send the packet.   The communication protocol of the terminal node access request is shown in Table 2. The 1st byte represents the frame header, with 0x3c as the identifier. The 2nd byte represents the length, with a maximum of 126 bytes. The 3rd byte represents the network type, and the character "A" represents the network access request. The 4th and 5th bytes represent the network identifier (NI), which is used for network differentiation. Only two LoRa modules with the same NI can form a network communication link. The 6th and 7th bytes represent the device address and the unique device address identifier, respectively. Finally, the reliability of data transmission is guaranteed by the redundancy check of CRC8. The packet protocol is shown in Table 3. The 1st byte serves as the frame header of the packet and is represented by the character "P". The 4th to 21st bytes are packets, in which each packet header is represented by 0x21. The length represents the packet length. Device type 0x00 represents the collection terminal, and 0x01 represents the control terminal. Data type 0x00 represents the sent and received data, and 0x01 represents the control command. Each sensor value is represented by 2 bytes.
The system collects 6 kinds of environmental parameters (temperature, relative humidity, NH 3 , CO 2 , PM 2.5 , and illumination), occupying 12 bytes. The 22nd byte uses CRC8 for packet verification.

Software design of the control terminal
The control component mainly operates on the control equipment through the visualization platform. The flow chart of the control terminal program is shown in Figure 9. After receiving a request command sent by the visualization platform, the cloud server sends the control command to the LoRa gateway. The LoRa gateway sends instructions to the LoRa module of the control terminal to trigger the relay and conduct remote control. Meanwhile, the LoRa gateway returns the results of the executed instructions to the cloud server and writes them to the database. The software design of the LoRa gateway is divided into data uploading and command issuing. The workflow is shown in Figure 10. The timer frequency division coefficient is configured and interrupted to determine whether the terminal node has timed out. Then, the LoRa module opens the wireless serial port to conduct a network search and waits for the terminal node to enter the network. When receiving a network access request from the terminal node, the data parsing interface is called to parse the network access response protocol according to Table 3, and wait for the environment data collection command. When the maximum data collection time is reached, the LoRa module enters the data receiving mode and uploads the environmental data to the server through the GPRS network.
Command issuing: The LoRa gateway establishes a TCP/IP protocol with the cloud server. When the handshake is successful, the gateway waits for the server to send a data packet. After receipt, the data packet is analyzed. When the control command is obtained, it is sent to the corresponding control terminal through the LoRa module.
As shown in Table 4, the access response communication protocol in the LoRa gateway corresponds to the access request of the terminal node in Table 1. The 3rd byte is the network type (represented by "R"), which denotes successful network access and automatically assigns a node number to the terminal node. The LoRa gateway resolves the terminal node's network type, network identifier, and device address to complete the network access request. This part of the design realizes the conversion of the LoRa-Internet network protocol so that the LoRa gateway achieves real-time processing and stability.
Compared with the wireless mesh networking mode, the star ad hoc networking mode used in the current study not only reduces the power consumption and hardware scale of terminal nodes but also reduces the load of wireless communication and increases the application scope of environmental data collection and control.

Design of the elastic cloud server
The overall framework of the server is shown in Figure 11. The server is developed using the Java language and built by the SpringBoot framework. It mainly includes the following five points: 1) The LoRa gateway encapsulates the collected data and control terminal state information into a JSON file and then uploads it to the server through the TCP/IP protocol.
2) The various types of the information uploaded by the LoRa gateway are captured by the front-end communication controller of the server (DispatcherServlet) and processed by the relevant components of the Spring MVC. The Spring MVC saves the terminal data to the Mysql database.
3) The access page of the visualization platform is developed using HTML, CSS, and JAVAScript (JS), and it sends functional operation requests to the server through the HTTP and HTTPS protocols combined with a MySQL database and Redis server. It executes add, delete, query, and modify commands, updates the collection frequency of the LoRa gateway and switch states of the control terminal, and provides feedback to the Spring MVC.
4) The Spring MVC returns object values to the DispatcherServlet. These values are encapsulated as JSON files and returned to the gateway. Additionally, the returned results are rendered and displayed on the visualization platform. 5) After the LoRa gateway receives the return file, the collection frequency of the collection terminal and the switch state of the control terminal are reset. Figure 11 Overall framework of the elastic cloud server Figures 12a and 12b are the data trend analysis page on the computer, and the control device management page on the WeChat mini program respectively. The design achieves comprehensive but user-friendly, to enable field workers and researchers are able to understand the values behind the data in a short time, and ensure that environmental control of the livestock houses can rely on "numbers".

Improved RBAR rate-adaptive algorithm
Although the LoRa spread spectrum modulation technology used in this system increased the communication distance, when the number of terminal nodes continues to increase, the amount of data becomes increasingly larger. Each terminal node will use the same transmission rate, which will be close to that of LoRa. Thus, the transmission rate of the gateway terminal node will be too low, and the power consumption will be too high [19] .
The current study designs an improved RBAR rate-adaptive algorithm based on the private protocol of LoRa communication. This algorithm uses the characteristics of LoRa technology, such as multiple spread factors (SFs) and data rates (DRs), to automatically assign different SFs and DRs to terminal nodes within different distances from the LoRa gateway [20] . Meanwhile, the DR of each node is adjusted according to the actual packet loss rate (PLR) and channel load (CL). The process of the improved RBAR algorithm is shown in Figure 13. The steps are as follows: 1) First, the terminal node sends a request to send (RTS) to the LoRa gateway to confirm whether the channel is free.
2) Second, after the LoRa gateway receives the RTS, the effective signal-to-noise ratios (SNRs) in the previous five times transmissions are averaged to the SNR .
The calculation equation of the SNR value selected the first time is as follows: where, BW is the channel bandwidth, kHz. And an appropriate BW can effectively improve the data rate to shorten the transmission time; CR is the coding rate for forward error detection and correction.
After evaluating and weighing the LoRa modulation and demodulation calculations, the BW is set to 250 kHz and the CR is chosen as 1. The LoRa gateway adds the calculated DR to the FCS field of the clear to send (CTS). Feedback information is transmitted to the terminal node, and data continue to be transmitted at this DR while pausing the transmission of the RTS/CTS. 4) After the terminal node sends the data packet, the LoRa gateway feeds back the confirmation frame ACK, indicating that the data transmission was successful. At this point, the terminal node calculates the PLR according to the continuity of the frame sequence number. If the terminal node determines that the PLR reaches the set packet loss threshold within a certain period, RTS/CTS is restarted for channel estimation, and the DR is updated according to Step 2). If the PLR has not reached the packet loss threshold, then Step 5) is performed to calculate the CL of the SF subchannels.
5) The CL is used to calculate the occupancy time of each frame of data in the SF subchannel and to accumulate the total occupancy time T all_data [i] where, n preamble represents the set length of the leading code, which is obtained by reading the registers RegPreambleMsb and RegPreambleLsb. T s [k] is the period of a single LoRa symbol packet, and n is the total transmission times of packets within a statistical period.
The calculation equation of T payload [k] for the packet transmission time is as follows: where, payloadSymNb[k] is the payload time. Its value depends on the header mode used, which can be calculated by Equation (7) where, PL represents the number of bytes of the payload. H indicates whether to use the header and H=1. DE indicates that the speed is optimal at this time and DE=1. Therefore, T data [k] of 1 and n LoRa module iterations in the statistical cycle can be calculated by Equations (8) and (9). 7) The terminal node receives the normal rate adjustment command issued by the LoRa gateway and continues to complete the data transmission process.
Note: RTS/CTS is the request to send/clear to send protocol of the LoRa communication module; SNR is signal-to-noise ratio; DR is data rate; ACK is the confirmation frame of the LoRa communication module; CL is the channel load. Figure 13 Improved RBAR rate adaptive algorithm flow chart In Step 5), the transmission mode of the RTS/CTS frame format is modified. The LoRa gateway places the rate information in the FCS field of the CTS and returns it to the terminal node. The terminal node performs data transmission at this rate.

Results and discussion
This system was tested at the Northeast Agricultural University A-Cheng Holstein experimental dairy cow base from December 20, 2019, to March 15, 2020.

Object of the experiment
As shown in Figure 14, four cow houses (lactating house 1, lactating house 2, calf house 1, calf house 2) were selected in this experiment. A collection terminal was placed in the center of each cow house, namely, C 1 -C 4 . LoRa gateway G1 was placed at the front door of lactation house 1. All collection terminals were approximately 2 m above the ground. The 4 cowsheds were 122 m long, 30 m in span, and 5.6 m in eave height. There were sliding windows on the north and south sides. All windows were closed during winter. The lactation house was divided into four rows, with each row containing 110 beds. Each row of calves was divided into 8 equally sized areas, which were separately fenced. A walkway was set in the middle of each shack, and feeding troughs and drinking fountains were placed on both sides of the walkway. A fan was installed above the two sides of the aisle every 6 m. The external dimensions of the fan were 800 mm× 800 mm×300 mm, the operating power was 0.35 kW, and the installation and vertical directions of the fan were offset by 17°-25°. The suspension height from the ground to the bottom of the fan was 2.2 m. At a position 6 m away from the fan, the measured wind speed was 2.3 m/s. The cows were milked in the milking hall twice a day meanwhile the dung in the lactation house was forklifted. The lactation and calf houses were treated by daily dung cleaning. The numbers of cows in C 1 -C 4 were 368, 379, 275, and 281, respectively. According to the measurements, the linear distances between the collection terminals and LoRa of the gateway were approximately 63 m, 128 m, 562 m, and 604 m.
Note: C1-C4 are the collection terminals; G1 is the LoRa gateway Figure 14 Physical drawing of cowshed distribution and equipment installation

Data collection results
In this experiment, data were uploaded every 20 min on the computer side of the visualization platform. A total of 6140 groups of data (87 d) were collected during the experiment, as shown in Figure 15 and Figure 16.

Data collection results of the lactation house
For adult Holstein cows, the optimal temperature and relative humidity are 4°C-22°C and 40%-70%, respectively [21] . However, Figure 15 shows that the highest and lowest recorded temperatures were 14.7°C and −0.9°C, respectively, and most of the temperatures were between 0°C and 14°C. The average relative humidity was above 70%. Xiong et al. [22] found that low-temperature and high-humidity environment make the fur of cows very wet, and the thermal capacity of the environment is greatly reduced. After March, the relative humidity in the lactation houses decreased with increasing ventilation. The concentration of NH 3 was mostly lower than 7 mg/m 3 , which was related to the hay laid on the ground. Furthermore, the farm workers cleaned the manure, feed, and other waste from the cowshed twice a day (in the morning and evening), which also helped reduce the decomposition and volatilization of NH 3 . The CO 2 content of an environment can reflect its ventilation conditions and air pollution degree, and it is an indirect index for evaluating the condition of the air in lactation houses. The concentration of CO 2 in the two houses was usually higher than 2694 mg/m 3 at night [23] , especially in lactation house 1, and the CO 2 even rose to 4490 mg/m 3 [24,25] . Jentsch et al. [26] found that a high concentration of CO 2 would lead to chronic hypoxia and slow weight gain in dairy cows, which allows easy infection with various diseases. Dust concentrations and illumination also impact the health of dairy cows [27,28] . The daily average dust concentrations in the two houses were between 112-421 µg/m 3 , which meets the imposed standard. The data show that the illumination in the houses was mostly between 0-320 lx. The illumination was more intense at noon, reaching above 400 lx. According to the calculation, the daylight duration was less than 10 h in the daytime. Li et al. [28] found that appropriately increasing the daylight duration is beneficial to milk yields. Therefore, a combination of natural lighting and artificial lighting should be adopted in the cowshed to ensure that the daylight duration experienced by cows is between 14-16 h. 3.2.2 Data collection results of the calf house Too low of a temperature in winter will weaken the resistance of calves [29,30] ; thus, shed film was hung on the front and rear walls of the calf houses. The doors and windows were closed for windproofing and cold protection. As shown in Figure 16, the calf houses temperature ranged from 0°C to 12°C, which was much lower than the comfortable temperature range for calves (13°C to 25°C) [31] . The relative humidity was as high as 80%-100%, which was more than 20% greater than the appropriate relative humidity (50%-70%) for calf houses [21] . Compared with the lactation house, straw was used as the bedding material in the calf house. The bedding material was replaced and cleaned every 2-3 d, and accordingly, the NH 3 concentration in the house was lower than 2 mg/m 3 . However, due to insufficient ventilation, the average CO 2 concentration was above 2694 mg/m 3 . The PM 2.5 value ranged from 368 to 659 µg/m 3 , with the highest value reaching 1127 µg/m 3 . These values were much higher than those in the lactation house. Hillman et al. [32] found that long-term exposure to high dust concentrations is likely to cause respiratory damage and digestive disorders in calves. The illumination was mostly maintained in the range of 75-225 lx, which was slightly different from that in the lactation house. As shown in Figure 17, during the experiment, the average SNRs of the collection terminal were −5.3 dB, −7.6 dB, −11.2 dB, and −13.5 dB, and the PLRs were 0.26%, 0.41%, 0.77%, and 0.89%, respectively. Due to the continuous attenuation of electromagnetic waves during the transmission process, the greater the distance between the collection terminal and the LoRa gateway, the worse the signal and the higher the PLR. However, the PLR was less than 1.0%, and the success rate was more than 99% in this system. The experimental results provide a basis for actual node layouts.
a. Comparison chart of SNR b. Comparison chart of PLR Note: PLR is the packet loss rate. Figure 17 Comparison chart of SNR and PLR

System control performance analysis
To verify the stability of the control terminal performance, an experiment was carried out on the air circuit in lactation house 1 from March 3 to 5, 2020. As shown in Figure 18. The control terminals F 1 -F 3 completed the orderly control of the positive-pressure fans on the west wall, negative-pressure fans on the east wall, and relay fans in the barn. When the humidity in the house was greater than 70%, the fans were turned on. When the humidity was less than 50%, the fans were turned off.  Figure 18 Ventilation system controller structural drawing The results showed that the relative humidity and NH 3 , CO 2 , and PM 2.5 concentrations in the house were reduced after the fans were turned on. Most of the relative humidity values were within the range of 44.4%-75.2%. However, turning on the ventilation equipment will also reduce the temperature. Sé bastien et al. [33] found that the cowshed environment is a microclimate system formed by the interactions of various environmental parameters, and there was a strong coupling and nonlinear relationship among the environmental parameters. It is insufficient to adjust the relative humidity content in the house. Only by dealing with the relations between the temperature, relative humidity, concentrations of harmful gases, and ventilation in winter can the needs of cattle production be met to obtain better economic benefits.
In addition, the response time and success rate of the fans and other control equipment are indicators for measuring the response performance of the control terminal. To test whether the response time of the control terminal met the requirements, the actual value of the response time was compared with the theoretical value calculated according to the pulse frequency and pulse number in the program. The results showed that the fans received opening and closing instructions seven times during the test. The response times with respect to the instructions are shown in Table 5. The response time for most actual instructions was less than 10 s, and the actual response time was slightly longer than the theoretical response time. This may be because the actual response time included both the data processing time and mechanical device response time delays, but the time differences were within the acceptable error range.

Conclusions
The current study proposed a remote monitoring system for livestock environments based on LoRa wireless ad hoc network technology. The system test results showed that the collection terminals, control terminals, LoRa gateway, and ECS were interconnected through LoRa WWAN and GPRS technology, forming a one-stop service system for dairy cow houses environmental information collection, transmission, calculation, processing, and visualization platform. The improved RBAR rate-adaptive algorithm improved network throughput and optimized the system performance.
The above conclusions showed that the proposed system was stable and reliable, and provided data support and references for precise environmental monitoring in animal husbandry.