ESP32 MQTT client
So far the device is distributing the data within the local network. To make the information available on the ‘Internet‘, MQTT was chosen as the transportation protocol. I have a VPS rented at Contabo, which I can recommend, it is fast, reliable and cheap. I use this VPS for several purposes. It hosts this website, it runs an MySQL server that is used to store data collected from my solar panel and it runs as well a MQTT server. For the MQTT server I chose EMQ “The Massively Scalable MQTT Broker for IoT and Mobile Applications”. It is an alternative to the maybe better known Mosquitto. For me EMQ was the better solution as it has already websockets integrated and offers a dashboard to display status and messages.
For the MQTT client on the ESP32 I chose the library MQTT by Joel Gaehwiler that I already used successful in ESP8266 projects.
To use the library the header file must be included, the class be initiated and the access credentials defined.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include "MQTTClient.h" MQTTClient mqttClient; /** MQTT broker URL */ static const char * mqttBroker = "YOUR_SERVER_IP"; /** MQTT connection id */ static const char * mqttID = "YOUR_CLIENT_ID"; /** MQTT user name */ static const char * mqttUser = "YOUR_USER_NAME"; /** MQTT password */ static const char * mqttPwd = "YOUR_PASSWORD"; /** WiFi client class to receive messages from mqtt broker */ WiFiClient mqttReceiver; |
Initiate a connection to the MQTT server
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 |
/** * Connect to MQTT broker * * @return <code>bool</code> * true if connection was successful * false if connection failed **/ bool connectMQTT() { Serial.println("Connecting to MQTT broker"); // Connect to MQTT broker mqttClient.begin(mqttBroker, mqttReceiver); // Setup callback function for messages from broker mqttClient.onMessage(messageReceived); int connectTimeout = 0; while (!mqttClient.connect(mqttID, mqttUser, mqttPwd)) { delay(100); connectTimeout++; if (connectTimeout > 10) { // Wait for 1 seconds to connect Serial.println("Can't connect to MQTT broker"); return false; } } Serial.println("Connected to MQTT"); // Set MQTT last will mqttClient.setWill("/DEV/ESP32", "Dead"); return true; } |
The callback function for messages received from the MQTT server is not used, as the application does not subscribe to any messages. I added it only for testing purpose.
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * messageReceived * Called when subscribed message was received from MQTT broker * * @param topic * topic of received message * @param payload * payload of message */ void messageReceived(String &topic, String &payload) { Serial.println("MQTT - received from topic: " + topic + " payload: " + payload); } |
To send a message to the server the publish() function is used. The first parameter is the topic, the second the payload, the third the retained flag and the forth the QOS level
1 |
mqttClient.publish("/wei","test message",true,0); |
That is quite easy to setup. To send/receive messages the class has to be called in the loop(). There I check as well if I got disconnected from the server and try to reconnect
1 2 3 4 5 6 7 8 9 10 11 |
void loop() { //... some code /** Handle MQTT subscribtions */ mqttClient.loop(); if(!mqttClient.connected()) { // Try to reconnect to MQTT connectMQTT(); } //... some code } |






