MQTT (Message Queuing Telemetry Transport) Protocol is a M2M (Machine to Machine) lightweight messaging protocol widely used in IOT (Internet of Things) that provides a resource constrained network clients. Almost IOT cloud platform support MQTT protocol to send and receive data from smart objects in other words it is a simple way to distribute telemetry information which uses a publish & subscribe communication pattern together with your favorite microcontrollers & SBC. for further reading please refer to this link.
The main objective to this demonstrations is how we going to publish our topic to MQTT using ESP32 and Arduino IDE below are the required applications we need.
Required Applications
- Create a free account to https://www.cloudmqtt.com/ CloudMQTT
- CloudMQTT Documentations
- Arduino Client for MQTT https://github.com/knolleary/pubsubclient
- Arduino core for ESP32 WiFi chip https://github.com/espressif/arduino-esp32
- MQTTLens > Google Chrome application, which connects to a MQTT broker and is able to subscribe and publish to MQTT topics.
When you finish the signup procedure go to instance information page. Keep note to the credentials you will going to use it on your ESP32 code for SERVER, USER, PASSWORD, PORT
This is an easy way to integrate MQTT library via Arduino IDE Library Manager. This library comes with a number of example sketches. See File > Examples > PubSubClient within the Arduino application. Full API documentation is available here: http://pubsubclient.knolleary.net
The code below needed to connect into MQTT broker to push a message for a topic. However still you need to include some required libraries such as <Wifi.h> library in demand to be connected to MQTT broker as global variable.
1 2 3 4 5 6 7 8 9 |
#include <WiFi.h> //Required Library #include <PubSubClient.h> //Required Library const char* ssid = "YOUR-NETWORK-NAME"; const char* password = "YOUR-NETWORK-PASSWORD"; const char* mqttServer = "YOUR-SERVER-NAME.cloudmqtt.com"; const int mqttPort = 12948; //Your port number const char* mqttUser = "YOUR-CLOUD-MQTT-USERNAME"; const char* mqttPassword = "YOUR-CLOUD-MQTT-PASSWORD"; |
You can now declare object of class WiFiClient which is required to establish a connection to the defined IP address and Port Number. Next is you required to declare an object of class PubSubClent and pass as input of the constructor for the previously defined WiFiClient
1 2 |
WiFiClient ESPClient; //Define as variable espClient PubSubClient client(ESPClient); //Set ESPClient as client |
At the function area, you need to open the serial communication, this is an easy way to see the output status from the ESP8266 / ESP32 connected and publish topic to the MQTT broker.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Serial.begin(115200); Serial.println("14CORE | MQTT PUSH TOPIC TEST CODE"); delay(1000); Serial.println("Initializing......................"); Delay(4000); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Please wait while connecting to WiFi"); } Serial.println("ESP > Connected to WiFi"); |
Setting the address and the communication port number of the MQTT server, which is previously set to the global variable. Then call the connect method passing as input parameter for the client along with the username and password.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
client.setServer(mqttServer, mqttPort); while (!client.connected()) { Serial.println("ESP > Connecting to MQTT..."); if (client.connect("ESP32Client", mqttUser, mqttPassword )) { Serial.println("connected to CloudMQTT"); } else { Serial.print("ERROR > failed with state "); Serial.print(client.state()); delay(2000); } } |
Now we successfully sending a message topic to the CloudMQTT, as “client.publish(“esp/test”, “Message from ESP8266/ESP32”);” Below are the full source code, you can just call the loop method of the “PubSubClient” this method should be called regularly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <WiFi.h> //Required Library #include <PubSubClient.h> //Required Library const char* ssid = "YOUR-NETWORK-NAME"; const char* password = "YOUR-NETWORK-PASSWORD"; const char* mqttServer = "YOUR-SERVER-NAME.cloudmqtt.com"; const int mqttPort = 12948; //Your port number const char* mqttUser = "YOUR-CLOUD-MQTT-USERNAME"; const char* mqttPassword = "YOUR-CLOUD-MQTT-PASSWORD"; WiFiClient ESPClient; PubSubClient client(ESPClient); void setup() { Serial.begin(115200); Serial.println("14CORE | MQTT PUSH TOPIC TEST CODE"); delay(1000); Serial.println("Initializing......................"); Delay(4000); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Please wait while connecting to WiFi"); } Serial.println("ESP > Connected to WiFi"); client.setServer(mqttServer, mqttPort); while (!client.connected()) { Serial.println("ESP > Connecting to MQTT..."); if (client.connect("ESP32Client", mqttUser, mqttPassword )) { Serial.println("connected to CloudMQTT"); } else { Serial.print("ERROR > failed with state "); Serial.print(client.state()); delay(2000); } } client.publish("esp/test", "Message from ESP8266/ESP32"); } void loop() { client.loop(); } |
For testing you will need MQTTLens which is available in Chrome Application which is allow you to see a receive topics which is publish to MQTT. client.publish(“esp/test”, “Message from ESP8266/ESP32”); you can open your Serial Communication Monitor to view similar output on MQTTLens.
Downloads
- Download ESP-WROVER Datasheet | PDF
- Download ESP32 AT Command Set Manual | PDF
- Download Espressif Flash Download Tool | Zip
- Further reading please refer to this link
- Download https://git-scm.com/download/win
- Download ESP 32S Bluetooth User Guide | PDF
- Download ESP 32S Datasheet | PDF
- Further Learning Using PlatformIO | Using as ESP-IDF component | Link
- Download Firmware for ESP8266 here
- ESP8266 and ESP32 serial bootloader utility