AgroMind backend is a full-stack FastAPI-powered engine that delivers AI crop recommendations, plant disease detection, real-time IoT soil telemetry, market insights, and an LLM-powered advisory system for farmers. It integrates weather, soil, and market APIs with device-level IoT data and advanced ML models to provide actionable agricultural insights.
- Uses soil chemistry, temperature, rainfall, humidity, and market price signals.
- Supports future integration with soil sensors & weather IoT nodes.
- Upload leaf images or feed IoT camera inputs.
- Runs ML inference and returns disease + cure recommendations.
- Device registration, provisioning, and secure token-based access.
- Telemetry ingestion for soil moisture, pH, temperature, etc.
- IoT image ingestion for edge disease detection.
- OpenWeather API for weather data
- SoilGrids API for soil nutrient and texture information
- Market API (mock or real) for commodity pricing data
- Farmers ask questions via text or voice.
- Backend uses an LLM + retrieval pipeline to give precise answers.
- STT β LLM β TTS pipeline
- Returns both text and audio response.
- Clear separation: routes, services, ML logic, database, integrations
- Ready for Docker, CI/CD, and cloud deployment
- IoT-ready MQTT support (optional)
backend/
app/
api/
routes/
user.py
auth.py
crop_recommendation.py
disease.py
devices.py
iot_webhook.py
feedback.py
llm_agent.py
voice.py
integrations.py
router.py
core/
config.py
logger.py
security.py
middleware.py
integrations/
weather_client.py
soil_client.py
market_client.py
services/
user_service.py
token_service.py
storage.py
utils.py
iot_service.py
ml_services/
crop_service.py
image_service.py
models/
db_models.py
schemas.py
workers/
celery_app.py
tasks.py
main.py
git clone https://github.com/<your-username>/agromind-backend.git
cd agromind/backendpython -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activatepip install -r requirements.txtCreate a .env file using .env.example:
SECRET_KEY=<generated_key>
DATABASE_URL=postgresql://postgres:password@localhost:5432/agromind
OPENWEATHER_API_KEY=<your_key>
SOIL_API_KEY=
MARKET_API_URL=
MARKET_API_KEY=
REDIS_URL=redis://localhost:6379/0
Generate a secure key:
openssl rand -hex 32uvicorn app.main:app --reloadAPI docs available at:
http://localhost:8000/docs
http://localhost:8000/redoc
GET /health/POST /crop/recommend
{
"soil_type": "loamy",
"temperature": 28,
"humidity": 60,
"rainfall": 140
}Upload image using:
POST /disease/predict (multipart/form-data)
POST /iot/telemetry
{
"device_id": "device123",
"temperature": 28,
"moisture": 18,
"ph": 6.2
}integrations/weather_client.py
Functions:
get_current_weather(lat, lon)get_forecast(lat, lon, days)get_historical(lat, lon, start_unix)
integrations/soil_client.py
Functions:
get_soil_properties(lat, lon)
integrations/market_client.py
Functions:
get_latest_price(commodity, region)get_price_history(commodity, region)
API:
POST /agent/query
{
"user_id": 1,
"text": "What should I do if my crop leaves turn yellow?"
}
Uses retrieval + LLM pipeline to generate exact farming advice.
POST /voice/ask
audio_file + user_id
Pipeline:
- Speech-to-Text
- LLM advisory
- Text-to-Speech
- Returns audio + transcript
Uses SQLAlchemy ORM.
Tables include:
- Users
- Devices
- Telemetry
- Feedback
- Images
- TokenBlocklist (for refresh-token revocation)
Supports PostgreSQL, SQLite (dev), MySQL.
- user_service β user CRUD & auth
- token_service β JWT + refresh token system
- storage β local/S3 file upload handling
- iot_service β telemetry + device images
- ml_services β crop & disease prediction models
- utils β image validation, job utils
- ONNX/TFLite edge deployment
- Full MQTT ingestion pipeline
- Retraining automation via Celery
- Role-based access control
- Region-language support for LLM answers
- Offline-first IoT sync
Pull requests are welcome. Open an issue for feature requests or discussions.
MIT License