SunFounder ESP32 Starter Kit
(continued from previous page)
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// If a message is received on the topic "SF/LED", you check if the␣
˓→message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "SF/LED") {
Serial.print("Changing state to ");
if (messageTemp == "on") {
Serial.println("on");
digitalWrite(ledPin, HIGH);
} else if (messageTemp == "off") {
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}
3. After selecting the correct board (ESP32 Dev Module) and port, click the Upload button.
4. Open the serial monitor and if the following information is printed, it indicates a successful connection to the
MQTT server.
WiFi connected
IP address:
192.168.18.77
Attempting MQTT connection...connected
Message Publication via HiveMQ
HiveMQ is a messaging platform that functions as an MQTT broker, facilitating fast, efficient, and reliable data transfer
to IoT devices.
Our code specifically utilizes the MQTT broker provided by HiveMQ. We have included the address of the HiveMQ
MQTT broker in the code as follows:
// Add your MQTT Broker address, example:
const char* mqtt_server = "broker.hivemq.com";
1. At present, open the in your web browser.
2. Connect the client to the default public proxy.
1.47. 8.4 IoT Communication with MQTT 183