#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <DHT.h>
#define DHTPIN 4 // Chân dữ liệu DHT22
#define DHTTYPE DHT22 // Kiểu cảm biến DHT22
// Thông số OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Không cần reset, nếu có reset thì gán chân cụ thể
DHT dht(DHTPIN, DHTTYPE); // Tạo đối tượng DHT
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
dht.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Không thể khởi động màn hình OLED"));
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("OLED Ready!");
display.display();
delay(2000);
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Không thể đọc dữ liệu từ cảm biến DHT22!"));
return;
}
Serial.print(F("Độ ẩm: "));
Serial.print(humidity);
Serial.print(F("% Nhiệt độ: "));
Serial.print(temperature);
Serial.println(F("°C"));
// Hiển thị dữ liệu lên màn hình OLED
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.print("T: ");
display.print(temperature);
display.println("oC");
display.setCursor(0, 30);
display.print("H: ");
display.print(humidity);
display.println("%");
display.display();
delay(2000);
}