1- MQTT Publish/Subscribe API

MQTT (Message Queue Telemetry Transport) is an ISO standard publish/subscribe based and light-weight messaging protocol.

With small IoT devices in mind, the MQTT is designed for connections with remote locations where the network bandwidth is limited or a small code footprint is required.The MQTT protocol works over three transport protocols: TCP, UDP, and WebSockets.

While HTTP adheres to the standard request response model and using the common client-server pattern, the MQTT clients connect to a broker and the remote devices publish reduced message size with low latencies, which makes the MQTT more efficient in IoT applications.

a) Publish Subscribe:

Here are the main entities of MQTT protocol:

  • A message has a topic and a payload.
  • The Publisher sends a message to the network.
  • The Subscriber listens for messages with a particular topic.
  • The AskSensors broker is responsible for coordinating the communication between publishers and subscribers: Receiving all messages, filtering the messages, decide who is interested in them and publishing the messages to all subscribed clients.

The sender publishes a message to a topic, and any software with a subscription for that topic gets a copy of this message. The sender never really knows who is listening, and just provide his message to a set of shared queue.

The receiver subscribes to a narrow selection of topics and only receives the information he is looking for. This saves both processing time and network bandwidth.

b) AskSensors MQTT structure:

The AskSensors topic is structured in a simple way: "publish/username/apiKeyIn"

  • username: Your AskSensors username.
  • apiKeyIn: The sensor Api Key In generated by AskSensors during the creation of your sensor.

The message payload structure:“m1=value1&m2=value2...m6=value6”.
Where “value1…value6” are the modules values. Here is an example of payload with two modules: “m1=10&m2=20”.
As response to the MQTT publish sequence, The AskSensors broker returns the number of modules updated successfully.

c) Send MQTT data with timestamp:

The following structure shows the integration of the timestamp in the MQTT payload:
“m1=value1&m2=value2...m6=value6&t=timestamp”.

Where “timestamp” is the local timestamp of the values being sent in the MQTT payload.
The timestamp format is UNIX Epoch time in ms(or Unix time or POSIX time or Unix timestamp), which describes the number of milliseconds that have elapsed since January 1, 1970 (midnight UTC/GMT)

Below is an example of a MQTT payload sent with timestamp of Human time (GMT): Tuesday 1 January 2019 00:00:00:
“m1=10&m2=20&t=1546300800000”.

Was this article helpful to you? Yes 4 No

How can we help?