# mearslanahmed/agriguard architecture

> Architecture diagram of the GitHub repository mearslanahmed/agriguard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mearslanahmed/agriguard
- Repository: https://github.com/mearslanahmed/agriguard

## Overview

AgriGuard lets farmers authenticate in the mobile app, submit crop images for two-stage disease classification, and receive pesticide guidance; it also provides scan history and ESP32 irrigation controls. The Express backend routes requests to domain controllers, persists user, scan, advisory, OTP, and device data in MongoDB, and proxies ML and IoT operations. The README documents several cross-service links and capabilities; unsampled implementation details are represented only where that documentation supports them.

Main entry points: [`backend/server.js`](https://github.com/mearslanahmed/agriguard/blob/main/backend/server.js) (API server), [`mobile_app/src/navigation/AppNavigator.js`](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/navigation/AppNavigator.js) (App navigation), [`mobile_app/src/screens/DetectScreen.js`](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/DetectScreen.js) (Image capture), [`backend/controllers/authController.js`](https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/authController.js) (Auth operations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_mobile["Mobile app"]
  node_navigator["App navigation<br/>[AppNavigator.js]"]
  node_authscreens["Login and registration<br/>[LoginScreen.js]"]
  node_authcontext["Session state<br/>[AuthContext.js]"]
  node_authservice["Auth requests<br/>[authService.js]"]
  node_detectscreen["Image capture<br/>[DetectScreen.js]"]
  node_detectservice["Detection requests<br/>[detectService.js]"]
  node_result["Detection results<br/>[ResultScreen.js]"]
  node_history["Scan history<br/>[HistoryScreen.js]"]
  node_profile["Profile management<br/>[ProfileScreen.js]"]
  node_editprofile["Edit profile"]
  node_home["Farm dashboard<br/>[HomeScreen.js]"]
  node_waterui["Water controls"]
end

subgraph group_backend["Express backend"]
  node_backend["API server<br/>[server.js]"]
  node_authroutes["Auth routes<br/>[authRoutes.js]"]
  node_authcontroller["Auth operations<br/>[authController.js]"]
  node_authmiddleware["JWT protection<br/>[authMiddleware.js]"]
  node_scanroutes["Scan routes<br/>[scanRoutes.js]"]
  node_scancontroller["Scan records<br/>[scanController.js]"]
  node_pesticideroutes["Advisory routes<br/>[pesticideRoutes.js]"]
  node_pesticidecontroller["Pesticide lookup"]
end

subgraph group_detection["Disease detection"]
  node_ml["Two-stage classifier<br/>[app.py]"]
end

subgraph group_data["Persistence and advice"]
  node_db[("MongoDB")]
  node_user_model["User records<br/>[User.js]"]
  node_otp_model["OTP records<br/>[OTP.js]"]
  node_scan_model["Scan records<br/>[Scan.js]"]
  node_pesticide_model["Advisory records<br/>[Pesticide.js]"]
  node_device_model["Device registry<br/>[EspDevice.js]"]
end

subgraph group_iot["IoT irrigation"]
  node_esp_routes["IoT routes<br/>[espRoutes.js]"]
  node_espcontroller["IoT proxy<br/>[espController.js]"]
  node_espservice["Device requests<br/>[espService.js]"]
  node_device{{"ESP32 devices"}}
end

node_farmer(("Farmer"))
node_email{{"Email delivery"}}

node_farmer -->|"uses"| node_navigator
node_navigator -->|"routes to"| node_authscreens
node_navigator -->|"routes to"| node_detectscreen
node_navigator -->|"routes to"| node_history
node_navigator -->|"routes to"| node_profile
node_navigator -->|"routes to"| node_editprofile
node_navigator -->|"routes to"| node_result
node_navigator -->|"routes to"| node_waterui
node_authscreens -.->|"updates session"| node_authcontext
node_authscreens -->|"submits credentials"| node_authservice
node_authservice -->|"sends auth requests"| node_backend
node_backend -->|"mounts routes"| node_authroutes
node_authroutes -->|"dispatches"| node_authcontroller
node_authroutes -->|"protects routes"| node_authmiddleware
node_authcontroller -->|"reads and writes"| node_user_model
node_authcontroller -->|"reads and writes"| node_otp_model
node_authcontroller -->|"sends OTP"| node_email
node_detectscreen -->|"requests classification"| node_detectservice
node_detectservice -->|"uploads image"| node_ml
node_detectscreen -->|"saves scan"| node_backend
node_backend -->|"mounts routes"| node_scanroutes
node_scanroutes -->|"dispatches"| node_scancontroller
node_scanroutes -->|"protects routes"| node_authmiddleware
node_scancontroller -->|"reads and writes"| node_scan_model
node_scan_model -->|"persists in"| node_db
node_detectscreen -->|"shows result"| node_result
node_result -.->|"requests advisory"| node_backend
node_backend -->|"mounts routes"| node_pesticideroutes
node_pesticideroutes -->|"dispatches"| node_pesticidecontroller
node_pesticideroutes -->|"protects routes"| node_authmiddleware
node_pesticidecontroller -->|"looks up advisory"| node_pesticide_model
node_pesticide_model -->|"persists in"| node_db
node_history -->|"loads scan history"| node_backend
node_profile -.->|"requests profile"| node_backend
node_editprofile -.->|"updates profile"| node_backend
node_backend -->|"mounts routes"| node_esp_routes
node_waterui -->|"controls irrigation"| node_backend
node_esp_routes -->|"dispatches"| node_espcontroller
node_esp_routes -->|"protects routes"| node_authmiddleware
node_espcontroller -->|"calls device operations"| node_espservice
node_espservice -->|"proxies commands"| node_device
node_espcontroller -->|"registers devices"| node_device_model
node_device_model -->|"persists in"| node_db
node_user_model -->|"persists in"| node_db
node_otp_model -->|"persists in"| node_db

click node_navigator "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/navigation/AppNavigator.js"
click node_authscreens "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/LoginScreen.js"
click node_authcontext "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/context/AuthContext.js"
click node_authservice "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/services/authService.js"
click node_detectscreen "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/DetectScreen.js"
click node_detectservice "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/services/detectService.js"
click node_result "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/ResultScreen.js"
click node_history "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/HistoryScreen.js"
click node_profile "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/ProfileScreen.js"
click node_editprofile "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/EditProfileScreen.js"
click node_home "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/HomeScreen.js"
click node_waterui "https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/WaterControlScreen.js"
click node_backend "https://github.com/mearslanahmed/agriguard/blob/main/backend/server.js"
click node_authroutes "https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/authRoutes.js"
click node_authcontroller "https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/authController.js"
click node_authmiddleware "https://github.com/mearslanahmed/agriguard/blob/main/backend/middleware/authMiddleware.js"
click node_scanroutes "https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/scanRoutes.js"
click node_scancontroller "https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/scanController.js"
click node_pesticideroutes "https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/pesticideRoutes.js"
click node_pesticidecontroller "https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/pesticideController.js"
click node_ml "https://github.com/mearslanahmed/agriguard/blob/main/flask_api/app.py"
click node_esp_routes "https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/espRoutes.js"
click node_espcontroller "https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/espController.js"
click node_espservice "https://github.com/mearslanahmed/agriguard/blob/main/backend/services/espService.js"
click node_user_model "https://github.com/mearslanahmed/agriguard/blob/main/backend/models/User.js"
click node_otp_model "https://github.com/mearslanahmed/agriguard/blob/main/backend/models/OTP.js"
click node_scan_model "https://github.com/mearslanahmed/agriguard/blob/main/backend/models/Scan.js"
click node_pesticide_model "https://github.com/mearslanahmed/agriguard/blob/main/backend/models/Pesticide.js"
click node_device_model "https://github.com/mearslanahmed/agriguard/blob/main/backend/models/EspDevice.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_navigator,node_authscreens,node_authcontext,node_authservice,node_detectscreen,node_detectservice,node_result,node_history,node_profile,node_editprofile,node_home,node_waterui toneBlue
class node_backend,node_authroutes,node_authcontroller,node_authmiddleware,node_scanroutes,node_scancontroller,node_pesticideroutes,node_pesticidecontroller toneAmber
class node_ml toneMint
class node_db,node_user_model,node_otp_model,node_scan_model,node_pesticide_model,node_device_model toneRose
class node_esp_routes,node_espcontroller,node_espservice,node_device,node_farmer,node_email toneIndigo
```

## Components

### Mobile app

- [App navigation](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/navigation/AppNavigator.js): `mobile_app/src/navigation/AppNavigator.js`
- [Login and registration](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/LoginScreen.js): `mobile_app/src/screens/LoginScreen.js`
- [Session state](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/context/AuthContext.js): `mobile_app/src/context/AuthContext.js`
- [Auth requests](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/services/authService.js): `mobile_app/src/services/authService.js`
- [Image capture](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/DetectScreen.js): `mobile_app/src/screens/DetectScreen.js`
- [Detection requests](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/services/detectService.js): `mobile_app/src/services/detectService.js`
- [Detection results](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/ResultScreen.js): `mobile_app/src/screens/ResultScreen.js`
- [Scan history](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/HistoryScreen.js): `mobile_app/src/screens/HistoryScreen.js`
- [Profile management](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/ProfileScreen.js): `mobile_app/src/screens/ProfileScreen.js`
- [Edit profile](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/EditProfileScreen.js): `mobile_app/src/screens/EditProfileScreen.js`
- [Farm dashboard](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/HomeScreen.js): `mobile_app/src/screens/HomeScreen.js`
- [Water controls](https://github.com/mearslanahmed/agriguard/blob/main/mobile_app/src/screens/WaterControlScreen.js): `mobile_app/src/screens/WaterControlScreen.js`

### Express backend

- [API server](https://github.com/mearslanahmed/agriguard/blob/main/backend/server.js): `backend/server.js`
- [Auth routes](https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/authRoutes.js): `backend/routes/authRoutes.js`
- [Auth operations](https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/authController.js): `backend/controllers/authController.js`
- [JWT protection](https://github.com/mearslanahmed/agriguard/blob/main/backend/middleware/authMiddleware.js): `backend/middleware/authMiddleware.js`
- [Scan routes](https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/scanRoutes.js): `backend/routes/scanRoutes.js`
- [Scan records](https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/scanController.js): `backend/controllers/scanController.js`
- [Advisory routes](https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/pesticideRoutes.js): `backend/routes/pesticideRoutes.js`
- [Pesticide lookup](https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/pesticideController.js): `backend/controllers/pesticideController.js`

### Disease detection

- [Two-stage classifier](https://github.com/mearslanahmed/agriguard/blob/main/flask_api/app.py): `flask_api/app.py`

### Persistence and advice

- **MongoDB**
- [User records](https://github.com/mearslanahmed/agriguard/blob/main/backend/models/User.js): `backend/models/User.js`
- [OTP records](https://github.com/mearslanahmed/agriguard/blob/main/backend/models/OTP.js): `backend/models/OTP.js`
- [Scan records](https://github.com/mearslanahmed/agriguard/blob/main/backend/models/Scan.js): `backend/models/Scan.js`
- [Advisory records](https://github.com/mearslanahmed/agriguard/blob/main/backend/models/Pesticide.js): `backend/models/Pesticide.js`
- [Device registry](https://github.com/mearslanahmed/agriguard/blob/main/backend/models/EspDevice.js): `backend/models/EspDevice.js`

### IoT irrigation

- [IoT routes](https://github.com/mearslanahmed/agriguard/blob/main/backend/routes/espRoutes.js): `backend/routes/espRoutes.js`
- [IoT proxy](https://github.com/mearslanahmed/agriguard/blob/main/backend/controllers/espController.js): `backend/controllers/espController.js`
- [Device requests](https://github.com/mearslanahmed/agriguard/blob/main/backend/services/espService.js): `backend/services/espService.js`
- **ESP32 devices**

### Other components

- **Farmer**
- **Email delivery**

## Connections

- Farmer → App navigation: uses (evidence: `README.md`)
- App navigation → Login and registration: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- App navigation → Image capture: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- App navigation → Scan history: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- App navigation → Profile management: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- App navigation → Edit profile: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- App navigation → Detection results: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- App navigation → Water controls: routes to (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- Login and registration → Session state: updates session (evidence: `mobile_app/src/screens/LoginScreen.js`)
- Login and registration → Auth requests: submits credentials (evidence: `mobile_app/src/services/authService.js`)
- Auth requests → API server: sends auth requests (evidence: `mobile_app/src/services/authService.js`)
- API server → Auth routes: mounts routes (evidence: `backend/server.js`)
- Auth routes → Auth operations: dispatches (evidence: `backend/routes/authRoutes.js`)
- Auth routes → JWT protection: protects routes (evidence: `backend/routes/authRoutes.js`)
- Auth operations → User records: reads and writes (evidence: `backend/controllers/authController.js`)
- Auth operations → OTP records: reads and writes (evidence: `backend/controllers/authController.js`)
- Auth operations → Email delivery: sends OTP (evidence: `backend/controllers/authController.js`)
- Image capture → Detection requests: requests classification (evidence: `mobile_app/src/screens/DetectScreen.js`)
- Detection requests → Two-stage classifier: uploads image (evidence: `README.md`)
- Image capture → API server: saves scan (evidence: `README.md`)
- API server → Scan routes: mounts routes (evidence: `backend/server.js`)
- Scan routes → Scan records: dispatches (evidence: `backend/routes/scanRoutes.js`)
- Scan routes → JWT protection: protects routes (evidence: `backend/routes/scanRoutes.js`)
- Scan records → Scan records: reads and writes (evidence: `backend/controllers/scanController.js`)
- Scan records → MongoDB: persists in (evidence: `README.md`)
- Image capture → Detection results: shows result (evidence: `mobile_app/src/navigation/AppNavigator.js`)
- Detection results → API server: requests advisory
- API server → Advisory routes: mounts routes (evidence: `backend/server.js`)
- Advisory routes → Pesticide lookup: dispatches (evidence: `backend/routes/pesticideRoutes.js`)
- Advisory routes → JWT protection: protects routes (evidence: `backend/routes/pesticideRoutes.js`)
- Pesticide lookup → Advisory records: looks up advisory (evidence: `backend/controllers/pesticideController.js`)
- Advisory records → MongoDB: persists in (evidence: `README.md`)
- Scan history → API server: loads scan history (evidence: `README.md`)
- Profile management → API server: requests profile
- Edit profile → API server: updates profile
- API server → IoT routes: mounts routes (evidence: `backend/server.js`)
- Water controls → API server: controls irrigation (evidence: `README.md`)
- IoT routes → IoT proxy: dispatches (evidence: `backend/routes/espRoutes.js`)
- IoT routes → JWT protection: protects routes (evidence: `backend/routes/espRoutes.js`)
- IoT proxy → Device requests: calls device operations (evidence: `backend/controllers/espController.js`)
- Device requests → ESP32 devices: proxies commands (evidence: `README.md`)
- IoT proxy → Device registry: registers devices (evidence: `backend/controllers/espController.js`)
- Device registry → MongoDB: persists in (evidence: `README.md`)
- User records → MongoDB: persists in (evidence: `README.md`)
- OTP records → MongoDB: persists in (evidence: `README.md`)
