MQTT Basics
MQTT Basics
Section titled “MQTT Basics”MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol ideal for IoT devices.
Why MQTT?
Section titled “Why MQTT?”- Lightweight - Minimal overhead, perfect for constrained devices
- Reliable - Quality of Service levels ensure message delivery
- Bi-directional - Devices can send and receive messages
- Low bandwidth - Works well on slow 2G/GPRS connections
Core Concepts
Section titled “Core Concepts”Broker
Section titled “Broker”The MQTT broker is the central server that receives and routes all messages. SiliconWit.IO provides a managed broker at:
mqtt.siliconwit.ioClient
Section titled “Client”Any device or application that connects to the broker. Each client has a unique Client ID (your Device ID in SiliconWit.IO).
Topics
Section titled “Topics”Topics are hierarchical strings that categorize messages:
devices/abc123/telemetrydevices/abc123/commandssensors/temperature/officePublish/Subscribe
Section titled “Publish/Subscribe”- Publish - Send a message to a topic
- Subscribe - Receive messages from a topic
Connection Details
Section titled “Connection Details”Standard Connection
Section titled “Standard Connection”| Setting | Value |
|---|---|
| Broker | mqtt.siliconwit.io |
| Port | 1883 |
| Username | Your Device ID |
| Password | Your Access Token |
| Client ID | Your Device ID |
Secure Connection (TLS)
Section titled “Secure Connection (TLS)”| Setting | Value |
|---|---|
| Broker | mqtt.siliconwit.io |
| Port | 8883 |
| TLS | Enabled |
| Username | Your Device ID |
| Password | Your Access Token |
WebSocket Connection
Section titled “WebSocket Connection”For browser-based clients:
| Setting | Value |
|---|---|
| URL | wss://mqtt.siliconwit.io:8084/mqtt |
| Username | Your Device ID |
| Password | Your Access Token |
Quality of Service (QoS)
Section titled “Quality of Service (QoS)”MQTT offers three QoS levels:
| QoS | Name | Description | Use Case |
|---|---|---|---|
| 0 | At most once | Fire and forget | Frequent sensor data |
| 1 | At least once | Guaranteed delivery, may duplicate | Important data |
| 2 | Exactly once | Guaranteed single delivery | Critical commands |
Keep Alive
Section titled “Keep Alive”The keep-alive interval tells the broker how often to expect a heartbeat:
client.setKeepAlive(60); // 60 secondsIf the broker doesn’t receive a message or ping within 1.5x the keep-alive time, it marks the device as offline.
Last Will and Testament (LWT)
Section titled “Last Will and Testament (LWT)”LWT lets you specify a message to publish if your device disconnects unexpectedly:
client.connect( device_id, device_id, access_token, "devices/abc123/status", // LWT topic 0, // LWT QoS true, // LWT retain "{\"status\":\"offline\"}" // LWT message);Retained Messages
Section titled “Retained Messages”Retained messages are stored by the broker and sent to new subscribers immediately:
client.publish(topic, payload, true); // true = retainedUse for:
- Device status
- Configuration
- Last known values
Clean Session
Section titled “Clean Session”- Clean session = true - Start fresh, no stored subscriptions
- Clean session = false - Resume previous session, receive missed messages
client.connect(device_id, user, pass, NULL, 0, false, NULL, false);// ^ clean sessionExample: Complete Connection
Section titled “Example: Complete Connection”#include <PubSubClient.h>
WiFiClient espClient;PubSubClient client(espClient);
void setup() { client.setServer("mqtt.siliconwit.io", 1883); client.setKeepAlive(60); client.setCallback(messageReceived);
// Connect with LWT client.connect( "device123", // Client ID "device123", // Username "access_token_here", // Password "devices/device123/status", // LWT topic 1, // LWT QoS true, // LWT retain "{\"online\":false}" // LWT payload );
// Publish online status client.publish("devices/device123/status", "{\"online\":true}", true);
// Subscribe to commands client.subscribe("devices/device123/commands");}
void messageReceived(char* topic, byte* payload, unsigned int length) { // Handle incoming messages}Next Steps
Section titled “Next Steps”- Topics & Payloads - Message format guide
- MQTT Authentication - Security details