If you want to use Cloud MQTT broker with your ESP32, instead of running your local Mosquitto MQTT broker on a Raspberry Pi, follow these next instructions to modify the example sketch provided in the ESP32 course:
1. Open the details of your MQTT instance: https://customer.cloudmqtt.com/instance
Copy your server, user, password, port number:
2. Type your MQTT_HOST and MQTT_PORT, it should look like this:
#define MQTT_HOST "m12.cloudmqtt.com" #define MQTT_PORT 134---
3. After the .setServer() (in the setup() function), you also need to call the .setCredentials() method with your own username and password:
mqttClient.setServer(MQTT_HOST, MQTT_PORT); mqttClient.setCredentials("zkk-----", "BtH------");
That’s it! Your ESP32 should be able to connect to the Cloud MQTT broker.