pico-lorawan-circuitpython

Raspberry Pi Pico and LoRaWAN from CircuitPython.

Enable LoRaWAN communications on your Raspberry Pi Pico or any RP2040-based board using CircuitPython and the Adafruit TinyLoRa library. Based on the TinyLoRa example code by Adafruit.

Bill of Materials

The following hardware is needed:

Item Link
Raspberry Pi Pico https://www.raspberrypi.org/products/raspberry-pi-pico/
Adafruit RFM95x Lora Radio https://www.adafruit.com/product/3072
Edge-Mount SMA Connector https://www.adafruit.com/product/1865
868MHz or 915MHz Antenna https://www.adafruit.com/product/3340
Male-Female Jumper Wires https://www.adafruit.com/product/1953
Breadboard https://www.adafruit.com/product/64

Wiring the RFM9x Radio Module

Wiring diagram

After soldering your RFM95x module and attaching an antenna the mapping between the pins on the module breakout board and your Pico should be as follows:

Pico RP2040 SX1276 Module RFM95W Breakout
3V3 (OUT) VCC VIN
GND GND GND GND
Pin 10 GP7 DIO0 G0
Pin 11 GP8 NSS CS
Pin 12 GP9 RESET RST
Pin 14 GP10 DIO1 G1
Pin 21 GP16 (SPI0 RX) MISO MISO
Pin 24 GP18 (SPI0 SCK) SCK SCK
Pin 25 GP19 (SPI0 TX) MOSI MOSI

The Things Network

To make use of a LoraWAN-enabled Pico you’re going to need to be in range of a LoRa gateway. Fortunately there is The Things Network, an open-source community LoRaWAN network that has global coverage. Depending on where you are located, it’s quite possible that you’re already in coverage. However, if you aren’t, then you needn’t worry too much, the days when the cost of a LoRaWAN base station was of the order of several thousand dollars are long gone. You can now pick up a LoRa gateway for under $100.

While any LoRa device in range of your new gateway will have its packets received and sent upstream to The Things Network, the data packets will be dropped on the ground unless they have somewhere to go. In other words, The Things Network needs to know where to route the packets your gateway is receiving.

Setting up The Things Network

Adafruit has written up a full walkthrough on how to set up and application and register your device with The Things Network. You'll need to set three unique identifiers in the code.py file; the Device Address, Network Session Key, and Application Session Key. These can be found on the Device Overview page.

NOTE: The example code uses ABP rather than OTAA as the Activation Method.

Deploying to your Pico

Copy the contents of the src/ directory in the repo to your CIRCUITPY drive. This includes the code.py file and the lib/ folder and all of its contents, including subfolders and any .mpy files present in the library directory.

Sending data

Restart the board. The code should start running immediately, there will be debug output available on the USB CDC Serial console. If you see "Packet Sent!" then the packets are being sent up to The Things Network via LoRaWAN and you should be able to see your data arriving in the Network Console.

Adding a decoder

We're sending out temperature reading as a byte array.

temp = microcontroller.cpu.temperature
temp = int(temp * 100)

data = bytearray(2)
data[0] = (temp >> 8) & 0xFF
data[1] = temp & 0xFF

By default the payload is displayed as a hexidecimal values in the Network Console. However we can add a data decoder;

function Decoder(bytes, port) {
  var decoded = {};
  var celciusInt = (bytes[0] << 8) | bytes[1];
  decoded.temp = celciusInt / 100;

  return decoded;

this will auto-magically decode the raw payload and display the real value in The Things Network Console.

More information

You can find more information about using LoRaWAN and The Things Network from CircuitPython in the Adafruit RFM95x tutorial pages. Alternatively you may want to use the RFM95x module using C, in which case you should take a look at Sandeep Mistry's pico-lorawan library, and getting started instructions.

GitHub

https://github.com/aallan/pico-lorawan-circuitpython