# Low-Cost IoT and LoRaWAN-Based System for Laying Hen Identification in Family Poultry Farms
## 3 System Design and Implementation
### IT Layer  
The IT layer consists of a set of software applications designed to support the poultry farmer. The network server utilized is The Things Stack (TTS), an advanced version of the solution known as The Things Network. This server facilitates the management and monitoring of devices, gateways, and end-user applications, thereby ensuring the security, scalability, and reliability of data routing across the entire network. This is an open-source platform that continues to evolve through community contributions, thereby offering broad and expanding coverage. In relation to the application server, it operates on a Raspberry Pi 4B, using Docker containers to facilitate both service management and system scalability. The following services have been deployed (see Figure 1):  
* PostgreSQL and pgAdmin 4: A PostgreSQL database has been employed to store messages received from the nodes. PgAdmin is used for database management. In addition, a Firebase Realtime Database has been used in the cloud. It is a database hosted in the cloud. Data are stored as JSON objects and synchronized in real time with each connected client. * Node-RED: It plays a pivotal role in the reception of messages from both the nesting box nodes and the environment node. This function is achieved through the subscription to the network server's MQTT server, which facilitates the integration of these messages into both the PostgreSQL and Firebase Realtime Databases. Furthermore, Node-RED handles the transmission of real-time alerts to the user via the Telegram messaging application. * Grafana: Grafana has been employed to generate dynamic visualizations and graphs based on the data stored in the database. The three user applications are shown in Figure 4. The applications were intentionally developed to be user-friendly, easily understandable, and equipped with an intuitive interface, thus reducing to a minimum, if not eliminating entirely, the need for initial training by the end user. Each was developed with a dedicated interface to provide the poultry farmer with information as follows:  
* Web application: The Grafana web application allows advanced graphical visualization of collected data. The dashboard developed as an example shows the following data: laying activity by hour, laying activity by day, laying hens, nesting box visits, etc. * Mobile application: A mobile application has been developed for the poultry farmer to view the status of the nesting boxes and receive real-time alerts about hen visits. Android Studio has been used as the development IDE. The application uses two data sources, an MQTT server and the Firebase Realtime Database. The mobile application has the following functionalities: present nesting boxes' status information, receive alerts when a hen visits a nesting box, and present the data collected in the database. In this sense, once a visit is registered, it is maintained as long as successive positive readings are received at intervals of less than 10 s (a configurable threshold). If no readings are received for more than 10 s, the nesting box is marked as EMPTY. Daily visits are recorded, and the counter is automatically reset at the beginning of each day. The main screen of the application, which can be seen in the center of Figure 4, includes in a scrollable list (ScrollView) a card (CardView) for each nesting box, indicating its status (green = free, pink = occupied), the identifier of the nesting box, and the number of visits since the last reset of the counter. * Telegram Bot: A Telegram bot was developed using BotFather, Telegram's official service for creating and managing bots. The bot receives messages from Node-REDand notifies the user whenever the NFC reader detects a tag, thereby registering a hen's visit to a nesting box. Messages continue to be sent as long as the hen remains inside and is then continuously detected. Based on the duration of the received messages, it is possible to infer, with a low probability of error, whether the hen has laid an egg.