#include <DHTesp.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define pinDHT 27
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define redLedPin 2
#define yellowLedPin 4
DHTesp dhtSensor;
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
dhtSensor.setup(pinDHT, DHTesp::DHT22);
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
// Inisialisasi OLED
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (true);
}
delay(5000);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
}
void loop() {
delay(5000);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temp = data.temperature;
float hum = data.humidity;
if (isnan(hum) || isnan(temp)) {
Serial.println(F("DHT sensor reading failed!"));
return;
}
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Nama: Rayhan Farras R.");
oled.setCursor(0, 10);
oled.print("NIM: 22515070711007");
oled.setCursor(0, 25);
oled.print("Temperature: ");
oled.print(temp);
oled.print(" C");
oled.setCursor(0, 35);
oled.print("Humidity: ");
oled.print(hum);
oled.print(" %");
if (temp < 0) {
digitalWrite(redLedPin, HIGH);
} else {
digitalWrite(redLedPin, LOW);
}
if (hum > 60) {
digitalWrite(yellowLedPin, HIGH);
} else {
digitalWrite(yellowLedPin, LOW);
}
if (temp < 0 && hum > 60) {
oled.setCursor(0, 50);
oled.setTextSize(1);
oled.print("Warning!");
}
oled.display();
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print("% \tTemperature: ");
Serial.print(temp);
Serial.println(" C");
}