Getting Started with the Raspberry Pi LoRa Node pHAT

Our IoT LoRa Node pHAT allows you to create an inexpensive LoRa node, compatible with The Things Network, in conjunction with a Raspberry Pi or other single board computers. This pHAT allows quicker prototyping as it has the LoRa stack on the chip. Add sensors, buttons and more to complete your LoRa network!

Features

  • Uses RAK811 LoRa Radio with full LoRaWAN Stack embedded.
  • Communicates with the Raspberry Pi over UART only using a total of 3 GPIO pins for the module.
  • Support LoRaWAN connections and LoRa P2P modes.
  • u.FL connector and on-board antenna allowing different antennas to be used or integrated into enclosures with external antennas.
  • Also adds 8 extra GPIO pins controllable via the UART including 2 pins which can read analogue sensors!
  • Low power – uses less than 50mA during transmission

What you need

Note: The LoRa node pHAT will work with any Raspberry Pi with a 40 way header

Setting up a TTN Device

Step 1 - Login to TTN

First go to https://console.thethingsnetwork.org

  • If not logged in login using your TTN Username and password.
  • If you don’t have an account, click create an account and follow the steps to setup a TTN Account

Step 2 - Create an application

  • Click on applications

  • Then click “add application

  • For application ID use “my-iotlora-application-<Yournamehere>“, in my case it is  “my-iotlora-application-chris” or use something unique to you.
  • Description: use something like “PiSupply IoT nodes” 
  • Handler registration use “ttn-handler-eu
  • Click “add Application

  • Next click payload formats
    • Click the custom bar and change it to Cayenne LPP

Step 3 - Register new device

  • Now click “Devices” in the toolbar
    • Then “register device

  • Here create a unique ID, in my case I’ve done “chris-lora-node
  • For device EUI click the two arrows to the left to generate one (we will change this later)
  • Then click “Register

  • In this device, next we want to click “Settings
    • Change the activation method to OTAA by clicking the OTAA Button (This may be the default setting)
    • For development we want to disable frame counter checks, do this by unticking this field at the bottom

  • Now TTN Is ready for our Node pHAT! Keep this page open in a tab for later on

Install RAK811 Python Library

Step 1 – Download and flash the latest Raspbian OS image to your micro SD card. You can download the latest image from here – https://www.raspberrypi.org/downloads/raspbian/

Note: We recommend to download the Raspbian Lite version

Step 2 – Firstly you will need to connect your Raspberry Pi to the Wi-Fi. In the terminal type in the following command:

sudo raspi-config

Select option 2 “Networking Options” then N2 “Wi-Fi” then follow the instructions to enter your Wi-Fi credentials and connect to the Wi-Fi for internet access.

 

 

 

 

 

 

Step 3 – Before we reboot we need to enable the hardware serial on the Raspberry Pi and disable the serial console. So you can go back to the main menu and select option 5 “Interfacing Options” then P6 “Serial” and then select “No” and then “Yes“.

 

 

 

 

 

 

 

 

 

 

 

 

Step 4 – Now before we reboot, lets swap the serial interfaces over for the better one, which is by default assigned to the Bluetooth controller. Open up the terminal window and type in the following command:

sudo nano /boot/config.txt

Scroll down to the bottom and add the following line:

dtoverlay=pi3-miniuart-bt

Save and exit with “CTRL+O” and “CTRL+X

Reboot your Raspberry Pi for all the changes to take effect.

Step 5 – Now lets install the RAK811 Python library. In the terminal window type in the following commands:

sudo apt update

sudo apt upgrade 

sudo apt install python3-pip

sudo pip3 install rak811

Creating a Simple program

There are two different methods for joining the TTN network from your Raspberry Pi pHAT node; OTAA and ABP.

Over-the-Air Activation (OTAA)

Over-the-Air Activation (OTAA) is the preferred and most secure way to connect with The Things Network. Devices perform a join-procedure with the network, during which a dynamic DevAddr is assigned and security keys are negotiated with the device.

Activation by Personalization (ABP)

In some cases you might need to hardcode the DevAddr as well as the security keys in the device. This means activating a device by personalization (ABP). This strategy might seem simpler, because you skip the join procedure, but it has some downsides related to security.

In the examples below we will show you how to connect to both methods.

OTAA Connection

We have already setup the “Device” in the TTN Console to use the OTAA method as this much more preferred because its more secure. First we need to find out what our device EUI is for our pHAT node and add that to the device on the TTN Console. Open up the terminal window and type the following two lines to get the device EUI:

rak811 hard-reset
rak811 get-config dev_eui

Note: The first line resets the RAK811 so you only need to run this once per power up.

Got to your TTN Console and open your device, then go to settings and add the device EUI to the field and click “Save“.

Go back to your Raspberry Pi and open up a terminal window and type in the following to create a Python script:

sudo nano lora_node.py

Add the following replacing the credentials with your device credentials:

#!/usr/bin/env python3
from rak811 import Mode, Rak811

lora = Rak811()
lora.hard_reset()
lora.mode = Mode.LoRaWan
lora.band = 'EU868'
lora.set_config(dev_eui='xxxxxxxxxxxxxxxx',
app_eui='xxxxxxxxxxxxxxxx',
app_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
lora.join_otaa()
lora.dr = 5
lora.send('Hello world')
lora.close()

Now you can run the program with the following and you should see in the TTN Console > Device > Data the received data that was sent from your node to a Gateway.

If you are using US915 or AU915
You may require to add the following lines just below the lora.set_config block where the keys are set.

lora.set_config(ch_mask = '0,FF00')
lora.set_config(ch_mask = '1,0000')
lora.set_config(ch_mask = '2,0000')
lora.set_config(ch_mask = '3,0000')
lora.set_config(ch_mask = '4,0000')

 

ABP Connection

To use the ABP method we first must change this in the device settings in the TTN console. Go to your Device and click on Settings. Here you can select ABP, where it will generate new session keys.

Make sure you save the settings at the bottom of the page.

Go back to your Raspberry Pi and type in the following:

sudo apt-get install git

git clone https://github.com/AmedeeBulle/pyrak811.git

cd pyrak811

cd examples

Now lets add our session keys to the following file:

sudo nano ttn_secrets_template.py

Save and exit: CTRL+O and CTRL+X

cp ttn_secrets_template.py ttn_secrets.py

If you are using US915 or AU915
You may require to add the following lines just below the lora.set_config block where the keys are set.

lora.set_config(ch_mask = '0,FF00')
lora.set_config(ch_mask = '1,0000')
lora.set_config(ch_mask = '2,0000')
lora.set_config(ch_mask = '3,0000')
lora.set_config(ch_mask = '4,0000')

Now run the following example, which will send some random data:

sudo python3 abp.py

You should see the following in the TTN console:

Summary

Hopefully after following this tutorial you should have been able to setup your Raspberry Pi pHAT node and send packets of data to your Gateway using The Things Network. If for whatever reason that your node is not working or you need help with something then please email us at sales@pi-supply.com or visit our Discord channel – https://pisupp.ly/chat

First published at 12:41pm on May 23, 2019
Last updated at 2:18pm on August 19, 2019