Private LoRaWAN: MikroTik setup and connect to ChirpStack

Before we start to set up MikroTik, make sure you have successfully installed ChirpStack. With topology described on following diagram you are using MikroTik as packet forwarder and all work with messages such as decryption and all working with payload is done on ChirpStack LoRa server.

Prepare ChirpStack

Step 1: Create network server

In menu Network-servers click Add. Fill name of your network-server and in Network-server box fill localhost:8000.

If new Network-server is accessible, you should see it in Network-servers list. If not, look into logs by following command:

sudo journalctl -u chirpstack-network-server.service -f

Step 2: Create Service Profile

Navigate to Service-profiles -> Add, fill name with your own and as Network-server, choose previously added one. Then click CREATE-SERVICE-PROFILE. More about service can be found on ChirpStack documentation.

Step 3: Create Device Profile

Navigate to Device-profiles -> Add. Device-profile defines parameters of device we will be connecting to LoRaWAN. As example will be used HARDWARIO KIT LoRa Climate Monitor. Fill Device-profile same as following screenshots.

On JOIN (OTAA / ABP) tab check Device supports OTAA. There are two types of network activation and we will choose OTAA for this tutorial.

Last tab we will change is CODEC. Here you define how you Decode Radio buffer into JSON human friendly object. However you can also define Encoding if you need to communicate to device and from JSON object make LoRa byte buffer.

In CODEC section choose Custom JavaScript codec functions and paste following code in Decode section:

// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes, variables) {
var header = bytes[0];
var voltage = bytes[1] / 10.0;
var orientation = bytes[2];
var temperature = ((bytes[3] << 8) | bytes[4]) / 10.0;
var humidity = bytes[5] / 2;
var illuminance = ((bytes[6] << 8) | bytes[7]);
var pressure = ((bytes[8] << 8) | bytes[9]) * 2.0;
var decoded = {
header: header,
voltage: voltage,
orientation: orientation,
temperature: temperature,
humidity: humidity,
illuminance: illuminance,
pressure: pressure
};
return decoded;
}

Click CREATE DEVICE-PROFILE and you should see your newly created Device-profile now.

Step 4: Add Application

Navigate to Applications -> Create. Name your Application, describe it and choose as Service-profile created Service-profile from previous chapter. Then hit CREATE APPLICATION.

Setup MikroTik

Now we will set up MikroTik as LoRaWAN gateway. MikroTik in this mode will forward radio traffic on network to ChirpStack (LoRaWAN server). You can set up MikroTik via web interface, WinBox, or mobile app.

Step 1: Connect to MikroTik Setup WiFi

Because we will be setting up MikroTik with WiFi, download MikroTik app (Android | iOS). After unpacking, MikroTik will boot in factory default mode and provide WiFi access point. Connect to this access point to configure the LoRaWAN gateway.

Step 2: Connect to MikroTik administration

Open app and choose discover tab. Wait until your MikroTik won't show up.

Then click on a discovered one and login with default username - admin and leave password blank. After a while, you will see the MikroTik administration.

Step 3: Quick setup

Click on Quick setup, follow instructions and plug in Ethernet cable into MikroTik. In the end of the quick setup, you should find MikroTik in your network, e.g. via Angry IP Scanner. If you have problems seeing or login to administration from your network, try to switch off Firewall rules in MikroTik.

Step 4: Login with web interface

Now we will continue the setup procedure in the web interface. In Quick setup you change admin password so use it now in web administration login.

Step 5: Register LoRaWAN server

In LoRa menu section choose Servers tab, click on the Add New button.

Fill name, IP address of the LoRaWAN server and keep ports to 1700.

Step 6: Disable LoRa gateway

Navigate to Devices section and disable LoRa Gateway by clicking on the D button.

Step 7: Edit gateway

Edit LoRa gateway by clicking on its name a configure it like following, then enable gateway again. Save somewhere Gateway ID, we will need it later.

Step 8: Save settings

Now save settings and we will continue by configuring LoRa device.

HARDWARIO LoRa Climate Kit

As demo device we will use our HARDWARIO IoT Kit with LoRa Climate Kit firmware. Flash firmware hardwario/bcf-lora-climate-monitor using BigClown Playground and access Climate Kit with serial communication. For OTAA authentication we need to know device EUI and application Key of Climate Kit so send following AT command:

Device EUI

AT$DEVEUI?

You should get response like following:

AT$DEVEUI?
$DEVEUI: 373836377A377204
OK

Application Key

AT$APPKEY?

You should get response like following:

AT$APPKEY?
$APPKEY: 8048100F81DF3AE49CB1AB06E0B6380F
OK

Add Gateway to ChirpStack

Now we need to add MikroTik gateway to ChirpStack to authenticate it's requests. We do it by navigating in ChirpStack to Gateways item in menu -> CREATE. Fill Gateway name, description. As Gateway ID write down ID from MikroTik and Network-server choose created one in tutorial. Then click CREATE GATEWAY.

Add Device in ChirpStack

Step 1: Add device

Add device to already created Application from previous chapter. Fill device EUI into ChirpStack.

on following page paste Application Key from serial communication with Climate KIT, then click on SET DEVICE-KEY.

Step 2: Check data form device

Now should be everything set it up and you should see LoRa messages in DEVICE DATA tab in KIT01 device.