324 lines
8.7 KiB
C++
324 lines
8.7 KiB
C++
#include <Arduino.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESPAsyncTCP.h>
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <ESPDash.h>
|
|
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include "MQ135.h"
|
|
#include <Adafruit_Sensor.h>
|
|
#include <DHT.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
|
|
/* Your Soft AP WiFi Credentials */
|
|
const char *ssid = "IoT Smart Kitchen"; // WiFi Name
|
|
const char* password = "1234567890"; // Password
|
|
|
|
#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 DHTTYPE DHT22 // DHT 22
|
|
#define DHTPIN D4 //DHT22 Pin D4(GPIO 2)
|
|
#define relay1 D5 // Lampu D5(GPIO 14)
|
|
#define relay2 D6 // Kulkas D6(GPIO 12)
|
|
#define relay3 D3 // Pompa D7(GPIO 13)
|
|
#define relay4 D8 // Kipas D8(GPIO 15)
|
|
#define buzzer 10 //Buzzer Pin(GPIO 10)
|
|
#define PIR D7 //PIR Sensor Pin D3(GPIO 0)
|
|
#define Api D0 // Flame sensor Pin D0
|
|
|
|
#define MQ135_PIN A0 // Pin analog yang digunakan untuk sensor MQ135
|
|
#define RLOAD 10.0 // Nilai resistansi beban (10kΩ)
|
|
#define VCC 5.0
|
|
|
|
MQ135 gasSensor(MQ135_PIN);
|
|
float R0 = 0.41; // Nilai R0 yang telah dikalibrasi, sesuaikan dengan hasil kalibrasi
|
|
|
|
int pompa_air;
|
|
int alarm_status;
|
|
int pir_status = 0;
|
|
int flame_sensor = 0;
|
|
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
|
|
|
/* Start Webserver */
|
|
AsyncWebServer server(80);
|
|
|
|
/* Attach ESP-DASH to AsyncWebServer */
|
|
/* ESPDash dashboard(&server); */ /*Bisa Mengunakan IP Default */
|
|
ESPDash dashboard(&server, "/rezainalvito-e32210750-smartkitchen");
|
|
/*
|
|
Dashboard Cards
|
|
Format - (Dashboard Instance, Card Type, Card Name, Card Symbol(optional) )
|
|
*/
|
|
Card AIQ(&dashboard, GENERIC_CARD, "Index Kualitas Udara", "PPM");
|
|
Card temperature(&dashboard, TEMPERATURE_CARD, "Temperatur", "°C");
|
|
Card humidity(&dashboard, HUMIDITY_CARD, "Kelembaban", "%");
|
|
Card Buzzer(&dashboard, STATUS_CARD, "Alarm");
|
|
Card Motion_PIR(&dashboard, STATUS_CARD, "Objek");
|
|
Card Motion_Flame(&dashboard, STATUS_CARD, "Api");
|
|
Card light(&dashboard, BUTTON_CARD, "Lampu");
|
|
Card fridge(&dashboard, BUTTON_CARD, "Kulkas");
|
|
Card oven(&dashboard, BUTTON_CARD, "Pompa");
|
|
Card fan(&dashboard, BUTTON_CARD, "Kipas");
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
/* Connect WiFi */
|
|
WiFi.softAP(ssid, password);
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.softAPIP());
|
|
/* Start AsyncWebServer */
|
|
server.begin();
|
|
|
|
dht.begin();
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
|
|
|
|
pinMode(PIR, INPUT);
|
|
pinMode(Api, INPUT);
|
|
pinMode(buzzer, OUTPUT);
|
|
//pinMode(pompa, OUTPUT);
|
|
pinMode(relay1, OUTPUT);
|
|
pinMode(relay2, OUTPUT);
|
|
pinMode(relay3, OUTPUT);
|
|
pinMode(relay4, OUTPUT);
|
|
|
|
digitalWrite(buzzer, LOW);
|
|
//digitalWrite(pompa, LOW);
|
|
digitalWrite(relay1, HIGH);
|
|
digitalWrite(relay2, HIGH);
|
|
digitalWrite(relay3, HIGH);
|
|
digitalWrite(relay4, HIGH);
|
|
delay(100);
|
|
}
|
|
|
|
void loop() {
|
|
// Baca nilai resistansi sensor (Rs)
|
|
int sensorValue = analogRead(MQ135_PIN);
|
|
float voltage = sensorValue * (VCC / 1023.0); // Konversi nilai bacaan analog ke tegangan (dalam volt)
|
|
float Rs = (VCC - voltage) / voltage * RLOAD;
|
|
|
|
// Hitung PPM menggunakan rumus yang sesuai
|
|
float PPM = pow(10, ((log10(Rs / R0) - 0.12) / -0.27));
|
|
|
|
// Tampilkan hasil PPM
|
|
Serial.print("Nilai PPM: ");
|
|
Serial.println(PPM);
|
|
|
|
float t = dht.readTemperature();
|
|
float h = dht.readHumidity();
|
|
|
|
pir_status = digitalRead(PIR);
|
|
flame_sensor = digitalRead(Api);
|
|
alarm_status = digitalRead(buzzer);
|
|
//pompa_air = digitalRead(pompa);
|
|
|
|
if (t > 30) {
|
|
// Suhu melebihi 30 derajat Celsius
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("Kondisi Suhu Saat Ini");
|
|
|
|
display.setCursor(0, 20); //oled display
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.println("Panas");
|
|
display.display();
|
|
delay(1500);
|
|
} else if (t < 30) {
|
|
// Suhu di bawah 30 derajat Celsius
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("Kondisi Suhu Saat Ini:");
|
|
|
|
display.setCursor(0, 20); //oled display
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.println("Normal");
|
|
display.display();
|
|
delay(1500);
|
|
}
|
|
|
|
if (pir_status == 1)
|
|
{
|
|
Serial.println("Terdeteksi");
|
|
Motion_PIR.update("Terdeteksi", "s");
|
|
dashboard.sendUpdates();
|
|
}
|
|
else if (pir_status == 0)
|
|
{
|
|
Serial.println("Tidak Terdeteksi Objek");
|
|
Motion_PIR.update("Tidak Terdeteksi", "d");
|
|
dashboard.sendUpdates();
|
|
}
|
|
|
|
if (flame_sensor == 1)
|
|
{
|
|
//digitalWrite(buzzer, HIGH);
|
|
digitalWrite(relay3, LOW);
|
|
Motion_Flame.update("Bahaya Timbul Api", "d");
|
|
//Buzzer.update("Aktif", "w");
|
|
oven.update(HIGH);
|
|
dashboard.sendUpdates();
|
|
}
|
|
else if (flame_sensor == 0)
|
|
{
|
|
//digitalWrite(buzzer, LOW);
|
|
digitalWrite(relay3, HIGH);
|
|
Motion_Flame.update("Situasi Aman", "s");
|
|
//Buzzer.update("Mati", "s");
|
|
oven.update(LOW);
|
|
dashboard.sendUpdates();
|
|
}
|
|
|
|
if (PPM> 150)
|
|
{
|
|
digitalWrite(buzzer, HIGH);
|
|
digitalWrite(relay4, LOW);
|
|
Buzzer.update("Aktif", "w");
|
|
fan.update(HIGH);
|
|
dashboard.sendUpdates();
|
|
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0);
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.println("Terdeteksi");
|
|
|
|
display.setCursor(0, 20); //oled display
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.println("Kebocoran");
|
|
|
|
display.setCursor(0, 40); //oled display
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.println("Gas");
|
|
|
|
display.display();
|
|
delay(1500);
|
|
}
|
|
else
|
|
{
|
|
digitalWrite(buzzer, LOW);
|
|
digitalWrite(relay4, HIGH);
|
|
Serial.println("Buzzer Status: OFF");
|
|
Serial.println("Exhaust Fan: OFF");
|
|
Buzzer.update("Mati", "s");
|
|
fan.update(LOW);
|
|
dashboard.sendUpdates();
|
|
}
|
|
|
|
temperature.update(t);
|
|
humidity.update(h);
|
|
AIQ.update(PPM);
|
|
//Button for Light
|
|
light.attachCallback([&](bool value){
|
|
Serial.println("[light] Button Callback Triggered: "+String((value)?"HIGH":"LOW"));
|
|
digitalWrite(relay1, (value));
|
|
light.update(value);
|
|
dashboard.sendUpdates();
|
|
});
|
|
//Button for Fridge
|
|
fridge.attachCallback([&](bool value){
|
|
Serial.println("[fridge] Button Callback Triggered: "+String((value)?"HIGH":"LOW"));
|
|
digitalWrite(relay2, (value));
|
|
fridge.update(value);
|
|
dashboard.sendUpdates();
|
|
});
|
|
//Button for Oven
|
|
oven.attachCallback([&](bool value){
|
|
Serial.println("[oven] Button Callback Triggered: "+String((value)?"HIGH":"LOW"));
|
|
digitalWrite(relay3, (value));
|
|
oven.update(value);
|
|
dashboard.sendUpdates();
|
|
});
|
|
//Button for Fan
|
|
fan.attachCallback([&](bool value){
|
|
Serial.println("[fan] Button Callback Triggered: "+String((value)?"HIGH":"LOW"));
|
|
digitalWrite(relay4, (value));
|
|
fan.update(value);
|
|
dashboard.sendUpdates();
|
|
});
|
|
/* Send Updates to our Dashboard (realtime) */
|
|
dashboard.sendUpdates();
|
|
dashboard.setAuthentication("rezainalvito", "12121212");
|
|
delay(1500);
|
|
|
|
// Display Rezainal Vito
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("Dapur Pintar - IoT");
|
|
|
|
display.setCursor(0, 20);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("Rezainal Vito");
|
|
|
|
display.setCursor(0, 35);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("E32210750");
|
|
|
|
display.setCursor(0, 50);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("Teknik Komputer");
|
|
|
|
display.display();
|
|
delay(2000);
|
|
|
|
// display temperature
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setCursor(0, 0);
|
|
display.print("Temperature: ");
|
|
display.setTextSize(2);
|
|
display.setCursor(0, 10);
|
|
display.print(t);
|
|
display.print(" ");
|
|
display.setTextSize(1);
|
|
display.cp437(true);
|
|
display.write(167);
|
|
display.setTextSize(2);
|
|
display.print("C");
|
|
|
|
// display humidity
|
|
display.setTextSize(1);
|
|
display.setCursor(0, 35);
|
|
display.print("Humidity: ");
|
|
display.setTextSize(2);
|
|
display.setCursor(0, 45);
|
|
display.print(h);
|
|
display.print(" %");
|
|
display.display();
|
|
delay(1500);
|
|
|
|
// Display Air Quality Index
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0); //oled display
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println("Air Quality Index");
|
|
|
|
display.setCursor(0, 20); //oled display
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.print(PPM);
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.println(" PPM");
|
|
display.display();
|
|
delay(1500);
|
|
}
|