This class provides a LoRaWAN 1.0.2 compliant driver for the LoRa network processor in the LoPy, LoPy4 and FiPy.
Please ensure that there is an antenna connected to your device before sending/receiving LoRa messages as improper use (e.g. without an antenna), may damage the device.
For various complete LoRa examples, check here.
Create and configure a LoRa object. See init for params of configuration.
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
This method is used to set the LoRa subsystem configuration and to specific raw LoRa or LoRaWAN.
The arguments are:
mode can be either
LoRa.LORA: For LoRa MAC / RAWLoRa.LORAWAN: For use in the LoRa Wide Area Network and services like TTN and Chirpstackregion can take the following values:
LoRa.AS923LoRa.AU915LoRa.EU868LoRa.US915LoRa.CN470LoRa.IN865If no region is provided, it will default to the setting provided in the CONFIG partition, set by the Firmware Updater tool.
frequency accepts values within the selected Region frequency bands.
tx_power is the transmit power in dBm.
bandwidth is the channel bandwidth in KHz.
LoRa.BW_125KHZLoRa.BW_250KHZLoRa.BW_500KHZsf sets the desired spreading factor. Accepts values between 7 and 12.
preamble configures the number of pre-amble symbols. The default value is 8.
coding_rate can take the following values:
LoRa.CODING_4_5LoRa.CODING_4_6LoRa.CODING_4_7LoRa.CODING_4_8power_mode can be either
LoRa.ALWAYS_ON: the radio is always listening for incoming - packets whenever a transmission is not taking placeLoRa.TX_ONLY: he radio goes to sleep as soon as the transmission completesLoRa.SLEEP: the radio is sent to sleep permanently and won’t accept any commands until the power mode is changed.tx_iq enables TX IQ inversion.
rx_iq enables RX IQ inversion.
adr enables Adaptive Data Rate.
public selects between the public and private sync word.
tx_retries sets the number of TX retries in LoRa.LORAWAN mode.
device_class sets the LoRaWAN device class. Visit the TTN Website to learn more about the LoRa device classes. Can be either:
LoRa.CLASS_ALoRa.CLASS_CIn
LoRa.LORAWANmode, onlyadr,public,tx_retriesanddevice_classare used. All the other params will be ignored as they are handled by the LoRaWAN stack directly. On the other hand, inLoRa.LORAmode from those 4 arguments, only the public one is important in order to program the sync word. InLoRa.LORAmodeadr,tx_retriesanddevice_classare ignored since they are only relevant to the LoRaWAN stack.
Join a LoRaWAN network. Internally the stack will automatically retry every 15 seconds until a Join Accept message is received. The parameters are:
activation: can be either:
LoRa.OTAA: Over the Air ActivationLoRa.ABP: Activation By Personalisationauth: is a tuple with the authentication data.
LoRa.OTAA the authentication tuple is: (dev_eui, app_eui, app_key) where dev_eui is optional. If it is not provided the LoRa MAC will be used.LoRa.ABP the authentication tuple is: (dev_addr, nwk_swkey, app_swkey).timeout: is the maximum time in milliseconds to wait for the Join Accept message to be received. If no timeout (or zero) is given, the call returns immediately and the status of the join request can be checked with lora.has_joined().dr: is an optional value to specify the initial data rate for the Join Request. values are region specific.Get or set the frequency in raw LoRa mode (LoRa.LORA). The allowed range is region-specific.
Get or set the bandwidth in raw LoRa mode (LoRa.LORA). Bandwidth can be either: (depending on the region setting)
LoRa.BW_125KHZLoRa.BW_250KHZLoRa.BW_500KHZGet or set the coding rate in raw LoRa mode (LoRa.LORA). The allowed values are: (depending on the region setting)
LoRa.CODING_4_5LoRa.CODING_4_6LoRa.CODING_4_7LoRa.CODING_4_8Get or set the number of preamble symbols in raw LoRa mode (LoRa.LORA).
Get or set the spreading factor value in raw LoRa mode (LoRa.LORA). The minimum value is 7 and the maximum is 12:
Get or set the power mode in raw LoRa mode (LoRa.LORA). The accepted values are:
LoRa.ALWAYS_ONLoRa.TX_ONLYLoRa.SLEEPReturn a named tuple with useful information from the last received LoRa or LoRaWAN packet. The named tuple has the following form:
(rx_timestamp, rssi, snr, sftx, sfrx, tx_trials, tx_power, tx_time_on_air, tx_counter, tx_frequency)
Where:
rx_timestamp is an internal timestamp of the last received packet with microseconds precision.rssi holds the received signal strength in dBm.snr contains the signal to noise ratio id dB (as a single precision float).sfrx tells the data rate (in the case of LORAWAN mode) or the spreading factor (in the case of LORA mode) of the last packet received.sftx tells the data rate (in the case of LORAWAN mode) or the spreading factor (in the case of LORA mode) of the last packet transmitted.tx_trials is the number of tx attempts of the last transmitted packet (only relevant for LORAWAN confirmed packets).tx_power is the power of the last transmission (in dBm).tx_time_on_air is the time on air of the last transmitted packet (in ms).tx_counter is the number of packets transmitted.tx_frequency is the frequency used for the last transmission.Note that the tuple will only contain the respective information after receiving and/or sending LoRa packets.
Returns True if a LoRaWAN network has been joined. False otherwise.
Add a LoRaWAN channel on the specified index. If there’s already a channel with that index it will be replaced with the new one. By default, the regulated LoRaWAN channels are assigned according to the region settings.
The arguments are:
index: Index of the channel to add. Accepts values between 0 and 15 for EU and between 0 and 71 for US.frequency: Centre frequency in Hz of the channel.dr_min: Minimum data rate of the channel (0-7).dr_max: Maximum data rate of the channel (0-7).Example:
lora.add_channel(index=0, frequency=868000000, dr_min=5, dr_max=6)
Removes the channel from the specified index. On EU868, the channels 0 to 2 cannot be removed, they can only be replaced by other channels using the lora.add_channel method. A way to remove all channels except for one is to add the same channel, 3 times on indexes 0, 1 and 2.
Returns a byte object with the 8-Byte MAC address of the LoRa radio.
Specify a callback handler for the LoRa radio. The trigger types are
LoRa.RX_PACKET_EVENT is raised for every received packetLoRa.TX_PACKET_EVENT is raised as soon as the packet transmission cycle ends, which includes the end of the receive windows. In the case of non-confirmed transmissions, this will occur at the end of the receive windows, but, in the case of confirmed transmissions, this event will only be raised if the ack is received.LoRa.TX_FAILED_EVENT will be raised after the number of tx_retries configured have been performed and no ack is received.An example of how this callback functions can be seen the in method lora.events().
This method is used to check for radio activity on the current LoRa channel, and if the rssi of the measured activity is lower than the rssi_threshold given, the return value will be True, otherwise False. Example:
lora.ischannel_free(-100)
Set the battery level value that will be sent when the LoRaWAN MAC command that retrieves the battery level is received. This command is sent by the network and handled automatically by the LoRaWAN stack. The values should be according to the LoRaWAN specification:
0 means that the end-device is connected to an external power source.1..254 specifies the battery level, 1 being at minimum and 254 being at maximum.255 means that the end-device was not able to measure the battery level.lora.set_battery_level(127) # 50% battery
This method returns a value with bits sets (if any) indicating the events that have triggered the callback. Please note that by calling this function the internal events registry is cleared automatically, therefore calling it immediately for a second time will most likely return a value of 0.
Example:
def lora_cb(lora):
events = lora.events()
if events & LoRa.RX_PACKET_EVENT:
print('Lora packet received')
if events & LoRa.TX_PACKET_EVENT:
print('Lora packet sent')
lora.callback(trigger=(LoRa.RX_PACKET_EVENT | LoRa.TX_PACKET_EVENT), handler=lora_cb)
Save the LoRaWAN state (joined status, network keys, packet counters, etc) in non-volatile memory in order to be able to restore the state when coming out of deepsleep or a power cycle.
Restore the LoRaWAN state (joined status, network keys, packet counters, etc) from non-volatile memory. State must have been previously stored with a call to nvram_save before entering deepsleep. This is useful to be able to send a LoRaWAN message immediately after coming out of deepsleep without having to join the network again. This can only be used if the current region matches the one saved. Note that the nvram will be cleared after using this method.
Remove the LoRaWAN state (joined status, network keys, packet counters, etc) from non-volatile memory.
See the tutorials for an example on how to use nvram
Enable the Mesh network. Only after Mesh enabling the lora.cli() and socket can be used.
Send OpenThread CLI commands, the list is here. The output is multiline string, having as line-endings the \r\n.
>>> print(lora.cli("ipaddr"))
fdde:ad00:beef:0:0:ff:fe00:fc00
fdde:ad00:beef:0:0:ff:fe00:e800
fdde:ad00:beef:0:e1f0:783c:1e8f:c763
fe80:0:0:0:2c97:cb65:3219:c86
LoRa raw sockets are created in the following way:
import socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
And they must be created after initialising the LoRa network card.
LoRa-Mesh socket is created, if the Mesh was enabled before (lora.mesh() was called).
The LoRa-Mesh socket supports only the following socket methods: close() , bind(), sendto(), and recvfrom().
LoraWAN sockets are created in the following way:
import socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 0) #where 0 represents the datarate used DR_0
Data rate table (also applies to the socket setting)
| Region | DR_0 | DR_1 | DR_2 | DR_3 | DR_4 | DR_5 | DR_6 | DR_7 |
|---|---|---|---|---|---|---|---|---|
| EU868 / AS923 / IN865 | SF12BW125 | SF11BW125 | SF10BW125 | SF9BW125 | SF8BW125 | SF7BW125 | SF7BW250 | FSK50 |
| US915 / AU915 | SF10BW125 | SF9BW125 | SF8BW125 | SF7BW125 | SF8BW500 |
For Join requests in OTAA, EU868 uses DR_0 to DR_5, this is handled in the firmware by default.
For EU868, AS923 and IN865, the TX rates are used for RX as well. US915 and AU915 use different RX rates. Note that the RX settings are all handled in the firmware.
The default receive window is 1 second.
| Region | DR_8 | DR_9 | DR_10 | DR_11 | DR_12 | DR_13 |
|---|---|---|---|---|---|---|
| US915 /AU915 | SF12BW500 | SF11BW500 | SF10BW500 | SF9BW125 | SF8BW500 | SF7BW500 |
For more information about LoRaWAN specifications, you can check this document by the LoRa Allience: https://lora-alliance.org/lorawan-for-developers
LoRa.LORA, LoRa.LORAWANLoRa.OTAA, LoRa.ABPLoRa.ALWAYS_ON, LoRa.TX_ONLY, LoRa.SLEEPLoRa.BW_125KHZ, LoRa.BW_250KHZ, LoRa.BW_500KHZLoRa.CODING_4_5, LoRa.CODING_4_6, LoRa.CODING_4_7, LoRa.CODING_4_8LoRa.RX_PACKET_EVENT, LoRa.TX_PACKET_EVENT, LoRa.TX_FAILED_EVENTLoRa.CLASS_A, LoRa.CLASS_CLoRa.AS923, LoRa.AU915, LoRa.EU868, LoRa.US915LoRa.timeout