Have you bought your first MKR WAN 1300 or MKR WAN 1310 and you are not so sure about the very first setup? Then this guide is for you!
We'll cover the usage of the MKR WAN 1300 or MKR WAN 1310 with the LoRaWAN technology, which allows for things to talk to the Internet without WiFi or 3G / 4G.
It features long range, low battery usage and low bandwidth, and that makes LoRa perfect for the IoT!
The most known global LoRaWAN network is The Thing Network, which is a decentralized and collaborative network, counting thousands of gateways around the world.
We'll use the public community network of The Thing Network (hereafter only TTN).
RequirementsIn order to be able to follow this guide, you need to be under coverage of one of the TTN gateways.
You can check for this coverage using the map of all the TTN gateways (you can find the map on the TTN homepage):
Otherwise you'll need to build up your own! (and sorry, this part is not covered by this guide)
Create an account and your first app on TTNJust point your browser to www.thethingsnetwork.org and use the Sign Up button:
Then fill all the required fields:
Once done, log to your console
Basically in your console you can do 2 things:
- register a new gateway or manage your own: here you can register and manage your own gateway - of course you can use for free one of the thousands of gateways around the world already running!
- create or manage your applications: an application is the way to aggregate data from different devices, and then use these data with other online / offline tools - or in other terms, you can create an integration
The setup and configuration of a gateway is not the aim of this guide, so let's proceed selecting APPLICATIONS
Here you'll have listed all your applications. Now create your first app by pressing the "add application" link or "Get started by adding one!"
You have now to fill only the first two fields:
- the first one is the ID of your app: this must be lowercase and without spaces.
- the second one is a Description of your app, and there's no restrictions on formatting
After filling these 2, go on by pressing the "Add application" button on the bottom right of the page.
After that, the loaded page will show you an overview of the newly created app. Let's see the sections:
- Application Overview + Application EUIS: in order to use this app, you'll need the Application ID and its EUIs.
Each EUI is a globally unique identifier for networks, gateways applications and devices. The EUIs are used to identify all of the parts of the LoRaWAN inside the backend server.
- Devices: here you can see and manage all the associated devices (e.g. your Arduino MKR WAN 1300 or MKR WAN 1310), or proceed with the registration of new one
- Collaborators: here you can see and manage all the app collaborators. Like other collaborative platforms, also here is possible to grant access with different rights to the app, to other TTN registered profiles.
- Access keys: it's the most sensible information. It is basically the key to gain access to your app, so keep it safe. At the right, there's a little button that allows you to copy the Access Key.
It's now time to connect your Arduino to TTN. You'll need to upload code to the board, and here the options are 2:
- use the Arduino Web Editor, that is the one I'll use in this guide
- use the Arduino IDE, that is quite similar to the previous one, with the difference that you have to download and install it on your pc
You can find more information on the Web Editor and IDE setup for the MKR WAN 1300 or MKR WAN 1310 at this link or this link.
After connecting your Arduino to the usb port, be sure to have selected the right board and the right port
The first code you need to upload and run is from an external library - named MKRWAN, and it's the FirstConfiguration sketch
The only row you may need to update in the example code, is the one about the frequency:
... // change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) { ...
You have to change the frequency according to your country.
Please, 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.
You can find more information about frequency by country at this TTN link.
After you've upload the sketch, open the Serial Monitor. The very first rows will be like these:
Your module version is: ARD-078 1.1.9
Your device EUI is: a8xxxxxxxxxxxx0a
Are you connecting via OTAA (1) or ABP (2)?
Before to go on, let's switch to TTN and start registering a new device.
For first, you have to open the "Register Device" page, by selecting the "register device" link on the header of the Devices section.
The following page will be displayed: you have to fill only two fields.
- the first one is the ID of your device: this must be lowercase and without spaces; the ID should be unique, so you can use i.e. the name of the board followed by the first and the last byte of the EUI
- the second one is the EUI: this is is exactly the device EUI appeared in the Serial Monitor! So copy that value into this field
After pressing the Register button, it will show the Device Overview page. You can now see all the information needed to complete the Arduino setup.
Let's come back to the Serial Monitor and proceed: it will ask for:
- activation mode (that in this case is OTAA),
- the Application EUI and
- the App Key.
You can copy these information by clicking on the small icon at the end of the relative rows in the TTN device page.
Your module version is: ARD-078 1.1.9
Your device EUI is: a8xxxxxxxxxxxx0a
Are you connecting via OTAA (1) or ABP (2)?
Enter your APP EUI
Enter your APP KEY
You can go deep into OTA vs ABP activation mode at this link
If everything has gone fine, you should see in the Serial Monitor this message:
Message sent correctly!
and the page of the Device on TTN should be changed into this:
Now that you have tested App EUI and Key, and registered your device, it's time to upload a new sketch into your board.
This new code will allow you to exchange (send and receive) data with TTN.
You can find the new sketch in the previously used library - MKRWAN: it's the LoraSendAndReceive sketch
This code has to be filled in the Secret tab; basically you have to fill:
- the SECRET_APP_EUI field with the Application EUI field from the TTN device overview page
- the SECRET_APP_KEY field with the App Key field from the same page
After that, you can upload the sketch to your board.
The output in the SerialMonitor will be like this
Your module version is: ARD-078 1.1.9
Your device EUI is: a8xxxxxxxxxxxx0a
Enter a message to send to network
(make sure that end-of-line 'NL' is enabled)
It' now time to do a little bit deeper on uplink and downlink
The communication from the device to TTN is called uplink, viceversa the communication starting from TTN to the device, is called downlink.
Open the Data section on the Device page: here you'll see every kind of messages between your device and TTN
The thundericon is used to identify an Activation: typically when you turn on your device or you reset it, it will generate a new activation.
You can click on that row and expand it, looking at more detailed information.
Try now to send an uplink from your board.
From the SerialMonitor, digit one or two words (i.e. "Hello TTN!") and hit the Send button
the ouput in the SerialMonitor will be:
Sending: Hello TTN! - 48 65 6C 6C 6F 20 54 54 4E 21
Message sent correctly!
No downlink message received at this time.
Enter a message to send to network
(make sure that end-of-line 'NL' is enabled)
We can see the message in a few seconds in the APPLICATION DATA page on TTN
You see for each uplink, 2 rows:
- the first (the lower one) is the uplink itself; you can recognize that is an uplink also thanks to the arrow up icon; the content is labelled "payload"
- the second is a downlink (arrow down icon) that confirms the uplink reception. This downlink is not necessary - unless you need it, of course.
You can click on each row to see message details.
Notice that you've sent the string "Hello TTN!" and it was received a sequence of bytes in hexadecimal notation.
The content is of course the same. Let's use this online tool:
ASCII to Hex...and other free text conversion tools
and try to copy the sequence of bytes into the "Hexadecimal" area, and then press the Convert button
you'll see in the "Text (ASCII / ANSI)" area the content will be converted back to a readable format.
Now let's try to send a Downlink.
Come back to the Device Overview page, and scroll to the Downlink section.
You can use the same ASCII to Hex tool to convert a string into a sequence of bytes; i.e. convert
"Hi MKR WAN!"
from Text to Hexadecimal: the result will be
"48 69 20 4d 4b 52 20 57 41 4e 21"
Copy the result into the Payload field and set:
- Scheduling: replace
- Payload (type): bytes
- Fport: 1
- Confirmed: yes
and press the Send button
The downlink is queued waiting for the next uplink: only in that "window" the downlink will sent back to your board.
You'll see it in the Application Data page:
Now try to send something else from your board, and you'll receive the downlink as response:
Sending: Hello again! - 48 65 6C 6C 6F 20 61 67 61 69 6E 21
Message sent correctly!
Received: 48 69 20 4D 4B 52 20 57 41 4E 21
You'll see as well in the Application Data window the flow (bottom-up) of the queued message, the uplink and the next downlink of the queued message.
As already know, you can click on every rows to expand it and see message details.
TIP if you want to see the received message on your board in plain text, you need to add these 3 rows of code as last, in the loop:
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j]);
}
Serial.println();
they will output the content in a readable format, i.e.
Sending: Hello for the last time! - 48 65 6C 6C 6F 20 66 6F 72 20 74 68 65 20 6C 61 73 74 20 74 69 6D 65 21
Message sent correctly!
Received: 48 69 20 61 67 61 69 6E 20 4D 4B 52 20 57 41 4E 21
Hi again MKR WAN!
Payload decoding on TTNYou can of course decode the message on TTN as well! There's indeed a section aimed to format the payload.
You can find this section in the Application page, and the section is called "Payload Format"
In order to enable the decoding, update the Decoder function, replacing the one already there with the following one:
function Decoder(bytes, port) {
//source: https://flows.nodered.org/flow/845bb5b8cf788939dd261f472c289f77
var result = "";
for (var i = 0; i < bytes.length; i++) {
result += String.fromCharCode(parseInt(bytes[i]));
}
return { payload: result, };
}
and then press the "save payload functions" button!
From now on, you'll see the uplink in plain beside the sequence of bytes! Let's see an example:
It will be easier in this way to integrate your TTN Application with other services or tools!
Improve bandwidth usageThere are policy -not only for technical reason, also for government rules- on how much we can use the bandwidth in uplink and in downlink in LoRaWAN. You can read these TTN interesting guides on this topic:
I've already said that we are using the public community network of TTN: in this case there's also a Fair Access Policy that limits:
- the uplink airtime to 30 seconds per day (24 hours) per node and
- the downlink messages to 10 messages per day (24 hours) per node.
So the suggestions are:
- keep the payload (the message) as smaller as possible, and
- avoid not necessary messages
Let's start from a small thing: removing the confirmation request for the uplink messages from your Arduino. You need to change only this line:
err = modem.endPacket(true);
to
err = modem.endPacket(false);
Enjoy!And now, let's test the built-in integrations, the API or the available SDKs / Libraries!
You can find the full list of them here!
Comments