//ass 3 Q5 (Pubsub)
// #include <WiFi.h>
// #include <PubSubClient.h>
// #include <Wire.h>
// #include <LiquidCrystal_I2C.h>
// #include <WiFiClientSecure.h>
// #include <Crypto.h>
// #include <AES.h>
// #include <DHT.h>
// #include <Adafruit_Sensor.h>
// #include <FS.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_ILI9341.h>
// // WiFi and MQTT Server Configuration
// const char* ssid = "Wokwi-GUEST";
// const char* password = "";
// const char* mqttServer = "ba2f2af4644d4fb990991fedeca6cf08.s1.eu.hivemq.cloud";
// const int mqttPort = 8883; // SSL/TLS port
// const char* mqttUser = "Adham";
// const char* mqttPassword = "Adham12345";
// // Initialize secure WiFi client
// WiFiClientSecure espClient;
// PubSubClient client(espClient);
// // Initialize the LCD with the I2C address
// LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the address (0x27) and size as needed
// // DHT Sensor Configuration
// #define DHTPIN 32 // GPIO pin where the DHT sensor is connected
// #define DHTTYPE DHT11 // DHT 11 or DHT22
// DHT dht(DHTPIN, DHTTYPE);
// // Function declarations
// void reconnect();
// void displayData(float temperature, float humidity);
// void setup() {
// Serial.begin(115200);
// // Initialize WiFi
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
// Serial.print(".");
// }
// Serial.println("\nConnected to WiFi");
// // Set the secure connection settings
// espClient.setInsecure(); // Use this only if you don't have a root certificate. It bypasses SSL verification.
// // Initialize MQTT server
// client.setServer(mqttServer, mqttPort);
// // Initialize LCD
// lcd.init();
// lcd.backlight();
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Connecting...");
// // Initialize DHT sensor
// dht.begin();
// Serial.println("DHT sensor initialized.");
// // Connect to MQTT broker
// reconnect();
// }
// void loop() {
// if (!client.connected()) {
// reconnect();
// }
// client.loop();
// // Read data from the DHT sensor
// float temperature = dht.readTemperature();
// float humidity = dht.readHumidity();
// // Check if any reads failed and exit early (to try again).
// if (isnan(temperature) || isnan(humidity)) {
// Serial.println("Failed to read from DHT sensor!");
// delay(2000); // Increase the delay before the next reading
// return;
// }
// // Debug output of sensor readings
// Serial.print("Temperature: ");
// Serial.print(temperature);
// Serial.print(" °C, Humidity: ");
// Serial.print(humidity);
// Serial.println(" %");
// // Format data as JSON
// String payload = "{\"temperature\":" + String(temperature) + ",\"humidity\":" + String(humidity) + "}";
// // Publish data to MQTT
// client.publish("sensor/data", payload.c_str());
// // Display data on the LCD
// displayData(temperature, humidity);
// delay(60000); // Publish and update every minute
// }
// void reconnect() {
// // Loop until we're reconnected
// while (!client.connected()) {
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Connecting to");
// lcd.setCursor(0, 1);
// lcd.print("MQTT...");
// if (client.connect("ESP32Client", mqttUser, mqttPassword)) {
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Connected!");
// delay(2000);
// } else {
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Failed, rc=");
// lcd.setCursor(0, 1);
// lcd.print(client.state());
// delay(5000);
// }
// }
// }
// void displayData(float temperature, float humidity) {
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Temp: ");
// lcd.print(temperature);
// lcd.print(" C");
// lcd.setCursor(0, 1);
// lcd.print("Hum: ");
// lcd.print(humidity);
// lcd.print(" %");
// }