Datacake

To record the measurements to a specific Database Field of a Device, we will update the MQTT publish topics as follows: This section covers how to create your first device on Datacake and connect the Conexio Stratus running ZephyrRTOS to the Datacake platform via MQTT broker. MQTT is a lightweight publish/subscribe messaging protocol designed for low-bandwidth IoT devices.

Furthermore, the sample application used for this tutorial also demonstrates how to fetch the important Conexio Stratus device vitals such as the battery voltage, LTE signal strength, firmware version, device IMEI, and environmental data.

Required Toolchains

This tutorial assumes that one has already installed and set up the nRF Connect SDK, the main toolchain required for building and compiling applications for the Stratus device. If not, please refer to this section. So let’s dive in.

Registration and Device setup on the Datacake Cloud

Register and create a user account on the Datacake platform here. Your first two devices are free.

Before any measurement readings can be stored via MQTT, we will need to set up a device on the Datacake platform. After registration and account activation, head over to the fleet view of your Datacake workspace.

Click Add Device in the upper right corner which will bring up the following pop-up window.

In STEP 1, select the device type as “API”, and under Datacake Product choose New Product. Then assign a name to your device under “Product Name”. For this tutorial, we named our product to be “Conexio Stratus”.

In STEP 2, you can add one or more API devices. Next, assign the device name and hit Next.

Finally, in STEP 3, choose the Datacake plan. To create a device it is necessary to choose a payment plan. Since Datacake allows you to create up to two devices for free, you can choose the “Free” plan and click “Add 1 device”.

Hooray! Your device has now been registered to the Datacake platform and should appear under devices in the “Fleet” view. Click on your registered device and it will take you to its workspace.

Adding Database Fields

All right. At this point, we need to define fields in the database of the device, which will host the measured values that are sent from the Stratus device via MQTT. You can read more about the Datacake fields and types here.

In the Datacake, navigate to the “Configuration” tab and scroll down to the Fields section and click the “Add Field” button.

This will open a modal with a variety of data types. For this tutorial, we will add multiple fields starting with the temperature of type “Float”. The Datacake will automatically fill in the “identifier” field. See the snippet below for details.

Once the field details are complete, click “Add Field” and you’re done with this field. Below you will see all the different fields we have added for this sample application. These fields include:

  • RSRP: the LTE signal strength value

  • Battery: for recording the voltage of the connected LiPo battery

  • IMEI: the Conexio Stratus International Mobile Equipment Identity (IMEI) number

  • Version: Firmware version running on the device

  • Temperature: environmental temperature readings from the SHT40 sensor

  • Humidity: relative humidity readings from the SHT40 sensor

Adding Integrations

Next, just below the Fields section, you will find the Integrations section. For forwarding data from your devices via MQTT, a connection to Datacake must be established. Click Configure and the MQTT Integration information window will pop up.

The Datacake platform offers an MQTT broker with TLS encryption, which allows both subscription and recording of data. With the help of this broker, you can:

  • Forward incoming device data to external services via MQTT

  • Store data via MQTT into the Datacake Cloud

For recording measurements into the Datacake Cloud, we will publish the data to the respective topic structure as shown in the MQTT Integration window.

Copy the above broker name and the topic which we will use later in our firmware configuration.

The MQTT topic prefix for Datacake follows the following structure:

dtck-pub/<product_slug>/<device_id>/<field_name>

Head over to Datacake’s MQTT documentation to read more about this structure. The last element in the topic structure is the field name of the measured value, as it appears in the database field that we created earlier above. The field (identifier) is where we will publish different measurements from the Stratus device.

To view your access token, click “Show”. Now copy this access token in a safe place as we will need it later.

At this point, we have all the required details to connect and publish data from our Conexio Stratus device to the Datacake. Let’s now head over to the device firmware side.

MQTT Application Code

We have extended the sample MQTT application provided in the nRF Connect SDK to easily connect the Stratus kit to the MQTT broker, send, and receive data from the Datacake platform.

