#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHT_pin 4
#define DHT_type DHT22
DHT dht(DHT_pin,DHT_type);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
void setup(){
Serial.begin(115200);
dht.begin();
if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) //Nếu khởi tạo thất bại, in thông báo lỗi và dừng chương trình bằng while(1).
{Serial.println("SSD1306 allocation failed");
while(1);
}
oled.clearDisplay(); //Xóa nội dung màn hình OLED.
oled.setTextSize(1.8); //Cài đặt kích thước chữ (mặc định là 1).
oled.setTextColor(WHITE); //Cài đặt màu chữ (WHITE là màu trắng).
oled.setCursor(30,30); //Đặt vị trí con trỏ tại tọa độ (x,y) (pixel).
oled.println("OLED READY!"); //Hiển thị chuỗi ký tự lên màn hình OLED.
oled.display(); //Cập nhật màn hình OLED để hiển thị nội dung vừa vẽ.
delay(2000);
}
void loop()
{
float humid= dht.readHumidity();
float temp= dht.readTemperature();
Serial.print("Nhiệt độ: ");
Serial.print(temp);
Serial.println(" °C");
Serial.print("Độ ẩm: ");
Serial.print(humid);
Serial.println(" %");
Serial.print("\n");
oled.clearDisplay(); //Xóa nội dung màn hình OLED.
oled.setTextSize(1.9); //Cài đặt kích thước chữ (mặc định là 1).
oled.setTextColor(WHITE); //Cài đặt màu chữ (WHITE là màu trắng).
oled.setCursor(20,20); //Đặt vị trí con trỏ tại tọa độ (x,y) (pixel).
oled.print("Temp: "); //Hiển thị chuỗi ký tự lên màn hình OLED.
oled.print(temp); //Hiển thị nhiệt độ thu được từ cảm biến lên OLED
oled.println("*C");
oled.setCursor(20,40);
oled.print("Humid: ");
oled.print(humid);
oled.println("%");
oled.display();
delay(1000);
}