MQTT is a lightweight and flexible IoT message exchange and data transmission protocol, which is dedicated to achieving the balance between flexibility and hardware/network resources for IoT developers.
ESP32 is an upgraded version of ESP8266. In addition to the Wi-Fi module, this module also includes a Bluetooth 4.0 module. The dual-core CPU operates at a frequency of 80 to 240 MHz. It contains two Wi-Fi and Bluetooth modules and various input and output pins. ESP32 is an ideal choice for IoT projects.
In this project, we will connect ESP32 to the free public MQTT broker operated and maintained by EMQX MQTT Cloud, and use the Arduino IDE to program the ESP32. EMQX Cloud is a secure MQTT IoT cloud service platform which provides MQTT 5.0 access service with one-stop operation and maintenance management and a unique isolation environment.
Required IoT components
- ESP32
- Arduino IDE
- MQTT 5.0 client tool - MQTT X
- A free public MQTT broker deployed on EMQX Cloud
- Broker: broker.emqx.io
- TCP Port: 1883
- Websocket Port: 8083
Arduino configuration
Install ESP32 development board
Click Tools -> Development Board -> Development Board Management -> Search ESP32 -> Install
Install PubSub client
Project -> Load library -> Library manager... -> Search PubSubClient -> Install PubSubClient by Nick O’Leary
ESP32 Pub/Sub diagram
Programming ESP32 Board with Arduino IDE
Connect to MQTT step by step
1.First, we will import the WiFi and PubSubClient libraries. The ESP8266WiFi library can connect ESP32 to Wi-Fi networks, and the PubSubClient library can connect ESP32 to the MQTT server to publish messages and subscribe to topics.
#include <WiFi.h>
#include <PubSubClient.h>
2.Set the Wi-Fi name and password, as well as the MQTT server connection address and port, and set the topic to "esp32/test".
// WiFi
const char *ssid = "mousse"; // Enter your WiFi name
const char *password = "qweqweqwe"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic = "esp32/test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
3.Open a serial connection to output the results of the program and connect to the Wi-Fi network.
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
4.Use PubSubClient to connect to the public MQTT broker.
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
5.After the MQTT server is successfully connected, ESP32 will publish messages to the MQTT server of esp/test
and subscribe to the topic messages of esp/test
.
// publish and subscribe
client.publish(topic, "Hi EMQX I'm ESP32 ^^");
client.subscribe(topic);
6.Set the callback function to print the topic name to the serial port and print the message received from the esp32/test
topic.
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
Full code
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi
const char *ssid = "mousse"; // Enter your WiFi name
const char *password = "qweqweqwe"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic = "esp32/test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.publish(topic, "Hi EMQX I'm ESP32 ^^");
client.subscribe(topic);
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void loop() {
client.loop();
}
Running and testing
1.Use Arduino to upload the complete code and power on the esp32.
2.Open the serial monitor, select 115200 baud rate, and check the ESP32 connection status.
3.Use the MQTT X client to connect to the public MQTT server and publish messages to ESP32.
Summary
So far, we have successfully connected ESP32 to the MQTT server. This is just one of ESP32's basic capabilities. ESP32 can actually connect to various IoT sensors and report sensor data to the MQTT server.
Originally published at https://www.emqx.com.
Top comments (0)