The extended sample application connects to the Datacake and publishes the data to the configured publish topic. On a button press event, the application publishes the device vitals to the Datacake and periodically publishes the environmental data such as temperature and humidity.

The full application can be found in the conexio-stratus-firmware repo on GitHub.

Add Datacake credentials to the Application Code

First, we will have to add the Datacake access token to the application code. You will need to edit conexio_stratus_firmware/samples/datacake/prj.conf with your Datacake access token. Update the following parameters.

# MQTT application configuration authentication
CONFIG_MQTT_PASS="DATACAKE_ACCESS_TOKEN"
CONFIG_MQTT_USER="DATACAKE_ACCESS_TOKEN"

The username and password are the same.

MQTT Broker Configuration

Next, we need to configure the MQTT broker hostname and the port. Update the Datacake broker configuration with the following:

# MQTT broker configuration
CONFIG_MQTT_BROKER_HOSTNAME="mqtt.datacake.co"
CONFIG_MQTT_BROKER_PORT=8883

We will be using Port 8883 which uses a CA-signed server certificate.

To record the measurements to a specific Database Field of a Device, we will update the MQTT publish topics as follows:

# MQTT topics for recording measurement values
# Change this as per your Datacake MQTT Integration and fields
CONFIG_MQTT_PUB_TOPIC_TEMP="dtck-pub/<product_slug>/<device_id>/TEMPERATURE"
CONFIG_MQTT_PUB_TOPIC_HUM="dtck-pub/<product_slug>/<device_id>/HUMIDITY"
CONFIG_MQTT_PUB_TOPIC_VER="dtck-pub/<product_slug>/<device_id>/VERSION"
CONFIG_MQTT_PUB_TOPIC_IMEI="dtck-pub/<product_slug>/<device_id>/IMEI"
CONFIG_MQTT_PUB_TOPIC_BAT="dtck-pub/<product_slug>/<device_id>/BATTERY"
CONFIG_MQTT_PUB_TOPIC_RSRP="dtck-pub/<product_slug>/<device_id>/RSRP"
CONFIG_MQTT_PUB_TOPIC_BUTTON="dtck-pub/<product_slug>/<device_id>/BUTTON"

# MQTT subscription topics
CONFIG_MQTT_SUB_TOPIC="dtck/conexio-stratus/<product_slug>/<device_id>/+"

For instance, with the Product-Slug my-product, the Device-ID 6b98a3bb-9ae1-418f-9375-f23091a849cd, and the Field-Identifier TEMPERATURE, will publish a message to:

dtck-pub/my-product/6b98a3bb-9ae1-418f-9375-f23091a849cd/TEMPERATURE

The payload will hold the value which you want to be recorded into that specific database-Field.

publish("dtck-pub/my-product/6b98a3bb-9ae1-418f-9375-f23091a849cd/TEMPERATURE", 25.00)

Now we are all set in terms of the MQTT parameter configurations.

Building and Flashing the application

To compile the application, open a terminal window in the application directory and issue the following west command

west build -b conexio_stratus_ns

Once the application is compiled successfully, connect the Stratus device and put it into the DFU mode.

Flash the compiled firmware using newtmgr:

newtmgr -c serial image upload build/zephyr/app_update.bin

Open up a serial console and reset the Stratus device. The following serial UART output will be displayed in the terminal. If you are connecting your Stratus device for the first time, give it a few minutes to register to the network and establish the LTE connection with the tower. No extra SIM activations are required as the Stratus device comes preconfigured for automatically connecting to the LTE network.

