LoRaWANSim: A Flexible Simulator for LoRaWAN Networks

Among the low power wide area network communication protocols for large scale Internet of Things, LoRaWAN is considered one of the most promising, owing to its flexibility and energy-saving capabilities. For these reasons, during recent years, the scientific community has invested efforts into assessing the fundamental performance limits and understanding the trade-offs between the parameters and performance of LoRaWAN communication for different application scenarios. However, this task cannot be effectively accomplished utilizing only analytical methods, and precise network simulators are needed. To that end, this paper presents LoRaWANSim, a LoRaWAN simulator implemented in MATLAB, developed to characterize the behavior of LoRaWAN networks, accounting for physical, medium access control and network aspects. In particular, since many simulators described in the literature are deployed for specific research purposes, they are usually oversimplified and hold a number of assumptions affecting the accuracy of their results. In contrast, our simulator has been developed for the sake of completeness and it is oriented towards an accurate representation of the LoRaWAN at the different layers. After a detailed description of the simulator, we report a validation of the simulator itself and we then conclude by presenting some results of its use revealing notable and non-intuitive trade-offs present in LoRaWAN. Such simulator will be made available via open access to the research community.


Introduction
The Internet of Things (IoT) represents an ecosystem of autonomous devices able to sense, communicate and provide actuation without the need for human intervention. This technology is already adopted in many scenarios, from industry to agriculture, and from wildlife to smart cities [1], and the number of IoT devices is expected to grow to 22 billion by 2025 [2]. Focusing the attention, in particular, on large-scale IoT networks, long-range connectivity and energy efficiency are challenging requirements that have been met by low power wide area network (LPWAN) technologies, such as LoRaWAN, NB-IoT [3] and SigFox [4]. Such networks combine long battery life (even ten years) and wide coverage (even several kms), at the cost of low bit rate.
Among the LPWAN technologies, LoRaWAN is deemed as one of the most promising, being relatively flexible and straightforward, both technology-wise and business-wise [5,6]. In a LoRaWAN network, end devices (EDs), representing IoT nodes (e.g., sensors or actuators), send packets to gateways (GWs), which forward them to a network server (NS) they are connected to via Internet (using WiFi, 3G/4G, Ethernet, etc.). The NS serves as a centralized entity responsible for the network's upkeep.
In the physical layer, LoRaWAN relies on LoRa, which is a proprietary physical layer solution patented by Semtech Corporation (https://www.semtech.com/lora), and the medium access control (MAC) and network layers have been defined by the LoRa Alliance

Original Contributions
The original contributions of this paper are twofold. First, we describe the architecture of the LoRaWAN simulator, which we release as open access; it integrates both a MAC layer and physical layer functionalities. Second, we provide original results obtained by means of the simulator, which give insights into the performance of LoRaWAN and the impacts of different setups (e.g., in terms of coding rate).
As for the simulator itself, the novelty of our approach lies in the integration of two separate simulators, namely, the MAC layer simulator and the physical layer simulator, which interact with each other at run-time. In particular, the physical layer simulator is in charge of providing the outcome (success/failure) of each single transmission to the MAC layer simulator, which knows who is transmitting to whom and when. This depends on a number of aspects and parameters (e.g., experienced signal-to-noise ratio, adopted SF and coding rate, interleaving, gray coding, modulation/demodulation) that are accurately considered/reproduced at the physical layer. Said mechanism will be addressed more precisely in Section 4. This level of integration is not usual in currently available LoRaWAN simulators, or in network simulators in general. Among the other unique features, which, to the best of our knowledge, are not present in the state-of-the-art tools are: (i) support of multiple gateways; (ii) the possibility of prioritizing and enabling/disabling the receive windows; (iii) accounting for uplink-downlink interference in RX1, and modeling half or full-duplex LoRaWAN gateways.
As for the numerical results, several original results are provided. Specifically, we investigate the impacts of different coding rates in interference-limited and noise-limited scenarios, also showing that in heavily interference limited conditions, the adoption of powerful coding rates is counterproductive for both the delivery rate and the energy consumption. Moreover, we assess the impact of downlink transmissions (e.g., acknowledgments) on the average energy consumption of EDs, also showing that increasing the number of gateways affects not only the packet delivery rates in uplink and downlink, but also the average consumption of devices, and hence, ultimately, the battery life.

Technology Background: LoRa and LoRaWAN
In this section, the fundamentals of LoRa and LoRaWAN relevant to understanding the key aspects of the protocol and architecture are presented and discussed.

LoRa PHY
At the physical (PHY) layer, LoRa adopts the M-ary chirp spread spectrum (CSS) modulation, which is based on chirps, that is, sine-wave signals whose frequencies sweep linearly with time. Denoting with f 0 the central frequency of the sweep interval , and assuming t = 0 as the signal starting instant, a single LoRa chirp can be mathematically expressed as is the symbol-dependent instantaneous frequency-offset, ranging in the interval [− BW 2 , BW 2 ]; • φ 0 is the signal phase at the initial instant t = 0; • T s is the chirp duration.
In particular, LoRa uses M differently-shaped chirps, each of which is in one-to-one correspondence with the M symbols of the modulation alphabet S = {0, · · · , M − 1}: Given a modulation symbol s ∈ S, the corresponding ∆ f (s, t) linearly increases starting from − BW 2 . Then, when the maximum frequency-offset BW 2 is reached, ∆ f (s, t) wraps around to − BW 2 and keeps on increasing linearly until ∆ f (s, t = T s ) = ∆ f (s, 0). T s , which represents the chirp duration, is usually referred to as symbol interval.
By operating with high SF values, LoRa transmitters increase their communication ranges [20]; robustness against channel impairments and interference from third systems, along with frequency selectivity and the Doppler effect, are also enhanced by increasing SFs. On the other hand, this results in a low data rate and long ToA, which are the main drawbacks of operating with high SFs.
The physical layer bit rate R b depends on SF, the sweep interval BW and the coding rate CR of the forward error correction (FEC) mechanism (which encodes 4 data bits into codewords of 5-8 bits for CR ∈ [1, .., 4], respectively) and is given by [21]: The indicative values (denoting the maximum instantaneous data rate) are provided in Table 1 assuming, as an example case, BW = 125 kHz and CR = 1. The bit rate has a direct impact on the ToA of transmissions, which is the time needed to transmit a packet on the wireless channel, and it is computed [22] with the following equations: where B is the payload size in bytes; H = 0 when the header is enabled and H = 1 when no header is present; L preamble and L payload are the lengths in symbols of the preamble and the payload respectively.

LoRaWAN
On top of the LoRa PHY, the LoRaWAN protocol builds up the upper protocol layers. Communication between EDs and GWs is implemented via LoRa, whereas GWs are connected via standard Internet Protocol (IP) connections, such as Wi-Fi, Ethernet or 3G/4G, to the NS, resulting in a star-of-stars topology ( Figure 1). EDs are not associated with a specific GW but rather to a NS, and all GW data received from an ED forward them to the NS, which is then in charge of discarding duplicates, sending acknowledgements (ACKs) and managing the overall network. The access to the radio channel is ALOHA-based complemented by selecting one of the available frequency channels, so an ED sends a packet whenever it has data to send. By standard, the network channels can be freely attributed by the network operator. However, in the EU868MHz band, three default channels must be implemented in every end-device [10] . Those channels are the minimum set that all network gateways should always be listening on. They are reported in Table 2. In addition to these, extra channels (up to 16 in total) can be optionally configured. LoRaWAN defines three classes of devices, which differ primarily with respect to the support of the downlink communication capabilities and which are labelled A, B and C.

•
Class A devices' uplink transmission is followed by up to two short downlink receive slots (denoted receive window 1 (RX1) and receive window 2 (RX2), respectively) after two different fixed RECEIVE_WINDOW_DELAY intervals (the standard suggests using 1 s delay between the end of an uplink and RX1, and 2 s delay between the end of an uplink and RX2), as can be seen in Figure 2. Class A is primarily intended for EDs with limited energy availability (e.g., battery-powered ones), such as sensors, since it results in the lowest power consumption. • Class B devices allow for more than two receive slots, opening extra receive windows at scheduled times. Synchronization between EDs and GWs is kept via periodic beacons broadcast by a GWs. • Class C devices have a nearly continuously open receive window except for the time spent in uplink transmissions, so they are always reachable by the network, which is attained at the cost of an increased power consumption. Furthermore, two transmission modes, which can be selected on a per-packet basis, are defined by the LoRaWAN specifications:

•
Confirmed mode: When an ED sends an uplink packet, it expects to receive an ACK packet from the NS (through the GW) after its transmission, and it may continue transmitting the same message if no ACK is received. • Unconfirmed mode: No ACK is sent by the NS, so the ED does not know if the packet has been correctly received.
The NS is also in charge of controlling the transmission parameters (SF and transmit power P T ) of the EDs when the adaptive data rate (ADR) algorithm is active. The ADR algorithm is specifically designed to increase the data rate and reduce the energy consumed by the ED. It is not specified in the standard; however, Semtech provides a recommendation in [23] and most of NSs, to the best of our knowledge, operate based on it; its performance has been evaluated in [24]. Note that ADR should be used only if an ED operates in sufficiently stable RF conditions (e.g., static devices with fixed locations). As a matter of fact, the ED itself is in charge of deciding when the ADR should be active, so it should be able to understand whether it is in stable conditions on its own. If this is the case, the ADR bit in the frame header of the packet should be set to 1, informing the NS that the ED is ready to receive ADR commands. When it is set, the NS starts collecting information about the uplink transmissions by the device, which contain the frame counter, signal-to-noise ratio (SNR) measurements and the number of GWs that received each uplink frame. Then, after 20 transmissions, the algorithm will go through a number of iterations based on the maximum SNR measured among these transmissions, and at the end, it will provide the SF and the P T the device should use starting from the next transmission. This information will be sent to the device via a LinkADRReq command, which should be acknowledged with a LinkADRAns. Algorithm 1 shows the implementation of ADR at the NS.

Algorithm 1: NS ADR Algorithm for a given ED.
Input: SF, P T , SF min = 7, Margin = 10 dB SNR max = max{last 20 uplink packets received}, SNR min given in Table 3 by setting SF,

LoRaWAN Simulator Description
This section details the key features of the LoRaWAN simulator which is available free from https://github.com/kvmikhayl/LoRaWAN_simulator. As shown in the block scheme illustrated in Figure 3, it implements both PHY and MAC layer functionalities.
The PHY-layer simulator implements a complete LoRa transceiver (transmitter + receiver), thereby generating the modulated signal and performing the demodulation tasks, whereas the MAC layer simulator manages the channel multiple access, thereby implementing the data traffic (who transmits to whom and when), accounting for mutual interference and the presence of multiple gateways. Specifically, the simulator considers class A LoRaWAN devices (support of class C is also available) operating in the EU 863-870 MHz ISM band, and supports a number of features, including both uplink and downlink communications, imperfect SF orthogonality, capture effect, full/half-duplex GW operation, uplink-downlink interference, DC limitations and energy consumption estimation.
The simulation tool is extremely flexible, as it allows one to tune a large number of parameters, related to the PHY layer, the LoRaWAN protocol and the network itself. The outcomes are provided in terms of overall delivery rate, for both uplink and downlink, and energy consumption values for the individual devices and the network as whole.
In the following, the function of each block shown in Figure 3 is discussed.

Offline Configuration Operations
Prior to the simulation execution, a configuration step must be carried out offline, which consists of the definition of the scenario (e.g., the network layout) and of a number of parameters, which rule the network behavior. This step is discussed hereafter.
Network layout configuration. For each simulation, an area of interest is defined by the user (by default, a circular shape is assumed), in which N EDs and G GWs are deployed either manually (e.g., where they are actually located in a real deployment), or automatically, in fixed (e.g., on a grid) or in random positions.
Radio propagation configuration. An Okumura-Hata channel model [25] is implemented as the default model in the simulator, even though other channel models can be defined by the user. The power received by a GW, P R , is computed as a function of the transmit power, P T , as , where G T A is the transmitting antenna gain; G R A is the receiving antenna gain; L represents the path loss in dB, where f is the frequency in MHz, h b is the height of the GW in m, d is the distance between the transmitter and the receiver in km and C H is the antenna height correction factor, which varies according to the frequency and the size of the city, with h m denoting the height of the ED antenna in m. It should be highlighted that (9) is valid for large urban areas and f > 400 MHz. Finally, s represents random channel fluctuations due to shadowing, modeled as a Gaussian random variable, with zero mean and standard deviation σ; that is, s ∼ N (0, σ 2 ). The user can choose the transmit power P T , σ and the height of the EDs/GWs. Note that the discussed layout and propagation models can be defined by the user manually based, e.g., on empirical measurements. The respective functionality is supported by the simulator.
Radio resource management configuration. The policy adopted for the choice of the SF can be defined by the user, along with the DC configuration. As for the former, the user can dictate a specific SF for each ED or let the simulator make a decision based on the ADR. With reference to the DC, instead, the DC constraints can be disabled or enabled; in the latter case, these are automatically satisfied according to the regional limitation reported in [10]. In particular, DC limitations are specified according to LoRaWAN specification version 1.0.1, which dictates that a device (i.e., ED or GW), after sending a packet of duration ToA seconds, must not use the same frequency sub-band for the next ToA( 1 DC − 1) seconds. In addition, the user can also define the transmission parameters related to both RX windows. For RX1, the RX1DROffset parameter, which is the difference between the SF used in uplink and in RX1, is chosen by default according to Table 4 (the user can introduce  a different table, if needed). For RX2, instead, SF is fixed to 12 by the standard, even though the user can decide to change such parameter. Table 4. RX1DROffset [10]. RX1DROffset  0  1  2  3  4  5   7  7  8  9  10  11  12  8  8  9  10  11  12  12  9  9  10  11  12  12  12  10  10  11  12  12  12  12  11  11  12  12  12  12  12  12  12  12  12 12 12 12 Data traffic configuration. The traffic generation models, for both the uplink and the downlink, are also configured by the user, along with the size of uplink and downlink packets in bytes (B UL and B DL , respectively), the presence of the packet header H and the length of the preamble L preamble . When the default configuration is adopted, data packets are generated by EDs periodically every T seconds. The user can also define the probability of generating a downlink message after an uplink packet; by default, this is set to 1.

Uplink SF
SIR threshold configuration. The MAC layer simulator is in charge of reproducing the ALOHA-based access protocol adopted by LoRaWAN, which does not depend on parameters to be configured. However, within the simulator this block also evaluates the signal-to-interference ratio (SIR) experienced by a receiver due to simultaneous transmissions, in order to check whether the packet is correctly received or not. As detailed in Section 5, the SIR is compared with a threshold γ, dubbed SIR-threshold, which can be defined by the user. Clearly, evaluating the success/failure of a transmission in the presence of interference is by no means a MAC layer task; in the simulator perspective, however, the MAC layer simulator block is the most appropriate for the SIR assessment, because only this block knows which nodes (if any) are simultaneously transmitting hence might interfere each other.
Physical layer configuration. The bandwidth BW of the modulated signal is a userdefined parameter, along with the coding rate CR of the forward-error-correcting code adopted to reveal/correct transmission errors. Indeed, the encoding process should be carried out at the logical link control (LLC) sub-layer but, for the sake of simplicity, in our simulator it is carried out by the PHY-layer simulator. Note that in the current LoRaWAN specification both BW and CR are pre-specified for each region. Our models allow one to update these parameters, thereby adapting the simulator to other regions and/or novel modulation-coding schemes. Table 5 summarizes the key parameters a user can define.

Run-Time Operations
During the execution, the data traffic simulator, which oversees the whole network, provides the MAC layer simulator with the time instants when data packets, generated either by EDs or GWs, enter the respective transmission queues. The MAC layer simulator, in turn, reproduces the behavior of the ALOHA channel access protocol for all devices in the network, thereby deciding which nodes, among those with queued packets, transmit and when.
This information is passed to the SNR assessment block that, given the positions of the transmitting nodes and the adopted channel model/channel measurements, derives the SNR experienced by receivers. Such information, along with the current SF value provided by the radio resource management, is used by the physical layer simulator, which generates the modulated signal corrupted by noise and demodulates it in order to assess if the packet is correctly received or not. Clearly, all accompanying operations, such as interleaving/de-interleaving, gray coding/decoding and channel coding/decoding, are carried out as well. The transmission outcome, either success or failure, is passed to the MAC layer simulator for any consequent action. The MAC layer simulator also gets from the SNR assessment block the values of the useful and interfering power at the receiver under investigation. This information is used to assess whether the interference level is such to prevent the correct reception. As detailed in Section 5, this assessment is carried out by comparing the SIR experienced by the receiver under investigation and the SIRthreshold γ. Given the result of such comparison and the success/failure outcomes of the physical layer simulator (which does not account for interference), the MAC layer simulator updates the performance counters, which are then used to compute the final performance metrics.

Performance Metrics
At the end of the simulation, three default performance metrics are provided, which are discussed hereafter.

Performance Metrics: Uplink Delivery Rate
The delivery rate provided by the simulator refers to the packets received by GWs. Two impairments are taken into account that might prevent GWs from correctly receiving transmitted packets, namely, noise and interference.
As mentioned above, the LoRa PHY simulator implements the whole transmitter → AWGN channel → receiver chain. In particular, the PHY-layer simulator reproduces the operations carried out by the transmitter (channel coding, interleaving, gray coding and modulation), the addition of AWGN noise in the channel and the receiver behavior (demodulation, deinterleaving, decoding). Thus, given the frame of data bits to be transmitted, the adopted SF, CR and BW and the actual SNR that characterizes a given link, the PHY-layer simulator assesses whether the currently transmitted packet is correctly received or not.
The possible presence of interference is also considered, taking into account the possibility that, even though a collision happens between two LoRa packets (considering both uplink and downlink), one of them could be correctly received if one of the signals is strong enough. In this case, the simulator supports the capture effect mechanism, so the packet is correctly received provided that the SIR that is the ratio between the useful received power and the sum of the interfering powers is above a given threshold, γ: where P R is the received power of the target ED, P R i is the received power of the i-th interfering signal and γ depends on the SF used, as specified by a Table 6 [9] (the table can also be modified by a user, if desired). In particular, since we account for inter-SF interference, the inequality should be verified for all the interfering signals by summing the received power for each SF. Condition (10) is used by the MAC layer simulator to decide whether the reception of a packet by GWs is prevented by the interference. In addition, since our simulator supports both full-duplex and half-duplex GWs, interference between transmissions takes into account also the potential uplink/downlink interference and the very possibility of the GW to transmit and receive packets simultaneously. The type of the GWs is specified as a part of network pre-configuration.

Performance Metrics: Energy Consumption
The simulator is able to estimate the LoRaWAN energy-performance by computing the energy consumed by each ED. We assume an ED working in class A and transmitting an uplink packet, followed, optionally, by the reception of a downlink packet. Therefore, the simulator takes into account the energy spent in: uplink transmission, RX delay 1, downlink reception in RX1, RX delay 2, downlink reception in RX2 and sleep until the next uplink transmission, as reported in (11).
In (11), V represents the voltage, I is the current and T is the time spent in each state. In particular, the time passed in transmission and reception, T TX and T RX (in case a downlink packet has been sent), are considered equal to the ToA of the packet, which is computed from (7). The time intervals spent in T RX Delay1 and T RX Delay2 are defined by the standard, as reported in Section 3. If a downlink packet has not been sent or the packet has not been correctly received due to interference, the time intervals spent in reception during RX1 and RX2 are defined based on Table 7, which corresponds to the duration of five preamble symbols, required to effectively detect the downlink packet preamble. The other parameters used to characterize the consumption in the different phases have been taken from [26,27]; they refer to the Microchip RN2483 LoRa Mote [28], and they are reported in Table 8.

Performance Metrics: Downlink Response Rate
The simulator allows also to estimate the rate of correctly delivered downlink packets. In particular, a downlink packet can be generated for each uplink packet sent by an ED with a user-defined probability. The simulator schedules the downlink packets for each ED based on the number of GWs which have received this packet and their availability (accounting for the DC restrictions). The downlink response rate is then computed as the ratio between the number of downlink packets delivered and those generated.
When modeling the delivery of a downlink, a procedure similar to that discussed above in the case of an uplink is used. However, there are some differences between the RX1 and RX2 cases. According to the LoRaWAN specification, in the case of RX1, data are sent in the same frequency channel used for the uplink, therefore both downlink-todownlink (i.e., caused by simultaneous downlink transmission from the other GW) and uplink-to-downlink (i.e., caused by simultaneous uplink transmissions of the other ED) are considered. Meanwhile, in the case of RX2, which is typically carried within a standalone frequency channel, only the downlink-to-downlink interference between different GWs is considered.

Validation
To check the accuracy of the simulator results, several tests have been carried out (results are averaged over 10,000 iteration cycles), and their results have been mainly checked against that of the analytical models and the state-of-the-art literature, as summarized in this section. Comparisons with experimental results have been also carried out, as discussed in the following.

PHY-Layer Simulator
The behavior of the PHY-layer simulator has been validated through the comparison with the results reported in [29,30]. The symbol error rate (i.e., the chirp error rate) as a function of the SNR = P u P w , where • P u denotes the average power of the signal; • P w = N 0 BW denotes the noise power within the nominal signal bandwidth BW.
are presented in Figure 4. Even the visual comparison shows a very close match between the two.

Packet Delivery Rate
The default set of the parameters adopted in our simulations is summarized in Table 9. Note that T there stands for the average period between the uplink packets for each ED. As for the scenario, we considered a GW placed at the center of a circular area of radius R and the EDs were randomly, and uniformly distributed within this area.  Figure 5 shows the uplink delivery rate as a function of the offered traffic, defined as: The uplink delivery rate has been defined as the percentage of packets correctly received by the GW according to the conditions described in Section 5.1. As a reference, the figure shows the rate computed with the well-known equation for ALOHA success probability, P s , by Abramson [31]: It can be seen that the two curves match very closely. Note that, since the analytic equation accounts only for intra-spreading factor interference and it implies the loss of all collided packets, we disabled, specifically for this comparison, the capture effect and the inter-SF interference computation.
Besides validating the simulator by means of analytical models, an experiment has been carried out by setting up a small LoRaWAN network. To this purpose, we used devices (Idesio Rigers Board 1.0) equipped with the Microchip RN2483 radio transceiver, fully certified 433/868 MHz SX1276 LoRa module, which supports LoRaWAN Class A, and one gateway, placed 50 m away from the devices. Five boards have been programmed to send a packet of B UL = 16 bytes every T = 60 seconds for 1 h. The uplink delivery rate has been estimated as the ratio between the number of packets received over the number of the sent packet. All devices used the same SF and the experiment has been replicated with SF = 7 and SF = 10. The results reported in Table 10 show the good accuracy of the simulator. Note that the minor difference in the results (i.e., the additional packet losses) may have been caused by an interference from the third-party systems.

Energy Consumption
The validation of the energy consumption results provided by the simulator, has been carried out configuring the GW to respond in RX1 to each packet received in uplink and removing the DC restrictions. The results are presented in Figure 6. For comparison we have used a simple analytical model given by where E UL is the energy required for the uplink transmission; E RX Delay1 is the energy consumed between the uplink and RX1 (having duration of 1 s); in downlink, E DL f ull is the energy spent if a packet is received; and finally, E DL empty denotes the consumption of a device when it opens the RX Window, but no packet is received. The NS will send a packet in downlink only if it receives an uplink packet, whose success probability is equal to P s , so it knows the device has transmitted something and it will open the receive window. It should be highlighted that in this case, we do not take into account duty cycle constraints or the possibility of a downlink message being sent in RX2, which would require a more complex model. As can be seen from the presented charts, the results are almost coincident.

Numerical Results
Having validated the simulator in simple scenarios, in this section, we proceed by delivering some initial insight into less trivial cases, which show some notable trade-offs. Unless stated otherwise, for these simulations, we use the same parameters and configurations, which have been used in the previous section. In all the considered scenarios, GWs operate in full-duplex mode. Figure 7a depicts the uplink delivery rate as a function of the radius of the circular area where N = 50 nodes are distributed for both CR = 1 and CR = 4, when varying T. As expected, when the area dimension increases, the delivery rate decreases. In particular, in an interference-limited scenario (T = 30 s), performance does not depend on CR, but for very small areas, CR = 1 is the best case due to lower ToA, and therefore, collisions. On the other hand, when the scenario is limited by noise (T = 300 s), CR = 4 performs better for bigger areas, thanks to the stronger encoding it is able to offer.
Observing the energy performance in this scenario, reported in Figure 7b, it can be noticed that the consumption increases with the area size. This happens because EDs are forced to use higher SFs when they are far from the GW, which causes higher energy consumption. In addition, nodes using CR = 4 consume more energy mainly because the ToA is higher; therefore, more time is spent in transmission. Figure 8a shows the delivery rate as a function of the number of EDs in the network for both CR = 1 and CR = 4. As a matter of fact, the higher is the number of EDs in the network, the higher is the rate of collisions, which results in the degradation of the overall performance of the network. In addition, when the scenario is limited by interference (T = 30 s), CR = 1 shows better performance. This happens because the ToA is smaller with respect to CR = 4 case; therefore, there are fewer collisions. On the other hand, when the scenario is limited by noise (T = 300 s) and all EDs can successfully reach the GW since no interference is present, no difference between the two approaches are highlighted. The respective energy consumption is revealed in Figure 8b, which shows that the average energy consumption decreases when the number of ED increases. This result may seem counter-intuitive; however, it is explained by the increase of the uplink collisions, resulting in fewer packets being correctly received by the GW, and consequently, fewer packets sent in downlink to the ED. As a matter of fact, if the uplink message is lost, no downlink packet will be sent to the ED, which will then consume less power. In addition, also in this case, nodes using CR = 4 consume more energy on average.  For the sake of completeness, an analysis of the uplink response rate in presence of class C downlink traffic has been carried out, whose functioning has been described in Section 3.2. In this scenario, N = 50 EDs transmit every T = 60 s. G = 2 GWs have been placed at (x, y) = [R/2, R/2], [−R/2, −R/2] of a circular area of radius R. More specifically, we assumed that one GW may be transmitting a class C downlink packet with probability p at an instant t ∈ [0, T], and therefore, it may be interfering with the uplink packet sent to the other GW, according to the capture effect mechanism already described in Section 5.1. As expected, the higher the probability p, the higher the interference, so the uplink response rate decreases. Results are provided in Table 11. Finally, an analysis of the downlink response rate, as described in Section 5.3, has been carried out. Two scenarios have been considered: one with a single GW, placed at the center of the circular area of radius R, and one with four GWs, placed respectively at coordinates (x, y) = [R/2, R/2]; [−R/2, −R/2]; [R/2, −R/2]; [−R/2, R/2], with RX2 disabled. In Figure 9a the downlink response rate as a function of the duty cycle for RX1 window is reported. The higher the DC, the higher is the number of packets sent in the downlink. Per intuition, with more GWs present in the network, the downlink response rate is higher. However, this results in increased average energy consumption, since the reception of a downlink packet often requires more energy than that needed for checking the two empty receive windows.

Conclusions
With the number of LoRaWAN connections approaching 200 million worldwide [32], there is significant interest from both scientific and industrial communities in this technology. However, despite the apparent simplicity of the LoRaWAN protocol, assessing its performance and finding the proper trade-offs by means of analytical tools is not always possible. Therefore, in this paper, we present and openly deliver to the community a new MATLAB-based simulator tool covering the physical and the upper layers of the LoRa/LoRaWAN protocol stack. As we demonstrate in the paper, the proposed simulator has a number of the unique features (including, but not limited to (i) support of multiple gateways both for uplink and downlink; (ii) accounting for uplink-uplink, uplinkdownlink, downlink-uplink and downlink-downlink interference; (iii) receiving window prioritization; (iv) modeling of energy consumption) whilst being decently simple to use and easy to configure. Importantly, it is built using MATLAB, which is already familiar to many scholars in the field and thus has a less steep learning curve than many other LoRaWAN simulators.
Besides, we have presented some illustrative results obtained using the developed simulator, which demonstrate exciting and not always intuitive trade-offs. First, we investigated the impacts of different coding rates in interference-limited and noise-limited scenarios, showing that some benefit on the packet delivery rate, although marginal, can be achieved only in the latter scenario. Notably, we showed that such benefit comes at the price of an increased energy consumption. Second, we showed that as the number of EDs increases significantly, thereby making the network operate in heavily interference limited conditions, the adoption of more powerful coding rates is counterproductive, for both the delivery rate and the energy consumption. Third, we assessed the impact of downlink transmissions (e.g., acknowledgments) on the average energy consumption of EDs, also showing that increasing the number of GWs affects not only the packet delivery rates in uplink and downlink but also the average consumption of devices, and hence, ultimately, the battery life.
All these and many other aspects of LoRaWAN require further investigations, and we believe that the simulator presented in this paper can be a great help. In the future, we plan to extend the simulator with new functionalities missing as of today, such as more advanced propagation models, enabling the support of multiple channels and other device classes and providing a more dynamic formation of data traffic.