ESP32-based soil moisture monitoring system for garden irrigation with temperature compensation and MQTT publishing.
- 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 schematics and KiCad project available in separate repository: moisture-sensor-hardware
- ESP32 microcontroller
- Frequency-based moisture sensors (up to 4)
- DS18B20 temperature sensors
- Relay control for sensor power management
- WiFi connectivity for MQTT
- 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
- ESP-IDF: Version 6.0 or later
- Python: 3.8+ (for ESP-IDF tools)
- USB driver: CH340 driver for ESP32 communication (macOS/Windows)
# 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# Load ESP-IDF environment (run in each new terminal session)
. ~/esp/esp-idf/export.sh# Build the project
idf.py build
# Or use the convenience script (if configured)
./startmeup.sh# Flash and monitor
idf.py -p /dev/cu.usbserial-XXXX flash monitor
# Or let it auto-detect the port
idf.py flash monitoridf.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 commandCurrently 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
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
};MoistureSensor.c: Main application with sensor reading state machinenetwork.c/h: WiFi and MQTT connectivityhardware.c/h: Hardware initialization and pin configurationtemperature_lib.c/h: OneWire temperature sensor handlingwebserver.c/h: Async HTTP server for monitoringonewire_custom.c/h: Native ESP-IDF OneWire implementation
readAndPublishSensorData(): Non-blocking sensor reading with frequency measurementreadTemperatureBus(): OneWire temperature reading with retry logicconvertHzToKPa(): Frequency to pressure conversion (Hz → kPa)applyTemperatureCompensation(): Temperature compensation for moisture readings
- Sensor activation: Relay powers sensor, wait for stabilization
- Frequency measurement: Count pulses over measurement period
- Temperature reading: Read DS18B20 on appropriate bus
- Conversion: Hz → kPa with temperature compensation
- Publishing: Send data via MQTT
- Web interface: Update real-time display
Access at http://[ESP32_IP_ADDRESS]/ after device connects to WiFi.
Features:
- Real-time sensor readings
- Manual relay control
- Pause/resume measurements
- System diagnostics
Current: ✅ Fully converted to native ESP-IDF (no Arduino dependencies)
- 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
- Migrated from Arduino framework to ESP-IDF native APIs
- Removed all Arduino library dependencies
- Implemented native OneWire protocol
- ESP-IDF 6.0 compatible
# 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.*# Clean and rebuild
idf.py fullclean
idf.py buildEnable diagnostic logging in temperature_lib.c to troubleshoot sensor detection and reading issues. Check serial monitor output for detailed bus diagnostics.
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.
Issues and pull requests welcome!
Ian Harris - GitHub