# An IoT-Based Solution for Intelligent Farming +
## 3 IoT-Based Stack Architecture for Intelligent Farming Solutions
### Gateway  
As depicted in Figure 3, the interface between the WSN, the CP and the User Interface domains is carried out by the gateway. In addition to the standard gateway functionality, which is essentially joining networks to allow devices to intercommunicate irrespectively of the network where they belong, the SheepIT gateway supports additional services developed to address the specific requirements of farming applications, as referred to in Section 2.1.7. Therefore, the most elemental functionality of the gateway is forwarding data between the WSN, the CP and the User Interface. This activity requires minimal processing, consisting essentially in decoding the data frames coming from each one of the interfaces, placing the data in a local database and triggering the processes responsible for fetching the data and sending it to the target interface(s). Many farms are located in remote places and, as such, permanent access to the Internet cannot be considered as granted. Moreover, there are some services that require low response times and, as such, it is essential to deploy them as close to the data sources as possible. Consequently, the gateway should support locally at least a few critical services, namely:  
* **Alarms:** alarms should be raised as quickly as possible in the event of anomalous situations. The gateway collects data from all the animals, so it has a global view that allows it to correlate the received data, thus being a privileged place to detect abnormal situations. For example, if all animals suddenly exhibit a running behavior that may indicate that the flock may be under attack of a predator, and thus an alarm should be raised. A sheep that disappears from all the beacons can have escaped, and so an alarm should also be raised;
* **Local management:** local web interface, to allow performing on-site administration tasks (e.g., defining the height at which animals are allowed to feed, if the restrictions are active or not, etc. ), consult real-time and historical state data, among others. These operations shall be possible through a WiFi or Bluetooth enabled PDA or smartphone, without requiring access to the CP's web interface;
* **Technical management:** local web interface, to enable technical interventions, such as tuning the communication parameters to the specific characteristics of the exploration (kind of terrain, dimension of the flock, etc. ), as well as to allow system-level debug via the network. Figure 7: Collar Registration Message Sequence Chart. The number of gateway devices is expected to be small (just one, in most of the cases). Moreover, they are not moved frequently and are usually deployed in accessible places. Therefore, we assume that gateways are not subject to the stringent power, size and energy consumption limitations that beacons, and particularly collars, face. Thus, gateways can use more powerful hardware, both in terms of processing and storage capacity, which is essential to enable the realization of the services mentioned above. Considering the complexity of the services to be supported, we based our proposal on an Embedded PC platform running Linux. As illustrated in Figure 8, the gateway comprises Internet and WiFi/Bluetooth, made available through suitable network interface cards. The Gateway also contains a beacon module, interconnected with the Embedded PC via a serial interface. The beacon module ensures compliance with the communication scheme of the WSN. Combined, these interfaces abstract the communication services offered to the local services. Figure 9a illustrates the gateway's WLAN interface processing flow. After system initialization, which includes initialization of data structures and hardware, the system waits for the arrival of frames through the serial port. When a new frame arrives it is subject to pre-processing, which includes validation, parsing and time-stamping. The processed data is then stored on a local database. Afterwards, the processing flow forks into two different lines.