Skip to content

MQTT Basics

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol ideal for IoT devices.

  • 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

The MQTT broker is the central server that receives and routes all messages. SiliconWit.IO provides a managed broker at:

mqtt.siliconwit.io

Any device or application that connects to the broker. Each client has a unique Client ID (your Device ID in SiliconWit.IO).

Topics are hierarchical strings that categorize messages:

devices/abc123/telemetry
devices/abc123/commands
sensors/temperature/office
  • Publish - Send a message to a topic
  • Subscribe - Receive messages from a topic
SettingValue
Brokermqtt.siliconwit.io
Port1883
UsernameYour Device ID
PasswordYour Access Token
Client IDYour Device ID
SettingValue
Brokermqtt.siliconwit.io
Port8883
TLSEnabled
UsernameYour Device ID
PasswordYour Access Token

For browser-based clients:

SettingValue
URLwss://mqtt.siliconwit.io:8084/mqtt
UsernameYour Device ID
PasswordYour Access Token

MQTT offers three QoS levels:

QoSNameDescriptionUse Case
0At most onceFire and forgetFrequent sensor data
1At least onceGuaranteed delivery, may duplicateImportant data
2Exactly onceGuaranteed single deliveryCritical commands

The keep-alive interval tells the broker how often to expect a heartbeat:

client.setKeepAlive(60); // 60 seconds

If the broker doesn’t receive a message or ping within 1.5x the keep-alive time, it marks the device as offline.

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 are stored by the broker and sent to new subscribers immediately:

client.publish(topic, payload, true); // true = retained

Use for:

  • Device status
  • Configuration
  • Last known values
  • 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 session
#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
}