ESP32-Based Smart Environmental Monitoring System with Wi-Fi Connectivity
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to monitor environmental conditions using various sensors and control a traffic light and a fan based on the sensor readings. The ESP32 microcontroller is the central unit that reads data from the sensors, processes it, and controls the traffic light and fan. The data is also sent to a Firebase database for remote monitoring.
Component List
ESP32
- Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, VIN, 3V3, D15, D2, D4, RX2, TX2, D5, D18, D19, D21, RX0, TX0, D22, D23, BOOT
Moisture Sensor
- Description: Measures soil moisture levels.
- Pins: 5v+, GND, A0
BMP180
- Description: A barometric pressure sensor.
- Pins: 3.3, SDA, SCL, GND, VCC
Traffic Light
- Description: A simple traffic light with red, yellow, and green LEDs.
- Pins: Green, Yellow, Red, GND
120 Fan 12V
- Description: A 12V fan for cooling purposes.
- Pins: 12V+, GND
BH1750
- Description: A light intensity sensor.
- Pins: VCC, GND, SCL, SDA, ADDR
Power Supply 24V
- Description: Provides 24V DC power from an AC source.
- Pins: 220V Positive Pole (AC), 220V Negative Pole (AC), GND, GND (DC), 12V-24V Output (DC)
DHT22
- Description: Measures temperature and humidity.
- Pins: +, Out, -
2 Channel Relay Module
- Description: Controls high voltage devices using low voltage signals.
- Pins: N.O. 2, COM 2, N.C. 2, N.O. 1, COM 1, N.C. 1, pin 7, pin 8, pin 9, pin 10, pin 11, pin 12, VCC+, VCC- (GND), IN 1, IN 2
Wiring Details
ESP32
- 3V3: Connected to VCC of BH1750, 3.3 of BMP180, + of DHT22, VCC+ of 2 Channel Relay Module, and 5v+ of Moisture Sensor.
- GND: Connected to GND of BH1750, GND of BMP180, - of DHT22, GND of Traffic Light, GND of 120 Fan 12V, VCC- (GND) of 2 Channel Relay Module, and GND of Moisture Sensor.
- D34: Connected to A0 of Moisture Sensor.
- D32: Connected to Red of Traffic Light.
- D33: Connected to Yellow of Traffic Light.
- D14: Connected to Green of Traffic Light.
- D4: Connected to Out of DHT22.
- D5: Connected to IN 1 of 2 Channel Relay Module.
- D18: Connected to IN 2 of 2 Channel Relay Module.
- D21: Connected to SDA of BH1750 and BMP180.
- D22: Connected to SCL of BH1750 and BMP180.
Moisture Sensor
- 5v+: Connected to 3V3 of ESP32.
- GND: Connected to GND of ESP32.
- A0: Connected to D34 of ESP32.
BMP180
- 3.3: Connected to 3V3 of ESP32.
- SDA: Connected to D21 of ESP32.
- SCL: Connected to D22 of ESP32.
- GND: Connected to GND of ESP32.
Traffic Light
- Green: Connected to D14 of ESP32.
- Yellow: Connected to D33 of ESP32.
- Red: Connected to D32 of ESP32.
- GND: Connected to GND of ESP32.
120 Fan 12V
- 12V+: Connected to 12V-24V Output (DC) of Power Supply 24V.
- GND: Connected to GND of ESP32.
BH1750
- VCC: Connected to 3V3 of ESP32.
- GND: Connected to GND of ESP32.
- SCL: Connected to D22 of ESP32.
- SDA: Connected to D21 of ESP32.
Power Supply 24V
- 12V-24V Output (DC): Connected to 12V+ of 120 Fan 12V and COM 2, N.O. 1, COM 1 of 2 Channel Relay Module.
DHT22
- +: Connected to 3V3 of ESP32.
- Out: Connected to D4 of ESP32.
- -: Connected to GND of ESP32.
2 Channel Relay Module
- VCC+: Connected to 3V3 of ESP32.
- VCC- (GND): Connected to GND of ESP32.
- IN 1: Connected to D5 of ESP32.
- IN 2: Connected to D18 of ESP32.
Code Documentation
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <DHT11.h>
#include <Wire.h> // For I2C communication
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h> // BMP180 library
// Wi-Fi credentials
const char* ssid = "OPPO Reno5";
const char* password = "zyad@2020";
// Firebase project configuration
#define FIREBASE_PROJECT_ID "brick-system-bf594"
#define FIREBASE_API_KEY "AIzaSyC2Rq9AKZ6GE878ru0fzwwwzok_LyGiww0"
// DHT11 settings
#define DHTPIN 4 // GPIO pin connected to the DHT11
DHT11 dht(DHTPIN); // Create DHT11 object
// Soil moisture sensor settings
#define SOIL_MOISTURE_PIN 35 // Analog pin connected to soil moisture sensor
// BMP180 settings
Adafruit_BMP085_Unified bmp;
// Firebase configurations
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// Global variables
String sessionDocumentId;
int sensorReadingId = 1; // Incremental ID for each sensor reading
// Traffic light settings
const int buzzerPin = 23;
const int tempRedLedPin = 25;
const int tempYellowLedPin = 26;
const int tempGreenLedPin = 27;
const int moistureRedLedPin = 32;
const int moistureYellowLedPin = 33;
const int moistureGreenLedPin = 14;
// Function to synchronize the ESP32's time with NTP
void setupTime() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for time sync...");
while (!time(nullptr)) {
delay(1000);
Serial.print(".");
}
Serial.println("\nTime synchronized");
}
// Function to get the current ISO 8601 timestamp in UTC
String getIso8601Timestamp() {
time_t now = time(nullptr);
struct tm* timeinfo = gmtime(&now); // Use gmtime for UTC
char buffer[25];
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", timeinfo); // UTC timestamp ends with 'Z'
return String(buffer);
}
// Function to create a new session document in Firestore
void createNewSession() {
String documentPath = "sessions/session_" + String(millis()); // Unique session ID
sessionDocumentId = documentPath;
FirebaseJson sessionData;
sessionData.set("fields/startTime/timestampValue", getIso8601Timestamp());
if (Firebase.Firestore.createDocument(&fbdo, FIREBASE_PROJECT_ID, "", documentPath.c_str(), sessionData.raw())) {
Serial.println("New session created: " + documentPath);
} else {
Serial.print("Failed to create session: ");
Serial.println(fbdo.errorReason());
}
}
// Function to store sensor data in Firestore
void storeSensorData(const String & sensorName, FirebaseJson& data) {
String documentPath = sessionDocumentId + "/" + sensorName + "/reading_" + String(sensorReadingId++);
if (Firebase.Firestore.createDocument(&fbdo, FIREBASE_PROJECT_ID, "", documentPath.c_str(), data.raw())) {
Serial.println(sensorName + " data stored successfully at " + documentPath);
} else {
Serial.print("Failed