Skip to content

MQTT

Connecting and Publishing Data to MQTT

One of the key features of the DataLogger IoT is it's simplified access to IoT service providers and servers. This document outlines how output from a DataLogger device is sent to an MQTT Broker.

MQTT Logo
Image Courtesy of MQTT

The following is covered by this document:

  • Overview of the MQTT connection
  • How a user configures and uses the MQTT connection
  • MQTT examples

General Operation

MQTT connectivity allows data generated by the DataLogger IoT to be published to an MQTT Broker under a user configured topic. MQTT is an extremely flexible and low overhead data protocol that is widely used in the IoT field.

The general use pattern for MQTT is that data is published to a topic on a MQTT broker. The data is then sent to any MQTT client that has subscribed to the specified topic.

MQTT Overview

The DataLogger IoT supports MQTT connections, allowing an end user to enter the parameters for the particular MQTT Broker for the application to publish data to. When the application outputs data to the broker, the DataLogger IoT publishes the available information to the specified "topic" with the payload that is a JSON document.

Data Structure

Data is published to the MQTT broker as a JSON object, which contains a collection of sub-objects. Each sub-object represents a data source in the sensor, and contains the current readings from that source.

The following is an example of the data posted - note, this representation was "pretty printed" for readability.

{
  "MAX17048": {
    "Voltage (V)": 4.304999828,
    "State Of Charge (%)": 115.0625,
    "Change Rate (%/hr)": 0
  },
  "CCS811": {
    "CO2": 620,
    "VOC": 33
  },
  "BME280": {
    "Humidity": 25.03613281,
    "TemperatureF": 79.64599609,
    "TemperatureC": 26.46999931,
    "Pressure": 85280.23438,
    "AltitudeM": 1430.44104,
    "AltitudeF": 4693.04834
  },
  "ISM330": {
    "Accel X (milli-g)": -53.31399918,
    "Accel Y (milli-g)": -34.03800201,
    "Accel Z (milli-g)": 1017.236023,
    "Gyro X (milli-dps)": 542.5,
    "Gyro Y (milli-dps)": -1120,
    "Gyro Z (milli-dps)": 262.5,
    "Temperature (C)": 26
  },
  "MMC5983": {
    "X Field (Gauss)": -0.200622559,
    "Y Field (Gauss)": 0.076416016,
    "Z Field (Gauss)": 0.447570801,
    "Temperature (C)": 29
  }
}

MQTT Broker Connection Setup

To connect to a MQTT Broker, the following information is needed:

  • The server name/address
  • The server port
  • The topic to post to
  • [optional] The name of the device/Client name publishing the data
  • [optional] A username - if required
  • [optional] A password - if required

These values are set using the standard DataLogger methods - the interactive menu system, or a JSON file.

MQTT Menu System

We'll need to adjust the settings for the MQTT Client using the MQTT Menu System.

For users that are interested in using the menu system, open a Serial Terminal, connect to the COM port that your DataLogger enumerated to, and set it to 115200 baud. In this case, we connected to COM13. Press any key to enter the Main Menu. Type 1 to enter the Settings menu. Then type 9 to enter the MQTT Client Menu. When the menu system for the MQTT connection is presented, the following options are displayed:

MQTT Menu

The options are:

  • Enable/Disable the connection
  • Broker Port - The standard port for mqtt is 1883
  • Broker Server - This is just the name of the server
  • MQTT Topic - A string
  • Client Name
  • Username
  • Password
  • Buffer Size

At a minimum, the Broker Port, Broker Server Name, and MQTT Topic need to be set. What parameters are required depends on the settings of the broker being used.

Note

If a secure connection is being used with the MQTT broker, use the MQTT Secure Client option of the DataLogger IoT. This option supports secure connectivity.

Note

The Buffer Size option is dynamic by default, adapting to the size of the payload being sent. If runtime memory is a concern, set this value to a static size that supports the device operation.

Once all these values are set, the system will publish data to the specified MQTT Broker, following the JSON information structure noted earlier in this document.

JSON File Entries

If a JSON file is being used as an option to import settings into the DataLogger IoT, the following entries are used for the MQTT IoT connection:

"MQTT Client": {
    "Enabled": false,
    "Port": 1883,
    "Server": "my-mqttserver.com",
    "MQTT Topic": "/sparkfun/datalogger1",
    "Client Name": "mysensor system",
    "Buffer Size": 0,
    "Username": "",
    "Password": ""
  },

Where:

  • Enabled - Set to true to enable the connection.
  • Port - Set to the broker port.
  • Server - The MQTT broker server.
  • MQTT Topic - The topic to publish to.
  • Client Name - Optional client name.
  • Buffer Size - Internal transfer buffer size.
  • Username - Broker user name if being used.
  • Password - Broker password if being used.

Tip

To load the values by the system at startup using a JSON file and microSD card, you will need to configure the Save Settings. This JSON file will be created with the "Save to Fallback" option. Make sure to enable the MQTT Client as well.

Testing the MQTT Connection

Use of a MQTT connection is fairly straightforward - just requiring the entry of broker details into the connection settings.

To test the connection, you need a MQTT broker available. A quick method to setup a broker is by installing the mosquitto package on a Raspberry Pi computer. Our basic MQTT Tutorial provides some basic setup for a broker.

This MQTT Broker Tutorial has more details, covering the setup needed for modern mosquitto configurations.

And once the broker is setup, the messages published by the IoT sensor are visible using the mosquitto_sub command as outlined. For example, to view messages posted to a the topic "/sparkfun/datalogger1", the following command is used:

mosquitto_sub -t "/sparkfun/datalogger1"

This assumes the MQTT broker is running on the same machine, and using the default port number.