Prototipe sistem penyiraman multi-pot otomatis berbasis Internet of Things (IoT) ini dirancang untuk mengoptimalkan efisiensi kerja dan ketepatan volume air pada lahan pembibitan tanaman hortikultura. Sistem ini mengintegrasikan sensor kelembapan tanah untuk memantau kondisi media tanam secara real-time dan solenoid valve yang berfungsi mengatur distribusi air secara independen dan spesifik pada setiap pot. Terhubung dengan aplikasi mobile berbasis cloud, alat ini memungkinkan pengelola melakukan pemantauan kondisi tanah dan kontrol penyiraman dari jarak jauh, yang divalidasi melalui pengujian skala terbatas pada sampel blok tanaman cabai.
Go to file
Wizznu c79033de91 fix: Add polling mechanism for sensor threshold monitoring
Problem:
- Firebase SDK listener not triggering (SDK timeout)
- No sensor data being checked despite sensor=true
- Log shows no SENSOR CHECK output

Solution:
- Add polling every 30 seconds using REST API (reliable)
- Keep Firebase listener as backup if SDK recovers
- Check kontrolConfig.sensor flag before processing
- Shared checkSensorThresholds() function for both methods

This ensures sensor mode works even when Firebase SDK fails
2026-02-16 19:45:29 +07:00
.dockerignore feat: Add Dockerfile for Railway deployment 2026-02-10 11:29:49 +07:00
.env.example Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
.env.template Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
.gitignore Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
DEBUGGING_LOGS.md Add comprehensive debugging guide for scheduler and aktuator issues 2026-02-11 10:29:15 +07:00
Dockerfile feat: Add Dockerfile for Railway deployment 2026-02-10 11:29:49 +07:00
FLEXIBLE_SCHEDULE_GUIDE.md feat: Flexible jadwal system + kontrol_1 path support 2026-02-16 11:05:22 +07:00
README.md Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
SETUP_FIREBASE.md Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
URGENT_FIX.md Add urgent fix summary and action steps 2026-02-11 10:30:51 +07:00
check-queue.js Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
debug.js Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
extract-credentials.js Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
firebase-example.json feat: Flexible jadwal system + kontrol_1 path support 2026-02-16 11:05:22 +07:00
package.json Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
railway.json Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
setup-firebase-jadwal.js feat: Flexible jadwal system + kontrol_1 path support 2026-02-16 11:05:22 +07:00
setup-firebase-threshold.js feat: Dynamic threshold system for sensor-based watering 2026-02-16 12:31:26 +07:00
test-firebase-connection.js Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
test-firebase-direct.js Initial commit: Railway Worker for ApsGo IoT System 2026-02-11 07:40:33 +07:00
worker.js fix: Add polling mechanism for sensor threshold monitoring 2026-02-16 19:45:29 +07:00

README.md

ApsGo Railway Worker

Background worker service untuk sistem otomasi IoT ApsGo. Service ini berjalan 24/7 di cloud untuk menjalankan penjadwalan dan automation bahkan ketika aplikasi mobile ditutup atau handphone pengguna mati.

Features

  • Waktu Mode: Penjadwalan berdasarkan waktu (cron-based)
  • Sensor Mode: Otomasi berdasarkan threshold kelembapan tanah
  • Auto History Logging: Record data sensor setiap 10 menit
  • Redis Queue: Prevent race conditions dan manage concurrent tasks
  • Graceful Shutdown: Clean shutdown dengan safety turn-off semua aktuator
  • Health Monitoring: Auto health check setiap 5 menit
  • Auto Cleanup: Hapus history lama otomatis (retain 30 hari)

Tech Stack

  • Node.js: Runtime environment
  • Firebase Admin SDK: Realtime Database integration
  • BullMQ: Robust job queue dengan Redis
  • Redis: In-memory database untuk queue dan caching
  • Cron: Scheduled tasks

Setup Local Development

  1. Install dependencies:
npm install
  1. Copy .env.example ke .env dan isi dengan credentials Firebase Anda:
cp .env.example .env
  1. Setup Redis lokal (gunakan Docker):
docker run -d -p 6379:6379 redis:latest
  1. Run worker:
npm run dev  # Development mode dengan nodemon
# atau
npm start    # Production mode

Deploy to Railway

Lihat file DEPLOYMENT_GUIDE.md untuk step-by-step deployment ke Railway.

Environment Variables

Variable Description Required
FIREBASE_PROJECT_ID Firebase project ID
FIREBASE_CLIENT_EMAIL Firebase service account email
FIREBASE_PRIVATE_KEY Firebase service account private key
FIREBASE_DATABASE_URL Firebase Realtime Database URL
REDIS_HOST Redis hostname
REDIS_PORT Redis port (default: 6379)
REDIS_PASSWORD Redis password (if required)

Architecture

Flutter App (Mobile)
        ↕
Firebase Realtime DB ← ESP32/Hardware
        ↕
Railway Worker (This service)
        ↕
   Redis Queue

How It Works

Waktu Mode

  • Worker check Firebase /kontrol setiap 30 detik
  • Jika waktu_1 atau waktu_2 match dengan waktu sekarang, add job ke queue
  • Job akan diprocess oleh worker untuk nyalakan pompa dan valve
  • Setelah durasi selesai, otomatis matikan

Sensor Mode

  • Worker listen ke Firebase /data secara realtime
  • Jika soil_X < batas_bawah, trigger watering untuk pot tersebut
  • Ada cooldown 2 menit per pot untuk prevent over-watering
  • Support 2 mode: fixed (durasi tetap) dan smart (sampai mencapai batas_atas)

Safety Features

  • Concurrency: 1 (hanya 1 job diprocess pada satu waktu)
  • Debouncing: Minimum 2 menit antar penyiraman per pot
  • Error handling: Jika error, otomatis turn OFF semua aktuator
  • Graceful shutdown: Clean up resources saat restart/shutdown

Monitoring

Worker akan log semua aktivitas ke console:

  • Success operations
  • Errors dengan details
  • 💧 Watering jobs progress
  • 📊 History logging
  • 💚 Health check status

Di Railway dashboard, Anda bisa:

  • View logs realtime
  • Monitor CPU/Memory usage
  • Setup alerts untuk failures

Maintenance

Manual Queue Management

Untuk clear queue (jika ada masalah):

const { Queue } = require('bullmq');
const Redis = require('ioredis');

const redis = new Redis(process.env.REDIS_URL);
const queue = new Queue('watering', { connection: redis });

// Clear all jobs
await queue.obliterate();

Database Cleanup

History otomatis di-cleanup setiap hari jam 2 pagi, hanya retain 30 hari terakhir.

Troubleshooting

Worker tidak berjalan

  1. Check environment variables
  2. Check Firebase credentials
  3. Check Redis connection

Job tidak diprocess

  1. Check queue status di logs
  2. Verify Firebase rules mengizinkan admin access
  3. Check concurrency setting

Memory leak

  • Worker menggunakan BullMQ yang sudah optimize untuk long-running process
  • Auto cleanup completed jobs (retain last 100)
  • Auto cleanup failed jobs (retain last 50)

License

MIT