# Development of a Multi-Radio Device for Dry Container Monitoring and Tracking
## 3 Materials and Methods
### Software Design  
The objective of this subsection is to describe the CTD software (v0) functionalities for accomplishing both maritime and terrestrial scenario operations. The SW was developed for CTD with ESP32 + Quectel BG95 + sensors to enable the following functions for the intended use:  
* Monitoring and tracking unit sending UDP packets to a cloud OneM2M compliant platform. * Monitoring and tracking unit, provisioner of a BLE Mesh network from the device, which is also a gateway for NB-IoT/BLE Mesh networks. * Monitoring and tracking unit, a provisioned device of a BLE Mesh network (source node or relay) for the device with NB-IoT off. * Monitoring and tracking unit, a gateway for NB-IoT/BLE Mesh networks, sending packets to the cloud OneM2M compliant platform, with the information generated from the sensor mesh nodes. The activity consisted of developing the SW to establish the connection with the NB-IoT cellular network and sending UDP packets with JSON payload. Furthermore, the SW for enabling the gateway function NB-IoT/BLE Mesh has been developed in such a way that the sensor data from a source node of the BLE Mesh is used to fill the content of the JSON payload to be transmitted to the ICON platform. Regarding the BLE Mesh, the activity consisted of adapting the SGI sensor model [35] in order to provide the needed functions in two cases. Notably, once the mesh is established, it is possible to:* Send data requests from the provisioner/gateway node toward the source sensor nodes,
* Or send sensor data from the source node towards the provisioner/gateway node. The firmware allows managing both event-driven and periodic data transmission, with custom thresholds and time intervals set by the user. Furthermore, the firmware manages the energy-saving functions to make the battery last. The SW is implemented using the Real-time Operating System (RTOS) on ESP32. The algorithm for the communication and data transmission is illustrated by the flowchart given in Figure 8.  
The microcontroller firmware implements the following features:  
1. Reading the temperature, humidity, and pressure sensors;
2. Reading the accelerometer in order to identify significant movements and shocks;
3. Reading location (geographic coordinates, via GNSS);
4. Updating the time (via either GNSS, NB-IoT, or BLE Mesh);
5. Enabling communication (NB-IoT and/or BLE);
6. Provision (or join) a BLE mesh network;
7. Sending the read data to the ICON cloud platform; this can be achieved directly via NB-IoT or other BLE mesh nodes (Relay and Gateway). The device performs all these operations periodically, the interval of which is configurable. When not engaged in these tasks, the firmware puts all components in the lowest supported and usable power mode to maximize battery life. Thus, data are sent periodically during a transmission window where, depending on the connectivity status of the radio interfaces, the device can assume the following roles:  
* NB-IoT terminal: device that is under NB-IoT network coverage and sends data to the ICON platform via the NB-IoT network;
* Mesh node: device without NB-IoT network coverage but which is part of a BLE mesh network; it sends its data to another node in the mesh network so that it is propagated to the gateway node; each node in the mesh network also acts as a bridge (Relay) for other neighboring nodes, forwarding their data;
* Gateway NB-IoT/BLE Mesh: a device that is both under the coverage of the NB-IoT network and part of the BLE mesh network; it sends both its own data and the data of mesh nodes connected to it to the platform. The role of the device is dynamic and determined by operational conditions. The role has no impact on either the hardware or the firmware of the CTDs, which are all the same. In order to allow all nodes to send their data periodically, they awaken \"simultaneously\" and, if necessary, establish a mesh network (or reuse the previous one, if available). The simultaneity of awakening is related to the accuracy of the internal clock.