[2021-11-21 16:31:38] *** Booting Zephyr OS build v2.6.99-ncs1  ***
[2021-11-21 16:31:39] [00:00:00.213,592] <inf> mqtt_app: Stratus MQTT Datacake sample started, version: v1.0.0
[2021-11-21 16:31:39] [00:00:00.213,623] <inf> watchdog: Watchdog timeout installed. Timeout: 60000
[2021-11-21 16:31:39] [00:00:00.213,653] <inf> watchdog: Watchdog started
[2021-11-21 16:31:39] [00:00:00.213,653] <dbg> watchdog.watchdog_feed_enable: Watchdog feed enabled. Timeout: 30000
[2021-11-21 16:31:39] [00:00:00.213,684] <inf> mqtt_app: Provisioning certificates
[2021-11-21 16:31:39] [00:00:00.398,468] <inf> mqtt_app: Disabling PSM and eDRX
[2021-11-21 16:31:39] [00:00:00.399,353] <inf> mqtt_app: LTE Link Connecting...
[2021-11-21 16:31:40] +CEREG: 2,"412D","03382810",7
[2021-11-21 16:31:40] +CSCON: 1
[2021-11-21 16:31:41] +CEREG: 5,"412D","03382810",7,,,"11100000","11100000"
[2021-11-21 16:31:41] %CESQ: 35,1,10,1
[2021-11-21 16:31:42] [00:00:03.091,156] <inf> mqtt_app: LTE Link Connected!
[2021-11-21 16:31:42] [00:00:03.123,840] <dbg> mqtt_app.modem_rsrp_handler: Incoming RSRP status message, RSRP value is 35
[2021-11-21 16:31:42] [00:00:03.368,347] <inf> mqtt_app: IPv4 Address found 144.126.245.197
[2021-11-21 16:31:42] [00:00:03.369,140] <inf> mqtt_app: client_id: 352656103852334
[2021-11-21 16:31:42]
[2021-11-21 16:31:42] [00:00:03.369,140] <inf> mqtt_app: TLS enabled
[2021-11-21 16:31:42] [00:00:03.369,201] <inf> env_sensors: Environmental sensors initialized
[2021-11-21 16:31:46] [00:00:06.870,300] <inf> mqtt_app: MQTT client connected
[2021-11-21 16:31:46] [00:00:06.870,330] <inf> mqtt_app: Subscribing to: dtck/conexio-stratus/4fbfe839-c8aa-4882-a890-02f981753f6d/+ len 59
[2021-11-21 16:31:46] [00:00:07.249,328] <inf> mqtt_app: SUBACK packet id: 1234
[2021-11-21 16:31:47] [00:00:08.378,997] <inf> mqtt_app: Publishing: 26.06
[2021-11-21 16:31:47] [00:00:08.379,028] <inf> mqtt_app: to topic: dtck-pub/conexio-stratus/4fbfe839-c8aa-4882-a890-02f981753f6d/TEMPERATURE len: 73
[2021-11-21 16:31:47] [00:00:08.388,702] <inf> mqtt_app: Publishing: 23.24
[2021-11-21 16:31:47] [00:00:08.388,702] <inf> mqtt_app: to topic: dtck-pub/conexio-stratus/4fbfe839-c8aa-4882-a890-02f981753f6d/HUMIDITY len: 70
[2021-11-21 16:31:47] [00:00:08.910,614] <inf> mqtt_app: PUBACK packet id: 43056
[2021-11-21 16:31:47] [00:00:09.183,288] <inf> mqtt_app: PUBACK packet id: 62515
[2021-11-21 16:31:47] [00:00:09.205,596] <inf> mqtt_app: MQTT PUBLISH result=0 len=5
[2021-11-21 16:31:47] [00:00:09.206,390] <inf> mqtt_app: Received: 26.06
[2021-11-21 16:31:47] [00:00:09.252,532] <inf> mqtt_app: MQTT PUBLISH result=0 len=5
[2021-11-21 16:31:47] [00:00:09.253,234] <inf> mqtt_app: Received: 23.24

Once the LTE connection is established, you will notice that the Stratus connects to the Datacake MQTT broker after which it publishes the sensor data to the configured topics. Your Stratus device is now ALIVE and communicating to the Datacake cloud 🎉.

Visualize Data on Datacake Dashboard

Head back to the Datacake dashboard and add graphical widgets to your workspace.

You will now see the device data flowing into the Datacake and beautiful graphs being populated. Below is the sample dashboard that we have created. Fast and Simple!

Last updated