Skip to content
Β 
Β 

Latest commit

Β 

History

37 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CropGuard Logo

🌿 CropGuard AI

Intelligent Plant Disease Detection & IoT Soil Health Monitoring System

Python TensorFlow Flask Arduino MQTT License


πŸ”— Live Application

Access the production dashboard here:
πŸš€ https://cropguard-ai-1-ys7p.onrender.com


πŸ“– Overview

CropGuard AI is a full-stack agricultural intelligence platform that combines deep learning-based plant disease detection with real-time IoT soil monitoring and external weather data to deliver actionable farming insights.

The system fuses three data sources β€” an AI image classifier, live Arduino sensor readings, and Open-Meteo weather forecasts β€” through a rule-based Fusion Engine that produces crop-specific risk scores, treatment plans, and irrigation recommendations.

Recently upgraded from a Streamlit prototype to a fully unified, production-ready Flask Web Application, CropGuard AI now features a responsive, dynamic UI with direct hardware connectivity right from your browser.

✨ Key Features

Feature Description
πŸ”¬ AI Disease Detection Client-side image cropping + server-side MobileNetV2 classification
πŸ”Œ Web Serial Connect Browser-level hardware connection filters & reads Arduino USB directly
πŸ“‘ Live IoT Monitoring Robust Python background publisher auto-detects ports & heals from unplug/replug events
🌦️ Dynamic Weather Uses Browser Geolocation GPS to pull hyper-local real-time weather & AQI dynamically
🧬 Fusion Engine Cross-references disease diagnosis with 7-day sensor trends + weather to generate risk scores
πŸ“Š Interactive Dashboard Custom HTML/JS/CSS with real-time Chart.js graphs, gauge cards & threshold alerts
πŸ“„ Report Generation Export sensor data as CSV or professional PDF reports from the dashboard
πŸ—ƒοΈ Auto Data Retention Background thread purges records older than 7 days and auto-vacuums the database

πŸ› οΈ How to Use

1. Connecting Your Hardware

  • Plug and Play: Connect your Arduino Uno (with the flashed firmware) to your system via USB.
  • Manual Connection: If the dashboard does not automatically display sensor readings, use the "πŸ”Œ Connect USB Sensor" button located in the bottom-right corner of the interface. This uses the Web Serial API to bridge your hardware directly to the browser.
  • Stabilization Period: After connecting, reconnecting, or refreshing, please wait for approximately 1 minute for the system to stabilize, establish a consistent data stream, and update the "Online/Offline" status indicators.

2. Disease Diagnosis

  • Navigate to the Microscope icon (Disease Detection) tab.
  • Upload or drag-and-drop a photo of a plant leaf.
  • Use the interactive cropper to focus on the affected area and click "Run AI Diagnosis".
  • View the combined report which includes the AI's confidence score and the Fusion Engine's environmental risk analysis.

3. Monitoring & Reports

  • Check the Live Soil Monitor for real-time graphs of temperature, humidity, and soil moisture.
  • The system automatically stores your data for up to 7 days. Older data is purged automatically to maintain system performance.
  • Export your findings using the "Export CSV" or "Download PDF Report" buttons in the History tab.

πŸ—οΈ System Architecture & Pipeline

1. Data Acquisition (Hardware)

The Arduino Uno R3 acts as the edge node, polling data from DHT11 (Air) and HL-69 (Soil) sensors every 2 seconds. This data is packaged as a JSON string and broadcasted over the Serial USB interface.

2. Data Ingestion (Dual Pipeline)

  • Backend Reader: A local Python script (serial_reader.py) can run in the background, auto-detecting COM/tty ports and pushing data to the cloud API.
  • Web Serial Bridge: For a seamless experience without local setup, the frontend (app.js) uses the Web Serial API to read the USB port directly from the browser and sync it to the database.

3. Processing & Storage

The Flask Backend serves as the central orchestrator:

  • SQLite Database: Stores all historical readings, disease predictions, and user preferences.
  • Retention Worker: A background thread monitors the database and purges records older than 168 hours (7 days) to ensure high performance and low storage overhead.

4. Intelligence (Fusion Engine)

The core logic resides in fusion_engine.py. It evaluates the Cumulative Environmental Stress:

  • Disease Model: MobileNetV2 (TensorFlow) classifies the leaf pathogen.
  • Soil Context: Fetches 24-hour averages of soil moisture and humidity to check if conditions favor fungal growth.
  • Weather Context: Integrates live precipitation and temperature forecasts from Open-Meteo.
  • Outcome: Generates a unified Risk Score (0-100) and a prioritized action list.

πŸ“ Project Structure

