This tutorial explains how to connect your Portenta H7 to The Things Network (TTN) using the the Vision Shield's LoRa® Connectivity feature. A data communication channel will be enabled between the H7 and a TTN application that will be configured on your TTN console.
In order to connect your Portenta to the TTN, make sure you are within the range (max. 10 Km) from an available LoRa® Gateway. Indoor gateways will have a much shorter range. It is recommended that you check LoRa® Gateway availability on The Things Network map before you try this tutorial.
To be able to use the LoRa® functionality, we need to first update the firmware on the LoRa® modem. This can be done through Arduino IDE by running a sketch included in the examples from the MKRWAN library.
Connect the Portenta H7 and the Portenta Vision Shield - LoRa® to your computer and open the Arduino IDE.
Install/update the MKRWAN library from Arduino IDE menu Tools > Manage Libraries. Type "MKRWAN" to find the library and click 'Install' or 'Update' if necessary. This library provides all the APIs to communicate with LoRa® and LoRaWAN® networks.
Open the MKRWANFWUpdate_standalone sketch from the Arduino IDE menu: File > Examples > MKRWAN.
Upload the sketch.
The Portenta Vision Shield - LoRa® can be connected to the TTN and can transmit data to other devices connected to this network through a secure channel. This channel is nothing but an application on the TTN network dedicated for your board. In this tutorial, you will be guided through a step-by-step process of setting up your Portenta board and the Vision Shield - LoRa® to communicate with a TTN application. As stated before, to be able to follow this guide, you need to be under coverage of one of the TTN gateways. You can check for the coverage now if you have not done so yet.
Start by going here. First choose your region. Next, sign in with your The Things Network account. If you do not have an account, create a new one on the login page. Then fill all the required fields to complete a new registration.
Once you have created an account with TTN, you need to create a TTN application. An application provides a way to aggregate data from different devices, and then use these data with other 3rd party integrations. After signing in, click on Create an application, or Go to applications if you already have created one.
Here you will have a list of all your applications. Now create your first app by pressing the Create an application button.
You have now to fill only the first two fields:
After completing these two fields, press the "Create application" button located at the bottom left corner of the page. The dashboard will then show you an overview of the newly created app.
Let's take a closer look at these sections:
It iss now time to connect your Portenta H7 and Vision Shield - LoRa® to TTN. You will need to upload code to the board, so, as you probably already know, there are two options:
Plug the Portenta Vision Shield - LoRa® to the Portenta H7 and them to your PC through the USB port. Be sure to have selected the right board "Arduino Portenta H7 (M7 core)" and the right port.
The LoRa® module on the Vision Shield - LoRa® can be accessed by using the MKRWAN library(if you cannot find it in your examples list, you can go to Tools > Library Manager and type "MKRWAN library" to install it). This library provides all the APIS to communicate with LoRa® and LoRaWAN® networks and can be installed from the library Manager. The first code you need to upload and run is from the MKRWAN library, and its name is FirstConfiguration.
The only line you may need to change before uploading the code is the one that sets the frequency. Set the frequency code according to your country if needed. You can find more information about frequency by country at this TTN link.
1// change this to your regional band (eg. US915, AS923, ...)2 if (!modem.begin(EU868)) { ...
Consider that in Australia the boards connect correctly to TTN gateways on AS923 frequencies; AU915 frequencies requires the selection of sub band 2, not yet implemented in the firmware.
Once you have added to the sketch the frequency according to your country, you can upload it to the board. Then, when the upload is completed, open the Serial Monitor. The following details will show up:
1Your module version is: ARD-078 1.2.12Your device EUI is: a8xxxxxxxxxxxxxx3Are you connecting via OTAA (1) or ABP (2)?
In order to select the way in which the board is going to connect with TTN (OTAA or ABP), you need to configure it on the TTN portal. You will see which option you should select in the following steps.
Before your Portenta H7 can start communicating with the TTN, you need to register the board with an application. Go back to the TTN portal and scroll to End devices section on your Application dashboard, then click Add end device.
On the registration page, first you have to fill in information about your board. Select brand Arduino SA, and Portenta Vision Shield - LoRa® as the model. Hardware and firmware versions will automatically be set to the newest ones. Then set your preferred region.
In the second step of registering the device, fill in End device ID and DevEUI. You can click the generate button next to the AppKey field to generate an app key for this device. Similarly, you can press the button next to the AppEUI field to make it all zeros, or enter your own AppEUI.
Note: The Device ID must be lowercase and without spaces. The DevEUI should be copied from the Serial Monitor.
After pressing the Register button, your board will show up on the Device Overview page. You can now see all the information needed to complete the Arduino setup.
Once your board has been registered you can send information to TTN. Let's come back to the Serial Monitor and proceed. It will ask for:
Lets start by making a connection Over-The-Air (OTA). Enter "1" in the Serial Monitor input box and press ENTER. Then, find the EUI and the App key from TTN Device Overview page. You can read more into OTA vs ABP activation mode here.
1Your module version is: ARD-078 1.1.92Your device EUI is: a8xxxxxxxxxxxx0a3Are you connecting via OTAA (1) or ABP (2)?4Enter your APP EUI5Enter your APP KEY
Next, introduce the APP EUI and the APP KEY in the Serial Monitor. If this process is done successfully, you will see this message:
1Message sent correctly!
If you receive this message, you have managed to configure the Portenta H7 and the Vision Shield - LoRa® on TTN.
You have retrieved the device EUI, used it to register the device in the TTN console, and programmed the board using the data provided by TTN. Now, you can send data over the LoRa® network which can be viewed from anywhere in the world (as long as we have an Internet connection and your device is in the range of a TTN gateway).
The most common issue is that the device cannot connect to a TTN gateway. Again, it is a good idea to check if you have coverage in the area you are conducting this tutorial, by checking out this map.
If you are within good range of a gateway, you should also try to move your device and antenna to a window, and even hold it out the window and move it around. This has proven successful on numerous accounts, as the signal can travel less obstructed.