Please make sure you have prerequisites in place:
Installed the Arduino IDE
https://www.arduino.cc/en/Guide
Created an account on TheThingsStack V3
https://www.thethingsindustries.com/docs/getting-started/console/login/
Use the following code to get the device EUI of your board
#include <TheThingsNetwork.h>
#define loraSerial Serial2
#define debugSerial SerialUSB
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
void setup(){
loraSerial.begin(57600);
debugSerial.begin(9600);
}
void loop(){
debugSerial.println("Device Information");
debugSerial.println();
ttn.showStatus();
debugSerial.println();
debugSerial.println("Use the EUI to register the device for OTAA");
debugSerial.println("-------------------------------------------");
debugSerial.println();
delay(10000);
}
You should get something similar to this on the serial monitor
15:39:00.270 -> Device Information
15:39:00.270 ->
15:39:00.316 -> EUI: 000A.........3AC5
15:39:00.316 -> Battery: 3273
15:39:00.316 -> AppEUI: 0000000000000000
15:39:00.316 -> DevEUI: EUI: 000A.........3AC5
15:39:00.316 -> Data Rate: 0
15:39:00.316 -> RX Delay 1: 1000
15:39:00.363 -> RX Delay 2: 2000
15:39:00.363 ->
15:39:00.363 -> Use the EUI to register the device for OTA
TheThingsStack (TTNV3)
Create an application for the project on the TheThingsStack
more info about TTS via this link: https://www.thethingsindustries.com/docs/getting-started/
Add an end device for the application
Upload the following code via the using the arduino IDE
#include <TheThingsNetwork.h>
// Set your AppEUI and AppKey
const char *appEui = "0000000000000000";
const char *appKey = "Your_appkey_from_the_things_stack";
#define loraSerial Serial2
#define debugSerial SerialUSB
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
void setup()
{
loraSerial.begin(57600);
debugSerial.begin(9600);
// Wait a maximum of 10s for Serial Monitor
while (!debugSerial && millis() < 10000);
debugSerial.println("-- STATUS");
ttn.showStatus();
debugSerial.println("-- JOIN");
ttn.join(appEui, appKey);
}
void loop()
{
debugSerial.println("-- LOOP");
// Prepare payload of 1 byte to indicate LED status
byte payload[1];
payload[0] = (digitalRead(LED_BUILTIN) == HIGH) ? 1 : 0;
// Send it off
ttn.sendBytes(payload, sizeof(payload));
delay(10000);
}
Modify the code to get reading from the onboard temperature sensor (MCP9700AT)
#include <TheThingsNetwork.h>
// Set your AppEUI and AppKey
const char *appEui = "0000000000000000";
const char *appKey = "Your_appkey_from_the_things_stack";
#define loraSerial Serial2
#define debugSerial SerialUSB
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
void setup()
{
loraSerial.begin(57600);
debugSerial.begin(9600);
// Wait a maximum of 10s for Serial Monitor
while (!debugSerial && millis() < 10000)
;
debugSerial.println("-- STATUS");
ttn.showStatus();
debugSerial.println("-- JOIN");
ttn.join(appEui, appKey);
}
void loop()
{
uint16_t temp = getTemperature();
byte payload[2];
payload[0] = highByte(temp);
payload[1] = lowByte(temp);
ttn.sendBytes(payload, sizeof(payload));
delay(10000);
}
uint16_t getTemperature(){
// 10mv per C, 0C is 500mV
float mVolts = (float) analogRead(TEMP_SENSOR) * 3300.0 / 1023.0;
int temp = (mVolts - 500) * 10;
debugSerial.print((mVolts - 500) / 10);
debugSerial.println(" Celcius");
return int(temp);
}
Output on the serial monitor
17:32:24.739 -> Sending: mac join otaa
17:32:29.983 -> Join accepted. Status: 00000810
17:32:29.983 -> DevAddr: 2xxxxxxxxxx...
17:32:29.983 -> Sending: mac set class a
17:32:29.983 -> 24.19 Celcius
17:32:29.983 -> Sending: mac tx uncnf 1 0973
17:32:36.128 -> Successful transmission
We still need to decode the payload on TTS in order to get the values in string format.(later)
Downlink
Controlling the onboard LED via a downlink message
Code
Add the code within the setup()
// Set callback for incoming messages
ttn.onMessage(message);
pinMode(LED_BUILTIN, OUTPUT);
#include <TheThingsNetwork.h>
// Set your AppEUI and AppKey
const char *appEui = "0000000000000000";
const char *appKey = "Your_appkey_from_the_things_stack";
#define loraSerial Serial2
#define debugSerial SerialUSB
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
void setup()
{
loraSerial.begin(57600);
debugSerial.begin(9600);
// Wait a maximum of 10s for Serial Monitor
while (!debugSerial && millis() < 10000)
;
debugSerial.println("-- STATUS");
ttn.showStatus();
debugSerial.println("-- JOIN");
ttn.join(appEui, appKey);
// Set callback for incoming messages
ttn.onMessage(message);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
uint16_t temp = getTemperature();
byte payload[2];
payload[0] = highByte(temp);
payload[1] = lowByte(temp);
ttn.sendBytes(payload, sizeof(payload));
delay(10000);
}
uint16_t getTemperature(){
// 10mv per C, 0C is 500mV
float mVolts = (float) analogRead(TEMP_SENSOR) * 3300.0 / 1023.0;
int temp = (mVolts - 500) * 10;
debugSerial.print((mVolts - 500) / 10);
debugSerial.println(" Celcius");
return int(temp);
}
//- Downlink Led blink
void message(const uint8_t *payload, size_t size, port_t port){
if (payload[0] == 0){
digitalWrite(LED_BUILTIN, LOW);
}
else if (payload[0] == 1) {
digitalWrite(LED_BUILTIN, HIGH);
}
}
Send a downlink message to the board via the TTS. A response is only sent after the board receives an uplink (class A). Led is turned on with a value of '01'
Changing the payload to ' 00' sets the Led to OFF after the next uplink is received because the device is a class A device.
Decode the payload to TTS V3 format
function decodeUplink(input){
var bytes = input.bytes;
var temp = ((bytes[0] << 8) | bytes[1]) / 100.00;
return {
data: {
temperature: temp
},
warnings: [],
errors: []
};
}
Output in human readable format: