TinyLoRa Usage

Code Setup

To register your Feather with The Things Network using ABP, you'll need to set three unique identifiers in the sketch: the Network Session Key, the Device Address, and the Application Session Key.

Navigate to the Device Overview page for your Feather device.

 

Make sure the Activation Method is set to ABP

Before adding the unique identifiers to our sketch, we'll need to first expand them by clicking the <> icon

We're going to copy each unique identifier from the Device Overview to the variables within the sketch.

First, copy the Network Session Key from the TTN console to the NwkSkey variable in the sketch. 

Next, copy the Application Session Key from the TTN console to the AppSkey variable in the sketch. 

Finally, copy the Device Address from the TTN console to the DevAddr variable in the sketch. 

That's all for now - we're ready to upload the code onto our Arduino. 

Note that to see the output you'll need to open up the Serial console. Once you've debugged your example, you can comment out or remove the while (! Serial); line from setup()

Running the Sketch

Compile (cmd/ctrl+r) and upload (cmd/ctrl+u) the sketch to the Feather. 

Then, open the Serial Monitor. You should see the humidity and temperature values printed to the console. Then, the serial should print out the frame counter value. 

Unlike the example using the OTAA activation method, the device does not send join requests to the network. Each time lora.sendData() is called, it'll randomize the frequency (from region-specific bands) and broadcast.

Download: file
Starting LoRa...
Humidity: 33.20 %	Temperature: 23.30
Sending LoRa Data...
Frame Counter: 0
delaying...

Checking Data on The Things Network Console

We want to make sure the data has been received on the other end. To do this, we'll navigate to the The Things Network Console and select the application. From the menu on the right hand side of the page, Click Data.

If everything worked correctly, you'll see the payload from your device streaming into the page, in real time. 

Neat, right? But while we received a payload, we still don't understand what it means...

Decoding the Payload

If you're sending packets in strange formats or encodings (like we are!), The Things Network Console has a programmable data decoder to decode the packets, and assign useful labels to the data.

Copy and paste the decoder script below into the decoder's integrated text editor and click save

Then, click the data tab. Next to the raw payload data, you should see the decoded data for humidity and temperature.

Decoder Code

// TinyLoRa - DHT22 Decoder
function Decoder(bytes, port) {
  var decoded = {};

  // Decode bytes to int
  var celciusInt = (bytes[0] << 8) | bytes[1];
  var humidInt = (bytes[2] << 8) | bytes[3];
  
  // Decode int to float
  decoded.celcius = celciusInt / 100;
  decoded.humid = humidInt / 100;

  return decoded;
}
This guide was first published on Sep 21, 2018. It was last updated on Sep 21, 2018. This page (TinyLoRa Usage) was last updated on Jun 25, 2019.