CropGuard-AI/
β”‚
β”œβ”€β”€ run_all.py                      # πŸš€ Master launcher β€” starts services
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ server.py                   # Central server (DB, Retention, API Launcher)
β”‚   β”œβ”€β”€ dashboard_api.py            # Flask REST Endpoints & Routing
β”‚   β”œβ”€β”€ fusion_engine.py            # AI + Environmental logic
β”‚   β”œβ”€β”€ database.py                 # SQLite abstractions
β”‚   └── serial_reader.py            # USB-to-Cloud bridge script
β”‚
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ static/
β”‚   β”‚   β”œβ”€β”€ css/                    # Glassmorphism design system
β”‚   β”‚   └── js/app.js               # SPA logic, Charts, Web Serial API
β”‚   └── templates/                  # Jinja2 HTML templates
β”‚
β”œβ”€β”€ iot/
β”‚   └── arduino_sensor.ino          # Arduino C++ firmware
β”‚
β”œβ”€β”€ soil_data.db                    # πŸ—ƒοΈ SQLite database (auto-created)
└── plant_disease_model_new.keras   # 🧠 Trained AI Model

βš™οΈ Hardware Requirements

Component Model Pin Purpose
Microcontroller Arduino Uno R3 β€” Central controller
Temp/Humidity Sensor DHT11 D2 Air temperature & humidity
Soil Moisture Sensor HL-69 (Analog) A0 Soil moisture level (0–100%)
Alert LED Standard LED D8 Visual alert indicator
Alert Buzzer Passive Buzzer D9 Audible alert

πŸš€ Getting Started

Prerequisites

  • Python 3.10+ with pip
  • Arduino IDE (to flash the firmware)
  • Arduino Uno R3 with sensors connected
  • USB Cable connecting Arduino to your machine
  • Google Chrome or Edge (required for Web Serial API features)

1. Clone & Setup Environment

git clone <your-repo-url>
cd plant_disease_iot_v3
python -m venv .venv
source .venv/bin/activate        # Linux/macOS
# .venv\Scripts\activate         # Windows
pip install -r requirements.txt

2. Flash Arduino Firmware

  1. Open iot/arduino_sensor.ino in Arduino IDE
  2. Install the DHT sensor library (by Adafruit) via Library Manager
  3. Select Arduino Uno board and the correct serial port
  4. Click Upload

3. Launch the System

python run_all.py

This single command starts:

  1. mqtt_subscriber.py β€” The MQTT listener, SQLite DB manager, and the embedded Flask API server (Port 5000).
  2. mqtt_publisher.py β€” The background hardware scanner that bridges Arduino Serial to the cloud.

4. Access the Dashboard

Open your browser (Chrome/Edge recommended) to: https://cropguard-ai-1-ys7p.onrender.com


πŸ“‘ API Reference

All REST endpoints are served by the Flask API on port 5000.

Method Endpoint Description
GET /api/sensor Latest sensor reading + connection status
POST /api/sensor/upload Upload dynamic Web Serial client sensor readings to SQLite
POST /api/predict Run MobileNetV2 inference on an uploaded image crop
GET /api/history?hours=N Sensor history (max 168h / 7 days)
GET /api/weather?lat=X&lon=Y Local weather, hourly/daily forecasts
GET /api/insights?lat=X&lon=Y Rule-based crop insights with health score
GET /api/thresholds Current alert thresholds
POST /api/thresholds Update alert thresholds
GET /api/calibration Current sensor calibration offsets
POST /api/calibration Update calibration offsets
GET /api/export/csv?hours=N Download sensor data as CSV
GET /api/export/pdf?hours=N Download full PDF report

πŸ§ͺ Tech Stack

Layer Technology
AI Model TensorFlow/Keras (MobileNetV2), OpenCV
Backend Web Framework Flask, Jinja2, Flask-CORS
Frontend Utilities Chart.js, Cropper.js, Web Serial API, Geolocation API
IoT Hardware Arduino Uno R3, DHT11, HL-69 Soil Sensor
Communication MQTT (paho-mqtt) via HiveMQ public broker
Serial I/O PySerial (9600 baud)
Database SQLite3
Weather Open-Meteo API (free, no key)
Styling Custom Vanilla CSS (Glassmorphism, CSS Variables)

πŸ“„ License

This project is for educational and research purposes.


Built with πŸ’š for smarter agriculture

About

🌿 Intelligent Plant Disease Detection & IoT Soil Health Monitoring System - Combines deep learning-based plant disease detection with real-time IoT soil monitoring, weather integration, and a smart fusion engine for comprehensive crop health management.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages