#include "DHTesp.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define LEBAR_LAYAR 128 // Lebar layar OLED yang digunakan
#define TINGGI_LAYAR 64 // Tinggi layar OLED yang digunakan
const int DHT_PIN = 15;
DHTesp dhtSensor;
int suhu=0;
int buzzer = 25;
Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);
void setup()
{
Serial.begin(9600);
pinMode(18, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000);
oled.clearDisplay(); // clear display
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(buzzer, OUTPUT);
}
void loop()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
int suhu = data.temperature;
Serial.println("Suhu: " + String(data.temperature, 2) + "°C");
//Serial.println("Suhu: " + suhu);
Serial.println("---");
if (suhu < 29){
oled.clearDisplay();
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
oled.setCursor(0, 10); // Atur posisi text pada display
oled.println("Suhu Rendah" ); // Text yang dicetak
oled.display();
digitalWrite(18, HIGH);
delay(500);
digitalWrite(18, LOW); // menampilkan display OLED
}else if ((suhu >= 29) && (suhu < 35)) {
oled.clearDisplay();
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
oled.setCursor(0, 10); // Atur posisi text pada display
oled.println("Suhu Cukup"); // Text yang dicetak
oled.display();
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
}else if (suhu >= 35) {
oled.clearDisplay();
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
oled.setCursor(0, 10); // Atur posisi text pada display
oled.println("Suhu Panas"); // Text yang dicetak
oled.display();
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
tone(buzzer,100,500);
}
}