#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define ONE_WIRE_BUS 2
#define MQ2_PIN A0
#define LDR_PIN A1
#define BUZZER_PIN 7
#define LED_PIN 6
#define TFT_CS 10
#define TFT_RST 8
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
bool isFireDetected = false;
bool wasFireDetected = false;
void setup() {
Serial.begin(9600);
sensors.begin();
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.print("SISTEM DETEKSI DAN ALARM KEBAKARAN");
tft.setCursor(10, 30);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.print("BERBASIS MIKROKONTROLER");
tft.setCursor(10, 50);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.print("DI WOKWI: SENSOR API, ASAP, DAN SUHU");
delay(2000);
tft.fillScreen(ILI9341_BLACK);
delay(1000);
}
void loop() {
// Baca suhu dari sensor DS18B20
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Baca nilai asap dari sensor MQ-2
int smokeValue = analogRead(MQ2_PIN);
Serial.print("Smoke Value: ");
Serial.println(smokeValue);
// Baca nilai cahaya dari sensor LDR
int lightValue = analogRead(LDR_PIN);
Serial.print("Light Value: ");
Serial.println(lightValue);
tft.fillRect(10, 70, 300, 40, ILI9341_BLACK);
tft.setCursor(10, 70);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.print("Suhu : ");
tft.print(temperatureC);
tft.print(" C");
tft.fillRect(10, 110, 300, 40, ILI9341_BLACK);
tft.setCursor(10, 110);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.print("Lux : ");
tft.print(lightValue);
tft.setTextSize(1);
tft.print("(N. Analog)");
tft.fillRect(10, 150, 300, 40, ILI9341_BLACK);
tft.setCursor(10, 150);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.print("Asap : ");
if ((lightValue < 190 && temperatureC > 50 && smokeValue > 500) ||
(temperatureC > 50 && smokeValue > 500) ||
(lightValue < 190 && smokeValue > 500)) {
isFireDetected = true;
} else {
isFireDetected = false;
}
if (isFireDetected && !wasFireDetected) {
tft.setTextColor(ILI9341_RED);
tft.setCursor(10, 200);
tft.setTextSize(3);
tft.print("Terjadi Kebakaran");
tone(BUZZER_PIN, 400);
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
} else if (!isFireDetected && wasFireDetected) {
tft.fillRect(10, 200, 400, 40, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 180);
tft.setTextSize(3);
noTone(BUZZER_PIN);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
}
wasFireDetected = isFireDetected;
tft.setCursor(150, 150);
tft.setTextSize(3);
if (smokeValue > 500) {
tft.setTextColor(ILI9341_RED);
tft.print(" Ya");
} else {
tft.setTextColor(ILI9341_WHITE);
tft.print(" Tidak");
}
delay(1000);
}