LoRaWAN on Raspberry Pi

September 13, 2024 by
Boot & Work Corp. S.L., Arnau Tena

LoRaWAN

LoRaWAN (Long Range Wide Area Network) is a wireless communication protocol designed for low-power, long-range data transmission. It is used primarily in IoT (Internet of Things) applications where devices need to send small amounts of data over long distances.

LoRaWAN operates on unlicensed radio frequencies and can support a large number of devices with low data rates, making it ideal for applications like smart cities, agriculture, industrial monitoring, and environmental sensing. The protocol focuses on energy efficiency, enabling devices to operate on small batteries for years.

Hardware used

The idea behind this blog is to read data from an analogic sensor and send it to the LoRaWAN network. To do this, we've used:

Gateway configuration

To configure the gateway we've followed this easy guide:

Gateway configuration

Python files

To be able to read data from an analog sensor we first need to install Industrial Shield's python library. Instructions on how to install it can be found here:

python3-librpiplc

After that, you'll need to download three python files. The files lora.py and lorawan.py are libraries used to communicate with the LoRa module and LoRaWAN, analogtest.py reads from an analog input and sends it to LoRaWAN. Click on the icons to download the files, be sure to place them in the same folder:

analogtest.py

lora.py

lorawan.py


After you've downloaded the files they will not yet be ready to be executed. There are variables that have to be set up to match with your end device on The Things Network.

The file analogtest.py is configured to use the expansor 1 slot, so if your PLC has the LoRa module installed on the slot 2, you'll have to change ttySC0 for ttySC1 in line 8. Expansor 1 is the one closest to the Ethernet port, in case you are not sure which expansor slot your LoRa module is using.

Also note that these files are set up to use the European frequency. If you need to use a different frequency, you can go to line 300 of lora.py and change it .

Creating the end device

To create the end device, you'll need to go to the Applications tab in The Things Stack Sandbox and add a new application. Once created, you can configure a new end device by clicking on "+ Register end device".

It's very important to set the following settings correctly.

  • Input method: Select "Enter end device specifics manually".
  • Frequency plan: If you are in Europe, select "Europe 863-870 MHz (SF9 for RX2 - recommended)".
  • LoRaWAN version: 1.0.2.
  • Regional Parameters version: 1.0.2 revision B.
  • JoinEUI: This 64bit identifier has to match with the AppEUI from analogtest.py defined in line 11. 
  • DevEUI: As with JoinEUI, this has to match the DevEUI from analogtest.py defined in line 12.
  • AppKey: Also has to match with the AppKey defined in line 10 of analogtest.py.

You can use the JoinEUI, DevEUI and AppKey already defined in analogtest.py but, as long as they match with the end device configuration, you can use any values you want.

After creating the end device, go to the "Payload formatters", select "Custom Javascript formatter", paste the following code and save the changes.

function decodeUplink(input) {
  return {
    data: {
      bytes: String.fromCharCode.apply(null, input.bytes)
    },
    warnings: [],
    errors: []
  };
}

OTAA & ABP

OTAA (Over-the-Air Activation) and ABP (Activation by Personalization) are two methods for connecting LoRaWAN devices to a network:

  1. OTAA:
    • Devices dynamically join the network by exchanging keys with the server.
    • More secure, as keys are unique and can be refreshed periodically.
    • Requires more initial communication (join requests).
  2. ABP:
    • Devices are pre-configured with fixed keys and don’t need to join.
    • Less secure, as keys are static and can be reused.
    • Quicker startup since no join process is required.

The steps explained in the Creating an end device section are for OTAA by default. If you want to use ABP, after you set up the Regional Parameters version you'll want to click on Show advanced activation and switch to ABP.

If you've used OTAA with the same LoRa module before, you may have to go into Settings -> Network Layer and click on Reset session and Mac state to get it to work properly.

analogtestABP.py

Testing

After configuring the end device you can begin the testing with analogtest.py.

First we set the reset pin to high to enable the LoRa module:

  • For the expansor 1, execute sudo ~/test/[RPIPLC_Version]/[RPIPLC_Model]/set-digital-output EXP1_RST 1
  • For the expansor 2, execute sudo ~/test/[RPIPLC_Version]/[RPIPLC_Model]/set-digital-output EXP2_RST 1

Replace [RPIPLC_Version] and [RPIPLC_Model] with the corresponding ones for your PLC.

Navigate to the folder that contains the three python files and execute the file with python analogtest.py.

It will take a few seconds to apply the configuration to the LoRa model, but after a short while it should start sending messages like this:

In the page of the end device you should start seeing the data that's being sent to LoRaWAN:

​Search in our Blog

Boot & Work Corp. S.L., Arnau Tena September 13, 2024
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>