IOT Protocols - HTTP vs MQTT (Part 1)

IOT Protocols - HTTP vs MQTT (Part 1)

We live in an era of connected devices and we bother to know what's working behind them, We are developers !. With IOT the first thing that comes to mind is Internet and next thing which follows is HTTP. HTTP/S is the most common form of Internet protocol it stands for Hyper Text Transfer Protocol, whereas MQTT is it's brother who is well handling the IOT industry. MQTT stands for Message Query Transport Telemetery. There is much more to know with those two wizard protocols, so we will discuss both of them elaborately with examples, and yes there will be programming. In today's blog post we will learn,

  1. Http
  2. Practical Example with Http on ESP32.
  3. Mqtt
  4. Practical Example with Mqtt on ESP32
  5. Comparison and Conclusion

So let's get started,

HTTP Protocol

Directly from Mozilla HTTP WebDocs, "HTTP is a protocol which allows the fetching of resources, such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser. A complete document is reconstructed from the different sub-documents fetched, for instance text, layout description, images, videos, scripts, and more. Clients and servers communicate by exchanging individual messages (as opposed to a stream of data). The messages sent by the client, usually a Web browser, are called requests and the messages sent by the server as an answer are called responses."

myhttp.png

In the above figure as we see the Internet has a number of cloud servers and number of client interfaces then there are so many cloud databases, so what distinguishes them all is the IP address or the Internet Providers Address looks like this 192.168.0.25 for local or 206.32.05.25 global. Each device or device system in the internet has a unique IP address from which they can be recognised.

The IP is associated with another important thing called Port. There are total of 665535 ports in an IP. Ports are basically an address to a specific process among number of existing or possible processes running on a server instance with a particular IP.

Now we have to know about how the HTTP Protocol provides communication between devices and servers ?

HTTP Protocol comes with a facility called "requests/responses", each communication that takes place on Internet with HTTP, involves the play of requests and responses. Requests are like a function which runs over the internet, takes in an endpoint, may or maybe some arguments and run, returning a response. Endpoint is the combination of IP and Port n.o which is mapped to a custom domain name, it looks like "http://180.90.20.206:8000", where former is the IP and later is the port no, it can be mapped to a domain like, tecelit.hashnode.dev

Requests are the medium to send data, then is another very important piece in the game, it feels like nostalgia, the moment that enlightened my life as a developer was knowing this life-saver piece, Its is an REST API (Representational state transfer Application Programming Interface), these carry json data object throughout the internet to desired endpoint. Each Rest API is deployed and running on a particular endpoint. Read about REST API in detail here.

The API accepts or sends requests, which are of GET,POST,PUT,DELETE, as name suggests GET is mainly for fetching useful data from api endpoint, POST is mainly for sending data to api endpoint and PUT for replace and DELETE for deletion of data from database at the server which serves the endpoint.

That may be enough theory on HTTP, so we'll see an example of GET and POST with ESP32 shortly.

MQTT Protocol

This is the most interesting thing in IOT. MQTT is a protocol which has really made IOT popular, we'll have to see how it works,

realiot.png

In the above Fig we can see MQTT protocol and HTTP working together in a probable real world system. If we see the red and orange colour index it points to Publish and Subscribe. MQTT is based upon a Pub/Sub model. As we see in picture there is a cloud Broker, this broker is a server basically which routes the messages from one node to another based on their preferences. Each node in system, can directly send and receive messages to and from the broker. Every special class of data is grouped into topics, on the broker, a client node whether be a PC or a microcontroller can Publish data onto a particular topic and can also subscribe the data from various available topics. Learn more in depth about MQTT from here.

We'll now see something happening in real, lets get started playing around it.