Skip to content

noobosapien/soil_sensor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌱 ESP32 Soil Moisture Monitoring System

A production-style embedded IoT system built on the ESP32-WROOM-32 using ESP-IDF, featuring real-time sensing, persistent configuration, and a lightweight HTTP interface.


πŸš€ Features

  • πŸ“‘ Wi-Fi connectivity (STA mode) with event-driven handling

  • 🌐 Embedded HTTP server

    • /data endpoint returns live soil moisture in JSON
  • πŸ“Ÿ SSD1306 OLED display (I2C)

    • Custom framebuffer + bitmap font rendering
  • 🌑️ ADC-based soil moisture sensing

    • Averaged sampling for stability
  • πŸ’Ύ Non-Volatile Storage (NVS)

    • Stores Wi-Fi credentials persistently
  • βš™οΈ FreeRTOS-based execution

    • Deterministic periodic sampling loop
  • 🧠 Clean modular architecture

    • Separation of drivers, networking, storage, and application logic

πŸ—οΈ System Architecture

                +----------------------+
                |     Soil Sensor      |
                |   (Analog Output)    |
                +----------+-----------+
                           |
                           v
                +----------------------+
                |   ADC (Oneshot)      |
                |  Averaging Filter    |
                +----------+-----------+
                           |
                           v
                +----------------------+
                |   Application Logic  |
                | (main.c loop)        |
                +----+-----------+-----+
                     |           |
                     v           v
        +----------------+   +------------------+
        | OLED Display   |   | HTTP Server      |
        | (SSD1306 I2C)  |   | /data endpoint   |
        +----------------+   +------------------+
                     |
                     v
             +------------------+
             | Wi-Fi Stack      |
             | (Event Driven)   |
             +------------------+
                     |
                     v
             +------------------+
             | NVS Storage      |
             | (SSID/PASSWORD)  |
             +------------------+

πŸ“‘ API

GET /data

Returns current soil moisture:

{
  "status": "ok",
  "sensor_value": 0
}

βš™οΈ Hardware

  • ESP32-WROOM-32
  • Capacitive Soil Moisture Sensor
  • SSD1306 OLED Display (128x64, I2C)
  • Breadboard + jumper wires
  • Pull up resistors

πŸ”Œ Pin Configuration

Component ESP32 Pin
Soil Sensor GPIO34 (ADC1_CH6)
OLED SDA GPIO21
OLED SCL GPIO22

🧠 Key Technical Decisions

1. ADC Sampling Strategy

  • Uses oneshot mode instead of continuous
  • 32 samples averaged to reduce noise
  • Controlled delay ensures stable readings

2. Custom Display Driver

  • Full framebuffer implementation
  • Manual pixel control
  • Scaled bitmap font rendering (no external libs)

3. Event-Driven Wi-Fi

  • Uses ESP-IDF event loop

  • Handles:

    • Connection
    • Disconnection
    • Auth failures
  • Tracks state via FreeRTOS Event Groups

4. Persistent Storage (NVS)

  • SSID & password stored in flash
  • Abstracted via storage.c
  • Safe open/read/write pattern with fallback init

5. Lightweight HTTP Server

  • Based on esp_http_server
  • Minimal memory footprint
  • Shared state via controlled setter (set_moisture())

🧡 Runtime Flow

Boot
 β”œβ”€β”€ Initialize NVS
 β”œβ”€β”€ Store Wi-Fi credentials
 β”œβ”€β”€ Connect to Wi-Fi
 β”œβ”€β”€ Initialize ADC
 β”œβ”€β”€ Initialize OLED
 β”œβ”€β”€ Start HTTP server
 └── Loop:
      β”œβ”€β”€ Sample ADC (32 samples)
      β”œβ”€β”€ Compute moisture %
      β”œβ”€β”€ Update display
      β”œβ”€β”€ Update HTTP state
      β”œβ”€β”€ Maintain Wi-Fi connection
      └── Delay (~3.3s)

πŸ’Ύ Memory Architecture (NVS Focus)

NVS Namespace: "storage"

Key Type Description
ssid string Wi-Fi SSID
password string Wi-Fi password

Flow

storage_write_string()  β†’ nvs_set_str() β†’ nvs_commit()
storage_read_string()   β†’ nvs_get_str()
  • Lazy initialization handled internally
  • Default fallback values supported
  • Clean handle lifecycle (open β†’ use β†’ close)

πŸ”

  • βœ… Direct ESP-IDF usage
  • βœ… Understanding of embedded memory (NVS, buffers)
  • βœ… RTOS-aware design patterns
  • βœ… Driver-level hardware control
  • βœ… Networking stack integration

⚠️ Limitations / Future Improvements

  • No HTTPS (currently HTTP only)
  • No OTA updates
  • No sensor calibration curve (linear approximation used)
  • Web UI could be added (currently API only)
  • ADC calibration using eFuse could improve accuracy

πŸ§ͺ Build & Flash

Using PlatformIO:

pio run
pio run --target upload
pio device monitor

πŸ“Έ Demo

IMG_0426 IMG_0427 IMG_0428 IMG_0429 Screenshot from 2026-04-23 16-19-54 Screenshot from 2026-04-23 16-20-13

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors