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.

Initiate a connection to the MQTT server

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.

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

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



Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory