# Slotted ALOHA on LoRaWAN-Design, Analysis, and Deployment
## 6 S-ALOHA Implementation over LoRaWAN  
Once implemented, at the application level, an efficient and accurate synchronization system that guarantees acceptable uncertainties of the order of milliseconds, it is necessary to introduce specific functions for the transmission management on top of LoRaWAN MAC level. On the end node side, the application layer must schedule the uplink transmission only at the beginning of each slot, starting an immediate transfer request when the internal RTC triggers the event. For the calculation of the time \(T_{\mathrm{r}}\) it is necessary to consider the fact that the LoRa radios allow different types of configurations depending on the spreading factor, bandwidth, coding rate and, above all, depending on the length of the payload of the packet sent. The configuration used for the tests carried out in the following chapter is made up with these parameters: SF = 8, BW = 125 KHz, CR = 1, Preamble = 8 bytes, and Payload = 200 bytes. Following [32], we can estimate the uplink time of air, equal to 553.47 ms. This means that considering the eight bytes added into ACK needed for synchronization procedure, the lower bound of the slot duration is 1.615 s (\(T_{\mathrm{r}}\)), which is equal to \(T_{\mathrm{LoRaWAN}}\) portrayed in Figure 4. The size of the packets is considerable and close to the maximum allowed of 255 bytes, this to maintain an acceptable ratio between the time used to transfer the data (\(T_{\mathrm{r}}\)) and the overhead time (\(T_{\mathrm{b}}\)). This result has been calculated considering that the response in the first window RX1 must occur with the same transmission parameters of the end node. Moreover, it is avoided that the transmission of the ACK message collides with other transmissions being completely included within the time of a slot. The calculation of the \(T_{\mathrm{b}}\) time, the tolerance interval, takes place considering the average alignment error introduced by the synchronization algorithm and the clock drift introduced by the quartz oscillator embedded in the microcontroller. Considering a crystal with an error of 80 ppm/\({}^{\circ}\)C (worst case), a phase shift of 200 ms is generated every 40 min, a considerable and not negligible value in the case of devices that transmit a few times in a day. For the calculation of \(T_{\mathrm{b}}\), we considered a base-time of 5.4 ms given by the maximum alignment shift reached during the synchronization tests to which was added, to consider the clock drift, a time margin of about 400 ms in order to have an interval of at least 80 min between one synchronization and another. Lastly, a \(T_{\mathrm{b}}\) of about 385 ms has been chosen, then a slot time \(T\) with a total duration of 2 s, with a \(T_{\mathrm{b}}/T_{\mathrm{r}}\) ratio = 25%. In addition to the definition of the slot time, to implement the S-ALOHA protocol and make evaluations about the performances and improvements introduced, it was decided to force the transmission of the nodes on a single channel (channel 6). This is to avoid the random selection algorithm of the transmission channel by the LoRa MAC, which would involve the evaluation of the performance on several channels, which goes beyond the scope of this paper. Figure 8: (**a**) Median, 25 th, and 75 th of synchronization error between 2–4 end nodes; (**b**) typical crystal drift over time. The S-ALOHA protocol must provide confirmation of the receipt of packets. In case of loss of packets, the end node needs to schedule retransmission in a random slot (random backoff). The backoff algorithm has been implemented in the LoRaWAN application layer, which allows calculating the wait time before retransmitting as a multiple of the single slot time. In the function, a random natural number is calculated within the interval that can be set by the user [0, NSLOTS]. The randomness in the choice of the slot in which to perform the retransmission is a fundamental feature to avoid devices entering pathological repeated interference corner cases.