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.

Node-RED Nodes

Datacake does provide custom Node-RED extension:
node-red-contrib-datacake
nodered

Installing the Extension

  • Navigate into the "Manage Palette" setting on your Node-RED
  • Select "Install" and enter "Datacake" as the search term
  • If not yet installed press "Install" on the element in the List
  • The Datacake Nodes will now automatically installed

Configuring the Nodes

  • To start with Datcake Node-RED Nodes simply drag one of the Nodes onto an empty flow
  • Double-Click on the Node to open up Configuration:

Add Datacake Node Configuration

  • Each Datacake Node can access a Device
  • To access Devices the Node needs to communicate with a Workspace
  • You do this by providing an Access Key
  • Now click on the "Edit"-Icon next to "Add new Datacake-Configuration":
  • Now you have to enter an API Key which you need to create in your Workspace
  • This API Key needs all rights so that you can access all devices
  • Also it makes sense to give this API Key permission for all Devices in Workspace so that all current and future devices will be automatically added to the Node-RED Node.
Learn More about creating API Keys here:
  • The key should look like:

Add Key to Node-RED Node

  • Now you add the Key to your Node-RED configuration in your Node-RED
  • Shortly after entering you can select the Workspace
  • Select one Workspace and press "Add"
  • This will bring you back to the configuration of the Node and here you can now select the Device + Field from which you want to receive data:

Sending Data

  • If you want to Send Data from Node-RED into Datacake via Nodes simply drag the Output Node onto your flow and configure the Node like the input node:

Node-RED Function 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

1
// Set device_id to serial-number of Datacake API Device
2
var device_id = "be525e29-4398-4fc1-a928-dead7fdfe218";
3
4
// Set Token to your personal access token or individual token
5
var token = "put your token in here";
6
7
// This is the API Information for the HTTP Request Node
8
msg.url = "https://api.datacake.co/v1/devices/"+device_id+"/record/?batch=true"
9
10
// Create Header for Node-RED HTTP Node
11
msg.headers = {
12
"Authorization": "Token "+token,
13
"Content-Type": "application/json"
14
};
15
16
// Now we are going to create the Payload we forward to Datacake API
17
msg.payload = [
18
{
19
"field": "WATT",
20
"value": msg.payload.watt
21
},
22
{
23
"field": "AMPERE",
24
"value": msg.payload.ampere
25
},
26
{
27
"field": "VOLT",
28
"value": msg.payload.volt
29
}
30
];
31
32
// Return it - were all set!
33
return msg;
Copied!
Last modified 10mo ago