// Create an MQTT Connection
// TCP Connection
// import the WiFi and PubSubClient libraries
//The WiFi library allows ESP32 to establish connections with Wi-Fi networks
//The PubSubClient library enables ESP32 to connect to an MQTT broker for publishing messages and subscribing to topics.
#include <WiFi.h>
#include "PubSubClient.h"
// WiFi
char ssid[]="Wokwi-GUEST";
char pass[]="";
// MQTT Broker
const char *mqtt_broker = "broker.hivemq.com";//Public Broker
const char *mqtt_username = "taha.toplu";
const char *mqtt_password = "tahayunus123";
const int mqtt_port = 1883;
// App Protcol ---> Port
const char *topic_publish = "emqx/esp32/S";//Topic ESP32 Pusblish
const char *topic_subsrcibe = "emqx/esp32/S";//Topic ESp32 Subs
WiFiClient espClient;//WIFI Opject
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length);
#define LED 2
const int redPin = 14;
const int yellowPin = 12;
const int greenPin = 13;
bool ledState=false;
void setup(){
// Setting LED pin as output
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
digitalWrite(yellowPin, LOW); // Turn off the LED initially
digitalWrite(greenPin, LOW); // Turn off the LED initially
digitalWrite(redPin, LOW); // Turn off the LED initially
//Open a serial connection to display program results and establish a connection to the Wi-Fi network.
// Set software serial baud to 115200;
Serial.begin(115200);
// Connecting to a Wi-Fi network
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the Wi-Fi network");
// Utilize PubSubClient to establish a connection with the MQTT broker.
//connecting to a mqtt broker
Serial.println("Before Connect MQTT");
//Broker Configuration
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
//ESP32-ID -----> esp32-client-macaddress
//variable.c_str()--->char [] , String
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public MQTT broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
Serial.println("After Connect MQTT");
client.publish(topic_publish, "Hi, I'm ESP32 ^^");
client.subscribe(topic_subsrcibe);
}
void loop() {
client.loop();
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
String message;
if(String(topic)=="emqx/esp32/S"){
for (int i = 0; i < length; i++) {
message += (char) payload[i]; // Convert *byte to string
}
Serial.print(message);
if (message == "on" && !ledState) {
digitalWrite(LED, HIGH); // Turn on the LED
ledState = true;
}
if (message == "off" && ledState) {
digitalWrite(LED, LOW); // Turn off the LED
ledState = false;
}
}
Serial.println();
Serial.println("-----------------------");
}