Node RED to Datacake

In this tutorial we will show you how you bring your devices into the Datacake Portal using Node-RED.

Node-RED is a popular tool that can be installed on smaller Linux Computers and help you create things without the need to write code.

Datacake does provide custom Node-RED Nodes that interact with Node-RED over the MQTT API. These are however more suitable for real-time use-cases as they report live measurement value changes.

If you are however looking for a way to record multiple measurements simultaneously you should go for using the REST-API instead.

This following example shows a script that can be embedded in a Node-RED function-Node which then needs to be hooked up to an HTTP-Request-Node.

When a message hits the function-Node, the script performs a write operation with the Datacake API and records measurement values send through the function into a given Datacake Device.

Script

// Set device_id to serial-number of Datacake API Device
var device_id = "be525e29-4398-4fc1-a928-dead7fdfe218";
​
// Set Token to your personal access token or individual token
var token = "put your token in here";
​
// This is the API Information for the HTTP Request Node
msg.url = "https://api.datacake.de/v1/devices/"+device_id+"/record/?batch=true"
​
// Create Header for Node-RED HTTP Node
msg.headers = {
"Authorization": "Token "+token,
"Content-Type": "application/json"
};
​
// Now we are going to create the Payload we forward to Datacake API
msg.payload = [
{
"field": "WATT",
"value": msg.payload.watt
},
{
"field": "AMPERE",
"value": msg.payload.ampere
},
{
"field": "VOLT",
"value": msg.payload.volt
}
];
​
// Return it - were all set!
return msg;