IMsab/mongopublic
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![Python](https://img.shields.io/badge/Python-3.11+-blue?logo=python&logoColor=white)
6![FastAPI](https://img.shields.io/badge/FastAPI-0.129-009688?logo=fastapi&logoColor=white)
7![Supabase](https://img.shields.io/badge/Supabase-PostGIS-3ECF8E?logo=supabase&logoColor=white)
8![Gemini](https://img.shields.io/badge/Google%20Gemini-AI-4285F4?logo=google&logoColor=white)
9![License](https://img.shields.io/badge/License-MIT-green)
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```
54agri-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
86git clone https://github.com/your-username/agri-sentinel.git
87cd agri-sentinel
88```
89
90### 2. Create Virtual Environment
91
92```bash
93python -m venv .venv
94
95# Windows
96.venv\Scripts\activate
97
98# macOS/Linux
99source .venv/bin/activate
100```
101
102### 3. Install Dependencies
103
104```bash
105pip install -r requirements.txt
106```
107
108### 4. Configure Environment Variables
109
110Copy the example env file and fill in your keys:
111
112```bash
113cp .env.example .env
114```
115
116Edit `.env`:
117
118```env
119SUPABASE_URL=https://your-project.supabase.co
120SUPABASE_KEY=your-supabase-anon-key
121GEMINI_API_KEY=your-gemini-api-key
122OPENWEATHER_API_KEY=your-openweather-key
123AGROMONITORING_API_KEY=your-agromonitoring-key
124SESSION_SECRET_KEY=generate-a-random-64-char-string
125ENVIRONMENT=development
126```
127
128### 5. Set Up Supabase Database
129
1301. Go to your **Supabase Dashboard β†’ SQL Editor**
1312. Run `supabase_setup.sql` to create the required functions and columns
1323. 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
141uvicorn main:app --reload --host 0.0.0.0 --port 8000
142```
143
144Open **http://localhost:8000** in your browser.
145
146---
147
148## πŸ” Authentication Flow
149
150```
151User 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
176The 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
191For **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
197When a farmer registers a **Field** with GPS coordinates:
198
1991. A polygon is generated using `acres β†’ square meters β†’ degree offset`
2002. The polygon is registered with **Agromonitoring API**
2013. 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
2681. Push your code to GitHub
2692. Go to [Render Dashboard](https://dashboard.render.com/)
2703. Click **New β†’ Web Service**
2714. Connect your GitHub repo
2725. Render will auto-detect `render.yaml`
2736. 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`
2827. 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
308All 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
3311. Fork the repository
3322. Create a feature branch (`git checkout -b feature/amazing-feature`)
3333. Commit your changes (`git commit -m 'Add amazing feature'`)
3344. Push to the branch (`git push origin feature/amazing-feature`)
3355. Open a Pull Request
336
337---
338
339## πŸ“„ License
340
341This 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