#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define RelayPin 33
#define DHTPIN 32 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 11
// #define potPin 35 // Pin yang terhubung ke potensiometer
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT dht(DHTPIN, DHTTYPE);
int ldrPin = 14;
int buzzerPin = 18;
const int currentSensorPin = 34; // Change to your selected GPIO pin
float voltage, current;
void setup() {
// put your setup code here, to run once:
pinMode(buzzerPin, OUTPUT);
pinMode(ldrPin, INPUT);
Serial.begin(9600);
pinMode(RelayPin, OUTPUT);
// pinMode(potPin, INPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(2,25);
display.println(F("MUTIA DAN KHENSA"));
display.display();
Serial.begin(115200);
dht.begin();
}
void DisplayData() {
float h = dht.readHumidity();
float tm = dht.readTemperature();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 7);
display.println("----------");
display.setCursor(2, 20);
display.print(int(tm));
display.print((char)247);
display.println("C");
display.setCursor(60, 20);
display.print(": ");
display.print(int(h));
display.println("%");
display.setCursor(0, 34);
display.println("----------");
display.setTextSize(1);
display.setCursor(20, 45);
display.print("Automatic Mode");
if (tm >= 32) {
digitalWrite(RelayPin, HIGH);
Serial.println("FAN ON");
// Blynk.virtualWrite(VPIN_Heater, heaterState);
}
else if(h >= 50){
digitalWrite(RelayPin, HIGH);
Serial.println("FAN ON");
}
else {
digitalWrite(RelayPin, LOW);
Serial.println("FAN OFF");
// Blynk.virtualWrite(VPIN_Heater, heaterState);
}
display.display();
}
void loop() {
// int nilaiPotensiometer = analogRead(potPin); // Baca nilai potensiometer (0 - 1023)
// int kecepatanKipas = map(nilaiPotensiometer, 0, 1023, 0, 100); // Pemetaan nilai potensiometer ke rentang kecepatan kipas (0 - 255)
// analogWrite(RelayPin, kecepatanKipas); // Atur kecepatan kipas menggunakan PWM
// Serial.println(kecepatanKipas)
// delay(100); // Beri sedikit keterlambatan untuk menghindari pembacaan yang terlalu cepat
int sensorValue = analogRead(currentSensorPin);
// Convert the analog value to voltage
voltage = sensorValue * (3.3 / 4095.0); // ESP32 ADC resolution is 12 bits
// Calculate current (based on the sensor's characteristics)
// ACS712 provides 0.5V at 0A and 100mV/A sensitivity
current = (voltage - 0.5) / 0.1; // Adjust this calculation based on your sensor
// Print out the results
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, Current: ");
Serial.print(current);
Serial.println(" A");
delay(1000); // Wait for a second be
int sensorValues = analogRead(ldrPin);
if (sensorValues < 500) {
tone(buzzerPin, 1000);
Serial.println(" ad asp ");
} else {
noTone(buzzerPin);
Serial.println(" No asp.");
}
delay(1000);
DisplayData();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)){
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.println();
delay(2000); // Wait for a new reading from the sensor (DHT11 has ~0.5Hz sample rate)
if(t >= 32){
digitalWrite(RelayPin, HIGH); // To send high voltage signal 3.3/5V as ON
delay(1000); // this speeds up the simulation
}
else if(h >= 50){
digitalWrite(RelayPin, HIGH); // To send high voltage signal 3.3/5V as ON
delay(1000); // this speeds up the simulation
}
else{
digitalWrite(RelayPin, LOW);
delay(1000); // this speeds up the simulation
}
}