When I first got my hands on the ST Lora discovery kits, I was amazed with its capabilities, the STM32 low power, the embedded debugger, the high range Lora, and all sorts of technologies, and all of this is cool, but I had a lot of difficult get it up and running.
So in this article, I will explain, in a simple way, how all boards work, how to set them up, and how to connect them to the LoraWAN.
You can see in the video below the boards working, when I did some tests to measure the max lora range in my neighborhood.
Lets get started.
The Lora boardsP-NUCLEO-LRWAN2
This development kit provides exactly what you need to start adventuring on the Lora technology: a Gateway and a Node.
The biggest board is the Lora Gateway, it will connect to all Lora nodes and send their info to the LoraWAN. To do so, it has a RJ45 port, so you can plug it to your ethernet router.
The other board is the Lora node, it will send data to the gateway, and then the gateway will send it to the LoraWAN. This board is made by two boards, a common NUCLEO-L073RZ board, that has an STM32 and is the one that we are going to write code and program, and the I-NUCLEO-LRWAN1 board, that is the board that has de Lora radio, and talks to the STM32 using AT commands.
B-L072Z-LRWAN1
This board is another Lora node that we can connect to the gateway, but, different from the node we saw above, this one is just a single board, it has the STM32 and the Lora radio built in a single package.
And in this case, it doesn't use AT commands, the code is a bit different.
The LoraWAN providersThe LoraWAN providers, in a simple way, are just servers that receive the data forwarded by the Lora gateway, and make it available to use as you wish. One of the biggest advantages is that you can connect devices across the world.
You can use the Lora protocol without the WAN, just locally, there is an interesting topic that discuss it here, I recommend the reading. But in this case we are going to use the LoraWAN.
There are two major LoraWAN providers, LORIOT and TTN. Both are good, in this article I will be covering the process for LORIOT, but the process for TTN is very similar.
You can see schematic of our project on the image below.
So, to start, I would like to show you the technical data that we are going to use in this tutorial, you can see where we are going to get them and where we are going to use them.
LORIOT
Info you get: LoraWAN server address
Info you set: Gateway (MAC address, radio frequency), Node (devEUI, appEUI, appKEY)
LoraGateway
Info you get: Gateway (MAC address)
Info you set: Radio frequency, LoraWAN server address
Lora Node
Info you get: Node (devEUI, appEUI, appKEY)
Info you set: Data that you want to send, data port, radio frequency
Lets connect everything together
1. Setting up the LORIOT
Enter on this website, scroll the page, and select the server of your preference, then click on it.
After that, you are going to register a new account. The first info you get is the LoraWAN server address, it is located on the website address. In my case, "sa1.loriot.io".
2. Setting up the Gateway
Get two USB-MicroUSB cables, connect them to the ST-Link, the Lora Board and your pc. Them, use a Ethernet cable to connect it to your internet router.
The one that is going to transfer data is the ST-Link cable, but you need to connect the other cable to the Lora Board to power it, otherwise, it wont work.
Open a serial emulator software, in my case, I will be using TeraTerm. Set up the port as following.
And also set up the language to UTF-8.
Press the reset button on the board, and a lot of info should show up.
The first info you get is the gateway mac address, as you can see in the picture above, write it down somewhere, because we are going to use it in the next step.
The next step is make sure that the gateway is connected to the internet. If your router is using DHCP, then the gateway will get its ip address automatically and you don't have to worry about it. If not, use the command below and set it up.
AT+IP=STATIC,<ip>,<netmask>,<gatewayip>
So lets set the LoraWAN server address, that we got on the previous step. Use the command below.
AT+PKTFWD=<server_address>,1780,1780
In my case, "AT+PKTFWD=sa1.loriot.io, 1780, 1780"
The next thing we need to configure is the frequency plan that we are going to use, we have the following options available.
Then use the command.
AT+CH=<Freq>
In my case, I will be using US915, so my command will be: "AT+CH=US915".
After that, type "AT+RESET", and you should see your configs updated. You will be ready to the next step if the gateway displays the message below.
3. Adding the gateway to LORIOT
On the main screen of LORIOT, click on the "Register new gateway" button.
Then, click on the "Packet forwarder STM"
On the next screen, fill the "eth0 MAC address" field.
Then, on the next screen, it may take a few minutes, and you should see the "connected" status. Try resetting the gateway if it is taking too long.
The last thing you have to set up, is the gateway frequency, click on the configure button you saw above, and then "region". You will see a field with multiple options. In my case, I will select "US902-928".
Then, click on the "add band" button, and select one based on the list here.
In my case, the gateway is operating between 902.3 to 903.7, so I will select "US915_Default"
After all of this, you are ready to the next step.
4. Setting up the Lora Nodes.
Setting up the Lora nodes is the trickiest part of this process, because you need to be familiar with STM32 programming, but I will try to make as easy as I can.
For this step, you gonna need Arm Keil IDE and the ST I-CUBE-LRWAN expansion, that you can download here.
4.1 Setting up the P-NUCLEO-LRWAN1
First, we need to acquire the node devEUI, appEUI and appKEY. To do so, we need to speak to the Lora board directly, there is TX and RX pins on the top of the board. In my case, I will be using the STLink COM Port to connect the serial to my pc, but you can also use a FTDI board.
So, to use the STLink, we have to open the following jumpers on the nucleo board.
Then, you can connect the RX TX of the Lora board, to the STLink, exactly as the picture below.
Then, connect the USB cable to your PC, open Tera Term, and use the same serial settings as we used for the gateway.
In the Tera Term console, type the following commands:
AT+VER
AT+EUI
AT+APPEUI
AT+AK
And your screen should look like this, write this info somewhere, because we are going to use it later.
So, after you are done getting the node info, you have to disconnect the RX-TX jumpers, and short the jumpers SB62 and SB63, on the nucleo board. Don't forget to it, otherwise, it wont work.
Then, lets procced on the programming step, the Lora board uses AT commands to talk to the nucleo board, so we need to program it with the AT Master project, that comes with the I-CUBE-LRWAN expansion.
The project is located into: (I CUBE LRWAN Folder)>Projects>STM32L073RZ-NUCLEO>Applications>LoRa>AT_Master>MDK-ARM>Project.
Open it, then open the main.c file. By default, the code is defined to use EU868 frequency, so to change it to US915, you have to add an "1" to the LoraDriverParam, on the line 122.
With that done, just connect the board to the computer, build the project, and click on the debug button. If everything goes well, you should see this screen.
The lora node is ready to be connected to LORIOT.
4.2 Setting up the B-L072Z-LRWAN1
For this node, we also need the devEUI, appEUI and appKEY, but differently of the P-NUCLEO-LRWAN1, in this one, we have to set this parameters on the project before flashing it into the device.
The project we are going to use is located into: (I CUBE LRWAN Folder)>Projects>B-L072Z-LRWAN1>Applications>LoRa>End_Node>MDK-ARM>Lora.
The first thing we need to set up, is the frequency. To do so, click on the "Options for target" button, and then go to "C/C++".
And add the "REGION_US915" to the define line, as show on the picture below.
After that, lets set the NODE eui's. Click on the "+" close to the main.c file, and them click on the "Commissioning.h" file. You may have to compile to project before being able to find the file.
On the line 111, change the 0 to 1.
And then, on the lines 118, 123 and 129, we need to generate random hex for the node identification, use a hex generator, you can use this one here. Change the values for the "LORAWAN_DEVICE_EUI" ( 8 bytes ), "LORAWAN_JOIN_EUI" ( 8 bytes ) and "LORAWAN_APP_KEY" (16 bytes).
It should look like this, when you are done.
With that done, compile and flash the program to the board. You should see something like this on the serial monitor.
The node is ready to be connected to LORIOT.
5 Adding the nodes to LORIOT
After all hardware is setup up, just plug them all to a power source, and go to the Loriot website. The process is the same for both nodes. On the main page, click on the sample app.
On the left side of the screen, click on enroll device.
Make sure that the LoraWAN version is the 1.0, and the enrollment process is OTAA. Then fill the node info we acquired before.
Then, it is a good idea to reset the node. When connected, you will be able to see more information about the node on Loriot.
And that's it, you can now decide what data you want to send, the setup is complete.
Bonus: Sending custom data to the LoraWAN
So, to show you guys an example of an application, I created an application that counts how many times you have pressed the user button on the board, and then, it sends this data to the cloud, so you will see something like this on Loriot.
I used the B-L072Z-LRWAN1 board in this example, but the process is very similar to the P-NUCLEO-LRWAN1.
I used the same project that we used in the setup, but added some lines on the main.c file.
Basically, you just have to add to lines to send lora data, first you gotta declare a lora data variable, as show below.
lora_AppData_t data = { message, strlen(message), LORAWAN_APP_PORT };
And then, use the command below to send the data.
LORA_send(&data, LORAWAN_DEFAULT_CONFIRM_MSG_STATE);
And it is just this simple, I attached the full code of tha main.c file here, so you can copy and test it yourself : ).
Comments