How to build a LoRaWAN gateway

Revision as of 14:50, 30 August 2019 by Registered User (→‎Configuration of the lora-app-server)

Template:ArticleMainWriter Template:ArticleFirstDraftVersion

This LoRaWAN gateway package contains the frameworks to enable a LoRaWAN gateway that could be run on STM32MP1 Series with a local server or TheThingsNetwork. This package consists in an OpenEmbedded meta layer, named meta-st-stm32mpu-app-lorawan, to be added on top of the STM32MP1 Distribution Package. It brings a complete and coherent, easy to build / install LoRaWAN gateway on STM32MP1 Series.
The meta layer contains frameworks, tools and applications to run the LoRaWAN gateway. Different images are available targeting different use cases such as the single use of the Cortex-A7, the use of the Cortex-A7 and Cortex-M4 side by side and the possibility to choose between an Ethernet or a wifi connection.

STM32MP1 LoRaWan gateway

1. Hardware needed[edit source]

The concentrator LoRaWan RAK831

Concentrator LoRaWan

The antenna

Antenna
Warning white.png Warning
It is mandatory to plug the antenna on the concentrator before powering up the board

The converter board

Converter board

This bundle allows to start with all the hardware needed. (pick your geographic area)

You can now assemble the modules to the board.

Info white.png Information
STM32MP157X-DKX - hardware description and GPIO expansion connector for more informations

This is how it should look:

How to plug the modules

2. Prerequisites[edit source]

Install the STM32MP1 Distribution Package, but do not initialize the OpenEmbedded environment (sourcing the envsetup.sh) before having installed the meta-st-stm32mpu-app-lorawan meta layer (see next chapter).

3. Installation of the meta layer[edit source]

  • To start you need to clone the git repositories to <Distribution Package installation directory>/layers/meta-st.
 cd <Distribution Package installation directory>/layers/meta-st
 git clone https://gerrit.st.com/stm32mpuapp/meta/meta-st-stm32mpu-app-lorawan -b thud

4. Build the software image[edit source]

Setup for the LoRaWan gateway build environment

 cd <Distribution Package installation directory>

Different OpenSTLinux expansion packages are available to target different use cases. The following commands need to be executed in the build environment.
You can choose between two different machines:

  • stm32mp1-lorawan-a7 to only use the Cortex-A7 for the whole application's process
 DISTRO=openstlinux-weston MACHINE=stm32mp1-lorawan-a7 source layers/meta-st/scripts/envsetup.sh
 bitbake st-image-lorawan
  • stm32mp1-lorawan-m4 to use the Cortex-A7 and the Cortex-M4 side by side. The Cortex-A7 to manage the communication with The Things Network's server and the Cortex-M4 to communicate in real time with the concentrator.
 DISTRO=openstlinux-weston MACHINE=stm32mp1-lorawan-m4 source layers/meta-st/scripts/envsetup.sh
 bitbake st-image-lorawan
Info white.png Information
Note that building the image could take more than 2 hours depending on your host computer performances.

5. Flash the built image[edit source]

Follow this link to know how to flash the built image.

6. Launch the LoRaWAN gateway's software[edit source]

  • Power the board
  • Depending on how you want to connect your board to the internet
  • Ethernet connection
Connect the ethernet cable
 cd /usr/local/lorawan-gateway/
 ./LoRaWAN_gateway_launcher.sh ethernet
  • Wifi connection
Do not connect the ethernet cable
 cd /usr/local/lorawan-gateway/
 ./LoRaWAN_gateway_launcher.sh wifi
  • During the launcher script, enter your preferred server, up and down ports when it is asked.
Info white.png Information
If you want to use https://www.thethingsnetwork.org in europe

Their server is router.eu.thethings.network
Up and down ports 1700

Info white.png Information
If you want to use a local LoRaWAN server

The server is localhost
Up and down ports 1700

  • Important: copy your EUI before the restart of the gateway

All those informations can be found or changed manually in the global_conf.json and local_conf.json files in /usr/local/lorawan-gateway/ on the board.

Info white.png Information
Only if you choose a WiFi connection:

enter your SSID and password
To change those informations, refer to How_to_setup_wifi_connection


Your gateway will reboot and after a short time, you we will be able to receive or transmit informations through LoRaWAN.

7. Source code of the firmware running on the M4[edit source]

If you want to access or modify the code of the firmware running on the M4

 cd <working directory>
 repo init -u https://gerrit.st.com/stm32mpuapp/fw/manifests -b lorawan -m default.xml
 repo sync


8. LoRaWAN Node[edit source]

This chapter is an example of how to create a test node. An important thing is that you can choose how to define the Device EUI, address, network session key and application session key.

  • Either by defining them by your own in the Commissioning.h of your code and then copy the fields during the registration of the node on TheThingsNetwork or the local server
  • Either to let TheThingsNetwork or the local server defining them randomly during the registration of the node and then put them in the Commissioning.h of your code before compiling it.

Example for creating a test device. Be careful with your end node’s applications, the duty cycle is regulated by governments. This one is regulated so that it can send a new LoRaWAN message minimum every 3 minutes with this low amount of data. But you can change the APP_TX_DUTYCYCLE variable if you want to lengthen the delay between two dispatch.

