<p>This article introduces in detail the design ideas, implementation status and defect reflections of the SmartFarming Proj IoT architecture of XJTLU University’s 2019 summer scientific research.</p>
<h2id="1-Background"><ahref="#1-Background"class="headerlink"title="1 Background"></a>1 Background</h2><h3id="1-1-Requirements"><ahref="#1-1-Requirements"class="headerlink"title="1.1 Requirements"></a>1.1 Requirements</h3><p>In order to explore the feasibility of agricultural Internet of Things technology, instructor Alam Mohammed hopes that we can provide a <strong>Available</strong> and <strong>Reliable</strong> Internet of Things system to realize intelligent monitoring and management of farmland. This system will be able to timely detect the parameters related to plant health in the soil and the air, process and store these data (proposed by Alam). At the same time, the automatic control irrigation function (proposed by Caleb) and some data display methods (optional requirements proposed by Alam) are also expected to be provided by this system.</p>
<h3id="1-2-Positioning"><ahref="#1-2-Positioning"class="headerlink"title="1.2 Positioning"></a>1.2 Positioning</h3><p>As it is a small scientific research project designed for the agricultural Internet of Things, the goal of the project is to explore the feasibility of the agricultural Internet of Things, and the vision is to simulate the most common small-scale agricultural Internet of Things application scenarios. Therefore, the positioning of this architecture is <strong>applicable to most small agricultural IoT projects with sensing, control and display functions</strong>.</p>
<li><code>MQTT</code>: MQTT Message Queuing Telemetry Transport is a message protocol based on the publish/subscribe paradigm under the ISO standard (ISO/IEC PRF 20922). It works on the TCP/IP protocol suite and is a publish/subscribe message protocol designed for remote devices with low hardware performance and poor network conditions [1].</li>
</ul>
</blockquote>
<blockquote>
<ul>
<li><code>LoRa</code>: LoRa is an emerging technology in the current market, which operates in the public frequency band below 1 GHz for long-distance low-power communication [2].</li>
</ul>
</blockquote>
<h2id="3-Design-goals"><ahref="#3-Design-goals"class="headerlink"title="3 Design goals"></a>3 Design goals</h2><h3id="3-1-Implementing-functions"><ahref="#3-1-Implementing-functions"class="headerlink"title="3.1 Implementing functions"></a>3.1 Implementing functions</h3><p>The functional requirements of this architecture mainly include land data collection, weather data collection, irrigation system control, interaction with the drone group system, web console (can view real-time and historical sensor data, index data; be able to control the system).</p>
<h2id="4-System-Environment"><ahref="#4-System-Environment"class="headerlink"title="4 System Environment"></a>4 System Environment</h2><h3id="4-1-Related-software-and-hardware"><ahref="#4-1-Related-software-and-hardware"class="headerlink"title="4.1 Related software and hardware"></a>4.1 Related software and hardware</h3>
<td><ahref="https://detail.tmall.com/item.htm?spm=a230r.1.14.20.161f53ed47xg5U&id=41414863412&ns=1&abbucket=10"target="_blank"rel="noopener">LM35DZ temperature sensor</a></td>
<td><ahref="https://detail.tmall.com/item.htm?spm=a1z10.3-bs.w4011-21581912015.52.23e05535hDDYEB&id=13302081252&rn=ced14f01f4a9179e7b090c544cf5836a&abbucket=14"target="_blank"rel="noopener">MQ-135 air quality sensor </a></td>
<td><ahref="https://detail.tmall.com/item.htm?spm=a230r.1.14.9.791b1000Beu0tp&id=555610012670&cm_id=140105335569ed55e27b&abbucket=14"target="_blank"rel="noopener">DC frequency conversion water pump</a></td>
<h3id="4-2-Data-size-estimation"><ahref="#4-2-Data-size-estimation"class="headerlink"title="4.2 Data size estimation"></a>4.2 Data size estimation</h3><p>According to the production scenario, a data set is generated every 15 minutes, and a data set occupies about 253-291 bytes. According to 291 calculation, 27.93KB of data will be added every day.</p>
<li>Land node RAM resources are scarce, only 2K</li>
<li>LoRa communication adopts 433MHz, single-channel transceiving, much interference</li>
<li>The server needs to collect and aggregate hash data from multiple nodes</li>
<li>Reserve a method of interaction with the drone AI team</li>
<li>Push real-time messages to the front-end browser</li>
</ul>
<h3id="5-3-Scheme-selection"><ahref="#5-3-Scheme-selection"class="headerlink"title="5.3 Scheme selection"></a>5.3 Scheme selection</h3><p>In order to <strong>prioritize</strong> to ensure the <strong>stability</strong> and <strong>scalability</strong> of communication and services, we select <strong>layered model</strong> as the design mode of the architecture. The entire system is divided into multiple levels, with necessary interactions between levels. Each level runs independently, restarts quickly when it crashes, and has a high degree of reliability.</p>
<p>In terms of safety, the method of <strong>internal and external network isolation</strong> is adopted. Taking into account the scarcity of computing resources of some devices in the intranet, the interaction with the intranet server adopts an unencrypted communication mode. The intranet server interacts with the cloud server through SSL encryption. To ensure the security of the client’s data, the cloud server API only accepts HTTP and WebSocket requests with SSL encryption.</p>
<h2id="6-Architecture-Design"><ahref="#6-Architecture-Design"class="headerlink"title="6 Architecture Design"></a>6 Architecture Design</h2><h3id="6-1-Basic-introduction"><ahref="#6-1-Basic-introduction"class="headerlink"title="6.1 Basic introduction"></a>6.1 Basic introduction</h3><p>The main purpose of this architecture is to provide a <strong>stable (Available)</strong> and <strong>reliable</strong> Internet of Things system, which collects real-time data in farmland, combines pictures collected by drones and judged by CNN Plant health, realize intelligent management of irrigation system and plant health. This framework is theoretically suitable for most farm environments. The test environment is within a radius of 2~5km (depending on the antenna), and the wind is below level 3. This system acquires farmland data through sensors, controls the irrigation system through transistor circuits, relays, etc., and interacts with managers through web pages and APIs.</p>
<h3id="6-2-System-hierarchy-diagram"><ahref="#6-2-System-hierarchy-diagram"class="headerlink"title="6.2 System hierarchy diagram"></a>6.2 System hierarchy diagram</h3><p><imgsrc="https://api.yimian.xyz/img/?path=imgbed/img_c46f5639_577x906_8_null_normal.jpeg"alt="Smart Farm system hierarchical structure diagram"></p>
<p>This figure shows the abstract hierarchy of this architecture. As pointed out, this layered architecture can be clearly defined as the <strong>perception layer</strong>, <strong>network layer</strong>, <strong>application layer</strong>, three horizontal operation layers, and a vertical <strong>error handling layer</strong>.</p>
<p>Among them, the <strong>perception layer</strong> can be further refined into <strong>environment layer</strong> and <strong>hardware layer</strong>. The environment layer contains plants, soil, air, water and other natural objects that we want to detect and control. The hardware layer provides the underlying technology for the system’s interactive behavior (detection, control) of natural elements through artificial electrical devices such as sensors and controllers. Sex. When the perception layer realizes the interaction of natural information, the corresponding digital information is transmitted to the network layer for communication. Correspondingly, the perception layer will also receive corresponding control information from the network layer, and make corresponding control actions according to these instructions.</p>
<p><strong>Network layer</strong> is divided into three layers. The lowest layer is the <strong>preprocessing layer</strong>. Its main task is to package the data collected by the perception layer and transform it into a form suitable for network circulation, such as sockets. Therefore, the pre-processing layer requires a small amount of computing storage resources, such as a single-chip microcomputer, EEPROM, etc. After encapsulation by the preprocessing layer, the data is transmitted to the <strong>physical layer</strong>. Based on devices such as gateways and routers in the physical layer, these data packets can be distributed and transmitted in an orderly manner. The application of the MQTT protocol in the <strong>communication layer</strong> makes the management of these data packet distribution more efficient and convenient. In addition, the implementation of the QoS service quality detection system in the communication layer also makes the performance of the network layer more fault-tolerant and reliable.</p>
<p><strong>Application layer</strong> is mainly responsible for some application-level services for business logic, such as data analysis, event processing, etc. At the bottom of the application layer, the <strong>service layer</strong> is running for a long time. The service layer is the closest part of the application layer to the network layer. Its main purpose is to process the incoming data from the network layer in real time and respond quickly to corresponding requests and error events. At the same time, the service layer is also responsible for sorting and analyzing the data collected by the sensors and passing it to the <strong>database</strong> layer for storage. In order to ensure the reliability of the service, the implementation program of the service layer is registered as a system service and guarded by a special daemon. Unlike the long-term operation of the service layer, the <strong>business layer</strong> is invoked on demand. When a new request comes in from the interface layer or a specific event occurs in the service layer, the business layer will be called and processed. In addition to some basic data analysis, the business layer also has the authority to make analysis requests to the AI layer. <strong>AI layer</strong> After analyzing the image data returned from the drone, it will send back a judgment result to the business layer. According to the specific content of the request, the business layer will organize the image recognition results, historical sensor data, etc., and pass them to the interface layer. <strong>Interface layer</strong> is mainly used to provide different forms of interfaces, such as HTTP interface and WebSocket interface, to facilitate client calls.</p>
<h3id="6-3-System-Topology-Diagram"><ahref="#6-3-System-Topology-Diagram"class="headerlink"title="6.3 System Topology Diagram"></a>6.3 System Topology Diagram</h3><p><imgsrc="https://api.yimian.xyz/img/?path=imgbed/img_27df1756_1426x710_8_null_normal.png"alt="Smart Farm system topology diagram"></p>
<p>From the topological structure, as shown in the figure above, the entire system can be physically divided into land nodes, weather nodes, LoRa gateways, intranet servers, cloud servers, databases and user terminals.</p>
<p>From the perspective of functional mechanism, the entire system includes: sensor module, LoRa communication module, LoRa gateway module, MQTT-Proxy proxy module, water pump control module, MQTT+ data processing module, web interface module, daemon module and database module.</p>
<p><strong><em>Land Node</em></strong><br>Land nodes are deployed in farmland in different regions to detect soil temperature and humidity parameters, and can provide control of water flow solenoid valves at corresponding locations. The land node uses Arduino Nano as the computing unit, uses LoRa as the communication method to connect to the LoRa gateway (and other nodes), and uses battery power. Its large number, harsh working environment and unstable wireless communication are the biggest source of uncertainty in the system.</p>
<p><strong><em>Weather Node</em></strong><br>Meteorological nodes are deployed at specific locations in farmland to detect meteorological parameters such as air temperature, humidity, atmospheric pressure, rainfall, light, NH3 concentration, and CO concentration. The weather node uses Arduino Nano as the computing unit, uses LoRa as the communication method to connect to the LoRa gateway (and the land node), and uses battery or wired power. The working environment is harsh and the wireless communication quality is average.</p>
<p><strong><em>LoRa Gateway</em></strong><br>The LoRa gateway is deployed indoors. In order to expand the effective communication radius, its antenna can be extended outdoors. The LoRa gateway connects to various land nodes and weather nodes through LoRa, and connects to the intranet server through WiFi. The LoRa gateway can convert the socket packet incoming through LoRa into an mqtt message and pass it to the intranet server, and convert the mqtt message from the intranet server into a socket packet and send it to the designated node through LoRa.</p>
<p><strong><em>Intranet server</em></strong><br>The intranet server is a Raspberry Pi 3B, which contains three functional modules: MQTT-Proxy proxy module, water pump control module and daemon process module.</p>
<p><em>MQTT-Proxy proxy module</em> can receive the mqtt message sent by the LoRa gateway via WiFi, encrypt it with SSL and forward it to the cloud server. At the same time, the MQTT-Proxy proxy module will also accept the specified mqtt message from the cloud server, decrypt it and forward it to the LoRa gateway.</p>
<p><em>Water pump control module</em> The state of the water pump is controlled through a relay through the GPIO of the Raspberry Pi. The mqtt communication with the water pump control module directly communicates with the cloud server without passing through the MQTT-Proxy proxy.</p>
<p><em>Daemon module</em> runs on Linux and is used to monitor the MQTT-Proxy proxy module and the water pump control module. When modules are detected to stop running, the daemon module will restart these modules in time to ensure the stability and continuity of services.</p>
<p><strong><em>Cloud Server</em></strong><br>The cloud server runs on Huawei Cloud located in Beijing and includes MQTT+ data processing module, web interface module and daemon module.</p>
<p><em>MQTT+data processing module</em> includes the Service layer and the Business layer in the hierarchical structure. This module communicates with the intranet through mqtt, and interacts with the web interface module (and AI module) through mqtt and redis. After receiving various hashed data from the intranet, this module will automatically aggregate them, organize them into one piece of data, store it in the database, and push it to the web interface module through redis. In addition, this module will periodically send requests to each node to obtain their data, and these requests will be tracked to calculate the delay.</p>
<p><em>web interface module</em> provides a series of HTTP and WebSocket interfaces. These interfaces can be used to get data, send instructions, refresh data, and get push messages.</p>
<p><em>Daemon module</em> runs on Linux and is used to monitor MQTT+ data processing module and web interface module. When modules are detected to stop running, the daemon module will restart these modules in time to ensure the stability and continuity of services.</p>
<h2id="7-Module-description"><ahref="#7-Module-description"class="headerlink"title="7 Module description"></a>7 Module description</h2><h3id="7-1-LoRa-communication-module"><ahref="#7-1-LoRa-communication-module"class="headerlink"title="7.1 LoRa communication module"></a>7.1 LoRa communication module</h3><p>The LoRa communication module is used to realize stable and reliable socket communication in a single channel. This module includes LoRa-socket and LoRa-mqtt.</p>
<p>LoRa-socket encapsulates data packets on the basis of LoRa-Arduino, realizes simple udp and tcp communication, and makes traditional LoRa communication more reliable. By adding the sending and receiving ip address to the head of the data packet, and adding the hash check value at the end of the data packet, LoRa-socket supports point-to-point communication and broadcast communication, and has powerful noise processing capabilities and data tamper resistance.<br><strong><em>Project address</em></strong>: <ahref="https://github.com/IoTcat/LoRa-socket"target="_blank"rel="noopener">iotcat/LoRa-socket</a></p>
<p>LoRa-mqtt is a simple reproduction of the mqtt protocol based on LoRa-socket. Based on udp and tcp, LoRa-mqtt supports mqtt communication with qos 0 and 1.<br><strong><em>Project address</em></strong>: <ahref="https://github.com/IoTcat/LoRa-mqtt"target="_blank"rel="noopener">iotcat/LoRa-mqtt</a></p>
<h3id="7-2-MQTT-Proxy-proxy-module"><ahref="#7-2-MQTT-Proxy-proxy-module"class="headerlink"title="7.2 MQTT-Proxy proxy module"></a>7.2 MQTT-Proxy proxy module</h3><p>This module runs on the intranet server and realizes the encrypted forwarding of the intranet mqtt.<br><strong><em>Project address</em></strong>: <ahref="https://www.npmjs.com/package/sf-mqtt-broker"target="_blank"rel="noopener">iotcat/sf-mqtt-proxy</a></p>
<h3id="7-3-Water-pump-control-module"><ahref="#7-3-Water-pump-control-module"class="headerlink"title="7.3 Water pump control module"></a>7.3 Water pump control module</h3><p>This module runs on the intranet server and realizes the control of the relay water pump through GPIO.<br><strong><em>Project address</em></strong>: <ahref="https://www.npmjs.com/package/sf-pump"target="_blank"rel="noopener">iotcat/sf-pump</a></p>
<h3id="7-4-mqtt-data-processing-module"><ahref="#7-4-mqtt-data-processing-module"class="headerlink"title="7.4 mqtt+data processing module"></a>7.4 mqtt+data processing module</h3><p>This module runs on the cloud server and realizes the data aggregation, processing and storage of the underlying mqtt messages.<br><strong><em>Project address</em></strong>: <ahref="https://www.npmjs.com/package/sf-mqtt-broker"target="_blank"rel="noopener">iotcat/sf-mqtt-broker</a></p>
<h3id="7-5-web-interface-module"><ahref="#7-5-web-interface-module"class="headerlink"title="7.5 web interface module"></a>7.5 web interface module</h3><p>This module runs on the cloud server and realizes the provision of functional interfaces.<br><strong><em>Project address</em></strong>: <ahref="https://www.npmjs.com/package/sf-web-api"target="_blank"rel="noopener">iotcat/sf-web-api</a></p>
<h2id="8-Development-Document"><ahref="#8-Development-Document"class="headerlink"title="8 Development Document"></a>8 Development Document</h2><h3id="8-1-mqtt-theme"><ahref="#8-1-mqtt-theme"class="headerlink"title="8.1 mqtt theme"></a>8.1 mqtt theme</h3><ul>
<h2id="9-Running-status"><ahref="#9-Running-status"class="headerlink"title="9 Running status"></a>9 Running status</h2><h3id="9-1-System-delay"><ahref="#9-1-System-delay"class="headerlink"title="9.1 System delay"></a>9.1 System delay</h3><p><imgsrc="https://api.yimian.xyz/img/?path=imgbed/img_b36d5030_1806x546_8_null_normal.png"alt="qos"></p>
<p>The figure above shows the line graphs of the delay statistics of the land node (left), the weather node (right), and the water pump (bottom). It can be seen from the figure that the average delay of the land node is about 730ms, the delay of the weather node is about 900ms, and the delay of the water pump is relatively low, about 45ms.</p>
<h3id="9-2-Sensor-data"><ahref="#9-2-Sensor-data"class="headerlink"title="9.2 Sensor data"></a>9.2 Sensor data</h3><p><ahref="https://smartfarm.yimian.xyz/chart-station.html?f=1564484461&t=1564570861"target="_blank"rel="noopener">Click here to view the weather sensor data of 2019-7-31</a></p>
<hr>
<p><strong>Reference List:</strong></p>
<p>[1] OASIS, (2015,12.10). MQTT Version 3.1.1 Plus Errata 01 [Online]. Available: <ahref="https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt"target="_blank"rel="noopener">https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt</a> -v3.1.1-errata01-os-complete.html</p>
<p>[2] Sinha, R.S., Wei, Y. and Hwang, S.H., “A survey on LPWA technology: LoRa and NB-IoT”, in Ict Express, 3(1), pp.14-21, 2017.</p>
<spanclass="next-text nav-default"><spanclass=".zh">Arduino中[]=重载导致的String调用问题</span><spanclass=".en">String call problem caused by []= overload in Arduino</span></span>