Datacake and HiveMQ Integration with NCD Machine Uptime Sensor

Table of Contents


This tutorial is going to create an Integration between the NCD Machine Uptime Monitoring Sensor and two popular platforms – HiveMQ and DataCake. You will need the following NCD equipment (in addition to the Machine Uptime Sensor):

Enterprise IIoT Gateway or MQTT Micro Gateway – has Node-RED built-in, and is pre-configured to ingest data – you only need to add a block for the sensor ( this needs to be configured for the particular sensor) and one for the MQTT. We are going to use this option for our example as it is a more efficient process.

Additionally you would need a DataCake account. We are going to use the free, Public HiveMQ Broker as this is the quickest way to relialize this example, however it is not intended for production environment. If you need to use it in the field we recommend deploying your own instance. You can learn more at the HiveMQ website.

Connecting to the Gateway and Node-RED

For a quick refresher on how to set up your Gateway for the first time, please head to our article on the topic. Once you have it connected to your network and have it set up to connect to the Internet, you can connect directly to its Node-RED instance via the following address:


Where xx.xx.xx.xx stands for the local IP address of the Gateway (for example the one assigned to it via your router, or manually set via its settings) and the port used is 1880 (this is the default for the Node-RED instance running on the NCD Edge gateway).

A freshly provisioned gateway will greet you with the following:

The Gateway itself should be set up and Node-RED should be pre-configured to connect to it (you should see the green “Ready” message below the block).

Additionally, it comes with the NCD library installed so you can quickly proceed to connecting the sensor. This part is very straight forward as the Gateway comes pre-configured. The next portion will require a bit more tinkering.

Connecting your Machine Uptime Sensor to the Gateway

The next step is to connect the machine uptime sensor to the Gateway. The Gateway will be the Edge Device that will forward the data to Qubitro, thus it needs to first receive it wirelessly from the sensor and properly handle it.

Pull in a Wireless Device block into the flow and attach a debug block to it. This will represent the sensor and the debug block will give you the ability to examine the sensor readings that are sent to the gateway. Your flow should look like this.

As every NCD sensor is different, you would need to configure the Wireless Device block specifically for the one you are using. This example will focus on the Machine Uptime Monitoring Sensor.

Double click on the Wireless Device to get to its “Properties” window. You can give it a name (leaving it empty will populate it with the sensor type), configure the Serial Device interface and the Serial Device for Config interface and select the Sensor type.

As the Gateway block is pre-configured you should see its port listed in the two drop down menus for the Serial Device. Select whichever port is available to assign it. Leave the Mac Address empty for now (this would ingest data from any sensor, not filtering by MAC).

For Sensor Type select 108 – Machine Uptime Monitor (again this is specifically for this sensor, choose whichever matches your use-case).

Check the Auto Config and the OTF Config boxes so you can configure the sensor remotely. Submit your changes and deploy the flow and your sensor should be set up. If you power on the sensor at this point you should see data in the debug window (debug 2 node).

If the configuration was successful you should see data in the debug window. For this particular sensor you would see something similar to the following.

Now that the sensor is transmitting data and the gateway is ingesting it, we need to connect the gateway to the HiveMQ MQTT broker.

Pushing the Data to HiveMQ over MQTT

In order to push data from the Gateway to DataCake, you would need to configure Node-RED to send it via MQTT to the HiveMQ Broker first. The Gateway comes with the MQTT library pre-installed so you only need to drag the block into the flow and connect it to the output of the sensor.

Connecting things this way will forward the data only from this single sensor, thus you can choose to connect many sensors to one gateway. In this case, you might want to connect the Gateway to HiveMQ itself (this would send data from any sensor connected to the Gateway). However for this example we will keep it simple. Check out the image below, you flow should look like it.

Now you need to configure the “mqtt out” node to send to the HiveMQ Broker. Open it up and enter a “Topic” name (we are using ncd_muptime, if you decide to use another it must be reflected in all subsequent fields where it exists). Press on the Pen icon to edit the connection configuration (refer to the image below).

Enter the following (as per the image below):

Name: HiveMQ Public (you can choose another it won’t create an issue)

Server: (this is the address of the HiveMQ Public broker, you can also get it from their dashboard here)

Leave the rest with the default values, press the “Add: button followed by the “Done” button and your node is configured.

If you Deploy your flow your Node-RED will connect with the broker and any data that the sensor sends will be forwarded to the ncd_muptime topic.

Forwarding the data to DataCake

Creating the Integration

As you become more familiar with the DataCake platform you will need to create an account, if not already done so. If you do not have one yet, follow the link.

Log in and add a new device. This is required in order to get the data from the HiveMQTT broker, although the actual device sending the data will be the Sensor via the Gateway.

The type of device to select in this particular example is “API” and go to the next step.

Select the “New Product” option, give it a name and go to the next step.
Note: We are naming it NCD Edge as we are using the Edge Gateway, which can easily be used to relay data from all NCD sensors. Thus, this tutorial can be updated to work with other sensors as well.

