In this article we are going to configure The Things Network to send downlinks to our sensor device.
#include
// #include
#include "arduino_secrets.h"
LoRaModem modem;
void setup() {
Serial.begin(9600);
while (!Serial);
// ENV.begin();
pinMode(LED_BUILTIN, OUTPUT);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
Serial.print("Your module version is: ");
Serial.println(modem.version());
if (modem.version() != ARDUINO_FW_VERSION) {
Serial.println("Please make sure that the latest modem firmware is installed.");
Serial.println("To update the firmware upload the 'MKRWANFWUpdate_standalone.ino' sketch.");
}
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
Serial.println("Connecting...");
int connected = modem.joinOTAA(APP_EUI, APP_KEY);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
modem.minPollInterval(60); // Default is 300s
Serial.println("Waiting for messages...");
}
void loop() {
delay(60 * 1000); // Wait 60 secs before polling again
modem.poll();
// On The Things Stack the RX1 window is 5s which is the earliest moment
// to receive any downlink data. We add 1500ms to allow for the transmission
// of the data to complete.
delay(5000 + 1500);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char dataBuffer[64];
int i = 0;
while (modem.available()) {
dataBuffer[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(dataBuffer[j] >> 4, HEX);
Serial.print(dataBuffer[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
if(dataBuffer[0] == 1){
digitalWrite(LED_BUILTIN, HIGH);
} else if(dataBuffer[0] == 0) {
digitalWrite(LED_BUILTIN, LOW);
}
}
//for your board
#define APP_EUI "0000000000000000"
#define APP_KEY "WHATEVERYOURAPPKEYIS"
The yellow light is the onboard LED. The green light is the power indicator. The three connected wires are there because the temperature sensor is still connected.
The sketch we are using has a time delay of 60 seconds between send intervals, so you may have to wait a minute for the LED to switch on
We can now turn the LED off by sending a downlink with the value of zero (00 in hexadecimal).
So far, we have sent downlinks by scheduling them from your TTN account. We are now going to schedule downlinks using Datacake. Click here for the next steps.