Skip to content

gagan-devv/TerraDetect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

57 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌱 TerraDetect – A Smart Agriculture Solution

Live Go Backend React Native ESP32

πŸ”— 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.


πŸ”§ Key Features

πŸ“± Mobile App (React Native + Expo)

  • οΏ½ 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)

�️ Backend (Go + MongoDB)

  • ⚑ 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

πŸ”Œ IoT Integration (ESP32)

  • πŸ“‘ 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)

πŸ€– Machine Learning

  • 🌾 Crop recommendation (23 crop types)
  • πŸ§ͺ Fertilizer suggestion (7 fertilizer types)
  • πŸ“Š Crop suitability analysis
  • ⚑ ONNX models for fast inference
  • 🎯 Confidence scores for predictions

πŸ“ Project Structure

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

πŸš€ Getting Started

🧰 Prerequisites

  • 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)

βš™οΈ Backend Setup

# 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

πŸ“± Mobile App Setup

# 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

πŸ“‘ ESP32 Setup

  1. Open sketches/esp32_terradetect.ino in Arduino IDE
  2. Install required libraries:
    • WiFiManager
    • HTTPClient
    • ArduinoJson
  3. Copy secrets.h.template to secrets.h and configure:
    • API endpoint URL
    • Device ID
    • API key
  4. Upload to ESP32 board
  5. On first boot, connect to ESP32 AP to configure WiFi

πŸ€– ML Model Training (Optional)

# 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.py

πŸ” API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - User login
  • POST /api/v1/auth/guest - Get guest token (30 min)
  • POST /api/v1/auth/refresh - Refresh access token
  • POST /api/v1/auth/logout - Logout user

Sensor Data (Authenticated)

  • GET /api/v1/sensor/latest - Get latest sensor reading
  • GET /api/v1/sensor/history - Get historical data (paginated)

Predictions (Authenticated)

  • POST /api/v1/predict/crop - Crop recommendation
  • POST /api/v1/predict/fertilizer - Fertilizer suggestion
  • POST /api/v1/predict/suitability - Crop suitability analysis

Guest Predictions (No Auth Required)

  • POST /api/v1/guest/predict/crop - Guest crop prediction
  • POST /api/v1/guest/predict/fertilizer - Guest fertilizer prediction
  • POST /api/v1/guest/predict/suitability - Guest suitability analysis

Device Management

  • POST /api/v1/device/check - Check device availability
  • POST /api/esp32 - ESP32 sensor data upload (API key required)

See docs/api.md for detailed API documentation.


πŸ”’ Security Features

  • πŸ”‘ 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)

πŸ§‘β€πŸŒΎ Use Cases

  • 🌾 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

🎯 Key Improvements in v2.0

  • βœ… 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

πŸ–ΌοΈ Preview

TerraDetect Dashboard


βš™οΈ Tech Stack

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

πŸ“¬ Contact

Developed by Gagan Ahlawat and Team TerraDetect πŸ”— https://terradetect.onrender.com

Contributions, issues, and feature requests are welcome!


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors