ESP32-Based Environmental Monitoring System with Wi-Fi Connectivity
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a multi-sensor environmental monitoring system based on the ESP32 microcontroller. The system integrates various sensors to measure parameters such as temperature, humidity, air quality, pH, and TDS. The collected data is displayed on an OLED screen and sent to ThingSpeak for remote monitoring. Additionally, the system can send email alerts if any critical environmental parameters are detected.
Component List
TDS Sensor
- Description: Measures the Total Dissolved Solids (TDS) in water.
- Pins: P1, P2, +, -, A
RTD PT100
- Description: Measures temperature using a resistance temperature detector.
- Pins: 1, 2
MQ-7 Breakout
- Description: Measures carbon monoxide (CO) levels in the air.
- Pins: VCC, GND, DO, AO
PM2.5 Air Quality Sensor and Breadboard Adapter Kit - PMS5003
- Description: Measures particulate matter (PM2.5 and PM10) in the air.
- Pins: VCC, GND, SET, RX, TX, RST
DHT11 Humidity and Temperature Sensor
- Description: Measures humidity and temperature.
- Pins: VDD, DATA, NULL, GND
DS18B20 1-Wire Temperature Sensor Probe Cable
- Description: Measures temperature using a 1-Wire interface.
- Pins: shield, GND, DQ, VDD
0.96" OLED
- Description: Displays sensor data.
- Pins: GND, VDD, SCK, SDA
ESP32 (30 pin)
- Description: Microcontroller that integrates WiFi and Bluetooth capabilities.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
Resistor
- Description: Provides resistance in the circuit.
- Pins: pin1, pin2
- Properties: Resistance: 4700 Ohms
PH Meter
- Description: Measures the pH level of a solution.
- Pins: Signal, VCC, GND
Wiring Details
TDS Sensor
- P1: Connected to RTD PT100 pin 2
- P2: Connected to RTD PT100 pin 1
- +: Connected to ESP32 3V3
- -: Connected to GND
- A: Connected to ESP32 D35
RTD PT100
- 1: Connected to TDS Sensor P2
- 2: Connected to TDS Sensor P1
MQ-7 Breakout
- VCC: Connected to ESP32 3V3
- GND: Connected to GND
- AO: Connected to ESP32 D32
PM2.5 Air Quality Sensor and Breadboard Adapter Kit - PMS5003
- VCC: Connected to ESP32 3V3
- GND: Connected to GND
- TX: Connected to ESP32 TX2
- RX: Connected to ESP32 RX2
DHT11 Humidity and Temperature Sensor
- VDD: Connected to ESP32 3V3
- DATA: Connected to ESP32 D5
- GND: Connected to GND
DS18B20 1-Wire Temperature Sensor Probe Cable
- shield: Connected to GND
- GND: Connected to GND
- DQ: Connected to ESP32 D4 via Resistor pin1
- VDD: Connected to ESP32 3V3 via Resistor pin2
0.96" OLED
- GND: Connected to GND
- VDD: Connected to ESP32 3V3
- SCK: Connected to ESP32 D22
- SDA: Connected to ESP32 D21
ESP32 (30 pin)
- 3V3: Connected to VCC of various sensors
- GND: Connected to GND of various sensors
- D4: Connected to DS18B20 DQ via Resistor pin1
- D5: Connected to DHT11 DATA
- D21: Connected to OLED SDA
- D22: Connected to OLED SCK
- D32: Connected to MQ-7 AO
- D34: Connected to PH Meter Signal
- D35: Connected to TDS Sensor A
- TX2: Connected to PMS5003 TX
- RX2: Connected to PMS5003 RX
Resistor
- pin1: Connected to DS18B20 DQ and ESP32 D4
- pin2: Connected to DS18B20 VDD and ESP32 3V3
PH Meter
- Signal: Connected to ESP32 D34
- VCC: Connected to ESP32 3V3
- GND: Connected to GND
Documented Code
#include <WiFi.h>
#include <HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <SoftwareSerial.h>
#include <ESP32_MailClient.h> // Include the ESP32 Mail Client library
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
// WiFi Credentials
const char* ssid = "Hi";
const char* password = "Nitish$1";
// ThingSpeak Settings
const char* server = "https://api.thingspeak.com/update";
const char* apiKey = "SA5WC1ARSF8DR3X0";
// SMTP Credentials
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define EMAIL_SENDER "environmentalmonitoring25@gmail.com"
#define EMAIL_PASSWORD "avca vhui momg xecl"
#define EMAIL_RECIPIENT "nitishksn24@gmail.com"
// Pin Definitions
#define PH_SENSOR_PIN 34
#define TDS_SENSOR_PIN 35
#define DS18B20_PIN 4
#define DHT_PIN 5
#define DHT_TYPE DHT11
#define MQ7_SENSOR_PIN 32
#define PMS5003_RX_PIN 16
#define PMS5003_TX_PIN 17
#define NUM_SAMPLES 15
// Calibration Parameters
float pH_slope = -0.0123;
float pH_offset = 31.2000;
#define RL_VALUE 10.0
#define R0 10.0
// OLED Display Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT dht(DHT_PIN, DHT_TYPE);
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
SoftwareSerial pmsSerial(PMS5003_RX_PIN, PMS5003_TX_PIN);
SMTPData smtpData;
void sendEmailAlert(String subject, String message) {
smtpData.setLogin(SMTP_HOST, SMTP_PORT, EMAIL_SENDER, EMAIL_PASSWORD);
smtpData.setSender("Smart Env Monitor", EMAIL_SENDER);
smtpData.addRecipient(EMAIL_RECIPIENT);
smtpData.setSubject(subject);
smtpData.setMessage(message, false);
if (!MailClient.sendMail(smtpData)) {
Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
}
smtpData.empty(); // Clear all data after sending
}
void setup() {
Serial.begin(115200);
pmsSerial.begin(9600);
dht.begin();
sensors.begin();
analogReadResolution(12);
// Initialize OLED Display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("SSD1306 allocation failed");
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
// WiFi Setup
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
}
float calculateCOppm(float voltage) {
float Rs = (3.3 - voltage) * RL_VALUE / voltage;
float ratio = Rs / R0;
if (ratio > 1) {
return pow(10, (log10(ratio) - 0.08) / -0.67);
} else {
return pow(10, (log10(ratio) - 0.38) / -0.