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 Principles:
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) API to read actuator commands from AskSensors:
Users can control their actuator devices manually from AskSensors Application. First, they need to create an Actuator device and send commands as described in this section. Then, the actuator devices can receive these commands from the AskSensors cloud server as follows
- The AskSensors topic is structured in a simple way:
- username: Your AskSensors username.
- apiKeyOut: The actuator Api Key Out generated by AskSensors during the creation of your actuator.
- The message payload structure for a command received from the module 1 of the actuator is:
- In the AskSensors web app, when the user (with a specific username) sends command to the actuator (with a specific ApiKeyOut), The AskSensors MQTT broker publish this command on the Topic: actuator/username/apikeyOut.
- The Subscriber listens for messages with this particular topic “actuator/username/apikeyOut” will receive commands