© Copyright 2016 by The-Box Development
We are at the point where we are able to transmit data to The Things Network and to read the sensor values from the sensor on the shield. The next step is to combine the two to send the sensor readings to The Things Network.
Start by opening (if not still open) both the 'Hello World' sketch. Add the next few lines at the top, just below the existing '#include'.
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
Next delete the lines '// Set your message...' and 'String message = ...'
Below the line 'TheThingsUno ttu;' add:
DHT dht(DHTPIN, DHTTYPE);
// Array to contain data to transmit
byte data[8];
At the end of 'setup()' add:
dht.begin();
To read the values and transmit them we replace the contents of 'loop()' with:
void loop() {
// Read humidity
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// convert the values to integers, multiply by 100 to keep 2 digits behind the decimal point
int humidity = h * 100;
int temperature = t * 100;
// fill array with values
data[2] = (humidity >> 8) & 0xff; // high byte
data[3] = humidity & 0xff; // low byte
data[0] = (temperature >> 8 ) & 0xff; // high byte
data[1] = temperature & 0xff; // low byte
// transmit 4 bytes
ttu.sendBytes(data,4);
// wait 2 minutes before the next transmission
delay(120000);
}
The result should be something like this:
#include "TheThingsUno.h"
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Set your AppEUI and AppKey
const byte appEui[8] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x05, 0xDF };
const byte appKey[16] = { 0x30, 0x0B, 0x7E, 0xF7, 0x00, 0x34, 0x89, 0x71, 0x6C, 0x58, 0xCF, 0xC4, 0xDC, 0xFC, 0xD6, 0x99 };
#define debugSerial Serial
#define loraSerial Serial1
// Set your message to send
String message = "Hello world"; //sending a string of chars "Hello world"
#define debugPrintLn(...) { if (debugSerial) debugSerial.println(__VA_ARGS__); }
#define debugPrint(...) { if (debugSerial) debugSerial.print(__VA_ARGS__); }
TheThingsUno ttu;
DHT dht(DHTPIN, DHTTYPE);
// Array to contain data to transmit
byte data[8];
void setup() {
debugSerial.begin(115200);
loraSerial.begin(57600);
delay(1000);
ttu.init(loraSerial, debugSerial);
ttu.reset();
//the device will attempt a join every second till the join is successfull
while(!ttu.join(appEui, appKey)){
delay(6000);
}
digitalWrite(13, HIGH); //turn on LED to confirm join
delay(6000);
ttu.showStatus();
debugPrintLn("Setup for The Things Network complete");
dht.begin();
delay(1000);
}
void loop() {
// Read humidity
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// convert the values to integers, multiply by 100 to keep 2 digits behind the decimal point
int humidity = h * 100;
int temperature = t * 100;
// fill array with values
data[2] = (humidity >> 8) & 0xff; // high byte
data[3] = humidity & 0xff; // low byte
data[0] = (temperature >> 8 ) & 0xff; // high byte
data[1] = temperature & 0xff; // low byte
// transmit 4 bytes
ttu.sendBytes(data,4);
// wait 2 minutes before the next transmission
delay(120000);
}
Compile and upload the sketch. Check the web interface for the result: (0x0A46 == 2630)