#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define DHTPIN 2 // pin connected to DHT22 sensor
#define DHTTYPE DHT22
#define biru 3
#define kuning 4
#define merah 5
#define buzzer 6
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
DHT dht(DHTPIN, DHTTYPE);
String displayString;
void setup() {
Serial.begin(9600);
pinMode(biru, OUTPUT);
pinMode(kuning, OUTPUT);
pinMode(merah, OUTPUT);
pinMode(buzzer, OUTPUT);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
dht.begin(); // initialize DHT22 the temperature and humidity sensor
displayString.reserve(10); // to avoid fragmenting memory when using String
}
void loop() {
float tempC = dht.readTemperature(); // read temperature
// check if any reads failed
if (isnan(tempC)) {
displayString = "Failed";
} else {
if(tempC >35){
Serial.print(tempC);
Serial.println(" SUHU PANAS");
digitalWrite(biru, LOW);
digitalWrite(kuning, LOW);
digitalWrite(merah, HIGH);
displayString = "SUHU PANAS";
tone(buzzer, 150);
delay(200);
noTone(buzzer);
delay(200);
}else if(tempC>=29){
Serial.print(tempC);
Serial.println(" SUHU CUKUP");
digitalWrite(biru, LOW);
digitalWrite(kuning, HIGH);
digitalWrite(merah, LOW);
displayString = "SUHU CUKUP";
}else {
Serial.print(tempC);
Serial.println(" SUHU RENDAH");
digitalWrite(biru, HIGH);
digitalWrite(kuning, LOW);
digitalWrite(merah, LOW);
displayString = "SUHU RENDAH";
}
oledDisplayCenter(displayString);
}
// display temperature on OLED
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// display on horizontal and vertical center
oled.clearDisplay(); // clear display
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.println(text); // text to display
oled.display();
}