You can leave the next screen with its default values and proceed to the last one where you select your payment plan. Note that in order to use the MQTT functionality of DataCake you can’t go with the Free plan.

Now that you have your device create it will pop up in the table, click on it and lets start configuring it.

Go to the “Configuration” tab and note the Serial Number up top, you will need it for the Payload Decoder.

Scroll down to the MQTT Configuration section and “Add New MQTT Server.

Enter the address of the HiveMQTT Broker (the same we used in Node-RED) into the Hostname field, leave the rest with the default values and finalize.

You should see your new MQTT server appear in the field (a green dot in front the name indicated the connection is good).
Now we need to add a decoder for the particular payload of the Machine Uptime Sensor.

Click on the Add Uplink Decoder button and lets proceed.

You need to replace the default example with the code below. And also Subscribe to the right topic (ncd_muptime) for this example.
Note: In the example code below the “var serial” variable contains the device ID (you need this for DataCake to properly process the data). This is for our example, you will have a different value (the one we asked you to note in the beginning of the tutorial). When you import the decoder, make sure to replace this value with yours.

					function Decoder(topic, payload) {
    // Assuming topic "ncd_muptime" does not contain dynamic parts

    // Fixed device ID
    var serial = "a945aebd-fc47-4a18-a32b-05b956bff294";

    // Transform incoming payload to JSON
    payload = JSON.parse(payload);

    // Extract values from the payload
    var digital_input_1_counter = payload.digital_input_1_counter;
    var digital_input_1_uptime = payload.digital_input_1_uptime;
    var digital_input_2_counter = payload.digital_input_2_counter;
    var digital_input_2_uptime = payload.digital_input_2_uptime;
    var digital_input_3_counter = payload.digital_input_3_counter;
    var digital_input_3_uptime = payload.digital_input_3_uptime;
    var accelerometer_counter = payload.accelerometer_counter;
    var accelerometer_uptime = payload.accelerometer_uptime;
    var magnetometer_counter = payload.magnetometer_counter;
    var magnetometer_uptime = payload.magnetometer_uptime;

    // Forward Data to Datacake Device API using Serial, Field-Identifier
    return [

            device: serial,
            field: "DIGITAL_INPUT_1_COUNTER",
            value: digital_input_1_counter
            device: serial,
            field: "DIGITAL_INPUT_1_UPTIME",
            value: digital_input_1_uptime
            device: serial,
            field: "DIGITAL_INPUT_2_COUNTER",
            value: digital_input_2_counter
            device: serial,
            field: "DIGITAL_INPUT_2_UPTIME",
            value: digital_input_2_uptime
            device: serial,
            field: "DIGITAL_INPUT_3_COUNTER",
            value: digital_input_3_counter
            device: serial,
            field: "DIGITAL_INPUT_3_UPTIME",
            value: digital_input_3_uptime
            device: serial,
            field: "ACCELEROMETER_COUNTER",
            value: accelerometer_counter
            device: serial,
            field: "ACCELEROMETER_UPTIME",
            value: accelerometer_uptime
            device: serial,
            field: "MAGNETOMETER_COUNTER",
            value: magnetometer_counter
            device: serial,
            field: "MAGNETOMETER_UPTIME",
            value: magnetometer_uptime

Your Uplink decoder should look similar to the image below:

The last part of the configuration is to add the appropriate field to represent the sensor variables to be store in DataCake.

Each has to match one of the variables in the payload (it has the following structure, with example values):


Thus you need to add them one after the other. We are going to give example with the 1st one, the rest are added in the same manner.

Click on the “Add Field” button (it is located after the Decoder Section):

Chose the Type to be “Integer” and enter the Name as per the payload above, in this case:

The Identifier will be populated automatically, leave the rest with the default values and and press the “Add Field” button.

Your new field will pop up in the table, do the same for the rest (you can skip some if you decide you don’t need this particular data).

A complete list would look something like this:

If you have properly configured the decode and the topic and fields you should be able to see the data payload being properly processed if you go to the “Debug” tab:

Visualizing the Data (Dashboard)

We are going to make things really simple for you and provide you with a ready to use Dashboard presenting all the measured metrics. If you want you can modify it in any way you like, but it is very complete and is a good start.

You simply need to import it. Go to the Dashboard section, Flip the switch to the right of the menus and this will let you edit your dashboard (this brings the Dasboard into Editing mode, any changes you implement need to be applies by flipping the switch again to Live Data mode).

Press on the “More” button and choose “Paste dashboard”:

Copy the code from the Code box below and paste it in the DataCake window:


Once you hit the “Replace Dashboard” button the Dashboard will be imported and you should see it coming live (once you flip the aforementioned switch).

This will give you all the metrics the Machine Uptime Monitor sends in real time (together with a time chard for the measured AC current value.

It should looks like the one in the image below:

You can now observe your data in real time. Modify it any way you like (as long as you created the proper decoder and fields the complete data will be stored, so you can choose what to display and what not to).
Take a look at an example one at the link below (you can share a public dashboard via DataCake):