Create an account on the Things Network
Add your username to the Google sheet so that we can add you as a collaborator to our application.
Now you will explore setting up the Arduino board as a LoRa device.
First, install the TheThingsNetwork library using the Libary Manager:Sketch -> Include library -> Manage Libraries
Load the following sketch:File -> Examples -> TheThingsNetwork -> DeviceInfo
In the DeviceInfo sketch locate the line
#define freqPlan REPLACE_ME
and replace the phrase REPLACE_ME
with TTN_FP_EU868
Upload the sketch using the Upload button (as shown below)
Open the Serial Monitor:Tools -> Serial Monitor
and copy the DevEUI value as shown below:
Open a browser and navigate to the the Application for this workshop on the Things Network
In the Devices section click on ‘register device'. You should see a window simialar to below once you start filling out the drop down menus.
In the JoinEUI box enter all 0
's (0000000000000000)
In the ‘Device EUI' box paste the value of the DevEUI from the previous section.
Finally, click on the Register button at the bottom right of your screen. Your device is now registered with the application.
Return to the Arduino IDE and open this Arduino sketch from GIST
Update the code below with your values. You will need to replace <APP_EUI>
with the actual value of your Application_EUI copied from the Things Network console Device Overview (see below). Similarly, replace <APP_KEY>
with the actual value of your App Key obtained from the Things Network console Device Overview.
// Set your AppEUI and AppKey
const char *appEui = "<APP_EUI>";
const char *appKey = "<APP_KEY>";
(Hint: scroll to bottom of Device Information page if you prefer to copy paste...)
Find the line #define debugSerial Serial1
below it we have defined another serial connection for the Lora communication interface (Note, the ‘other' Serial connection is to your terminal so that you can see debug data):
#define loraSerial Serial1
Immediately below this your region is set to be EU:
#define freqPlan TTN_FP_EU868
The following line creates a constructor to setup a TTN object:
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
In the setup()
code we start both serial interfaces:
loraSerial.begin(57600);
debugSerial.begin(9600);
We establish a connection to the Lora Gateway:
ttn.showStatus();
debugSerial.println("-- JOIN");
ttn.join(appEui, appKey);
To minimise the size of the payload transmitted over LoRa, it is necessary to convert our sensor values to unsigned integers. To avoid loosing precision we multiply the sensor values by 100 (applying the reverse transformation at the Things Network backend).
In the loop()
code you should see:
// converting that reading to voltage, for 5.0v arduino use 5.0
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
//converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
float tempC = (voltage - 0.5) * 100 ;
// Read sensor values and multiply by 100 to effictively have 2 decimals
uint16_t temperature = tempC * 100;
Next we convert our sensor values to binary and upload them to the ThingsNetwork (note we are only using 2 bytes of a potential 8 byte payload here):
// Split both words (16 bits) into 2 bytes of 8
byte payload[2];
payload[0] = highByte(temperature);
payload[1] = lowByte(temperature);
Finally we upload the data to the Things Network as shown below
ttn.sendBytes(payload, sizeof(payload));
Your sketch should now look like this:
/*
Circuit:
* TMP36 middle pin attached to pin A0, left to VCC, right to GND
created Feb 2023
by Duncan Wilson
*/
#include <TheThingsNetwork.h>
// Set your AppEUI and AppKey
const char *appEui = "xxx";
const char *appKey = "xxx";
#define loraSerial Serial1
#define debugSerial Serial
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
//TMP36 Pin Variables
int sensorPin = A0; //the analog pin the TMP36's Vout (sense)
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");
// converting that reading to voltage, for 5.0v arduino use 5.0
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
//converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
float tempC = (voltage - 0.5) * 100 ;
// Read sensor values and multiply by 100 to effictively have 2 decimals
uint16_t temperature = tempC * 100;
// Split both words (16 bits) into 2 bytes of 8
byte payload[2];
payload[0] = highByte(temperature);
payload[1] = lowByte(temperature);
debugSerial.print("Temperature: ");
debugSerial.println(temperature);
ttn.sendBytes(payload, sizeof(payload));
delay(60000); // wait a minute
}
Compile the code and once you have the code compiling sucessfully you can upload the sketch to the Things Uno board.
Save the sketch.
Now return to the Things Network console for your device and open the Data tab (see below):
You should be able to see the data uploaded by your device.
To dig a little deeper into how messages can be sent to and from the Things Uno take a look at the quick start guide on TTN