Remove regression test code

If you see this chunk of code, you can remove it from your code

Code Setup

To register your Feather with The Things Network, you need to set three unique identifiers in the code: APPEUI, DEVEUI, and APPKEY.

Navigate to the Device Overview page for your Feather device. Make sure the Activation Method is set to OTAA

Before adding the unique identifiers to our sketch, we'll need to first expand them by clicking the <> icon. Then, we'll need to switch the order of the Device EUI and Application EUI to little-endian format. You can swap the order by clicking the button with two arrows

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

First, copy the Application EUI in lsb format from the TTN console to APPEUI variable in the sketch. 

Next, copy the Device EUI in lsb format from the TTN console to the DEVEUI variable in the sketch.

Finally, copy the App Key from the TTN console to APPKEY variable in the sketch.

The App Key is in msb format, unlike the EUI's above!!!

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()

Code Overview

Most of what happens in the code occurs in the setup(), on_event() and do_send() functions.

Within setup(), we initialize our DHT sensor and set up the LMIC (LoraWAN-in-C, formerly LoraMAC-in-C) framework for use with our radio (the RFM95) and region-specific radio settings. 

Our main loop() calls os_runloop_once(), which calls the LMIC runloop processor. This loop causes radio events to occur based on events and time - such as callbacks for when the transmission is complete. While you can place additional code within this loop() routine, we don't advise it - the LoRaWAN timing is tight. 

If there isn't a transmission job currently running, we're going to prepare the packet to send to The Things Network. This occurs in the do_send() function.

To read the temperature, we're going to first create a floating point variable, temperature, and assign it to the temperature value from the sensor. 

float temperature = dht.readTemperature();

While we can't directly send floating point numbers (like 50.62 degrees) to The Things Network dashboard, the MCCI-LMIC library includes some data encoding utility functions to encode floating point data into integer data using a special bit layout -sflt16.

Since the floating point is within the range -1 to -1, we'll need to divide the range (by 100 for temperature) to increase the exponent range. We're going to get the value back in the range by later multiplying it by the range (100, in this example) using the decoder on the TTN Console. 

// adjust for the f2sflt16 range (-1 to 1)
temperature = temperature / 100; rHumidity = rHumidity / 100;

Next, we're going to convert the float to an integer. To do this, we'll use the library's float-to-signed-float-16 function (LMIC_f2sflt16):

// float -> int
uint16_t payloadTemp = LMIC_f2sflt16(temperature);

Almost there! Before loading our values into the payload, we'll need to convert them from an integer (payloadTemp) to bytes. To do this, we'll use the Arduino lowByte and highByte functions:

// int -> bytes
byte tempLow = lowByte(payloadTemp);
byte tempHigh = highByte(payloadTemp);

We defined a payload further up in the code (static uint8_t payload[5]). Now, we place the bits (low byte first)  into the payload:

payload[0] = tempLow;
payload[1] = tempHigh;

and prepare the payload for sending to TTN at the next possible time.

LMIC_setTxData2(1, payload, sizeof(payload)-1, 0);

Check Sketch Output

Compile (cmd/ctrl + R) and Upload (ctrl/cmd+U) the sketch onto the Feather. Then, pop open the serial monitor (Tools->Serial Monitor). You should see the Feather joining the Things Network. Once it joins, it'll dump its connection info. Then, it'll show EV_TXStart (an event which begins the transmission) and EV_TXComplete (the transmission has been received and acknowledged by the gateway). The output should be similar to the following:

Temperature: 26.00 *C
%RH 48.10
105016549: EV_JOINING
105016641: EV_TXSTART
105357886: EV_JOINED
netid: 19
devaddr: 26022F78
artKey: 78-F6-78-6C-87-26-86-AE-E1-AC-6D-79-83-57-7E-11
nwkKey: FE-14-C4-A7-BF-D3-B6-E6-95-D4-2F-93-DC-F9-D7-25
105358155: EV_TXSTART
105579674: EV_TXCOMPLETE (includes waiting for RX windows)

If you're stuck on EV_JOINING or fail to join the network,  make sure your device is within range of a The Things Network gateway. 

If the code is looping EV_TXSTART, make sure a jumper wire is connecting the Feather's Pin 6 and Feather Pin 'io1'.

Check Things Network Data

Navigate to the The Things Network Console and select your 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...

This guide was first published on Sep 21, 2018. It was last updated on Jul 22, 2024.

This page (Arduino Code) was last updated on Mar 08, 2024.

Text editor powered by tinymce.