Skip to content

IMHarris/moisture-sensor-esp32

Repository files navigation

ESP32 Moisture Sensor

ESP32-based soil moisture monitoring system for garden irrigation with temperature compensation and MQTT publishing.

Features

  • Multi-sensor support: Up to 6 resistance-based moisture sensors
  • Temperature monitoring: DS18B20 sensors on dual OneWire buses
  • MQTT publishing: Real-time data to MQTT broker
  • Temperature compensation: Improves moisture reading accuracy
  • Web interface: Real-time monitoring and manual controls
  • Non-blocking architecture: State machine approach prevents interface freezing
  • Comprehensive diagnostics: OneWire bus troubleshooting and logging

Hardware

Hardware schematics and KiCad project available in separate repository: moisture-sensor-hardware

Key Components

  • ESP32 microcontroller
  • Frequency-based moisture sensors (up to 4)
  • DS18B20 temperature sensors
  • Relay control for sensor power management
  • WiFi connectivity for MQTT

Pin Configuration

  • Moisture sensor relays: GPIO 33, 5, 18, 25
  • OneWire buses: GPIO 13 (short), GPIO 14 (long)
  • Bus relay control: GPIO 12 (short), GPIO 26 (long)
  • Frequency input: GPIO 21
  • LED indicator: GPIO 2

Prerequisites

  • ESP-IDF: Version 6.0 or later
  • Python: 3.8+ (for ESP-IDF tools)
  • USB driver: CH340 driver for ESP32 communication (macOS/Windows)

Install ESP-IDF

# Follow official ESP-IDF installation guide
# https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/

# Quick start (macOS/Linux)
mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git
cd esp-idf
./install.sh esp32
. ./export.sh

Building and Flashing

Configure Environment

# Load ESP-IDF environment (run in each new terminal session)
. ~/esp/esp-idf/export.sh

Build

# Build the project
idf.py build

# Or use the convenience script (if configured)
./startmeup.sh

Flash to Device

# Flash and monitor
idf.py -p /dev/cu.usbserial-XXXX flash monitor

# Or let it auto-detect the port
idf.py flash monitor

Common Commands

idf.py build              # Build project
idf.py clean              # Clean build files
idf.py menuconfig         # Configure project settings
idf.py size               # Show memory usage
idf.py erase-flash        # Completely erase flash
idf.py build flash monitor # Build, flash, and monitor in one command

Configuration

WiFi and MQTT Settings

Currently hardcoded in source. Edit in main/network.c:

// WiFi credentials
#define WIFI_SSID "your-ssid"
#define WIFI_PASS "your-password"

// MQTT broker
#define MQTT_BROKER "mqtt://your-broker:1883"

Note: Configuration will be moved to NVS storage in future update

Temperature Sensor Configuration

Sensor addresses and names configured in main/MoistureSensor.c. Use diagnostic output to find your sensor addresses:

TempSensorConfig LONG_BUS_SENSORS[] = {
    {0x28, 0xFF, 0x64, 0x0E, 0x8E, 0x90, 0x3C, 0x5E, "Sensor1"},
    // Add your sensors here
};

Architecture

Main Components

  • MoistureSensor.c: Main application with sensor reading state machine
  • network.c/h: WiFi and MQTT connectivity
  • hardware.c/h: Hardware initialization and pin configuration
  • temperature_lib.c/h: OneWire temperature sensor handling
  • webserver.c/h: Async HTTP server for monitoring
  • onewire_custom.c/h: Native ESP-IDF OneWire implementation

Key Functions

  • readAndPublishSensorData(): Non-blocking sensor reading with frequency measurement
  • readTemperatureBus(): OneWire temperature reading with retry logic
  • convertHzToKPa(): Frequency to pressure conversion (Hz → kPa)
  • applyTemperatureCompensation(): Temperature compensation for moisture readings

Data Flow

  1. Sensor activation: Relay powers sensor, wait for stabilization
  2. Frequency measurement: Count pulses over measurement period
  3. Temperature reading: Read DS18B20 on appropriate bus
  4. Conversion: Hz → kPa with temperature compensation
  5. Publishing: Send data via MQTT
  6. Web interface: Update real-time display

Web Interface

Access at http://[ESP32_IP_ADDRESS]/ after device connects to WiFi.

Features:

  • Real-time sensor readings
  • Manual relay control
  • Pause/resume measurements
  • System diagnostics

Development Status

Current: ✅ Fully converted to native ESP-IDF (no Arduino dependencies)

Versions

  • v1.0.0-esp32 - Stable ESP32 version (working firmware before ESP32-S3 port)
    • Native ESP-IDF implementation
    • Full moisture sensor and temperature monitoring functionality
    • MQTT publishing and web interface
    • To use: git checkout v1.0.0-esp32

Recent Changes

  • Migrated from Arduino framework to ESP-IDF native APIs
  • Removed all Arduino library dependencies
  • Implemented native OneWire protocol
  • ESP-IDF 6.0 compatible

Troubleshooting

Serial Port Not Detected (macOS)

# Install CH340 driver
brew tap homebrew/cask-drivers
brew install --cask wch-ch34x-usb-serial-driver

# Restart Mac, then check for port
ls /dev/cu.*

Build Errors

# Clean and rebuild
idf.py fullclean
idf.py build

OneWire Sensor Issues

Enable diagnostic logging in temperature_lib.c to troubleshoot sensor detection and reading issues. Check serial monitor output for detailed bus diagnostics.

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

In brief: You are free to use, modify, and distribute this software, but any derivative works must also be licensed under GPL v3 and made open source.

Contributing

Issues and pull requests welcome!

Author

Ian Harris - GitHub

About

ESP32 soil moisture monitoring system with temperature compensation and MQTT

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages