Get Started

In this tutorial, we show you how you create your first device on Datacake and how you can connect your physical end-devices to it using MQTT over the Datacake internal MQTT Broker.

Register on the Datacake Cloud

This tutorial assumes that you have already created a user account on the Datacake platform. If this is not the case, please register now. Your first five devices are free and registration only takes a few minutes.

MQTT vs. GraphQL-API

You can connect your devices to Datacake and record measurement values using MQTT or the Datacake GraphQL-API. However, there are very clear use cases for both variants.

Using MQTT

MQTT is for Real-Time Communication. Choose MQTT if you want to exchange measurement data in real-time between device and platform or between devices.

Using GraphQL-API

Alternatively, use the Datacake GraphQL-API if you need to transfer multiple readings in one go.

Individual Timestamps

Using the GraphQL-API also provides additional features such as setting an individual timestamp. This allows measured values to be imported retroactively.

Create a new Device

To store readings via MQTT, you will need an appropriate device on the Datacake platform. To do so, navigate to the Devices view on your workspace.

There you will find a button "Add Device" in the upper right area of the listing. With one click you can create a new device. Select the device type "API", then select "New Product" to create a new product. If you are trying to add a device for which we have a template, select New Product from template and select the template from the catalog. Give your product a name and click on Next.

You can let Datacake auto-generate a serial number for your device or assign one and give your device a name. In this step you can type in a device location and add tags to your device.

Last step is selecting your device's plan.

Please take into consideration, the Datacake's MQTT internal broker is only available for paid devices.

Add Database Field

Once you created your device you will have to create some fields that will save information into the Datacake's database. Go ahead and click on your device and navigate to the Configuration section. Scroll down and on the Fields section click on "Add Field".

For this example, we will now create a field that will contain a temperature.

Enter the required information in the input areas. Select a name for the field and an identifier (In this example, we will call the field "Temperature").

Various types are available for the way data is stored. For this example we choose the type "Float".

If you want to learn more about the different data types or how database fields behave in general, please use the following link:

Database

Everything is now prepared for the next steps.

Connect to Datacake MQTT Broker

In order to be able to import measured values into the field via MQTT, you need to connect to the Datacake MQTT Broker. This uses the following URL:

mqtt.datacake.co

To connect, you must authenticate yourself using an access token. The easiest way to start is to use your personal token. You can find out exactly how this works and which steps are required in the MQTT documentation:

MQTT Documentation

Record Measurement Values

If you are connected to the Datacake Broker, you can use a Publish with measured value to upload the values to the "Temperature" database field. The topic structure looks like the following according to the general Datacake-Topic-Structure:

publish("dtck-pub/my-mqtt-device/YOUR_DEVICES_ID/TEMPERATURE", 23.02)

You only need to change the device ID in the topic to the ID of your device. You can find this in the metadata overview (if your device does not have its own serial number) or in any case in the URL of the current device.

In the following screenshot we used the free tool MQTT Explorer to simulate the recording of measured values via MQTT.

If you now perform a publish, the measured value is also shown next to the field in the configuration overview.

Troubleshooting

If you cannot see any change in the values shown in the database table, it could be the case that your client is incorrectly configured or that your access token (if you have created one yourself) does not have the required rights.

Another indicator for this is that the broker is disconnecting the client. Therefore please check the settings of your client and the token (if applicable).

If you still have problems, please contact our support via support@datacake.co

Record programatically

Of course the recording of measured values via MQTT is not limited to programs like the MQTT Explorer. You can use various programs, libraries and devices for communication via MQTT. You will find a few examples in the following section:

Using Javascript

The following code-snippet demonstrates how to use Javascript and MQTT functionality to send values from a device to Datacake.

const mqtt = require('mqtt');

const client = mqtt.connect('mqtts://mqtt.datacake.co', {
    username: "yourauthtoken",
    password: "yourauthtoken"
});

client.on('connect', () => {
    console.log('Connected to Datacake Cloud');
    // subscribe to all fields from that devices
    // can be used to send messages from datacake onto device
    client.subscribe('dtck/my-mqtt-device/b4fb3cae-9387-46ac-8d6c-687f3d94f1e2/+');
});

client.on('message', (topic, message) => {
    // do something here
});

// Publish a value of 23.02 to Datacake and Device Field
client.publish('dtck-pub/my-mqtt-device/b4fb3cae-9387-46ac-8d6c-687f3d94f1e2/TEMPERATURE', 23.02);

C++ / ESP32

The following GitHub Repository will show you how you can connect your ESP32 to Datacake over MQTT:

Republish

If you import measured values into a database field of your device, then this change will also be published from the device. You can subscribe to this republish as a client via the following Topic-Structure:

dtck/my-mqtt-device/b4fb3cae-9387-46ac-8d6c-687f3d94f1e2/TEMPERATURE

M2M

You can also use these republics as a basis for M2M communication. For more information on this topic, please click on the following link:

MQTT Documentation

Last updated