9. LoRaWAN server[edit source]

You can choose between two different types of server. The following instructions show how to use them.

9.1. How to proceed on The Things Network[edit source]

Login or create an account on https://www.thethingsnetwork.org

  1. Go into your CONSOLE.
  2. Clic on GATEWAYS.
  3. Clic on register gateway.
  4. Check I'm using the legacy packet forwarder and enter your EUI you previously copied. If it is not available, create one and change it in the global_conf.json and local_conf.json files in /usr/local/lorawan-gateway/. You will need to reboot your board afterwards.
  5. Enter a description of the gateway, this is the name you can see on the map of the site.
  6. Select your Frequency Plan.
  7. Select your Router (for europe choose ttn-router-eu).
  8. You can place your gateway on the map by clicking on it or by entering directly the precise coordinates.
  9. Tell if your antenna is indoor or outdoor.
  10. Not mandatory: for your application's devices, you can use cayenne, this is a framework that store the data and you can easily visualize them.

9.2. How to proceed with the LoRaWAN local server[edit source]

9.2.1. Configuration of the server[edit source]

A local LoRaWAN server is available natively with this layer.

Warning white.png Warning
You can find more informations about this server here.

The following instructions are the minimum necessary to launch the server.

In the first part you need to start postgresql in order to use psql

 sudo -i -u postgres
 initdb -D /var/lib/postgresql/data
 pg_ctl -D /var/lib/postgresql/data -l logfile start
Info white.png Information
You need to do the sudo and pg_ctl lines above in case of a reboot of the board.

You need now to create all the roles and databases for this application.

 psql
 create role loraserver_as with login password '<PASSWORD>';
 create role loraserver_ns with login password '<PASSWORD>';
Info white.png Information
<PASSWORD> is your password
 create database loraserver_as with owner loraserver_as;
 create database loraserver_ns with owner loraserver_ns;
 \c loraserver_as
 create extension pg_trgm;
 create extension hstore;
 \q
 logout
 cp /etc/loraserver/config/eu868.toml /etc/loraserver/loraserver.toml
Info white.png Information
Note that you need to take an other .toml if you are note in europe.

You can now start the different parts of this local LoRaWAN server.

 sudo systemctl enable lora-gateway-bridge
 sudo systemctl start lora-gateway-bridge
 sudo systemctl enable loraserver
 sudo systemctl start loraserver
 sudo systemctl enable lora-app-server
 sudo systemctl start lora-app-server

Your Local LoRaWAN is now running.

9.2.2. Configuration of the lora-app-server[edit source]

To access your application site on a web browser, with a computer on the same network.

 ifconfig 

The line above allows to know the IP address.
Then access the application web site : IP_address:8080. By default the login and password are admin.
On the application site:

  • Go to Network-servers, click ADD and then put localhost:8000 for the name and the server. Click on ADD NETWORK-SERVER.
Network-servers configuration
  • Go to Service-profiles, click CREATE. Choose a name, take the localhost:8000 server. Select Add gateway meta-data. All the rest is empty. Click on CREATE SERVICE-PROFILE.
Service-profiles configuration
  • Go to Device-profiles, click CREATE. Enter the informations relative to your Node. You need to save and re-click on you Node to unlock some configuration features. Click on CREATE DEVICE-PROFILE.
Device-profiles configuration

If you have a decode or Encode fonction for your node, you can put them in the CODEC section.

Device-profiles codec

Decode function example:


function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};
// Decode bytes to int
var batteryInt = bytes[7]
var humidityInt = (bytes[5] << 8) | bytes[6];
var pressureInt = (bytes[1] << 8) | bytes[2];
var temperatureInt = (bytes[3] << 8) | bytes[4];
// Decode int to float
decoded.battery = (batteryInt*100)/254 // Battery in %
decoded.humidity = humidityInt / 10; // Humidity in %
decoded.pressure = pressureInt / 10; // Pressue in hPa
decoded.temperature = temperatureInt / 100; // Temperature in °C
return decoded;
}
  • Go to Gateways, click CREATE. Choose a name, a description and take the localhost:8000 server. No gateway profile and discovery enabled is needed. Click on CREATE GATEWAY.
Gateway configuration
  • Go to Applications, click CREATE. Choose a name, a description, take your service-profile and a codec if you use one. Click on CREATE APPLICATION.
Application configuration
  • Click on your application's name. Click CREATE in the DEVICES menu. Choose a name, a description, enter the Device EUI of your Node and you Device-profile. Check Disable frame-counter validation and then click on CREATE DEVICE.
Node configuration
  • Click on your created node just above. In ACTIVATION, enter the informations reliative to this node. The Uplink frame-counter will incremente itself. Click on (RE)ACTIVATE DEVICE.
Node activation
  • You can now visualize your Node frames in LORAWAN FRAMES on each node section.
Device Data

10. Field test[edit source]

A successful 10 kilometers distance test without obstacles have been realized.

STM32MP1 LoRaWan gateway during the distance test
STM32 LoRaWan node during the distance test
STM32MP1 LoRaWan gateway distance test
No categories assignedEdit