complete

Real-Time Agricultural Environmental Monitor

IoT system preventing crop loss with dual-interface monitoring and less than 50ms response time for critical alerts

Duration

2 weeks

Role

Developer

View Code

$ Tech Stack

ESP32 C++ REST API WebSocket

Problem Statement

Storage facilities for potatoes require precise environmental control. Temperature above 50°F accelerates sprouting, while high humidity causes rot. Manual monitoring misses critical changes that happen overnight.

Solution Architecture

Built a dual-interface monitoring system:

  • Local: 1.5” OLED display for at-a-glance readings
  • Remote: Mobile-responsive web dashboard
  • Integration: JSON REST API for third-party systems

Technical Implementation

Hardware Configuration

  • MCU: ESP32-WROOM-32
  • Sensor: DHT22 (±0.5°C, ±2% RH accuracy)
  • Display: SSD1306 128x64 OLED
  • Power: USB with battery backup option

Non-Blocking Event Loop

Key to achieving less than 50ms response time:

void loop() {
    // Non-blocking sensor read
    if (millis() - lastSensorRead > SENSOR_INTERVAL) {
        readSensorAsync();
        lastSensorRead = millis();
    }
    
    // Handle web requests without blocking
    server.handleClient();
    
    // Update display without blocking
    if (millis() - lastDisplayUpdate > DISPLAY_INTERVAL) {
        updateDisplayAsync();
        lastDisplayUpdate = millis();
    }
    
    // Check alerts
    checkThresholds();
}

Web Interface

Mobile-responsive dashboard with real-time updates:

// Auto-refresh data every 5 seconds
setInterval(async () => {
    const response = await fetch('/api/sensor');
    const data = await response.json();
    updateDisplay(data);
}, 5000);

API Endpoints

RESTful API for integration:

  • GET /api/sensor - Current readings
  • GET /api/history - 24-hour history
  • GET /api/stats - Min/max/average
  • POST /api/alerts - Configure thresholds

OLED Burn-in Prevention

Implemented 4-phase pixel shifting:

void shiftDisplay() {
    static uint8_t phase = 0;
    int xOffset = (phase % 2) * 2;
    int yOffset = (phase / 2) * 2;
    display.setCursor(xOffset, yOffset);
    phase = (phase + 1) % 4;
}

Features

Dual Interface: Local OLED + Web Dashboard
Real-time Monitoring: Less than 50ms response time
Historical Data: 24-hour rolling buffer
Alert System: Configurable thresholds
NTP Time Sync: Accurate timestamps
Auto-Recovery: Reconnects after WiFi loss
JSON API: Easy integration

Performance Metrics

  • Response Time: 47ms average
  • Accuracy: ±0.5°C, ±2% RH
  • Uptime: 99.8% over 30 days
  • Memory Usage: 42% RAM, 67% Flash
  • Power Consumption: 0.8W average

Real-World Impact

During testing, the system detected a humidity spike at 3 AM caused by a failed ventilation fan. The alert allowed maintenance to fix the issue before condensation formed, preventing potential loss of 500 lbs of stored potatoes.

Future Enhancements

  • LoRaWAN for extended range
  • Multiple sensor support
  • Machine learning for predictive alerts
  • Solar power option
  • Mobile app with push notifications

Resources