The use of HTTP or MQTT is a popular topic of debate among embedded system developers. The supporters of HTTP argue that is worthy and extendable. The MQTT side stresses ease of use, especially when response time, throughput, lower battery and bandwidth usage are on the first place for IoT solutions.
No matter which side of the debate one takes, here are some reasons why a developer may decide to start with MQTT over a HTTP communication link for IoT development.
HTTP (Hypertext Transfer Protocol) is a request-response based protocol between a client and server. The HTTP can be used to enable communications between IoT devices (clients) and the AskSensors server for example.
The GET Request is one of the most common HTTP methods used to request data from a specified resource.
Browsers use the GET method to request a page from the server. In Internet of things applications, the HTTP GET Requests can be used to write data to or read data from the AskSensors IoT platform. This guide explains how to send ESP32 sensors data to the AskSensors IoT cloud over HTTP.
MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. The MQTT protocol is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is limited.
In IoT applications, MQTT is commonly used to push new messages to connected devices in real time. This example shows how to control ESP32 actuators in real time from the AskSensors web app over MQTT .
HTTP is the most popular and widely used protocol. A decade ago, HTTP would have seemed the obvious choice. However, it is not well suited to IoT use cases, which are mostly driven by triggers and events. HTTP would need to poll the data base continuously to check for those triggers. With MQTT, the subscribed device listens for the trigger without the need for continuous polling, which is more efficient and more appropriate for processing and battery power.
Looking at the measurement shown by differents benchmarks and comparisons. Here are the main advantages of MQTT over HTTP:
- Throughput of MQTT is faster than HTTP (check this report made by google cloud.)
- MQTT Protocol ensures high delivery guarantees. There are 3 levels of Quality of Services:
- at most once: guarantees a best effort delivery.
- at least once: guaranteed that a message will be delivered at least once. But the message can also be delivered more than once.
- exactly once: guarantees that each message is received only once by the counterpart
- MQTT also provides users with options such as Last will & Testament and Retained messages.
Imagine you are connecting your light control system to AskSensors and you are going to send a command to turn ON/OFF the light remotely from the AskSensors web app.
In the case of HTTP, the actuator device would have to continuously make HTTP GET requests to AskSensors server to check if there is a update in the command assigned to this actuator device, and then take action to Turn ON or Turn OFF the light depending on the last value being read. This is not real time because it depends on the frequency of HTTP GET requests. In addition, the actuator device has to make a lot of requests continuously which increases the device CPU and RAM consumption and batteries drain faster.
With MQTT, you only need to subscribe your actuator device to the command topic. Then, only when the user submits a new update on this command from the AskSensors web App, the device will get notified. This communication is performed in real time and saves battery network bandwidth and resources because the connection between the actuator device and the AskSenosrs server is left open in the background but data only travels when there is a new update.
MQTT is recommended over HTTP when clients need to send commands to their actuators devices with the optimal processing and battery consumption.
If you are interested with controlling devices and Hardware over MQTT, please review this post to learn how to control ESP32 actuators with AskSensors over MQTT.
Questions or Comments?
Have a question or a suggestion for a future article?
Don’t hesitate to comment and let us know let us know what you build with the MQTT capability.
All ideas are welcome.