Create a Raspberry Pi based LoRaWAN sensor.
Initiated by Michel of Lug Limbe (Map).
One or more Pi LoRa devices send data packets to a LoRa gateway, using LoRa long range radio.
The LoRa gateway forwards received data packets to The Things Network (TTN), a LoRa cloud backend.
The client computer, an Internet-connected Pi or laptop, can get the data from the backend, if it is authorised.
We use a Pi with a RN2483 LoRa module as our LoRaWAN device.
Create an SD Card with Pi Imager.
- Choose Pi OS Lite, 32-bit
- Create a user, e.g. pi
- Set Wi-Fi SSID, PW
- Enable SSH
- https://www.tindie.com/products/drazzy/lorawan-rn2483rn2903-breakout-board-assembled/ (RN2483)
- https://www.adafruit.com/product/1858 (Antenna)
Based on https://pinout.xyz/pinout/uart
Raspberry Pi | RN2483 |
---|---|
RX | TX |
TX | RX |
RTS | CTS |
CTS | RTS |
- | (RST) |
3V3 | 3V3 |
- | (NC) |
GND | GND |
$ sudo apt-get update
$ sudo apt-get install python3-pip
$ pip3 install wiringpi
$ sudo nano /boot/config.txt
enable_uart=1
$ sudo raspi-config # > Interface Options > Serial Port >
Login shell ...: No >
Serial port ...: Yes
$ sudo reboot
This section shows the steps to configure the TheThingsNetwork (TTN) backend.
An application is needed per type of LoRaWAN device you want to connect, e.g. temperature sensor devices.
- Create App
A separate device is needed for each physical LoRaWAN device you want to connect, e.g. sensor-1 and sensor-2.
- End devices
- Add end device
- Try manual device registration
- Frequency plan: Europe (recommended)
- LoRaWAN Version: 1.0.3
- Show advanced activation
- Activation by personalisation (ABP)
- Device Address: click icon to generate
- AppSKey: click icon to generate
- NwkSKey: click icon to generate
- End device ID: pi-lora-device-0
- Register end device
- General settings
- Network layer > Expand
- Advanced MAC settings
- Reset frame counter [x]
- Save changes
- Overview
- Device Address (This is your TTN_DEV_ADDR, required below)
- AppSKey: click icon to show key (This is your TTN_NWK_S_KEY)
- NwkSKey: click icon to show key (This is your TTN_APP_S_KEY)
$ wget https://raw.githubusercontent.com/tamberg/pi-lora/main/rn2483.py
$ cat rn2483.py
Get ABP keys from TTN console.
$ nano rn2483.py
TTN_DEV_ADDR = "00000000" # TODO
TTN_NWK_S_KEY = "00000000000000000000000000000000" # TODO
TTN_APP_S_KEY = "00000000000000000000000000000000" # TODO
$ python3 rn2483.py
The output should look something like this
sys get ver
RN2483 1.0.1 Dec 15 2015 09:38:09
sys get hweui
0000000000000000
mac reset 868
ok
mac set nwkskey 00000000000000000000000000000000
ok
mac set appskey 00000000000000000000000000000000
ok
mac set devaddr 00000000
ok
mac set adr off
ok
mac set ar off
ok
mac set pwridx 1
ok
mac set dr 5
ok
mac save
ok
mac join abp
ok
accepted
mac tx uncnf 1 000000000000000000000000000000000000
ok
mac_tx_ok
Check the TTN console to see data packets sent by your device.
Install a service to run rn2483.py whenever the Pi is plugged in.
$ sudo nano /lib/systemd/system/rn2483.service
[Unit]
Description=RN2483 Pi LoRa service
Requires=dev-serial0.device
After=dev-serial0.device
[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 rn2483.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
$ sudo systemctl daemon-reload
$ sudo systemctl enable rn2483.service
$ sudo systemctl start rn2483.service
$ ps aux | grep rn2483
pi 295 0.4 1.5 14372 6848 ? Ss 09:07 0:04 /usr/bin/python3 rn2483.py
$ sudo systemctl stop rn2483.service
$ sudo rm /etc/systemd/system/multi-user.target.wants/rn2483.service
$ sudo rm /lib/systemd/system/rn2483.service
$ journalctl -u rn2483.service
E.g. using an AP9 junction box and this laser-cut adapter:
E.g. a PIR sensor, use pir.py to test it, then try to run rn2483-pir.py.
Create a AP9 cover matching the sensor, e.g. this laser-cut cover.
This Python program can run on a Pi or any other computer.
$ pip3 install paho-mqtt
$ pip3 install pybase64
$ wget https://raw.githubusercontent.com/tamberg/pi-lora/main/ttn-client.py
$ cat ttn-client.py
See TTN console.
$ nano ttn-client.py
TTN_APP_ID = "..." # TODO, e.g. pi-lora-app
TTN_DEVICE_ID = "..." # TODO, e.g. pi-lora-device
TTN_APP_API_KEY = "..." # TODO, see Integrations > MQTT
$ python3 ttn-client
That's it! Data packets sent from your Pi LoRa device are now received by the client.
To run the Pi LoRa gateway, set up a Wi-Fi network with matching SSID and password.
Then plug in the Pi LoRa gateway and put the antenna in an upright position.
Make sure to choose an elevated location to reach maximum range.
Done! (The remaining steps are for setup from scratch.)
- https://shop.imst.de/wireless-modules/lora-products/8/ic880a-spi-lorawan-concentrator-868-mhz
- https://shop.imst.de/wireless-modules/accessories/20/u.fl-to-sma-pigtail-cable-for-ic880a-spi (Cable)
- https://shop.imst.de/wireless-modules/accessories/19/sma-antenna-for-ic880a-spi-wsa01-im880b-and-lite-gateway (Antenna)
E.g. an AP10 junction box and this laser-cut adapter.
$ sudo raspi-config # enable SPI
$ git clone https://github.com/ttn-zh/ic880a-gateway.git ~/ic880a-gateway
$ cd ~/ic880a-gateway
$ sudo ./install.sh spi
$ cat /opt/ttn-gateway/global_conf.json
$ sudo journalctl -u ttn-gateway --since="1h ago"
- http://www.tamberg.org/fhnw/2021/hs/IoT08LoRaWANConnectivity.pdf
- http://www.tamberg.org/fhnw/2021/hs/IoT09Dashboards.pdf
- https://pinout.xyz/pinout/uart
- https://github.com/raspberrypi/documentation/blob/develop/documentation/asciidoc/computers/configuration/uart.adoc