LoRaWAN with Marvin and The Things Network

My apologies to my regular Dutch readers, given the nature of the topic I am going to do this post in English also. I promise to return to the regular Dutch posts.

When I wrote about connecting the Marvin board to the LoRaWAN network in the previous post, my conclusion was: wow, great, KPN truly does offer easy access to LoRaWAN and it even has coverage in my hometown whereas The Things Network (TTN) has a far from complete coverage in the Netherlands. (Yes, I know, I can add my own gateway, but before I’m going to spend a lot of money on something like this, I would like to play with it first).

Downside of KPN was/is the costs involved in using it beyond the 71 (72?) day trial period. So I wanted to try to connect the Marvin to The Things Network. For that I first needed something that could act as a gateway. I do have a LoPy, but had been unable to get that setup as a gateway. Luckily I also had a Dragino Lora Shield that I had ordered via AliExpress for $19.- a while back. I hadn’t had the time to try and get it to work as a single channel gateway, but now I did. I used the easy to follow instructions available on mobilefish.com. In my case I used an old Raspberry Pi 1 since it does not have to do too much anyway and I am only going to use it for testing purposes.

Within 10 minutes I had my first gateway online. Thanks Robert!

Next step was getting the Marvin to connect to TTN. I am really new to everything LoRaWAN related, but have some Arduino experience, so a .ino file does not scare me. I took a look at the Arduino script that I used for the temperature / humidity measurements (side note: a working version of the script for KPN is available on github). I could not find too much that looked KPN specific.

The only thing that I had to set for KPN were the set_nwkskey,  set_appskey and set_devaddr variables. So next I had a look around in the devices section of the console at TTN. And what do you know? If you select Activation by Personalisation (ABP) as the authentication method, then TTN provides you with just those three keys. Would it really be that easy? Yes! 🙂

So in case you want a bit more of a step by step:

I am assuming you are within range of a TTN gateway (either yours or someone else’s) and you have a TTN account.

Logon to that account.

Click on Applications and then “add application”

Give your application an unique ID (you cannot change that later) and a description (you can change that) and click Add application

Within the application you register devices (one per node). Click on “register device” to register your Marvin

Enter the and device ID (just make something up) and the device EUI for the device. Now when I did this, I cheated and looked at the KPN site and used the Device EUI provided there. If you want the “real” Device EUI, just upload the MarvinMultiSerial script available here. Then open  the serial monitor and type the command “sys get hweui” to get the Device EUI.

Click on “Register”.

By default TTN will use OTAA (over the air authenication). We can change this however. Click on “settings” for the device.

Change the authentication method to “ABP” and click “Save”

Now you’ll get the values to add into set_nwkskey,  set_appskey and set_devaddr variables to add into your script, for example the temp/humidity script

After that, you should see the device show up in the TTN console. If it still is connected to the serial port, you can see it connecting to the server and sending the data.

The Things Network has some cool tools, for example the http://ttnmapper.org/ site where you can find a map with all the gateways and their registered signal strength. You can download an app to your phone and go LoRaWARdriving.

I did it (connected the LoPy to the antenna, uploaded the script, attached a 5V powerbank, tossed it in a small box and went for a bike ride) and learned that the reach of my small gateway is rather limited, about 300 meters. But I don’t care really, it is just for testing. And for that only a few meters are enough.
Next challenge is experimenting with the Node-Red server and getting the data back into a system that I control and where I can store or use it.


p.s. getting the LoPy online using this method was just as easy. Just use the code provided here and add the values for dev_addr, nwk_swkey, app_swkey using the same method as above. In case of the LoPy you get the Device EUI using this code in the console:

from network import LoRa
import binascii
lora = LoRa(mode=LoRa.LORAWAN)

(Note: while writing this blog post, my gateway lost the connection to TTN and was not able to get it back online during the last hour, nothing changed on my end after it worked for most of the day. It seems that Single Channel Gateways have their share of problems)


0 0 stemmen
Bericht waardering
2 Reacties
Inline Feedback
Bekijk alle reacties
Rienk Jan
7 jaren geleden

Werkt de single channel oplossing nog?

Ik heb met en raspberry ook deze opstelling gebouwd. Ik verstuur UDP packets en ontvang UDP packets. Maar het TTN zegt niet verbonden. Nu lees ik hier dat TTN het niet meer ondersteunt.