# Feasibility Analysis of a LoRa-Based WSN Using Public Transport
## 5 Software Implementation and Description  
A preliminary version of a software to manage the network has been developed and tested. The software to control the entire network, and specifically end-nodes, gateways, and supergateways, was written with the Arduino IDE and is based on the SX1272 library developed by Libelium, properly modified and adapted. This choice is based on the fact that Arduino is an open source platform that can be adapted to different purposes and control different potential sensors of a DMS. Since many aspects of software were common to all three types of nodes, a new library was created to improve modularity, code reuse, and maintenance. Of course, in this way, a significant reduction of the development time and costs has been achieved.  
LoRa reserves one byte for the specification of the network address of a node, with the special value 0 used for broadcast communication and therefore unassignable to any node. Since having a unique address for each end-node is an impracticable solution, they all share the same network address. This does not cause any conflict in the network, but has the downside of making end-nodes undistinguishable from each other. Consequently, there is the need to reserve some space in the packet payload for the indication of a unique sensor ID, in order to identify end-nodes appropriately. As opposed to these terminal nodes, the supergateway is unique and it has address 1, while five addressing classes are devised for the five levels of gateways (Table 3). Additional addresses, one for each level, are reserved for the multicast communication previously described: they target all gateways belonging a certain level, and this mechanism allows the flowing of data packets from higher layers to the central hub.  
The generic data packet is created at the end-node, which populates it with the measured data (including position and timing) and sends it to the first gateway it encounters. Each gateway adds the required information to allow the supergateway to know which path has been followed by the packet. Since the maximum number of levels has been limited to 5, this implementation choice does not cause any problem of exceeded packet size and respects the LoRa specification.  
The structure of the packet payload is reported in Table 4. The meaning of the definitions is as follow:  
* _TYPE_ is an integer defining the type of packet; as descripted in the previous section, the implementation provides not only for data packets but also for control information packets.
* _TRIP_ lists the path of intermediate gateways traversed, and it is there to allow the supergateway to discover malfunctioning gateways.
* _SID_ contains the sensor ID of the end-node that generated the packet.
* _TS_ is the date and time (timestamp) when the measurement was made.
* _POS_ reports the position (GPS coordinates) of the vehicle at the time that data from sensors were collected.
* _MSG_ contains the formatted sensor data, composed of environmental pollution information and meteorological parameters.  
\begin{table}
\begin{tabular}{c c c c} \hline \hline
**CLASS** & **START ADDRESS** & **END ADDRESS** & **MULTICAST** \\ \hline End-nodes & & 255 & - \\ Class 5 Gateways & 160 & & 249 & - \\ Class 4 Gateways & 94 & 153 & 7 \\ Class 3 Gateways & 50 & 89 & 6 \\ Class 2 Gateways & 24 & 43 & 5 \\ Class 1 Gateways & 10 & 19 & 4 \\ Supergateway & 1 & & - \\ \hline \hline \end{tabular}
\end{table}
Table 3: Addressing plan for the multilevel architecture.