Sending data to a MQTT broker

MQTT communications require a broker configured for data logger connections. Various brokers are available, so it is recommended to consult an IT professional when selecting one for your application.

Before configuring your data logger for MQTT communications, gather information about the destination server (MQTT broker) and configure the data logger with the appropriate settings.

Required server information

  • The server (MQTT broker) URL address

  • The port number for MQTT communication

  • The authentication method

    • Certificates (if using mutual authentication)

  • Determine a unique Client ID for each data logger in your network; default is data logger model, underscore serial number

  • A username (if required by your network)

  • A password (if required by your network)

  • MQTT base topic

Basic data logger MQTT settings

You will use the ClosedDevice Configuration Utility Software tool used to set up data loggers and peripherals, and to configure PakBus settings before those devices are deployed in the field and/or added to networks. Also called DevConfig., to configure these data logger settings with the appropriate sever information. The Mosquitto example demonstrates configuring these settings for data logger communications with the Mosquitto test broker.

  • MQTT Enable

  • MQTT Broker URL

  • MQTT Auto Publish Data

  • MQTT Server Port

  • MQTT Client ID: Must be unique for each data logger

  • MQTT Username: May not be required for your connection

  • MQTT Password: May not be required for your connection

  • MQTT Base Topic: By default it is cs/v1/

    MQTT settings in Device Configuration Utility

Additional MQTT settings

Following are additional MQTT settings that are not required for basic set up, but may be useful for specific applications.

  • MQTT connection: Persistent (default) or Clean

    • Clean means the broker will not retain any previous subscription information. No subscription details will be retained once the connection is closed

  • Status Info Publish Interval: Minutes between publishing data logger Status information; default is 30 minutes

  • State Publish Interval: Minutes between publishing Online/Offline/File Transfer States; default is 1 minute

  • Keep Alive : Time (in seconds) between sending MQTT ping packets to broker; default is 300 seconds

  • Last Will Topic: If device is disconnected without a MQTT Disconnect Command, the MQTT broker will publish to this topic

  • Last Will Message: The message that will be published on the Last Will Topic by the MQTT broker if disconnected without a MQTT Disconnect Command

  • MQTT Last Will Topic Publish QoS: Sets the Quality of Service for publishing the MQTT Last Will Message; default is 0

  • MQTT Last Will Message Retained by Broker: Default is Do Not Retain

See the ClosedDevice Configuration Utility Software tool used to set up data loggers and peripherals, and to configure PakBus settings before those devices are deployed in the field and/or added to networks. Also called DevConfig. Help for more information about these settings.

Mosquitto example

This example uses the public Mosquitto test broker https://test.mosquitto.org/  for testing. This section is provided as a convenience; Campbell Scientific does not provide technical support for Mosquitto.

  1. Ensure your data logger is connected to the internet.

  2. Using ClosedDevice Configuration Utility Software tool used to set up data loggers and peripherals, and to configure PakBus settings before those devices are deployed in the field and/or added to networks. Also called DevConfig., connect to the data logger.

  3. (Recommended) On the Logger Control tab, set the Reference Clock Setting to UTC.

  4. On the Settings Editor tab, click the MQTT sub-tab.



    (Click image to expand/collapse display)

  1. Enable MQTT.
  2. Enter the Broker URL. Enter test.mosquitto.org for this example.
  3. Select Persistent for MQTT Connection type.
  4. Enter 1883 for the Port Number.
  5. Write down the MQTT Base Topic; it is case sensitive; by default it is cs/v1/.
  6. Keep all other MQTT settings as their defaults.
  1. Click Apply.

Program the data logger

Use MQTTPublishTable() within a DataTable/EndTable declaration to publish stored data via MQTT. See the CRBasic help for details on these instructions.

DataTable(Five_Min,True,-1)

DataInterval(0,5,Min,10)

Average(1,Temp_C,FP2,False)

Minimum(1,BattV,FP2,False,False)

Publish every 5 min in CSIJSON format The last three parameters are optional for the CSIJSON format.They specify longitude, latitude, and altitude when using GeoJason. Here we use NaN as placeholders for these values.

MQTTPublishTable(0,0,5,Min,1,NaN,NaN,NaN)

EndTable

Using MQTTConnect() when managing modem power

When managing modem power and publishing to CampbellCloud or another MQTT broker, you can encounter timing issues with the execution of the instruction and the powering of the modem. If an MQTT transaction is triggered when the modem hasn’t completely powered on, it will result in a failure. You can address this by using the MQTTConnect() instruction which overrides the default retry schedule of the MQTT publish.

For example, by default the MQTT publish retries every minute for 3 times, then every 15 minutes twice, then every 30 minutes. Because that schedule is not synced at the top of the hour, a common modem power schedule of at the top of every hour will be out of sync with most MQTT retries. In the code example below, the modem is designed to turn on for only 10 minutes per hour. The PingIP() test ensures that the data logger has an internet connection. Using the MQTTConnect() instruction forces an attempt to publish when the modem connection is online.

IfTimeIsBetween (55,5,60,min) Then

SW12 (SW12_1,1 )

ping_time =PingIP ("8.8.8.8",2000)

If ping_time <> 0 Then MQTTConnect(1)

Else

MQTTConnect(0)

If settings.MQTTState = 0 Then SW12 (SW12_1,0) ' modem off

EndIf