Minimal getting started examples to connect the Arduino MKR WAN 1300 to The Things Network
Make sure you have an updated version of Arduino IDE installed.
You could run these examples on the Arduino Web Editor also, but the screenshots are from the desktop client.
-
Install board definitions
Go to
Tools
->Boards
->Board manager
, search forMKR
, and install theArduino SAMD Boards
package. -
Install LoRaWAN library
Go to
Sketch
->Include Library
->Manage Libraries..
, search forMKRWAN
, and install it.
- Check coverage
- Plug the antenna
- Provision the device
- Sending our first message
- A real use case: GPS tracker
In order to connect, you will need to be in range of a gateway of The Things Network. Check the world map to see if your local community already has gateways, and if not, consider installing one!
While you're there, also make sure you have an account on The Things Network.
Always make sure you have the antenna plugged to your Arduino before powering it up.
In order to send messages to the network, you need to register it on The Things Network, but before we do that, we need to find out its Device EUI
(identifier):
- Plug your Arduino to your computer
- Select the correct
Board
andPort
on the Arduino IDE menu - Open the first sketch of this repository:
mkrwan_01_get_deveui
- Update (if needed) the
lora_band region
variable according to your region. - Open the serial monitor and upload the sketch to your Arduino.
- The serial monitor will print the
Device EUI
, keep this at hand.
- Now, go to The Things Network Console
- Add a new
Application
- Register a new
Device
giving it a ID of your liking and paste theDevice EUI
that we got on Step 5.
Once the device is registered on the network, we can start sending data:
- Open the second sketch of this repository:
mkrwan_02_hello_world
- Update (if needed) the
lora_band region
variable according to your region. - On the second tab (
arduino_secrets.h
), paste theappEui
andappKey
values for your device. You can copy & paste it verbatim from the bottom of the device page on The Things Network Console (sectionEXAMPLE CODE
).
- Upload the sketch to your Arduino.
- If everything is in order, you will see messages coming through in the
Data
tab of The Things Network Console.
Q: Wait! Why do I see payload =
68 69
instead ofhi
?A: That's HEX for
hi
. That's because you should not be sending strings over LoRaWAN, you have to optimize the air time usage sending as much a compact payload as possible.
Check Working with Bytes for more details about optimizing your payload.
Now for a more complete application, let's build a GPS tracker node! For this example, you need to get a GPS module. I am using u-blox PAM-7Q but also tested it with the more widely available u-blox NEO series.
The wiring of the module is trivial:
Arduino pin | GPS pin |
---|---|
GND |
GND (3 ) |
VCC |
VCC (4 ) |
RX (13 ) |
TX (2 ) |
TX (14 ) |
RX (1 ) |
You will also need to install the TinyGPS++ library. Download it as ZIP, then go to Sketch
-> Include Library
-> Add .ZIP Library
and select it.
We will use a binary payload based on TTN Mapper reference, so we need to add a decoder
payload format function to The Things Network Console (as described in the documentation).
Go to the Applications
tab in The Things Network Console, select Payload Formats
and copy & paste the following decoder
function:
// Author:
// Copyright (c) 2016 JP Meijers
// Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
// https://github.com/jpmeijers/RN2483-Arduino-Library
function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};
// if (port === 1) decoded.led = bytes[0];
decoded.lat = ((bytes[0]<<16)>>>0) + ((bytes[1]<<8)>>>0) + bytes[2];
decoded.lat = (decoded.lat / 16777215.0 * 180) - 90;
decoded.lon = ((bytes[3]<<16)>>>0) + ((bytes[4]<<8)>>>0) + bytes[5];
decoded.lon = (decoded.lon / 16777215.0 * 360) - 180;
var altValue = ((bytes[6]<<8)>>>0) + bytes[7];
var sign = bytes[6] & (1 << 7);
if(sign)
{
decoded.alt = 0xFFFF0000 | altValue;
}
else
{
decoded.alt = altValue;
}
decoded.hdop = bytes[8] / 10.0;
return decoded;
}
We are all set to update our Arduino, the steps are almost identical to the previous example:
- Open the third sketch of this repository:
mkrwan_03_gps_tracker
- Update (if needed) the
lora_band region
variable according to your region. - On the second tab (
arduino_secrets.h
), paste theappEui
andappKey
values for your device. You can copy & paste it verbatim from the bottom of the device page on The Things Network Console (sectionEXAMPLE CODE
). - Upload the sketch to your Arduino.
- On start, the node will join the network and try to get a GPS fix. This usually takes up to a minute. NOTE: You will need to be outdoors for the GPS module to get a location.
- Check the
Serial Monitor
to see the progress. - If all went well, you can now check the
Data
tab in the console to see GPS data coming in. - Optionally, configure the TTN Mapper integration and have your node contribute to the mapping efforts of the community.
Fantastic! And now, how do I see my GPS traces on a map like this?
Check this sample web app!
This content is licensed under the MIT License, see the LICENSE file for details.
- Fork it (https://github.com/gonzalocasas/arduino-mkr-wan-1300)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request :D
Examples loosely based on Arduino MKRWAN library.
Check the reference documentation of the library for additional options.