DIY gateway with RAK 2245 and Rpi

This chapter contains a description of steps to setup a Raspberry Pi LoRaWAN gateway using Semtech Basic Station with the goal to connect to AWS IoT Core for LoRaWAN.

Required hardware

Required software


Step 1: Install Raspberry Pi OS on the SD Card

  1. Download and install the imager:
  2. From the drop down list, select the Operating System: “Raspberry Pi OS”
  3. select SD Card: “Apple SD Card Reader”,
  4. then click on “Write”.

After SD Card is ready, plug it into Raspberry Pi, connect monitor/keyboard/mouse to RPi, go through the setup process, select a password, write down your IP address (use cmd: “ifconfig” to find that), then enable SSH and SPI:

  1. Launch Raspberry Pi Configuration from the Preferences menu
  2. Navigate to the Interfaces tab
  3. Select Enabled next to SSH and SPI
  4. Click OK

Or simply use this command to turn SSH on:

sudo systemctl enable ssh

From this point forward, there are two choices, one is building BasicStation locally on the Raspberry Pi, the other is building it on the laptop using a cross compiler.

Step 2: clone and build the Basic Station repository

On the terminal of your Raspberry Pi, download Basic Station by issuing this command in your $HOME directory:

git clone
cd basicstation
make platform=rpi variant=std
make clean

Step 3: adjust the settings

In order for RAKWireless Pi Hat to work, we need to make some modifications. First, remove the libraries:

rm ./build-rpi-std/lib/liblgw.a
rm ./deps/lgw/platform-rpi/libloragw/libloragw.a

From your Basic Station directory, use your favorite editor to reduce the value of variable SPI_SPEED from 8000000 to 2000000

vi ./deps/lgw/platform-rpi/libloragw/src/loragw_spi.native.c
# Search in ./deps/lgw/platform-rpi/libloragw/src/loragw_spi.native.c for SPI_SPEED and update to 2000000

Below you see a screenshot of the editor:

Raspberri Pi Shell

Step 4: compile Basic Station on Raspberry Pi

make platform=rpi variant=std

Step 5: create script and config directory

