#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <PubSubClient.h>
#include <WiFi.h>
// Pin Definitions
#define DHT1_PIN 26
#define DHT2_PIN 16
#define PIR_PIN 15
#define POT1_PIN 27
#define POT2_PIN 21
#define LDR_PIN 18
// Initialization of DHT sensors
DHT dhtSensor1(DHT1_PIN, DHT11); // DHT1 Sensor
DHT dhtSensor2(DHT2_PIN, DHT11); // DHT2 Sensor
// Variables for sensor readings
String t = "0.00"; // Temperature DHT1
String h = "0.0"; // Humidity DHT1
int gasLevel = 0; // Gas consumption level
int waterConsumptionLevel = 0; // Water consumption level
int ldrValue = 0; // LDR sensor value
int pirValue = 0; // PIR sensor value
// Wi-Fi and MQTT configuration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
// Connect to Wi-Fi
void setup_wifi() {
delay(10);
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected to Wi-Fi");
}
// Publish data to MQTT
void publishMessage() {
String payload = "{\"GasConsumption\":\"" + String(gasLevel) + "\", \"WaterConsumption\":\"" + String(waterConsumptionLevel) + "\", \"LDR\":\"" + String(ldrValue) + "\", \"PIR\":\"" + String(pirValue) + "\"}";
client.publish("sensordata", payload.c_str());
Serial.println("Message published: " + payload);
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqttServer, mqttPort);
// Initialize DHT sensors
dhtSensor1.begin();
dhtSensor2.begin();
// Initialize other sensors
pinMode(PIR_PIN, INPUT);
pinMode(LDR_PIN, INPUT);
}
void loop() {
// Reconnect to MQTT server if disconnected
if (!client.connected()) {
while (!client.connected()) {
Serial.print("Connecting to MQTT server...");
if (client.connect("ESP32Client")) {
Serial.println("Connected");
} else {
Serial.print("Connection failed. Error code: ");
Serial.print(client.state());
delay(2000);
}
}
}
// Read temperature and humidity from DHT1
float t1 = dhtSensor1.readTemperature();
float h1 = dhtSensor2.readHumidity();
// Validate readings
if (isnan(t1) || isnan(h1)) {
Serial.println("Failed to read from DHT1 sensor. Using default values.");
t1 = 25.0; // Default temperature
h1 = 50.0; // Default humidity
}
Serial.println("DHT1 - Temp: " + String(t1));
Serial.println("DHT2 - Humidity: " + String(h1));
t = String(t1, 2); // Update temperature
h = String(h1, 1); // Update humidity
// Read gas consumption level from potentiometer
int potentiometerValue1 = analogRead(POT1_PIN);
gasLevel = map(potentiometerValue1, 0, 1023, 0, 100);
Serial.println("Gas Consumption: " + String(gasLevel) + "%");
// Read water consumption level from potentiometer
int potentiometerValue2 = analogRead(POT2_PIN);
waterConsumptionLevel = map(potentiometerValue2, 0, 1023, 0, 100);
Serial.println("Water Consumption: " + String(waterConsumptionLevel) + "%");
// Read LDR sensor value
ldrValue = analogRead(LDR_PIN);
Serial.println("LDR Value: " + String(ldrValue));
// Read PIR sensor value
pirValue = digitalRead(PIR_PIN);
Serial.println("PIR Sensor Value: " + String(pirValue));
// Publish sensor data via MQTT
publishMessage();
// Maintain MQTT connection
client.loop();
delay(5000); // Wait before the next loop iteration
}