MQTT Summary [Source: Wikipedia]

MQTT (Message Queuing Telemetry Transport) is an open messaging protocol for machine-to-machine communication (M2M) that enables the transmission of telemetry data in the form of messages between devices, e.g. sensors.
The Internet Assigned Numbers Authority (IANA) reserves ports 1883 and 8883 for MQTT. MQTT messages can be encrypted using the TLS protocol.
The MQTT specification distinguishes between TCP/IP-based and non-TCP/IP networks.
MQTT is a client-server protocol. Clients send messages with a topic to the server (“broker”) after establishing a connection; the topic hierarchically classifies the message, for example Kitchen/Fridge/Temperature or Car/Wheel/3/AirPressure. Clients can subscribe to these topics, and the server forwards received messages to the respective subscribers.

It is interesting that an MQTT server (“broker”) holds the entire data state of its communication partners and can thus be used as a state database. This makes it possible to connect small, low-performance MQTT devices to an MQTT broker, where the devices collect data and/or receive commands, while a complex situational picture arises only on the MQTT broker and can be analyzed there or by a more powerful communication partner.

Messages always consist of a topic and the message payload. Messages are sent with a definable quality of service:

  • at most once: the message is sent once and may not arrive if the connection is interrupted
  • at least once: the message is sent repeatedly until receipt is acknowledged and may arrive multiple times at the recipient
  • exactly once: this ensures that the message arrives exactly once even if the connection is interrupted

Additionally, the server can be instructed with the Retain flag to store the message for that topic.

When establishing the connection, clients can define a “last will” in the form of a message. If the connection to the client is lost, this message is published and sent to the respective subscribers.

MQTT is usually used over TCP and has a 2-byte header. The first byte contains the message type (4 bits), the quality of service (2 bits), and a retain flag. The second byte contains the length of the remaining MQTT packet. This is followed by a variable section that contains the MQTT topic, i.e., the subject. Finally comes the payload, i.e., the data content that is published under the topic.

There are the following message types:

  • CONNECT
  • CONNACK
  • PUBLISH
  • PUBACK
  • PUBREC
  • PUBREL
  • PUBCOMP
  • SUBSCRIBE
  • SUBACK
  • UNSUBSCRIBE
  • UNSUBACK
  • PINGREQ
  • PINGRESP
  • DISCONNECT

The bolded message types can be found in the following communication:

Topics can include wildcards:

  • Single level wildcard (+): house/+/fridge/temperature
  • Multi level wildcard at the end (#): kitchen/fridge/#

Installing MQTTBox

MQTTBox is an MQTT client that, among other things, gives us the ability to create new topics and/or subscribe to existing topics.

Configuration of MQTTBox for communication with the Cummulocity (C8Y) IoT platform:

Based on the MQTT client ID, future requests can be assigned to an existing device with the aforementioned ID. I’ll jump ahead a bit and show where this ID is located after creating a device:

Afterwards, a connection to C8Y can be established.

Device creation within the C8Y IoT platform via MQTT

A device can be created using a predefined C8Y MQTT template with the template number ‘100’.

100, deviceName, deviceType

It is possible to include further templates within the payload on new lines. For example, with template 110 the following attributes can be specified: serialNumber, model, revision

List of template IDs

A detailed description of the templates and their parameters can be found at the following URL.

100Device creation
101Child device creation
105Get child devices
106Get children of device
110Configure Hardware
111Configure Mobile
112Configure Position
113Set Configuration
114Set supported operations
115Set firmware
116Set software list
117Set required availability
200Create custom measurement
210Create signal strength measurement
211Create temperature measurement
212Create battery measurement
301Create CRITICAL alarm
302Create MAJOR alarm
303Create MINOR alarm
304Create WARNING alarm
305Update severity of existing alarm
306Clear existing alarm
400Create basic event
401Create location update event
402Create location update event with device update
500Get PENDING operations
501Set operation to EXECUTING
502Set operation to FAILED
503Set operation to SUCCESSFUL
510Restart
511Command
513Configuration
515Firmware
516Software list
517Measurement request operation
518Relay
519RelayArray
520Upload configuration file
521Download configuration file
522Logfile request
523Communication mode

The following image shows the creation of an MQTT device:

The device is then displayed within ‘Device Management’:

As you can see from the template IDs above, a new device was created with ID 100. Template ID 117 set the ‘response interval’, and ID 212 represents the battery charge status. Here, 97 stands for 97%. By entering, for example, 212,95 & 212,85 & 212,65 & 212,57 we can simulate a battery discharge.
I previously subscribed to topic ’s/e’ to read out the battery status.

Within the platform, the battery discharge looks as follows: