Upload files to "Kode Program Arduino"
This commit is contained in:
parent
f1e9ff047a
commit
160f9371fd
|
@ -0,0 +1,298 @@
|
|||
#include <WiFiManager.h>
|
||||
#include <FirebaseESP32.h>
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
|
||||
const int flameSensorPin = 34;
|
||||
const int smokeDetectionPin = 4; // Smoke detection pin (used for smoke detection)
|
||||
const int buzzerPin = 27;
|
||||
const int greenLEDPin = 25;
|
||||
const int blueLEDPin = 26;
|
||||
const int redLEDPin = 33;
|
||||
|
||||
#define IGNORE_TIME 200UL //ignore 2000ms at start
|
||||
#define ACTIVE_TIME 200UL //input 2000ms before
|
||||
|
||||
#define FIREBASE_HOST "projecttugasakhir-6d39f-default-rtdb.firebaseio.com"
|
||||
#define FIREBASE_AUTH "AIzaSyA2_Bc6_LAVn9UENlx8IRa_Ne6j9b_HCLI"
|
||||
|
||||
FirebaseData firebaseData;
|
||||
WebServer server(80);
|
||||
|
||||
int flameThreshold = 400;
|
||||
unsigned long wifiConnectedTime = 0;
|
||||
volatile uint32_t lastTime = 0;
|
||||
volatile uint32_t firstTime = 0;
|
||||
volatile bool inputActive = false;
|
||||
volatile bool prevInput = false;
|
||||
|
||||
unsigned long previousBuzzerMillis = 0;
|
||||
const long buzzerInterval = 500; // Interval for buzzer sound
|
||||
|
||||
unsigned long previousLEDMillis = 0;
|
||||
const long ledInterval = 500; // Interval for LED blink
|
||||
bool ledState = false;
|
||||
|
||||
String getESPSerial();
|
||||
void updateFirebase(int flameValue, String flameStatus, bool smokeDetected);
|
||||
void setLEDs(int ledColor);
|
||||
void handleRoot();
|
||||
void handleSetThreshold();
|
||||
void updateClientIP();
|
||||
void blinkLED(int ledColor);
|
||||
void blink(int pin, int duration, int repetitions);
|
||||
void soundBuzzer(int repetitions);
|
||||
|
||||
String clientIP = "";
|
||||
|
||||
void ICACHE_RAM_ATTR INTERRUPT_handler() {
|
||||
//debounce
|
||||
if ((millis() - lastTime) < 50) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (((millis() - firstTime) > IGNORE_TIME)) {
|
||||
if (digitalRead(smokeDetectionPin) == HIGH) { // Change condition to HIGH for smoke detection
|
||||
lastTime = millis();
|
||||
if (!inputActive) inputActive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InputRoutine() {
|
||||
//reset if timeout
|
||||
if (inputActive) {
|
||||
if ((millis() - lastTime) > ACTIVE_TIME) {
|
||||
lastTime = millis();
|
||||
inputActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetupInput() {
|
||||
pinMode(smokeDetectionPin, INPUT_PULLUP); // Smoke detection pin as input with pull-up resistor
|
||||
firstTime = millis();
|
||||
attachInterrupt(digitalPinToInterrupt(smokeDetectionPin), INTERRUPT_handler, RISING);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
WiFiManager wifiManager;
|
||||
|
||||
WiFi.softAPConfig(IPAddress(192, 168, 1, 1), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
|
||||
wifiManager.autoConnect("ESP32_alat2");
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("Connected to WiFi");
|
||||
wifiConnectedTime = millis();
|
||||
} else {
|
||||
Serial.println("Failed to connect to WiFi");
|
||||
}
|
||||
|
||||
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
|
||||
|
||||
pinMode(buzzerPin, OUTPUT);
|
||||
pinMode(greenLEDPin, OUTPUT);
|
||||
pinMode(blueLEDPin, OUTPUT);
|
||||
pinMode(redLEDPin, OUTPUT); // Ensure the red LED pin is configured as output
|
||||
pinMode(smokeDetectionPin, INPUT_PULLUP); // Set pin as input with pull-up resistor
|
||||
|
||||
digitalWrite(buzzerPin, HIGH);
|
||||
delay(500);
|
||||
digitalWrite(buzzerPin, LOW);
|
||||
|
||||
server.on("/", handleRoot);
|
||||
server.on("/setThreshold", handleSetThreshold);
|
||||
server.begin();
|
||||
Serial.println("Web server started");
|
||||
|
||||
setLEDs(flameThreshold);
|
||||
|
||||
SetupInput(); // Call SetupInput() to configure the input pin for smoke detection
|
||||
|
||||
// Update client IP in Firebase
|
||||
updateClientIP();
|
||||
}
|
||||
|
||||
bool buzzerActive = false; // Flag to keep track of buzzer state when smoke is detected
|
||||
|
||||
void loop() {
|
||||
server.handleClient();
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
|
||||
int flameValue = analogRead(flameSensorPin);
|
||||
Serial.print("Flame Value: ");
|
||||
Serial.println(flameValue);
|
||||
String flameStatus = (flameValue < flameThreshold) ? "2" : "1";
|
||||
|
||||
// Check smoke detection pin
|
||||
if (prevInput != inputActive) {
|
||||
prevInput = inputActive;
|
||||
if (inputActive) {
|
||||
Serial.println("Ada Asap");
|
||||
buzzerActive = true; // Activate buzzer when smoke is detected
|
||||
} else {
|
||||
Serial.println("Tidak Ada Asap");
|
||||
buzzerActive = false; // Deactivate buzzer when smoke is no longer detected
|
||||
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off
|
||||
}
|
||||
}
|
||||
|
||||
InputRoutine(); // Call InputRoutine() to handle smoke detection
|
||||
|
||||
if (flameStatus == "2") { // Buzzer active only if fire is detected
|
||||
if (currentMillis - previousBuzzerMillis >= buzzerInterval) {
|
||||
previousBuzzerMillis = currentMillis;
|
||||
soundBuzzer(5); // Sound buzzer 5 times when fire is detected
|
||||
}
|
||||
|
||||
if (currentMillis - previousLEDMillis >= ledInterval) {
|
||||
previousLEDMillis = currentMillis;
|
||||
ledState = !ledState;
|
||||
if (ledState) {
|
||||
setLEDs(flameThreshold); // Turn on the appropriate LED
|
||||
} else {
|
||||
setLEDs(0); // Turn off all LEDs
|
||||
}
|
||||
}
|
||||
} else if (!inputActive) { // Ensure buzzer is off when no fire is detected and no smoke is detected
|
||||
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off
|
||||
setLEDs(flameThreshold); // Kembalikan LED ke status semula
|
||||
}
|
||||
|
||||
// Sound buzzer continuously in loop if smoke is detected
|
||||
if (buzzerActive && inputActive) {
|
||||
soundBuzzer(5); // Keep sounding buzzer until smoke is no longer detected
|
||||
}
|
||||
|
||||
// Update client IP before updating sensor data
|
||||
updateClientIP();
|
||||
|
||||
updateFirebase(flameValue, flameStatus, inputActive); // Call updateFirebase() with smoke detection status
|
||||
|
||||
delay(10); // Reduced delay to allow more frequent checking
|
||||
}
|
||||
|
||||
|
||||
void handleRoot() {
|
||||
String html = "<html><body>"
|
||||
"<h1>ESP32 Sensor Configuration</h1>"
|
||||
"<form action=\"/setThreshold\" method=\"POST\">"
|
||||
"Flame Threshold: "
|
||||
"<select name=\"threshold\">"
|
||||
"<option value=\"400\" " + String(flameThreshold == 400 ? "selected" : "") + ">Mode 1 (400)</option>"
|
||||
"<option value=\"700\" " + String(flameThreshold == 700 ? "selected" : "") + ">Mode 2 (700)</option>"
|
||||
"<option value=\"1000\" " + String(flameThreshold == 1000 ? "selected" : "") + ">Mode 3 (1000)</option>"
|
||||
"</select><br>"
|
||||
"<input type=\"submit\" value=\"Set Threshold\">"
|
||||
"</form>"
|
||||
"</body></html>";
|
||||
server.send(200, "text/html", html);
|
||||
}
|
||||
|
||||
void handleSetThreshold() {
|
||||
if (server.hasArg("threshold")) {
|
||||
flameThreshold = server.arg("threshold").toInt();
|
||||
Serial.println("New Flame Threshold: " + String(flameThreshold));
|
||||
|
||||
setLEDs(flameThreshold);
|
||||
|
||||
digitalWrite(buzzerPin, HIGH);
|
||||
delay(500);
|
||||
digitalWrite(buzzerPin, LOW);
|
||||
}
|
||||
server.sendHeader("Location", "/");
|
||||
server.send(303);
|
||||
}
|
||||
|
||||
void updateClientIP() {
|
||||
// Get the current IP address from WiFi
|
||||
clientIP = WiFi.localIP().toString();
|
||||
Serial.println("Client IP: " + clientIP);
|
||||
}
|
||||
|
||||
void updateFirebase(int flameValue, String flameStatus, bool smokeDetected) {
|
||||
FirebaseJson json;
|
||||
json.set("espSerial", getESPSerial());
|
||||
json.set("nomorRumah", "A02");
|
||||
json.set("flameValue", flameValue);
|
||||
json.set("flameStatus", flameStatus);
|
||||
json.set("smokeDetected", smokeDetected ? "2" : "1"); // Convert smoke detection status to string "2" or "1"
|
||||
json.set("clientIP", clientIP); // Add clientIP to JSON
|
||||
|
||||
// Non-blocking call to update Firebase
|
||||
if (Firebase.updateNode(firebaseData, "/sensorData2", json)) {
|
||||
Serial.println("Data successfully updated to Firebase!");
|
||||
} else {
|
||||
Serial.println("Failed to update data to Firebase.");
|
||||
Serial.println("Reason: " + firebaseData.errorReason());
|
||||
}
|
||||
}
|
||||
|
||||
String getESPSerial() {
|
||||
uint64_t chipid = ESP.getEfuseMac();
|
||||
return String(chipid, HEX);
|
||||
}
|
||||
|
||||
void setLEDs(int ledColor) {
|
||||
switch (ledColor) {
|
||||
case 400:
|
||||
digitalWrite(greenLEDPin, HIGH);
|
||||
digitalWrite(blueLEDPin, LOW);
|
||||
digitalWrite(redLEDPin, LOW);
|
||||
break;
|
||||
case 700:
|
||||
digitalWrite(greenLEDPin, LOW);
|
||||
digitalWrite(blueLEDPin, HIGH);
|
||||
digitalWrite(redLEDPin, LOW);
|
||||
break;
|
||||
case 1000:
|
||||
digitalWrite(greenLEDPin, LOW);
|
||||
digitalWrite(blueLEDPin, LOW);
|
||||
digitalWrite(redLEDPin, HIGH);
|
||||
break;
|
||||
default:
|
||||
digitalWrite(greenLEDPin, LOW);
|
||||
digitalWrite(blueLEDPin, LOW);
|
||||
digitalWrite(redLEDPin, LOW);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void blinkLED(int ledColor) {
|
||||
switch (ledColor) {
|
||||
case 400:
|
||||
blink(greenLEDPin, 1000, 5); // Kedipkan LED hijau 3 kali dengan interval 500ms
|
||||
break;
|
||||
case 700:
|
||||
blink(blueLEDPin, 1000, 5); // Kedipkan LED biru 3 kali dengan interval 500ms
|
||||
break;
|
||||
case 1000:
|
||||
blink(redLEDPin, 1000, 5); // Kedipkan LED merah 3 kali dengan interval 500ms
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void blink(int pin, int duration, int repetitions) {
|
||||
for (int i = 0; i < repetitions; i++) {
|
||||
digitalWrite(pin, HIGH);
|
||||
delay(duration);
|
||||
digitalWrite(pin, LOW);
|
||||
delay(duration);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to sound the buzzer for a specified number of repetitions
|
||||
void soundBuzzer(int repetitions) {
|
||||
for (int i = 0; i < repetitions; i++) {
|
||||
digitalWrite(buzzerPin, HIGH);
|
||||
delay(100); // Reduced sound duration
|
||||
digitalWrite(buzzerPin, LOW);
|
||||
delay(100); // Reduced silence duration
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue