main
| 1 | # π± AGRI-SENTINEL |
| 2 | |
| 3 | > **AI-Powered Smart Agriculture Platform** β Real-time crop disease detection, satellite field monitoring, weather intelligence, and precision farming tools for Indian farmers. |
| 4 | |
| 5 |  |
| 6 |  |
| 7 |  |
| 8 |  |
| 9 |  |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## π Overview |
| 14 | |
| 15 | **AGRI-SENTINEL** is a full-stack smart agriculture web application that helps farmers detect crop diseases using AI, monitor their fields via satellite imagery, and get actionable treatment recommendations β all through a mobile-friendly interface designed for rural Indian farmers. |
| 16 | |
| 17 | ### β¨ Key Features |
| 18 | |
| 19 | | Feature | Description | |
| 20 | |---|---| |
| 21 | | π¬ **AI Disease Detection** | Upload a leaf photo β Google Gemini analyzes it β Get disease name, severity, treatment recipe | |
| 22 | | π°οΈ **Satellite Monitoring** | NDVI vegetation index + soil data via Agromonitoring API for registered fields | |
| 23 | | βοΈ **Local Weather** | Auto-detects farmer's village and shows real-time weather from OpenWeatherMap | |
| 24 | | π **Scan History** | Per-user tracking of all past scans with disease stats | |
| 25 | | πΊοΈ **Field Mapping** | GPS-based polygon generation with PostGIS for accurate field boundaries | |
| 26 | | π¨βπΎ **Farmer Onboarding** | Conditional form β Field vs. Garden β collects only relevant data | |
| 27 | | π± **Mobile-First UI** | Agriculture-themed responsive design optimized for phone screens | |
| 28 | | π€ **Market Help Voice AI** | Sarvam AI-powered voice assistant for crop prices, mandi info & selling tips (multilingual) | |
| 29 | | π¬ **Community Forum** | Link to [AgroBit Forum](https://agrobit-forum.vercel.app/) for farmer-to-farmer discussions | |
| 30 | | π€ **IoT Camera Integration** | ESP32-CAM triggered via MQTT β auto-captures leaf images and runs AI diagnosis remotely | |
| 31 | |
| 32 | --- |
| 33 | |
| 34 | ## ποΈ Tech Stack |
| 35 | |
| 36 | | Layer | Technology | |
| 37 | |---|---| |
| 38 | | **Backend** | FastAPI (Python 3.11+) | |
| 39 | | **Database** | Supabase (PostgreSQL + PostGIS) | |
| 40 | | **AI Engine** | Google Gemini 2.5 Flash | |
| 41 | | **Weather** | OpenWeatherMap API | |
| 42 | | **Satellite** | Agromonitoring API (NDVI, Soil, Imagery) | |
| 43 | | **Voice AI** | Sarvam AI (STT, LLM, TTS β multilingual) | |
| 44 | | **Frontend** | Jinja2 Templates + Vanilla JS (no framework overhead) | |
| 45 | | **Auth** | Session-based (phone-number login, no OTP) | |
| 46 | | **IoT** | ESP32-CAM + MQTT (HiveMQ Cloud) for remote camera trigger | |
| 47 | | **Deployment** | Render (Web Service) | |
| 48 | |
| 49 | --- |
| 50 | |
| 51 | ## π Project Structure |
| 52 | |
| 53 | ``` |
| 54 | agri-sentinel/ |
| 55 | βββ main.py # FastAPI application (all routes & logic) |
| 56 | βββ mqtt_client.py # MQTT client module for ESP32-CAM trigger |
| 57 | βββ esp32_cam_mqtt.ino # Arduino sketch for ESP32-CAM (MQTT + HTTPS upload) |
| 58 | βββ requirements.txt # Python dependencies (pinned versions) |
| 59 | βββ render.yaml # Render deployment config |
| 60 | βββ supabase_setup.sql # SQL migrations for Supabase |
| 61 | βββ .env.example # Environment variable template |
| 62 | βββ .gitignore # Git ignore rules |
| 63 | βββ templates/ |
| 64 | β βββ login.html # Phone-number login page |
| 65 | β βββ onboarding.html # Farmer profile form (Field/Garden) |
| 66 | β βββ dashboard.html # Main dashboard with weather, stats, map |
| 67 | β βββ index.html # Crop scan page (AI diagnosis + IoT trigger) |
| 68 | β βββ market-help.html # Voice assistant for market prices |
| 69 | β βββ history.html # Past scan records |
| 70 | βββ README.md |
| 71 | ``` |
| 72 | |
| 73 | --- |
| 74 | |
| 75 | ## π Getting Started |
| 76 | |
| 77 | ### Prerequisites |
| 78 | |
| 79 | - **Python 3.11+** |
| 80 | - **Supabase** account with PostGIS enabled |
| 81 | - API keys for: Google Gemini, OpenWeatherMap, Agromonitoring |
| 82 | |
| 83 | ### 1. Clone the Repository |
| 84 | |
| 85 | ```bash |
| 86 | git clone https://github.com/your-username/agri-sentinel.git |
| 87 | cd agri-sentinel |
| 88 | ``` |
| 89 | |
| 90 | ### 2. Create Virtual Environment |
| 91 | |
| 92 | ```bash |
| 93 | python -m venv .venv |
| 94 | |
| 95 | # Windows |
| 96 | .venv\Scripts\activate |
| 97 | |
| 98 | # macOS/Linux |
| 99 | source .venv/bin/activate |
| 100 | ``` |
| 101 | |
| 102 | ### 3. Install Dependencies |
| 103 | |
| 104 | ```bash |
| 105 | pip install -r requirements.txt |
| 106 | ``` |
| 107 | |
| 108 | ### 4. Configure Environment Variables |
| 109 | |
| 110 | Copy the example env file and fill in your keys: |
| 111 | |
| 112 | ```bash |
| 113 | cp .env.example .env |
| 114 | ``` |
| 115 | |
| 116 | Edit `.env`: |
| 117 | |
| 118 | ```env |
| 119 | SUPABASE_URL=https://your-project.supabase.co |
| 120 | SUPABASE_KEY=your-supabase-anon-key |
| 121 | GEMINI_API_KEY=your-gemini-api-key |
| 122 | OPENWEATHER_API_KEY=your-openweather-key |
| 123 | AGROMONITORING_API_KEY=your-agromonitoring-key |
| 124 | SESSION_SECRET_KEY=generate-a-random-64-char-string |
| 125 | ENVIRONMENT=development |
| 126 | ``` |
| 127 | |
| 128 | ### 5. Set Up Supabase Database |
| 129 | |
| 130 | 1. Go to your **Supabase Dashboard β SQL Editor** |
| 131 | 2. Run `supabase_setup.sql` to create the required functions and columns |
| 132 | 3. Make sure these tables exist: |
| 133 | - `users` (id, phone_number, profile_completed, created_at) |
| 134 | - `farmer_profiles` (id, user_id, farmer_name, village, district, state, usage_type, crop_name, watering_frequency, acres, land_length, land_width, latitude, longitude, location, polygon, agro_polygon_id, created_at) |
| 135 | - `predictions` (id, farmer_id, user_id, disease, confidence, container_a_ml, container_b_ml, container_c_ml, mix_time_seconds, created_at) |
| 136 | - `soil_logs` (id, device_id, moisture, ph, nitrogen, phosphorus, potassium, created_at) |
| 137 | |
| 138 | ### 6. Run the Application |
| 139 | |
| 140 | ```bash |
| 141 | uvicorn main:app --reload --host 0.0.0.0 --port 8000 |
| 142 | ``` |
| 143 | |
| 144 | Open **http://localhost:8000** in your browser. |
| 145 | |
| 146 | --- |
| 147 | |
| 148 | ## π Authentication Flow |
| 149 | |
| 150 | ``` |
| 151 | User enters phone number |
| 152 | β |
| 153 | βΌ |
| 154 | User exists? ββNoβββΊ Create new user |
| 155 | β β |
| 156 | βΌ βΌ |
| 157 | Store user_id in session |
| 158 | β |
| 159 | βΌ |
| 160 | profile_completed? |
| 161 | β β |
| 162 | Yes No |
| 163 | β β |
| 164 | βΌ βΌ |
| 165 | /dashboard /onboarding |
| 166 | ``` |
| 167 | |
| 168 | - **No OTP required** β phone-number-only login for simplicity |
| 169 | - **Session-based auth** β 24-hour cookie via Starlette SessionMiddleware |
| 170 | - **Protected routes** β All API endpoints require valid session |
| 171 | |
| 172 | --- |
| 173 | |
| 174 | ## πΎ Onboarding Logic |
| 175 | |
| 176 | The onboarding form adapts based on **usage_type**: |
| 177 | |
| 178 | | Field | Field (Farm) | Garden | |
| 179 | |---|:---:|:---:| |
| 180 | | farmer_name | β | β | |
| 181 | | village | β | β | |
| 182 | | district | β | β | |
| 183 | | state | β | β | |
| 184 | | crop_name | β | β | |
| 185 | | watering_frequency | β | β | |
| 186 | | acres | β Required | β | |
| 187 | | land_length | β | β Required | |
| 188 | | land_width | β | β Required | |
| 189 | | GPS (lat/lon) | β Required | β | |
| 190 | |
| 191 | For **Field** type, a square polygon is generated centered at the GPS coordinates with area equal to the specified acres, stored as PostGIS `GEOGRAPHY`. |
| 192 | |
| 193 | --- |
| 194 | |
| 195 | ## π°οΈ Satellite & Soil Monitoring |
| 196 | |
| 197 | When a farmer registers a **Field** with GPS coordinates: |
| 198 | |
| 199 | 1. A polygon is generated using `acres β square meters β degree offset` |
| 200 | 2. The polygon is registered with **Agromonitoring API** |
| 201 | 3. Dashboard shows: |
| 202 | - **NDVI** (Normalized Difference Vegetation Index) β crop health indicator |
| 203 | - **Soil temperature & moisture** β from satellite data |
| 204 | - **Satellite imagery** β recent captures of the field |
| 205 | |
| 206 | --- |
| 207 | |
| 208 | ## π‘ API Endpoints |
| 209 | |
| 210 | ### Auth |
| 211 | | Method | Endpoint | Description | |
| 212 | |---|---|---| |
| 213 | | `GET` | `/login` | Login page | |
| 214 | | `POST` | `/api/login` | Phone number login | |
| 215 | | `POST` | `/api/logout` | Clear session | |
| 216 | | `GET` | `/api/me` | Current user info | |
| 217 | |
| 218 | ### Onboarding |
| 219 | | Method | Endpoint | Description | |
| 220 | |---|---|---| |
| 221 | | `GET` | `/onboarding` | Onboarding form page | |
| 222 | | `POST` | `/api/onboarding` | Submit farmer profile | |
| 223 | |
| 224 | ### Dashboard & Pages |
| 225 | | Method | Endpoint | Description | |
| 226 | |---|---|---| |
| 227 | | `GET` | `/dashboard` | Main dashboard | |
| 228 | | `GET` | `/` | Scan page (crop disease detection) | |
| 229 | | `GET` | `/history` | Scan history | |
| 230 | |
| 231 | ### AI Diagnosis |
| 232 | | Method | Endpoint | Description | |
| 233 | |---|---|---| |
| 234 | | `POST` | `/api/init-session` | Initialize scan session | |
| 235 | | `POST` | `/api/upload-image` | Upload leaf image | |
| 236 | | `POST` | `/api/set-plant-type` | Set plant name | |
| 237 | | `POST` | `/api/diagnose` | Run AI diagnosis | |
| 238 | | `POST` | `/api/generate-recipe` | Generate treatment recipe | |
| 239 | |
| 240 | ### Weather & Monitoring |
| 241 | | Method | Endpoint | Description | |
| 242 | |---|---|---| |
| 243 | | `GET` | `/api/weather` | Local weather data | |
| 244 | | `POST` | `/api/update-location` | Update GPS location | |
| 245 | | `POST` | `/api/agromonitoring/create-polygon` | Register field polygon | |
| 246 | | `GET` | `/api/agromonitoring/satellite` | Satellite imagery | |
| 247 | | `GET` | `/api/agromonitoring/ndvi` | NDVI vegetation data | |
| 248 | | `GET` | `/api/agromonitoring/soil` | Soil temperature & moisture | |
| 249 | |
| 250 | ### Stats |
| 251 | | Method | Endpoint | Description | |
| 252 | |---|---|---| |
| 253 | | `GET` | `/api/stats` | User scan statistics | |
| 254 | | `GET` | `/healthz` | Health check | |
| 255 | |
| 256 | ### Market Help (Voice AI) |
| 257 | | Method | Endpoint | Description | |
| 258 | |---|---|---| |
| 259 | | `GET` | `/market-help` | Market Help page | |
| 260 | | `POST` | `/api/market-help` | Voice/text assistant (Sarvam AI proxy) | |
| 261 | |
| 262 | --- |
| 263 | |
| 264 | ## βοΈ Deployment on Render |
| 265 | |
| 266 | ### Automatic (Recommended) |
| 267 | |
| 268 | 1. Push your code to GitHub |
| 269 | 2. Go to [Render Dashboard](https://dashboard.render.com/) |
| 270 | 3. Click **New β Web Service** |
| 271 | 4. Connect your GitHub repo |
| 272 | 5. Render will auto-detect `render.yaml` |
| 273 | 6. Add environment variables in Render dashboard: |
| 274 | - `SUPABASE_URL` |
| 275 | - `SUPABASE_KEY` |
| 276 | - `GEMINI_API_KEY` |
| 277 | - `OPENWEATHER_API_KEY` |
| 278 | - `AGROMONITORING_API_KEY` |
| 279 | - `SARVAM_API_KEY` |
| 280 | - `SESSION_SECRET_KEY` (auto-generated) |
| 281 | - `ENVIRONMENT` = `production` |
| 282 | 7. Deploy! |
| 283 | |
| 284 | ### Manual Settings |
| 285 | |
| 286 | | Setting | Value | |
| 287 | |---|---| |
| 288 | | **Runtime** | Python | |
| 289 | | **Build Command** | `pip install -r requirements.txt` | |
| 290 | | **Start Command** | `uvicorn main:app --host 0.0.0.0 --port $PORT` | |
| 291 | | **Python Version** | 3.13.0 | |
| 292 | |
| 293 | --- |
| 294 | |
| 295 | ## π Security Notes |
| 296 | |
| 297 | - `.env` file is **gitignored** β never committed to version control |
| 298 | - All API keys are loaded from environment variables |
| 299 | - Session cookies expire after 24 hours |
| 300 | - Protected endpoints require authenticated session |
| 301 | - Supabase RPC functions use `SECURITY DEFINER` for PostGIS operations |
| 302 | - API docs (`/docs`) are **disabled in production** |
| 303 | |
| 304 | --- |
| 305 | |
| 306 | ## π± Mobile Compatibility |
| 307 | |
| 308 | All pages are fully responsive and optimized for mobile: |
| 309 | - Touch-friendly buttons and forms |
| 310 | - Responsive grid layouts that stack on small screens |
| 311 | - GPS capture works on mobile browsers |
| 312 | - Camera upload supported for AI scanning |
| 313 | |
| 314 | --- |
| 315 | |
| 316 | ## πΊοΈ Roadmap |
| 317 | |
| 318 | - [ ] OTP verification for phone login |
| 319 | - [ ] Multi-language support (Hindi, Telugu, Tamil, etc.) |
| 320 | - [ ] Push notifications for disease alerts |
| 321 | - [ ] IoT sensor integration for real-time soil monitoring |
| 322 | - [x] ESP32-CAM integration with MQTT-triggered image capture |
| 323 | - [ ] Crop calendar with scheduled reminders |
| 324 | - [ ] Marketplace for farming supplies |
| 325 | - [ ] Offline mode with PWA support |
| 326 | |
| 327 | --- |
| 328 | |
| 329 | ## π€ Contributing |
| 330 | |
| 331 | 1. Fork the repository |
| 332 | 2. Create a feature branch (`git checkout -b feature/amazing-feature`) |
| 333 | 3. Commit your changes (`git commit -m 'Add amazing feature'`) |
| 334 | 4. Push to the branch (`git push origin feature/amazing-feature`) |
| 335 | 5. Open a Pull Request |
| 336 | |
| 337 | --- |
| 338 | |
| 339 | ## π License |
| 340 | |
| 341 | This project is licensed under the MIT License β see the [LICENSE](LICENSE) file for details. |
| 342 | |
| 343 | --- |
| 344 | |
| 345 | ## π¨βπ» Authors |
| 346 | |
| 347 | **AGRI-SENTINEL Team** |
| 348 | |
| 349 | --- |
| 350 | |
| 351 | <div align="center"> |
| 352 | <br> |
| 353 | <img src="https://img.shields.io/badge/Made%20with-β€οΈ%20for%20Indian%20Farmers-green?style=for-the-badge" alt="Made for Indian Farmers"> |
| 354 | <br><br> |
| 355 | <b>πΎ Empowering farmers with AI-driven precision agriculture πΎ</b> |
| 356 | </div> |
| 357 |
357 lines Β· 11,946 bytesLast updated 6 hours ago