Table of Contents
Choosing the right communication protocol can make or break your IoT project. MQTT, HTTP, and CoAP are the three most popular choices, each with distinct strengths. This guide compares them head-to-head with real-world benchmarks and ESP32 code examples to help Indian developers make the right choice.
Understanding IoT Protocols
IoT protocols define how devices exchange data with servers and each other. The choice depends on:
- Bandwidth: How much data can your network handle? (important in rural India with 2G/3G)
- Power consumption: Is your device battery-powered?
- Latency: Do you need real-time data or can it be delayed?
- Reliability: Can you afford to lose messages?
- Complexity: How experienced is your development team?
MQTT: The IoT Standard
MQTT (Message Queuing Telemetry Transport) is the de facto standard for IoT communication:
- Architecture: Publish-subscribe model with a central broker
- Overhead: Minimum 2-byte header — extremely lightweight
- QoS levels: 0 (at most once), 1 (at least once), 2 (exactly once)
- Keep-alive: Persistent TCP connection with configurable ping interval
- Last Will: Automatic notification when a device disconnects unexpectedly
- Retained messages: New subscribers immediately get the last known value
MQTT was originally designed by IBM for satellite links with limited bandwidth — making it perfect for Indian IoT deployments in areas with poor connectivity.
HTTP and REST APIs for IoT
HTTP is the most familiar protocol for web developers entering IoT:
- Architecture: Request-response model (client asks, server responds)
- Overhead: Large headers (typically 200-800 bytes per request)
- Security: TLS/HTTPS well-established with certificate infrastructure
- Caching: Built-in caching support with ETags and cache headers
- Tooling: Every programming language has HTTP libraries
HTTP works well when devices send data infrequently (once per minute or less) and do not need real-time cloud-to-device communication.
CoAP: Lightweight for Constrained Devices
CoAP (Constrained Application Protocol) is designed for the most resource-limited devices:
- Architecture: Request-response over UDP (like HTTP but lighter)
- Overhead: 4-byte header — even lighter than MQTT
- Observe: Subscription mechanism similar to MQTT’s subscribe
- Multicast: Send one message to multiple devices simultaneously
- DTLS: Secure variant over UDP
Protocol Comparison Table
| Feature | MQTT | HTTP | CoAP |
|---|---|---|---|
| Transport | TCP | TCP | UDP |
| Header Size | 2 bytes | ~700 bytes | 4 bytes |
| Pattern | Pub/Sub | Request/Response | Request/Response |
| Power Usage | Low | High | Very Low |
| Bidirectional | Yes (native) | No (needs polling) | Yes (observe) |
| QoS | 3 levels | None built-in | Confirmable/Non |
| Best For | Real-time IoT | Cloud APIs | Sensor networks |
When to Use Which Protocol
Choose your protocol based on your specific use case:
- Use MQTT for: home automation, real-time dashboards, factory monitoring, any project needing cloud-to-device commands
- Use HTTP for: infrequent data uploads, integration with existing REST APIs, file uploads (images, firmware), projects where developers know web development
- Use CoAP for: battery-powered sensors, networks with high packet loss, constrained devices (8-bit MCUs), multicast scenarios
Build and Test All Three Protocols
ESP32 Code Examples for All Three
MQTT Example (PubSubClient):
#include
#include
WiFiClient espClient;
PubSubClient mqtt(espClient);
void setup() {
WiFi.begin("SSID", "PASS");
while (WiFi.status() != WL_CONNECTED) delay(500);
mqtt.setServer("broker.hivemq.com", 1883);
mqtt.connect("esp32-india");
mqtt.publish("iot/temperature", "32.5");
}
HTTP Example (HTTPClient):
#include
#include
void sendHTTP() {
HTTPClient http;
http.begin("http://your-server.com/api/sensor");
http.addHeader("Content-Type", "application/json");
int code = http.POST("{"temp":32.5}");
Serial.printf("HTTP response: %dn", code);
http.end();
}
Frequently Asked Questions
Which protocol should I use for my first IoT project?
Start with MQTT. It has the best balance of simplicity, efficiency, and features. Free brokers like HiveMQ and Mosquitto make it easy to get started without any server setup.
Can ESP32 use all three protocols?
Yes, ESP32 supports MQTT (via PubSubClient), HTTP (via HTTPClient), and CoAP (via coap-simple library). You can even use multiple protocols in the same project.
Is MQTT secure enough for production?
Yes, when used with TLS encryption (MQTTS on port 8883). All major cloud platforms (AWS, Azure, Google) enforce TLS for MQTT connections. Add username/password or certificate authentication for device-level security.
What about WebSocket for IoT?
WebSocket is useful for browser-based IoT dashboards. MQTT over WebSocket (supported by most brokers) lets web apps subscribe to device data in real-time without a backend proxy.
{“@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [{“@type”: “Question”, “name”: “Which protocol should I use for my first IoT project?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “Start with MQTT. It has the best balance of simplicity, efficiency, and features. Free brokers like HiveMQ and Mosquitto make it easy to get started without any server setup.”}}, {“@type”: “Question”, “name”: “Can ESP32 use all three protocols?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “Yes, ESP32 supports MQTT (via PubSubClient), HTTP (via HTTPClient), and CoAP (via coap-simple library). You can even use multiple protocols in the same project.”}}, {“@type”: “Question”, “name”: “Is MQTT secure enough for production?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “Yes, when used with TLS encryption (MQTTS on port 8883). All major cloud platforms (AWS, Azure, Google) enforce TLS for MQTT connections. Add username/password or certificate authentication for device-level security.”}}, {“@type”: “Question”, “name”: “What about WebSocket for IoT?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “WebSocket is useful for browser-based IoT dashboards. MQTT over WebSocket (supported by most brokers) lets web apps subscribe to device data in real-time without a backend proxy.”}}]}
Ready to Build Your IoT Project?
Browse our complete collection of ESP32 boards, sensors, and IoT components. Fast shipping across India with technical support.
Add comment