Create a script (called it anywhere you like on the Raspberry Pi with the following content:

#! /bin/bash
echo "$SX1301_RESET_BCM_PIN" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$SX1301_RESET_BCM_PIN/direction
echo "0" > /sys/class/gpio/gpio$SX1301_RESET_BCM_PIN/value
sleep 0.1
echo "1" > /sys/class/gpio/gpio$SX1301_RESET_BCM_PIN/value
sleep 0.1
echo "0" > /sys/class/gpio/gpio$SX1301_RESET_BCM_PIN/value
sleep 0.1
echo "$SX1301_RESET_BCM_PIN" > /sys/class/gpio/unexport

Create a directory to store configuration and credentials for AWS IoT Core for LoRaWAN:

mkdir $HOME/basicstation/iotcorelorawanconfig

Step 6: run Basic Station to identify the Gateway EUI of your gateway

Run script you have created in Step 5 it using sudo:

chmod 755
sudo ./

Now start the Basic Station with test server and credentials:

cd $HOME/basicstation/examples/
RADIODEV=/dev/spidev0.0 ../../build-rpi-std/bin/station

The basic station may stop with an error, this is an expected behavior in this step. Please scroll up to the top and identify the following outputs:

pi@gateway:~/basicstation/examples/ $ RADIODEV=/dev/spidev0.0 ../../build-rpi-std/bin/station
2021-02-25 08:42:15.037 [SYS:INFO] Logging     : stderr (maxsize=10000000, rotate=3)
2021-02-25 08:42:15.037 [SYS:INFO] Station Ver : 2.0.5(rpi/std) 2021-02-25 08:30:03
2021-02-25 08:42:15.037 [SYS:INFO] Package Ver : 1.0.0
2021-02-25 08:42:15.037 [SYS:INFO] proto EUI   : 0:dca6:3245:b3c0	(/sys/class/net/eth0/address)
2021-02-25 08:42:15.037 [SYS:INFO] prefix EUI  : ::1	(builtin)
2021-02-25 08:42:15.037 [SYS:INFO] Station EUI : dca6:32ff:fe45:b3c0

The value after Station EUI (e.g. dca6:32ff:fe45:b3c0 in the example above) the Gateway EUI of your gateway. It will be used in the next step to configure AWS IoT Core for LoRaWAN. Before you can use it in AWS IoT Core for LoRaWAN, please remove all the “:” characters. For example, dca6:32ff:fe45:b3c0 becomes dca632fffe45b3c0.

Step 7: configure AWS IoT Core for LoRaWAN

Please switch to the chapter “Connecting gateway” and perform Lab 1 (add IAM role) and Lab 3 (configure AWS IoT Core for LoRaWAN).

7.1 Check prerequisites
You should have the downloaded the following files to your workstation:

  • nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn.cert.pem: Gateway device certificate file
  • nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn.private.key: Gateway device private key file
  • Trust certificate for LNS (LoRaWAN Network Server) endpoint
  • Trust certificate for CUPS (Configuration and Update Server) endpoint

You should also have noted the CUPS endpoint URL ( CUPS (Configuration and Update Server).

7.2 Prepare Basic Station configuration files
Please rename the files on your workstation as following:

  • Rename nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn.cert.pem to cups.crt
  • Rename nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn.private.key to cups.key

7.3 Create cups.uri file
Please create a file on your workstation called cups.uri and insert the CUPS endpoint URL (e.g. https:\//XXXXXXXXXXXX.cups.lorawan.<region> into that file. Please don’t add a new line at the end.

Step 8: configure Basic Station on your Raspberry Pi

8.1 Copy, cups.uri, cups.crt and cups.key to your Raspberry Pi

If not done yet, please create a $HOME/basicstation/iotcorelorawanconfig directory on your Raspberry Pi:

mkdir $HOME/basicstation/iotcorelorawanconfig
cd $HOME/basicstation/iotcorelorawanconfig

Now please copy the files from your workstation to your Raspberry Pi:

scp pi@<IP address>:~/basicstation/iotcorelorawanconfig
scp cups.uri pi@<IP address>:~/basicstation/iotcorelorawanconfig
scp cups.crt pi@<IP address>:~/basicstation/iotcorelorawanconfig
scp cups.key pi@<IP address>:~/basicstation/iotcorelorawanconfig

8.2 Add station.conf and version.txt files

cd ~/basicstation/iotcorelorawanconfig
cp ~/basicstation/examples/ .
cp ~/basicstation/examples/ .

8.3 Verify files

Please run ls command. You should see the following files in directory ~/basicstation/iotcorelorawanconfig:

  • cups.uri
  • cups.crt
  • cups.key
  • station.conf
  • version.txt
pi@gateway:~/basicstation/iotcorelorawanconfig $ ls
cups.crt  cups.key  cups.uri  station.conf  version.txt

Step 9: start Basic Station

Run the following commands:

cd ~/basicstation/iotcorelorawanconfig
RADIODEV=/dev/spidev0.0 ../../build-rpi-std/bin/station -l 0

The Basic Station should establish connection to AWS IoT Core for LoRaWAN. Please see below a sample output you should see if it works as expected:

cert. version     : 3
serial number     : 06:7F:94:57:85:87:E8:AC:77:DE:B2:53:32:5B:BC:99:8B:56:0D
issuer name       : C=US, O=Amazon, CN=Amazon Root CA 1
subject name      : C=US, O=Amazon, OU=Server CA 1B, CN=Amazon
issued  on        : 2015-10-22 00:00:00
expires on        : 2025-10-19 00:00:00
signed using      : RSA with SHA-256
RSA key size      : 2048 bits
basic constraints : CA=true, max_pathlen=0
key usage         : Digital Signature, Key Cert Sign, C2021-02-25 09:27:24.896 [any:INFO] ./tc.crt:
cert. version     : 3
serial number     : 96:6D:80:27:F3:25:3B:7B:A0:1A:98:E3:EA:01:D5:A2:A3:EE:FD:4F
issuer name       : OU=Amazon Web Services Inc. L=Seattle ST=Washington C=US
subject name      : CN=AWS IoT Certificate
issued  on        : 2020-12-04 17:03:38
expires on        : 2049-12-31 23:59:59
signed using      : RSA with SHA-256
RSA key size      : 2048 bits
basic constraints : CA=false
key usage         : Digital Signature
2021-02-25 09:27:24.896 [AIO:INFO]
2021-02-25 09:27:25.020 [TCE:VERB] Connecting to MUXS...
2021-02-25 09:27:25.553 [TCE:VERB] Connected to MUXS.
2021-02-25 09:27:25.553 [CUP:INFO] Next CUPS interaction delayed by 1d.
2021-02-25 09:27:25.668 [S2E:WARN] Unknown field in router_config - ignored: protocol (0xFD309030)
2021-02-25 09:27:25.668 [S2E:WARN] Unknown field in router_config - ignored: regionid (0xE6FFB211)
2021-02-25 09:27:25.668 [RAL:INFO] Lora gateway library version: Version: 5.0.1;
2021-02-25 09:27:25.670 [RAL:VERB] Connecting to device: /dev/spidev0.0
2021-02-25 09:27:25.671 [RAL:DEBU] SX130x txlut table (0 entries)
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 rxrfchain 0: enable=1 freq=867.5MHz rssi_offset=-166.000000 type=2 tx_enable=1 tx_notch_freq=0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 rxrfchain 1: enable=1 freq=868.5MHz rssi_offset=-166.000000 type=2 tx_enable=0 tx_notch_freq=0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  0: enable=1 rf_chain=0 freq=-400000 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  1: enable=1 rf_chain=0 freq=-200000 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  2: enable=1 rf_chain=0 freq=0 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  3: enable=1 rf_chain=0 freq=200000 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  4: enable=1 rf_chain=0 freq=400000 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  5: enable=1 rf_chain=1 freq=-400000 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  6: enable=1 rf_chain=1 freq=-200000 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  7: enable=1 rf_chain=1 freq=0 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  8: enable=1 rf_chain=1 freq=-200000 bandwidth=2 datarate=2 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX1301 ifchain  9: enable=1 rf_chain=0 freq=0 bandwidth=0 datarate=0 sync_word=0/0
2021-02-25 09:27:25.671 [RAL:VERB] SX130x LBT not enabled
2021-02-25 09:27:25.671 [RAL:VERB] Station device: /dev/spidev0.0 (PPS capture disabled)
2021-02-25 09:27:28.651 [RAL:VERB] Concentrator started (2s979ms)
2021-02-25 09:27:28.651 [S2E:INFO] Configuring for region: EU868 -- 863.0MHz..870.0MHz
2021-02-25 09:27:28.651 [S2E:VERB]   DR0  SF12/BW125
2021-02-25 09:27:28.651 [S2E:VERB]   DR1  SF11/BW125
2021-02-25 09:27:28.651 [S2E:VERB]   DR2  SF10/BW125
2021-02-25 09:27:28.651 [S2E:VERB]   DR3  SF9/BW125
2021-02-25 09:27:28.651 [S2E:VERB]   DR4  SF8/BW125
2021-02-25 09:27:28.651 [S2E:VERB]   DR5  SF7/BW125
2021-02-25 09:27:28.651 [S2E:VERB]   DR6  SF7/BW250
2021-02-25 09:27:28.651 [S2E:VERB]   DR7  FSK
2021-02-25 09:27:28.651 [S2E:VERB]   DR8  undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR9  undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR10 undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR11 undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR12 undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR13 undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR14 undefined
2021-02-25 09:27:28.651 [S2E:VERB]   DR15 undefined
2021-02-25 09:27:28.651 [S2E:VERB]   TX power: 0.0 dBm EIRP
2021-02-25 09:27:28.651 [S2E:VERB]   JoinEUI list: 0 entries
2021-02-25 09:27:28.651 [S2E:VERB]   NetID filter: FFFFFFFF-FFFFFFFF-FFFFFFFF-FFFFFFFF
2021-02-25 09:27:28.651 [S2E:VERB]   Dev/test settings: nocca=0 nodc=0 nodwell=0

If you have a LoRaWAN device sending data, you should see log entries each times the device is forwarding packets to AWS IoT Core LoRaWAN:

021-02-25 09:35:54.490 [any:XDEB] RX mod=LORA f=867500000 bw=125 sz=24 dr=64 808890060180781E02480E60DB517577E3D2098B958B1C46
2021-02-25 09:35:54.490 [S2E:VERB] RX 867.5MHz DR0 SF12/BW125 snr=7.2 rssi=-65 xtime=0x5A00000ED7BCD4 - updf mhdr=80 DevAddr=01069088 FCtrl=80 FCnt=7800 FOpts=[] 02480E60..098B mic=1176275861 (24 bytes)
2021-02-25 09:35:54.490 [AIO:XDEB] [3|WS] > {"msgtype":"updf","MHdr":128,"DevAddr":17207432,"FCtrl":128,"FCnt":7800,"FOpts":"","FPort":2,"FRMPayload":"480E60DB517577E3D2098B","MIC":1176275861,"RefTime":0.000000,"DR":0,"Freq":867500000,"upinfo":{"rctx":0,"xtime":25332748152978644,"gpstime":0,"fts":-1,"rssi":-65,"snr":7.25,"rxtime":1614245754.490867}}

Now you can proceed with the next steps of the workshop in Lab 5: verify gateway connectivity