← Projects 日本語 →

BANTAY-DAGITAB

2026-04-11
Full Stack IoT Artificial Intelligence
Tech Stack:
Python Python
Django Django
PostgreSQL PostgreSQL
Next.js Next.js
Docker Docker
C++ C++

An Integrated IoT, OCR, and Conversational AI Database System for Post-Paid Residential Energy Monitoring and Bill Shock Prevention in Metro Manila.

Architecture Overview

Bantay-Dagitab is a robust, multi-service enterprise application designed to resolve the pervasive issue of "bill shock" for low-income households in Metro Manila. The system operates as a four-tier distributed platform orchestrated via Docker Compose:

  1. IoT Edge Layer: ESP32 microcontrollers equipped with CT sensors (SCT-013-030) push high-frequency energy usage telemetry to the backend every 15 minutes.
  2. Web Dashboard (Next.js): A strictly typed TypeScript dashboard visualizing monthly consumption, digitized bills, anomaly alerts, and a conversational chatbot interface.
  3. API Backend (Django): Acts as the orchestrator and schema manager. It handles authentication, intercepts requests, runs an OpenCV+Tesseract pipeline for physical bill digitization, and dynamically builds context for external LLM chatbot queries.
  4. ML Inference Service (FastAPI): Hosts decoupled workloads for anomaly detection (scikit-learn) and load forecasting.
  5. Data Layer (PostgreSQL 15): The single source of truth, heavily leveraging advanced DB features (materialized views, PL/pgSQL stored procedures, and triggers) to encapsulate domain logic.

Engineering Challenges Solved

High-Frequency Ingestion & Performance Optimization

A single household produces approximately 35,000 telemetry rows annually. Without proper indexing, aggregating this data bottlenecked the dashboard. By migrating to targeted composite B-tree indexes, query latencies for monthly consumption scans plummeted from ~247.3ms down to 3.8ms, achieving a 65x speedup.

Encapsulation of Complex Domain Logic

Instead of leaking business rules into application middleware, complex energy logic was pushed down to the relational data layer. For example, sp_ingest_iot_reading is a PL/pgSQL stored procedure that inserts readings and conditionally fires anomalies in one atomic transaction, while trg_iotreading_audit ensures tamper-evident logging.

Transaction Boundaries for AI Chat

The chatbot uses Django's @transaction.atomic() to safely wrap the retrieval of contextual data (recent bills, anomaly events, and live IoT wattage), the external LLM HTTP call, and the resulting ChatLog insert into a single transactional sweep, guaranteeing atomic history persistence.

OCR Pipeline Accuracy on Legacy Documents

MERALCO bills often suffer from physical degradation. The backend integrates an OpenCV pipeline to preprocess images (deskewing, binarization, noise reduction) before passing them to Tesseract OCR. This pipeline reliably transforms physical paper bills into structured JSON.

Strict Data Privacy Compliance

Built in strict compliance with the Philippine Data Privacy Act (RA 10173). Database-level constraints like ON DELETE CASCADE across all user-associated foreign keys were implemented to programmatically guarantee the absolute "right of erasure".


Strategic Outcomes

Bantay-Dagitab proved the viability of deploying highly scalable, microservice-based infrastructure to solve localized socioeconomic problems.

  • Mitigation of Bill Shock: Achieved through real-time telemetry visibility combined with an intelligent LLM chatbot that explains consumption trends in natural language to non-technical users.
  • Database-Driven Integrity: Ensured that impossible conditions (e.g., negative kWh billed or anomalies drastically mismatching recent readings) are blocked at the relational database level, offering a robust fallback against application errors.
  • Open-Source Blueprint: Delivered a functional, lightweight, Dockerized reference architecture that LGUs and researchers can deploy for community-scale, demand-side energy management pilots supporting UN SDG 7 (Affordable and Clean Energy).

View Original Repository (GitHub)

← Projects