188 lines
5.0 KiB
C++
188 lines
5.0 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <Firebase_ESP_Client.h>
|
|
#include <TinyGPSPlus.h>
|
|
|
|
// WiFi credentials
|
|
#define WIFI_SSID "KOPI"
|
|
#define WIFI_PASSWORD "digoreng123"
|
|
|
|
// Firebase credentials
|
|
#define API_KEY "AIzaSyBy731BFAPVs_rNPN5mFJfDzTHHTF7gNv8"
|
|
#define DATABASE_URL "https://kacamatapintar-default-rtdb.firebaseio.com/"
|
|
#define LEGACY_TOKEN "RSfBvDMW6VrXGqt2xnO03QMIytuyoJoG0OfK1r1p"
|
|
|
|
// GPS baudrate
|
|
#define GPSBaud 9600
|
|
|
|
// Pin definisi
|
|
#define TRIG_PIN D2 // GPIO4
|
|
#define ECHO_PIN D1 // GPIO5
|
|
#define BUZZER_PIN D5 // GPIO14
|
|
#define LED_STATUS_PIN D6 // GPIO12
|
|
#define BUTTON_PIN D7 // GPIO13
|
|
|
|
// Objek GPS
|
|
TinyGPSPlus gps;
|
|
|
|
// Firebase objects
|
|
FirebaseData fbdo;
|
|
FirebaseAuth auth;
|
|
FirebaseConfig config;
|
|
|
|
// Variabel pengaturan
|
|
unsigned long interval_lokasi = 5000; // default fallback
|
|
int jarakDekat = 20; // default fallback
|
|
int jarakJauh = 100; // default fallback
|
|
|
|
// Variabel tambahan
|
|
bool silentMode = false;
|
|
unsigned long lastSilentBuzz = 0;
|
|
unsigned long lastGPSUpdate = 0;
|
|
bool wasInBetween = false;
|
|
|
|
void setup() {
|
|
Serial.begin(115200); // Serial monitor debug
|
|
Serial.begin(GPSBaud); // GPS tetap pakai Serial biasa
|
|
|
|
// Inisialisasi pin
|
|
pinMode(TRIG_PIN, OUTPUT);
|
|
pinMode(ECHO_PIN, INPUT);
|
|
pinMode(BUZZER_PIN, OUTPUT);
|
|
pinMode(LED_STATUS_PIN, OUTPUT);
|
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
|
|
|
// Koneksi WiFi
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
Serial.print("Connecting to WiFi");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("\nConnected to WiFi");
|
|
|
|
// Setup Firebase
|
|
config.api_key = API_KEY;
|
|
config.database_url = DATABASE_URL;
|
|
config.signer.tokens.legacy_token = LEGACY_TOKEN;
|
|
|
|
Firebase.begin(&config, &auth);
|
|
|
|
if (Firebase.ready()) {
|
|
Serial.println("Firebase is ready!");
|
|
|
|
// Ambil pengaturan dari Firebase
|
|
if (Firebase.RTDB.getInt(&fbdo, "/pengaturan/interval_lokasi")) {
|
|
interval_lokasi = fbdo.intData();
|
|
Serial.print("interval_lokasi: "); Serial.println(interval_lokasi);
|
|
} else {
|
|
Serial.print("Gagal ambil interval_lokasi: "); Serial.println(fbdo.errorReason());
|
|
}
|
|
|
|
if (Firebase.RTDB.getInt(&fbdo, "/pengaturan/jarak_dekat")) {
|
|
jarakDekat = fbdo.intData();
|
|
Serial.print("jarak_dekat: "); Serial.println(jarakDekat);
|
|
} else {
|
|
Serial.print("Gagal ambil jarak_dekat: "); Serial.println(fbdo.errorReason());
|
|
}
|
|
|
|
if (Firebase.RTDB.getInt(&fbdo, "/pengaturan/jarak_jauh")) {
|
|
jarakJauh = fbdo.intData();
|
|
Serial.print("jarak_jauh: "); Serial.println(jarakJauh);
|
|
} else {
|
|
Serial.print("Gagal ambil jarak_jauh: "); Serial.println(fbdo.errorReason());
|
|
}
|
|
|
|
} else {
|
|
Serial.println("Firebase init failed!");
|
|
}
|
|
}
|
|
|
|
float getDistanceCM() {
|
|
digitalWrite(TRIG_PIN, LOW);
|
|
delayMicroseconds(2);
|
|
digitalWrite(TRIG_PIN, HIGH);
|
|
delayMicroseconds(10);
|
|
digitalWrite(TRIG_PIN, LOW);
|
|
long duration = pulseIn(ECHO_PIN, HIGH);
|
|
return duration * 0.034 / 2;
|
|
}
|
|
|
|
void loop() {
|
|
// === GPS HANDLING ===
|
|
while (Serial.available() > 0) {
|
|
gps.encode(Serial.read());
|
|
|
|
if (gps.location.isUpdated() && millis() - lastGPSUpdate >= interval_lokasi) {
|
|
lastGPSUpdate = millis();
|
|
float lat = gps.location.lat();
|
|
float lng = gps.location.lng();
|
|
|
|
if (Firebase.RTDB.setFloat(&fbdo, "/lokasi/latitude", lat)) {
|
|
Serial.println("Latitude sent to Firebase");
|
|
} else {
|
|
Serial.print("Failed to send latitude: ");
|
|
Serial.println(fbdo.errorReason());
|
|
}
|
|
|
|
if (Firebase.RTDB.setFloat(&fbdo, "/lokasi/longitude", lng)) {
|
|
Serial.println("Longitude sent to Firebase");
|
|
} else {
|
|
Serial.print("Failed to send longitude: ");
|
|
Serial.println(fbdo.errorReason());
|
|
}
|
|
}
|
|
}
|
|
// === END GPS ===
|
|
|
|
// === BUTTON MODE ===
|
|
static bool lastButtonState = HIGH;
|
|
bool currentButtonState = digitalRead(BUTTON_PIN);
|
|
if (lastButtonState == HIGH && currentButtonState == LOW) {
|
|
silentMode = !silentMode;
|
|
Serial.println(silentMode ? "Mode: Silent" : "Mode: Aktif");
|
|
delay(300);
|
|
}
|
|
lastButtonState = currentButtonState;
|
|
|
|
// === SENSOR ULTRASONIC ===
|
|
float distance = getDistanceCM();
|
|
Serial.print("Jarak: ");
|
|
Serial.print(distance);
|
|
Serial.println(" cm");
|
|
|
|
if (silentMode) {
|
|
if (millis() - lastSilentBuzz >= 30000) {
|
|
tone(BUZZER_PIN, 1000, 100);
|
|
lastSilentBuzz = millis();
|
|
}
|
|
} else {
|
|
if (distance < jarakDekat) {
|
|
tone(BUZZER_PIN, 1000);
|
|
delay(100);
|
|
noTone(BUZZER_PIN);
|
|
delay(100);
|
|
wasInBetween = false;
|
|
} else if (distance < jarakJauh) {
|
|
if (!wasInBetween) {
|
|
tone(BUZZER_PIN, 1000, 100);
|
|
delay(200);
|
|
tone(BUZZER_PIN, 1000, 100);
|
|
delay(300);
|
|
wasInBetween = true;
|
|
}
|
|
} else {
|
|
noTone(BUZZER_PIN);
|
|
wasInBetween = false;
|
|
}
|
|
}
|
|
|
|
// === LED STATUS ===
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
digitalWrite(LED_STATUS_PIN, HIGH);
|
|
} else {
|
|
digitalWrite(LED_STATUS_PIN, LOW);
|
|
}
|
|
|
|
delay(100); // loop delay
|
|
}
|