Datacake Docs
  • Welcome
  • Get Started
  • Changelog
  • Best practices
    • Best practices: Getting started
    • Best practices: Resolution (Timeframe)
    • Best practices: Dashboards
    • Best practices: Reports
    • Best practices: Grouping Devices
  • Datacake LNS
    • Getting Started
    • Gateways
      • Milesight LoRaWAN Gateway - UG6x
      • RAKwireless WisGate Edge Light 2
    • Devices
    • Add Devices without Gateway
  • Wireless IoT Hub
    • Overview
    • Datasheet
    • Getting Started
  • Device
    • Product
    • Configuration
    • Claiming
    • Historical Data
    • Database
      • Configuration Fields
      • Fields
        • Manual input
        • Field Roles
        • Formulas
        • Mapping Fields
      • Data retention & Datapoints
      • Examples
        • Mapping 4-20mA Signals
        • Converting Units
  • Dashboards
    • Global Dashboards
      • Setting Homepage
    • Device Dashboards
    • Multi-Device Dashboards
    • Widgets
      • Value Widget
      • Chart Widget
      • Image Map
      • Map Widget
      • Text Widget
      • SOS Widget
      • Boolean Widget
      • iFrame Widget
      • Downlink Widget
      • Set Value Widget
      • Measurement List Widget
      • Heatmap Widget
      • Table Widget
      • Image Widget
  • Portal
    • Multi-Tenancy (Workspaces)
    • Reports
      • Energy Report
    • Administrators
    • Members
      • API Users
    • Security & Privacy
    • Billing
      • Support Packages
      • VAT Exemption / Tax ID
      • SMS Credits
      • Access Invoices
      • Unused time and Remaining time in Invoices (Prorations)
      • Codes
    • White Label
    • Rules
      • Legacy Rule Engine
        • Sending notifications via Telegram
      • New Rule Engine
        • Rule Engine Table Overview
        • Copy/Paste and Template Functionality
        • Advanced Rule Engine E-Mail and Webhook Templates
        • Time Restrictions
        • Actions
          • Set Value
    • Zones
  • Cake Red
    • Get Started
    • Overview
  • LoRaWAN
    • Get Started
      • CSV Import
      • Custom LoRaWAN Device
        • How to create a device without a product template
    • Configuring LNS
      • The Things Stack (TTN / TTI) Automated Setup
      • The Things Stack (TTN / TTI) Manual Setup
      • Loriot
      • Kerlink Wanesy
      • Helium
      • ChirpStack
      • Tektelic
      • Actility ThingPark
      • Senet
      • Milesight Gateway
      • KPN
    • Downlinks (LoRaWAN)
      • Set Time Frame
    • Securing Webhooks
    • Payload Decoders
      • Location and GPS
      • Access Gateway Metadata
      • Access Measurements
      • Global Variables
    • Using Cayenne LPP
    • Converting Payload
  • Generic API Devices
    • HTTP Downlinks
  • Integrations
    • MQTT
    • Particle
      • Get Started
      • Adding Integrations
      • Decoding Payloads
      • Calling Functions
      • Templates
        • Particle Tracker One
    • Incoming Webhooks
    • Outgoing Webhooks
      • Securing Outgoing Webhooks in Datacake
    • Golioth
    • Blues Wireless Notecard
    • Sigfox
    • Swarm Asset Tracker
    • Grandcentrix Modbus Cloud Connect
    • YuDash LYNX IoT Gateway
    • Dragino NB-IoT
      • Changing NB-IoT Bands
    • Hardwario CHESTER
    • 1NCE OS
  • API
    • Exporting Data
    • Record Measurements via API
    • Node RED to Datacake
    • Generate Access Token
    • Internal MQTT
      • Get Started
      • MQTT Documentation
      • MQTT over WebSocket
      • Example Code
    • GraphQL API
      • Using GraphQL
      • Device Creation
      • Tutorials
        • Read Group via Tags
  • Guides
    • Python
    • Send Slack Notifications
    • Forward Data to Cake Red
    • Multiple Data Feeds
    • Automated Dynamic Downlinks
    • Ingesting JSON Data into Datacake API Devices
    • Working with Default HTTP Decoder on API Devices and Code Examples
    • Accessing Measurements in Decoders
    • Connecting Datacake to Notion Using Zapier
    • How to set up and use location on non-GPS devices
    • How to integrate with AWS IoT Core
    • How to Schedule Mass-Downlinks (Multicast) using Datacake Rule Engine
    • How to Map Sensor Values to Ranges or Booleans, Strings using Mapping Fields
    • Understanding Internal vs. External MQTT Brokers on Datacake
    • Sending UDP Messages between 1NCE OS and Datacake
    • Concepts of LoRaWAN Payload Decoders on Datacake
    • How to check if a value stays above or below limits for given time
Powered by GitBook
On this page
  • Register on the Datacake Cloud
  • MQTT vs. GraphQL-API
  • Using MQTT
  • Using GraphQL-API
  • Create a new Device
  • Add Database Field
  • Connect to Datacake MQTT Broker
  • Record Measurement Values
  • Troubleshooting
  • Record programatically
  • Using Javascript
  • C++ / ESP32
  • Republish

Was this helpful?

  1. API
  2. Internal MQTT

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:

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:

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).

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:

Last updated 1 year ago

Was this helpful?

If you still have problems, please contact our support via

Database
MQTT Documentation
support@datacake.co
MQTT Documentation
Datacake_MQTT/main.cpp at master · ScholliYT/Datacake_MQTTGitHub
Logo