## 4 Implementation
### Embedded Software
The linear and sequential structure of the flowchart emphasizes the straightforward and automated nature of the process, highlighting how each step directly leads to the next, ensuring the efficient handling of data in the DTN network. The embedded software of the SN consists of a firmware whose main objective is, as shown in Figure 7, to collect data from sensors, format them appropriately, and transmit them via advertising packets. Initially, the software routine establishes the header of the advertising packets. At this stage, the advertising message's variables are structured, focusing on the broadcast code frame. Additionally, the definition of BLE initialization and transmission interval occurs. Subsequently, data are collected from the BME680 and soil humidity sensors. These data variables are then stored in the Broadcast Code field, followed by a call to update the advertising message function. The data are transmitted only on BLE channel 37. Finally, the BLE radio and peripherals are deactivated, and the sensor is idle to reduce energy consumption. After a programmable interval, the device exits idle mode, activating the deactivated peripherals and returning, in a loop logic, to the data collection step. Figure 6: Flowchart of the _DTN Handler_ algorithm. Figure 7: Flowchart of the SN firmware. The flowchart in Figure 7 is designed to reflect the sequential and cyclical nature of the SN firmware's operation. The process begins with initializing and configuring advertising messages, ensuring the transmitted data are appropriately formatted and broadcast. The flowchart then moves to the sensor data collection step, where environmental parameters are gathered. These data are subsequently stored in the Broadcast Code protocol field, a step that directly links data collection with transmission. The flowchart also illustrates how, after data are transmitted, the system enters idle mode to conserve energy, disabling key components such as I2C, BLE, and ADC. Including a delay before exiting idle mode and resuming data collection emphasizes the energy-efficient design of the firmware. This structured approach in the flowchart highlights the firmware's efficiency in handling sensor data while minimizing energy consumption, which is critical for the SN's performance in field applications. Two approaches were developed for implementing the SN's embedded system: one using routines from the Software Development Kit (SDK) v2.3.0 and another with bare-metal firmware, which operates directly on the hardware without requiring an RTOS. This allows for the evaluation of the influence of firmware development methodology on the SN's energy consumption. Optimizations were implemented in the bare-metal firmware methodology to reduce the beacon's power consumption. During idle mode, the high-frequency clock was disabled, and 95% of the System-on-a-Chip (SoC) nRF52840s Random Access Memory (RAM) sectors were deactivated. Additionally, the power supply method of the SoC was optimized. The chip provides two types of regulators, Low-Dropout (LDO) and buck; in the bare-metal firmware, the LDO regulator was disabled. Moreover, compared to the RTOS firmware, the bare-metal firmware's size was reduced: there was a 99% reduction in RAM usage and a 97% reduction in flash memory usage.