Note: most of the instructions below relate to Board 4, as the procedure seems to remain the same for Board 6. We will be returning to Board 6 in the section ‘Webhook Integration on TTN’.
In a previous tutorial, Data visualisation – connecting TTN to Datacake - Part 1, we created a webhook in your TTN account. We now need to configure Datacake to use this webhook to receive and display data from TTN.
Scroll down to see this.
Here you can see the Dev EUI of your board. Immediately below the Dev EUI you will see a section called ‘Network Server’. Here, you can see the message ‘Downlinks not configured’. We will be configuring them in the next section.
For now, the important bit is the section called 'Payload Decoder'. This is the bit where we display the uploads from your Arduino. You will see it initially contains some placeholder code, written in JavaScript. We will be replacing this with our own decoder.
We will start by setting up a ‘temperature’ field to store the data from our DS18B20 temperature sensor.
Return to the Configuration page and scroll down to the Payload Decoder section. Currently, we have:
return [
{
field: "TEST",
value: 123
}
];
*/
}
‘field’ and ‘value’ are a ‘key - value pair’. In order to decode the data from your Arduino, we need to replace the field ‘TEST’ with whatever you have called it in the ‘Fields’ section that you have just set up. In the example above, that would be TEMPERATURE. As you can see, we are using the ‘Identifier’, not the name we gave it.
Also note that this section of code lies between the lines ‘/*’ and ‘*/’. These are opening and closing ‘comment’ tags. All code lying between these two tags is ignored, so you will have to remove them when you add your real code.
We first have to change this code to reflect what is written in the Arduino sketch. Open the sketch you created and you will see:
We now have to reference the first (in this case only) item in the array, and then save it in the ‘Temperature’ field we set up earlier. When counting items in an array, remember that arrays start at 0 (as in zero), so the second item would be 1, the third would be 2, and so on. The code then becomes:
return [
{
"field": "TEMPERATURE",
"value": payload[0]
},
];
}
If you return to the ‘Debug’ tab, you should see this.
Looking along the top line of data, you will see the field marked TEMPERATURE, and the value ‘19’.
Returning to the ‘Configuration’ tab and scrolling down to the ‘Fields’ section, you should see the value of 19 now inserted into the ‘CURRENT VALUE’ column.
You can now retrieve and save data from your Arduino sensors.
The example above is for just one piece of data. If you have more, just add more JavaScript (and more fields). For example, for two items,
var decoded = [];
return [
{
"field": "TEMPERATURE",
"value": payload[0]
},
{
"field": "SOMETHINGELSE",
"value": payload[1]
},
];
return decoded;
}
Don’t forget to increment your payload value by ‘1’ for every additional field. And to add the extra field to the ‘Fields’ in the Payload Decoder section.
The data is sent from the Arduino to Datacake as hexadecimal code. This code can only deal with integers between 0 and 255. We will deal with sending number larger than this (and numbers with decimal points) in a separate section.
Datacake allows you to set up a dashboard in your account that lets you graphically view the data coming from your sensors. We will set one up for our temperature measurements.
Scroll to the top of the page and click on the ‘Dashboard’ tab.
You have added your first widget.
Now add a chart widget. Click the blue ‘Add Widget’ button again, but this time add a ‘Chart’ widget.
Under the ‘Basic’ tab, add a title such as ‘Today’s Temperature Measurements’.
Under the ‘Data’ tab, click ‘Add’ and then select ‘Temperature’ from the drop-down list in the ‘Field’ box.
Under ‘Timeframe’ select ‘Day’.
We can now save and view data from our Arduino and associated sensors. Next, we will see how to send data back down to the Arduino in response to uploaded data. This will allow us to control devices connected to our Arduino.
In the next section we will cover sending instructions back to your sensor by configuring downlinks with Datacake. Click here for the next steps.