#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h> // Thêm thư viện DHT
// Định nghĩa chân kết nối
const int LDR_PIN = 34; // Chân analog kết nối với cảm biến
// Định nghĩa kích thước màn hình OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define pinLed 21 // Pin cho LED
#define DHTPIN 4 // Pin ESP32 kết nối với chân Data của DHT22
#define DHTTYPE DHT22 // Loại cảm biến DHT
DHT dht(DHTPIN, DHTTYPE); // Khởi tạo cảm biến DHT
// Thay đổi giá trị này tùy theo cảm biến của bạn
const float REF_VOLTAGE = 5; // Điện áp tham chiếu của ESP32
const int ADC_MAX_VALUE = 4095; // Giá trị tối đa của ADC
const float LUX_THRESHOLD = 1.0; // Ngưỡng lux để phân biệt sáng/tối
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
pinMode(LDR_PIN, INPUT);
pinMode(pinLed, OUTPUT); // Thiết lập chân LED là OUTPUT
Serial.begin(115200); // Khởi động kết nối serial
// Khởi tạo cảm biến DHT
dht.begin();
// Khởi tạo màn hình OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Khong the khoi tao OLED"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1); // Kích thước chữ
display.setTextColor(SSD1306_WHITE); // Màu chữ
display.setCursor(0, 0); // Vị trí bắt đầu hiển thị
display.print(F("Khoi tao thanh cong"));
display.display();
delay(2000); // Hiển thị thông báo khởi tạo trong 2 giây
}
void loop() {
// Đọc giá trị từ LDR_PIN
int analogValue = analogRead(LDR_PIN);
// Tính điện áp dựa trên giá trị ADC và điện áp tham chiếu 3.3V
float voltage = analogValue / 4095.0 * REF_VOLTAGE;
// Tính điện trở của LDR
float resistance = 2000 * voltage / (REF_VOLTAGE - voltage);
// Tính giá trị ánh sáng lux
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)) / 10;
// Đọc dữ liệu từ cảm biến DHT
delay(2000); // Đợi cảm biến ổn định
float humid = dht.readHumidity();
float temp = dht.readTemperature();
// Kiểm tra lỗi đọc cảm biến
if (isnan(humid) || isnan(temp)) {
Serial.println(F("Khong doc duoc du lieu tu DHT"));
return;
}
// In ra Serial Monitor để kiểm tra
Serial.print("Giá trị ADC: ");
Serial.print(analogValue);
Serial.print(" | Điện áp: ");
Serial.print(voltage, 2);
Serial.print(" V | Lux: ");
Serial.print(lux, 2);
Serial.println();
// Hiển thị dữ liệu lên màn hình OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("LDR Voltage: ");
display.print(voltage, 2); // Hiển thị điện áp với 2 chữ số thập phân
display.print(" V");
display.setCursor(0, 10);
display.print("Lux: ");
display.print(lux, 2); // Hiển thị lux với 2 chữ số thập phân
display.setCursor(0, 20);
if (lux > LUX_THRESHOLD) {
display.print("Status: Sang");
} else {
display.print("Status: Toi");
}
// Hiển thị dữ liệu cảm biến DHT
display.setCursor(0, 30);
display.print(F("Do am: "));
display.print(humid, 2);
display.print(F("%"));
display.setCursor(0, 40);
display.print(F("Nhiet do: "));
display.print(temp, 2);
display.print(F(" C"));
display.display(); // Cập nhật màn hình
// In dữ liệu DHT ra Serial Monitor
Serial.print(F("Độ ẩm: "));
Serial.print(humid);
Serial.print(F("% | Nhiệt độ: "));
Serial.print(temp);
Serial.println(F("°C"));
delay(1000); // Đọc dữ liệu mỗi giây
}