// 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"
#include <LiquidCrystal.h>
// Parameters: rs, enable, d4, d5, d6, d7
LiquidCrystal lcd(16, 17, 5, 18, 19, 21);
// WiFi
char ssid[]="Wokwi-GUEST";
char pass[]="";
// MQTT Broker
const char *mqtt_broker = "broker.hivemq.com";//"broker.emqx.io";//Public Broker
const char *mqtt_username = "emqmk";
const char *mqtt_password = "public";
const int mqtt_port = 8883;
// App Protcol ---> Port
//const char *topic_publish = "TOPIC TO SEND : esp/send";//Topic ESP32 Pusblish
const char *topic_subsrcibe = "TOPIC TO RETRIEVE: esp/send";//Topic ESp32 Subs
const char *topic_subsrcibe2 = "emqx/esp32/s2";//Topic ESp32 Subs "Not Usable"
WiFiClient hamada;//WIFI Opject
PubSubClient client(hamada);//OOP
//int y ;
//void function(int x);
//call_function(y);
void callback(char *topic, byte *payload, unsigned int length);
#define LED 2
bool ledState=false;
void setup(){
lcd.begin(16, 2);
lcd.clear();
// 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()) {
String client_id = "esp32-client-";
client_id =client_id+ String(WiFi.macAddress());
//consloe.log("x"+"y");//xy
//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());//1 , 2 , 5
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);
}
void loop() {
lcd.setCursor(1, 0);
lcd.print("IOT Training");
client.loop();
}
void callback(char* topic, byte* message, unsigned int length) {
printPayload(topic, message, length);
}
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];
}
if(String(topic) == "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);
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1