How to connect a Raspberry Pi Pico to LoRaWAN

LoRaWAN is a long-range, low-power protocol with a low bandwidth. With a good antenna, you might get 15 kilometres of range from an off-the-shelf LoRa radio, which is intended to link battery-powered remote sensors to the internet through a gateway. In the negative side, usable bandwidth would be calculated in bytes rather than megabytes or even kilobytes.

Sandeep Mistry, the author of the Arduino LoRa library, who also recently added Ethernet support for Pico, put together support for LoRa compatibility for Pico. Using the Semtech SX1276 radio module, his library adds LoRa support to Pico and other RP2040-based boards. As a result, breakout boards such as Adafruit’s RFM95W and LoRa FeatherWing are completely supported.

Is there any LoRaWAN coverage?

You’ll need to be within range of a LoRa gateway to use a LoraWAN-enabled Pico. The Things Network, an open-source community LoRaWAN network with worldwide reach, comes to the rescue.

It’s likely that you’re already covered, depending on where you’re based. If you aren’t, though, you shouldn’t be too concerned.

The days when a LoRaWAN base station will set you back several thousand dollars are long gone. A LoRa portal is now available for around £75. However, a few years ago, created my own portal. Perhaps unsurprisingly, it was built around a Raspberry Pi.

Obtaining a source

git clone — recurse-submodules https://github.com/sandeepmistry/pico-lorawan.git cd pico_lorawan

Before continuing, double-check that your PICO_SDK_PATHis fixed. If you're using a Raspberry Pi and have run the pico_setup.sh script or followed the directions in Pico Getting Started guide, for example, you'd set the PICO_SDK_PATH to

export PICO_SDK_PATH = /home/pi/pico/pico-sdk

After that, both the library and the sample programmes are ready to be developed. But first, we’ll need to setup the cloud infrastructure where our data will be stored, as well as link our LoRa radio board to our Raspberry Pi Pico.

Create a programme.

To provide it with this knowledge, we must first construct an application inside The Things Network Console. To do this, simply enter a specific Application ID string — whatever will do — and the console will create an Application EUI and a default Access Key, which we will use to register our devices to our application.

If we’ve registered an application, what we have to do is register our particular device — or, in the future, a group of devices — to that application so that the backend knows where to route packets from that device.

Device Registration

Our remote device is identified by the Device ID, which is a human-readable string. Since the Adafruit RFM9W breakout board comes with a sticker in the same bag as the radio with a special identifier written on it, we can use it to postpend a string to uniquely mark our Pico board, so we end up with something like pico-xy-xy-xy-xy-xy-xy

In addition, we’ll need to create a Device EUI 2. This is a 64-bit one-of-a-kind identifier. To produce our Device EUI, we can once again use the unique identifier from the sticker, except this time we can simply pad it with two leading zeros, 0000XYXYXYXYXYXY To create the Device EUI, you may also use pico_get_unique_board_id( ).

If you look at your Device page after registration, you’ll see that you’ll need the Application EUI2 and Application Key2 to allow your board to communicate with the LoRa network, or more specifically, to allow the network to correctly route packets from your board to your application.

2 Write down your Device EUI, Application EUI, and Application Key.

Putting items together on a breadboard

Fortunately, it is not a major issue, but you would most likely need to obtain a number of male-to-female jumper wires in addition to your breadboard. Connect the RFM95W module to your Raspberry Pi Pico now. The pin mapping between the breakout board’s pins and your Pico should be as follows:

Physical pin mapping, RP2040 pin mapping, SX1276 module mapping, and RFM95W This are the library’s default pins, which can be modified in apps.

Computer development and deployment

void internal_temperature_init() { adc_init(); adc_select_input(4); adc_set_temp_sensor_enabled(true); } float internal_temperature_get() { float adc_voltage = adc_read() * 3.3f / 4096; float adc_temperature = 27 - (adc_voltage - 0.706f) / 0.001721f; return adc_temperature; }

