// 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.
/* we were using Mqttx website whose url is
* https://mqttx.app/web */
#include <WiFi.h>
#include "PubSubClient.h"
// WiFi
char ssid[]="Wokwi-GUEST";
char pass[]="";
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";//Public Broker
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883; /* this is the port if this number ever changed
* it would seem that we used websocket */
// App Protcol ---> Port
const char *topic_publish = "emqx/esp32/p";//Topic ESP32 Pusblish
const char *topic_subsrcibe = "emqx/esp32/s";//Topic ESp32 Subs
const char *topic_subsrcibe2 = "emqx/esp32/s2";//Topic ESp32 Subs
WiFiClient espClient;//WIFI Opject
PubSubClient client(espClient);
void callback(char *topic, byte *message, unsigned int length);/*used when subscribing*/
#define LED 2
bool ledState=false;
void setup(){
// Setting LED pin as output
pinMode(LED, OUTPUT);
digitalWrite(LED, 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);
while (!client.connected()) { /* it will not enter the if condition unless
* the client is not connected to the broker
* (ESP32 is not connected to the broker) */
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress()); /* any device which is connected
* to the broker must have a unique id */
//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);
client.subscribe(topic_subsrcibe2);
client.setCallback(callback); /*used when subscribing*/
}
void loop() {
client.loop();
}
void callback(char* topic, byte* message, unsigned int length) {
printPayload(topic, message, length); /*used when subscribing*/
}
void printPayload(char* topic, byte* message, unsigned int length) {
// Printing Received Message
Serial.print("Message received on topic: ");
Serial.println(topic);
String messageTemp;
for(int i=0; i<length; i+=1) {
messageTemp += (char)message[i]; /* convert byte to string */
}
if(String(topic)/*this is typecasting in c++*/== "emqx/esp32/s")
{
Serial.println("OK");
if(messageTemp=="ON")
{
//Serial.println("Led ON");
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}
}
else if(String(topic) == "emqx/esp32/s2")
{
Serial.print("OK2");
}
else
{
Serial.print("Any Thing");
}
Serial.println(messageTemp); /* here we print the sent message from the mqttx for
* example (if the mqttx was the publisher and esp32 is the subscriber) */
}