This document describes how to integrate on-field IoT sensors (soil moisture, temperature, humidity, pH, water level, etc.) with the Krishi Sakhi platform. It covers ingestion patterns, database schema, security, advisory logic, and a minimal rollout plan.
-
HTTP Webhook (Gateway → API)
- Edge device or gateway POSTs JSON to
/api/iot/ingest. - Body signed with HMAC (X-Signature) using a per-device secret.
- Simple to deploy over HTTPS; works on 2G/4G/5G/Wi‑Fi.
- Edge device or gateway POSTs JSON to
-
MQTT (Broker ↔ Device)
- Devices publish to topics like
farms/{farmId}/sensors/{deviceId}/data. - A small subscriber service consumes the topics and writes to the DB.
- Works well for fleets; can be EMQX, HiveMQ, or Mosquitto.
- Devices publish to topics like
-
LoRaWAN via TTN/ChirpStack
- Use The Things Network/ChirpStack uplink webhooks to our
/api/iot/ttn. - Decode payloads in the integration; map to our device IDs.
- Use The Things Network/ChirpStack uplink webhooks to our
-
SensorDevice
- Links to
Farm. - Stores type (soil_moisture, temp, humidity, ph, water_level), unit, connectivity, metadata, and lastSeenAt.
- Keeps only a hash of the device API key for security.
- Links to
-
SensorReading
- Time-series values with (deviceId, at, value, unit, quality, raw).
- Indexed by (deviceId, at) for fast queries.
Example (schema intent):
- SensorDevice(id, farmId, name, type, unit, connectivity, metadata, apiKeyHash, lastSeenAt, createdAt, updatedAt)
- SensorReading(id, deviceId, at, value, unit, quality, raw)
- Endpoint:
POST /api/iot/ingest - Headers:
Content-Type: application/json,X-Signature: <hmac> - Body:
{ device_id, value, unit, at, ... } - Steps:
- Read raw body string.
- Lookup device by device_id; fetch per-device secret.
- Verify HMAC over raw body.
- Insert a SensorReading; update SensorDevice.lastSeenAt.
- Respond
{ ok: true }.
- Subscribe to
farms/+/sensors/+/data. - For each message: parse JSON → validate → insert SensorReading → update lastSeenAt.
- Deploy as a small Node service or serverless function.
- Endpoint:
POST /api/iot/ttn. - Extract
device_id,decoded_payload& timestamp. - Map to SensorDevice; create SensorReading.
- Admin flow to register SensorDevice and bind to a Farm.
- Generate device API key (display once; store only hash in DB).
- Enforce TLS (HTTPS/MQTTS). Rotate keys periodically.
- Per-device rate limits and input validation (units, ranges).
- Dashboard widgets per farm: latest sensor values, last-seen time, sparklines (24–72h).
- Alerts: threshold rules (e.g., soil_moisture < 20% → “Irrigation advisable”).
- Chat context: include latest sensor snapshot so AI answers are situational ("Soil moisture is 18% at 09:20—irrigation advisable today").
- Index
(deviceId, at)to speed reads. - Optional: TimescaleDB for larger volumes; downsample old data (1-min → 10-min → hourly).
- Gateways can buffer and batch when offline.
- DB migration: add SensorDevice & SensorReading.
- Build
/api/iot/ingestwith HMAC auth. - Admin: Register Sensor (bind to Farm), issue API key (QR code optional).
- Ingest from a test ESP32/gateway via HTTP POST.
- Dashboard: show latest soil moisture + last seen.
- Advisory: trigger low-moisture alert.
- Chat: include latest sensor snapshot in the Ask context.
{
"device_id": "sensor-soil-kerala-farmA-01",
"value": 17.6,
"unit": "%",
"at": "2025-09-30T10:17:00Z",
"battery": 3.72
}
- Works with low-power, low-bandwidth field conditions.
- Pluggable ingestion (HTTP/MQTT/LoRaWAN) with a single normalized data model.
- Secure-by-design: device keys, HMAC, TLS, rate limits.
- Directly enhances advisories and chat quality—sensor-aware guidance, not generic tips.
This roadmap is implementation-ready and aligns with our current codebase (Next.js + Prisma + PWA).
- ✅ Digital Literacy Friendly: Simple, guided user interactions
- ✅ Instant Distribution: App store deployment reaches unlimited users
- ✅ No Logistics: No physical shipping or installation required
- ✅ Rapid Updates: Software improvements delivered automatically
- ✅ No Hardware Failures: Eliminates sensor malfunction issues
- ✅ Weather Independent: Functions during monsoons and extreme weather
- ✅ Theft Resistant: No valuable hardware to steal or damage
- ✅ No Upfront Investment: Completely free to start using
- ✅ No Maintenance Costs: No battery replacement or device repairs
- ✅ Scalable Economics: Development costs shared across all users
Krishi Mitra's no-hardware approach is a deliberate architectural decision that prioritizes:
- Farmer Accessibility over technical sophistication
- Rapid Deployment over comprehensive sensing
- Cost Effectiveness over data precision
- Reliability over advanced automation
This approach ensures that the application can immediately serve small and marginal farmers in Kerala without requiring additional investment, technical expertise, or infrastructure beyond their existing smartphone.
The software-first design creates a sustainable, scalable, and accessible agricultural advisory platform that delivers real value through intelligent data processing and expert advisory systems rather than expensive hardware sensing capabilities.
Future hardware integration remains possible but will always be optional, ensuring that the core application functionality remains accessible to all farmers regardless of their economic situation or technical capabilities.