Data Visualisation – configuring downlinks with Datacake


In this article we are going to configure The Things Network to send downlinks to our sensor device.

Aims

  • To control our device by sending a downlink. Initially we will be turning the on-board LED on and off, just to test the system.

Research

Procedure

Configuring The Things Network to send downlinks

We are going to set up a downlink which will toggle the MKR WAN onboard LED on or off. We will send a downlink with a value of 1 (01 in hexadecimal) to turn the LED on, and a downlink with a value of 0 (00 in hexadecimal) to turn the LED off. But before we send the downlink, we must upload a sketch to the board.

Save the following sketch to your MKR WAN board. This is a slightly modified version of the sketch in the video link above. Here is the link again.

#include
// #include We are not using this.
#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);
}


Don’t forget to include your ‘arduino_secrets.h file in the same directory as your sketch.

//for your board
#define APP_EUI "0000000000000000"
#define APP_KEY "WHATEVERYOURAPPKEYIS" 

Select board

Before we can use the sketch, we have to configure The Things Network to send downlinks, so login to your account on TTN. Once you have logged in to your TTN account, go to the applications list and select your device (in this case board 6).


Select EUI

On the next screen, click on the EUI of your board.


Select messaging

From the next screen select ‘Messaging’.


Schedule downlink

Complete the downlink form. Make sure you are in the ‘Schedule downlink’ tab. In the ‘Payload’ box, insert 01 (hexadecimal for 1) and tick the ‘Confirmed downlink’ box. Your screen should look like this.

Now, click the blue ‘Schedule downlink’ button.


Live data on

In the left hand navigation, find the 'Live data' link and click on it. You should see something like this.

I have circled the payload ‘ledState on 01’.


Serial monitor led on

Return to your Arduino IDE and open the serial monitor.

Here you will see the downlink has been received

Once your Arduino picks up the downlink, the onboard LED should light.

Onboard led on

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


Schedule downlink off

Now go back to the 'Messaging' tab. This time, set the payload to 00, which is hexadecimal for zero. Then click the blue ‘Schedule downlink’ button. Now view live data.

Live data led off

I have circled the payload ‘ledState off 00’.

Now view the serial monitor.


Serial monitor led off

And you will see the downlink has been received.

Onboard led off

Once your Arduino picks up the downlink, the onboard LED should go out.


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.

© Copyright 2025 Dr Dulcamara - All Rights Reserved