Shift the directory in your checkout to the otaa_temperature_led sample programme. Since this example employs OTAA, we’ll need the Device EUI, Application EUI, and Application Key that we created.

otaa_temperature_led

Adjust the Area, REGION, DEVICE_EUI, APP_EUI values in the config.h file to the values displayed in the Network Console. Instead of the byte array representation, the code expects the (default) string format, with no spaces between the hexadecimal digits.

#define LORAWAN_REGION LORAMAC_REGION_EU868 #define LORAWAN_DEVICE_EUI “Insert your Device EUI” #define LORAWAN_APP_EUI “Insert your Application EUI” #define LORAWAN_APP_KEY “Insert your App Key” #define LORAWAN_CHANNEL_MASK NULL

I’m in the United Kingdom, and my LoRa radio broadcasts at 868MHz, so I’ll set my region to LORAMAC_REGION_EU868. If you live in the United States, you must set your country to LORAMAC_REGION_US915.

After you’ve modified the config.h file, you may proceed to construct the sample applications.

If all goes well, you should have a UF2 file named pico_lorawan_otaa_temperature_led.uf2 in build/examples/otaa_temperature_led/. You can now load this UF2 file into your Pico as normal.

Take your Raspberry Pi Pico board and a micro USB cable with you. Connect the cable to your Raspberry Pi or laptop, and then press and hold the BOOTSEL button on your Pico while inserting the other end of the micro USB cable into the board. Then, once the board is plugged in, release the switch.

RPI-RP2 can appear as a disc volume on your desktop. To open it, double-click it and then drag and drop the UF2 file into it. If you are experiencing difficulties, please see Chapter 4 of our Getting Started guide for more details.

Your Pico is now running your LoRaWAN programme, and if you open a USB Serial link to it, you should be able to see some debugging stuff. Start minicom in a Terminal pane.

Data Transmission

The payload value is the hexadecimal temperature determined by the Raspberry Pi Pico’s internal temperature sensor. It’s a little beyond the reach of this post, but you can now add a decoder and integrations that enable you to decrypt hexadecimal data into human-readable data and then save it to a database, among other things. To demonstrate the strength of what you can do here, go to your application’s “Payload Formats” tab and enter the following Javascript in the “decoder” box:

Scroll down and press the green “save payload functions” button.

Returning to the “Data” tab, you can note that the payload, in hexidecimal, is now post-pended with the temperature in degrees Celsius. Our basic decoder has converted our payload back into a Javascript object.

Sending commands

Go to the Network Console’s Device tab and type “01” into the Downlink Payload box before clicking the “Send” button. Then choose the Data column. You should see a line that says “Download planned,” and if you keep an eye on it, you should see the byte downlinked. When this occurs, the on-board LED on your Raspberry Pi Pico can illuminate! Returning to the Network Console and entering “00” into the Payload box would (at some point) switch off the Pico’s LED.

Keep in mind that LoRaWAN has a long range but a limited bandwidth. A downlinked order should not be expected to be responded to immediately.

The OTAA example framework is a great starting point for you to build on, as it allows you to take data and send it to the cloud via LoRa, as well as send commands back from the cloud to your LoRa-enabled Pico.

During this week’s Arm Innovation Coffee on Thursday at 10:00 a.m. PDT (18:00 a.m. BST), there will be more talk about the Things Network and a live demonstration of LoRaWAN from a Raspberry Pi Pico (29 April).

Finishing up

The Getting Started page contains all of the documentation, as well as a wealth of other resources and connections. If you lose track of where it is in the future, you can still access it from your Pico: simply press and hold the BOOTSEL button on your Pico, plug it into your laptop or Raspberry Pi, and then release the button to access the website. Open the RPI-RP2 volume and then double-click the INDEX.HTM file.

This will still redirect you to the Getting Started tab.

Credit: Alasdair Allan

Originally published at https://shopmakergenix.blogspot.com.

SB Components — We make things intelligent using Raspberry Pi, Micro:Bit, Arduino, robotics, etc. We offer a variety Robots DIY.