#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int redLed = 25;
int blueLed = 33;
int whiteLed = 32;
int buzzPin = 26;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht(23, DHT22);
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
dht.begin();
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(whiteLed, OUTPUT);
pinMode(buzzPin, OUTPUT);
}
void loop() {
delay(10); // this speeds up the simulation
float tempC = dht.readTemperature();
float tempF = tempC * 9 / 5 + 32; // Convert to Fahrenheit
float tempK = tempC + 273.15;
float humidity = dht.readHumidity();
if (isnan(tempC) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
if (tempC <= 15 && humidity <= 10) {
digitalWrite(whiteLed, HIGH);
tone(buzzPin, 1000); // Continuous tone
} else if (tempC > 15 && tempC <= 27 && humidity > 10 && humidity <= 50) {
digitalWrite(blueLed, HIGH);
noTone(buzzPin); // Stop the buzzer
} else if (tempC > 27 && humidity >= 51) {
digitalWrite(redLed, HIGH);
tone(buzzPin, 1000); // Continuous tone
} else {
digitalWrite(redLed, LOW);
digitalWrite(blueLed, LOW);
digitalWrite(whiteLed, LOW);
noTone(buzzPin); // Stop the buzzer
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
oled.println("Menyala abangkuu!!!");
oled.display();
}
displayTemperature(tempC, tempF, tempK);
// Turn off LEDs for next cycle
digitalWrite(redLed, LOW);
digitalWrite(blueLed, LOW);
digitalWrite(whiteLed, LOW);
delay(2000); // Delay before the next reading
}
void displayTemperature(float celsius, float fahrenheit, float kelvin) {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0, 0);
oled.print("Celsius: ");
oled.println(celsius);
oled.print("Fahrenheit: ");
oled.println(fahrenheit);
oled.print("Kelvin: ");
oled.println(kelvin);
oled.display();
}