#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
WiFiClientSecure net;
PubSubClient mqtt(net);
void setup() {
WiFi.begin("YOUR_WIFI", "YOUR_WIFI_PASSWORD");
while (WiFi.status() != WL_CONNECTED) delay(250);
net.setInsecure(); // for a first test; use net.setCACert(...) in production
mqtt.setServer("broker.connectmq.com", 8883);
}
void loop() {
if (!mqtt.connected()) {
mqtt.connect("esp32-01", "YOUR_USERNAME", "YOUR_PASSWORD");
}
mqtt.loop();
static unsigned long last = 0;
if (millis() - last > 5000) {
last = millis();
mqtt.publish("devices/esp32-01/telemetry", "{\"temperature\":21.5}");
}
}
main.go · go get github.com/eclipse/paho.mqtt.golang
Use 8883 (MQTT over TLS) for devices and services, and 8084 (secure WebSockets, path /mqtt) for browser apps. 1883 and 8083 are unencrypted and meant for quick tests only.
Where do I find my username and password?
In your ConnectMQ dashboard at app.connectmq.com after you sign up. Create separate credentials for each device or service.
My client cannot connect — what should I check?
Check the port matches the protocol (TLS on 8883, WSS on 8084 with path /mqtt), that the credentials are correct, and that each client uses a unique client ID.
Ready to connect your first device?
Create an account, get your broker credentials and connect your first device — no servers to install, patch or scale.
broker.connectmq.com is a free public MQTT broker for testing and learning. Here are the ports, the anonymous access rules, the limits and copy-paste examples for the CLI, Python, Node.js and the browser.
Browsers cannot open raw TCP sockets, so web apps speak MQTT over WebSocket. Build a working browser client and a Node.js client against broker.connectmq.com and learn how to fix the most common connection errors.
Connect an ESP32 to an MQTT broker with Arduino IDE and PubSubClient, publish JSON temperature readings, control the onboard LED from the command line and upgrade to TLS with portal credentials.