π Live Demo: https://terradetect.onrender.com
TerraDetect is a comprehensive smart farming platform that combines real-time soil monitoring, IoT sensor integration, and ML-powered crop & fertilizer recommendations. The system features a Go backend with ONNX inference, React Native mobile app with Expo, and ESP32 microcontrollers for environmental data collection.
- οΏ½ Modern Material Design 3 UI with dark mode support
- π Real-time sensor data visualization with telemetry cards
- πΎ ML-powered crop, fertilizer, and suitability predictions
- π Historical data tracking and analysis
- οΏ½ User authentication with JWT tokens
- π Guest Mode - Try predictions without registration
- οΏ½ Offline support with request queuing
- οΏ½ Cross-platform (iOS & Android)
- β‘ High-performance REST API with Gin framework
- π§ ONNX Runtime for ML inference (crop & fertilizer models)
- π JWT authentication with refresh tokens
- π₯ User and device management
- π Sensor data storage and retrieval
- π Guest token system for anonymous predictions
- π Rate limiting and security middleware
- π‘ CORS support for cross-origin requests
- π‘ WiFi connectivity with dynamic configuration
- π Device ID authentication (format: AB1234)
- π HTTPS sensor data uploads with API key validation
- πΏ Multi-sensor support:
- Soil pH
- Moisture
- Temperature & Humidity
- EC (Electrical Conductivity)
- NPK (Nitrogen, Phosphorus, Potassium)
- πΎ Crop recommendation (23 crop types)
- π§ͺ Fertilizer suggestion (7 fertilizer types)
- π Crop suitability analysis
- β‘ ONNX models for fast inference
- π― Confidence scores for predictions
TerraDetect/
βββ backend/ # Go backend server
β βββ handlers/ # API route handlers
β βββ middleware/ # Auth, rate limiting, CORS
β βββ models/ # Data models (User, Device, Sensor)
β βββ inference/ # ONNX ML inference engine
β βββ db/ # MongoDB connection
β βββ main.go # Server entry point
βββ mobile/ # React Native mobile app
β βββ app/ # Expo Router pages
β β βββ (app)/ # Authenticated screens
β β βββ (auth)/ # Auth screens (login, register, guest)
β βββ components/ # Reusable UI components
β βββ store/ # Zustand state management
β βββ lib/ # API client & utilities
β βββ assets/ # Images, icons, fonts
βββ ml/ # Machine learning models
β βββ crop-model.onnx # Crop recommendation model
β βββ fertilizer-model.onnx # Fertilizer suggestion model
β βββ *.py # Model training & conversion scripts
βββ sketches/ # ESP32 Arduino firmware
β βββ esp32_terradetect.ino
βββ docs/ # API documentation
- Backend: Go 1.21+, MongoDB 4.4+
- Mobile: Node.js 18+, npm/yarn, Expo CLI
- IoT: ESP32-WROOM-32, Arduino IDE or PlatformIO
- ML: Python 3.9+ (for model training/conversion)
# Navigate to backend directory
cd backend
# Install Go dependencies
go mod download
# Set up environment variables
cp .env.example .env
# Edit .env with your MongoDB URI and JWT secret
# Run the server
go run .The backend will start on http://localhost:8080
# Navigate to mobile directory
cd mobile
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your API URL
# Start Expo development server
npx expo start
# Scan QR code with Expo Go app (iOS/Android)
# Or press 'a' for Android emulator, 'i' for iOS simulator- Open
sketches/esp32_terradetect.inoin Arduino IDE - Install required libraries:
- WiFiManager
- HTTPClient
- ArduinoJson
- Copy
secrets.h.templatetosecrets.hand configure:- API endpoint URL
- Device ID
- API key
- Upload to ESP32 board
- On first boot, connect to ESP32 AP to configure WiFi
# Navigate to ml directory
cd ml
# Train models (if needed)
python train_crop_model.py
python train_fertilizer_model.py
# Convert to ONNX format
python export_onnx.py
# Validate ONNX models
python validate_onnx.pyPOST /api/v1/auth/register- Register new userPOST /api/v1/auth/login- User loginPOST /api/v1/auth/guest- Get guest token (30 min)POST /api/v1/auth/refresh- Refresh access tokenPOST /api/v1/auth/logout- Logout user
GET /api/v1/sensor/latest- Get latest sensor readingGET /api/v1/sensor/history- Get historical data (paginated)
POST /api/v1/predict/crop- Crop recommendationPOST /api/v1/predict/fertilizer- Fertilizer suggestionPOST /api/v1/predict/suitability- Crop suitability analysis
POST /api/v1/guest/predict/crop- Guest crop predictionPOST /api/v1/guest/predict/fertilizer- Guest fertilizer predictionPOST /api/v1/guest/predict/suitability- Guest suitability analysis
POST /api/v1/device/check- Check device availabilityPOST /api/esp32- ESP32 sensor data upload (API key required)
See docs/api.md for detailed API documentation.
- π JWT-based authentication with access & refresh tokens
- π Guest tokens with limited permissions (30-minute expiry)
- π API key validation for ESP32 devices
- π‘οΈ Rate limiting on all endpoints
- π« CORS protection with configurable origins
- π Password hashing with bcrypt
- π User-specific data isolation
- π§ Device identity validation (format: AB1234)
- πΎ Farmers: Real-time soil monitoring and crop recommendations
- π Students: Learning platform for Agri-IoT and ML applications
- π¬ Researchers: Field data collection and analysis
- π’ Agribusinesses: Precision agriculture and resource optimization
- π± Home Gardeners: Smart gardening with data-driven insights
- π₯ Consultants: Try predictions as guest without registration
- β Complete rewrite from Flask to Go for better performance
- β Native mobile app with React Native + Expo
- β Guest mode for trying predictions without registration
- β ONNX inference for faster ML predictions
- β Real-time telemetry dashboard with live sensor data
- β Offline support with request queuing
- β Material Design 3 UI with dark mode
- β Enhanced security with JWT refresh tokens
- β Better error handling and user feedback
- β Comprehensive API documentation
| Layer | Technologies |
|---|---|
| Mobile App | React Native, Expo, TypeScript, NativeWind |
| Backend | Go, Gin, MongoDB, ONNX Runtime |
| ML Models | Python, Scikit-learn, ONNX |
| IoT Hardware | ESP32, RS485 NPK sensor, analog sensors |
| Auth | JWT, bcrypt, guest tokens |
| State Mgmt | Zustand (mobile) |
| Styling | TailwindCSS (NativeWind), Material Design 3 |
| API | REST, JSON, CORS |
Developed by Gagan Ahlawat and Team TerraDetect π https://terradetect.onrender.com
Contributions, issues, and feature requests are welcome!
