From 581ab42fac8ec7caf39245fe6fd1dc58dc4f83cf Mon Sep 17 00:00:00 2001 From: Wizznu Date: Mon, 16 Feb 2026 19:30:34 +0700 Subject: [PATCH] feat: Add verbose logging for sensor mode threshold monitoring - Log every sensor check with full sensor data - Display threshold config for each check - Show detailed evaluation for each pot - Log cooldown status and trigger events - Help debug why mosfet not triggering --- worker.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/worker.js b/worker.js index ea0ad1c..ff6d63a 100644 --- a/worker.js +++ b/worker.js @@ -787,7 +787,12 @@ setTimeout(async () => { let sensorCheckCounter = 0; async function setupSensorMonitoring() { + console.log('šŸŒ”ļø ==================== SENSOR MODE ENABLED ===================='); console.log('āœ… Sensor Mode (Threshold System) monitoring started'); + console.log('šŸ“ Listening to Firebase path: /data'); + console.log('šŸ“ Config path: /' + FIREBASE_PATHS.kontrol); + console.log('ā±ļø Debounce time: 2 minutes between watering per pot'); + console.log('================================================================\n'); db.ref('data').on('value', async (snapshot) => { sensorCheckCounter++; @@ -809,13 +814,12 @@ async function setupSensorMonitoring() { // Detect all threshold_* nodes const allThresholds = Object.keys(kontrolConfig).filter(key => key.startsWith('threshold_')); - // Log sensor check (verbose hanya setiap 10 kali) - if (sensorCheckCounter % 10 === 0) { - console.log(`\nšŸŒ”ļø SENSOR CHECK #${sensorCheckCounter} | Total Thresholds: ${allThresholds.length}`); - } + // Log sensor check (ALWAYS LOG untuk debugging) + console.log(`\nšŸŒ”ļø SENSOR CHECK #${sensorCheckCounter} | Total Thresholds: ${allThresholds.length}`); + console.log(` šŸ“Š Sensor Data:`, JSON.stringify(sensorData, null, 2)); if (allThresholds.length === 0) { - // No thresholds configured + console.log(' āš ļø No thresholds configured'); return; } @@ -823,8 +827,14 @@ async function setupSensorMonitoring() { for (const thresholdKey of allThresholds) { const threshold = kontrolConfig[thresholdKey]; + console.log(`\n šŸ” Checking ${thresholdKey}:`); + console.log(` Config:`, JSON.stringify(threshold, null, 2)); + // Skip if threshold is not active or invalid - if (!threshold || !threshold.aktif) continue; + if (!threshold || !threshold.aktif) { + console.log(` āŒ Skipped: ${!threshold ? 'Not found' : 'Not active (aktif=false)'}`); + continue; + } const batasBawah = threshold.batas_bawah || 30; const batasAtas = threshold.batas_atas || 70; @@ -836,11 +846,16 @@ async function setupSensorMonitoring() { // Check each pot in this threshold for (const potNumber of potAktif) { - if (potNumber < 1 || potNumber > 5) continue; + if (potNumber < 1 || potNumber > 5) { + console.log(` āš ļø POT ${potNumber}: Invalid pot number (must be 1-5)`); + continue; + } const soilKey = `soil_${potNumber}`; const soilValue = parseInt(sensorData[soilKey]) || 0; + console.log(` 🌱 POT ${potNumber} (${soilKey}): ${soilValue}% | Threshold: ${batasBawah}-${batasAtas}%`); + // Check if below threshold if (soilValue < batasBawah) { const potKey = `pot_${potNumber}`; @@ -849,9 +864,7 @@ async function setupSensorMonitoring() { // Debounce: minimum 2 menit antar penyiraman if (lastTime && Date.now() - lastTime < config.worker.sensorDebounce) { const remainingSeconds = Math.ceil((config.worker.sensorDebounce - (Date.now() - lastTime)) / 1000); - if (sensorCheckCounter % 10 === 0) { - console.log(`ā³ ${thresholdKey.toUpperCase()} - POT ${potNumber}: Cooldown (${remainingSeconds}s)`); - } + console.log(` ā³ POT ${potNumber}: Cooldown active (${remainingSeconds}s remaining)`); continue; } @@ -889,6 +902,8 @@ async function setupSensorMonitoring() { // Update last watering time to prevent rapid re-triggering lastWateringTime[potKey] = Date.now(); + } else { + console.log(` āœ… POT ${potNumber}: OK (${soilValue}% >= ${batasBawah}%)`); } } }