Skip to content

grgrant/ESP32-Sensor-Logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ESP32 Sensor Logger

A robust, battery-powered IoT sensor logging system for ESP32 microcontrollers with cloud integration via Adafruit IO. Designed for reliable long-term operation with deep sleep power management, watchdog protection, and flexible multi-output debugging.

🌟 Features

  • Low Power Operation: Deep sleep between readings with I2C power-down for extended battery life
  • Robust Error Handling: Hardware watchdog timer, automatic retries, and graceful failure recovery
  • Multi-Output Debugging: Serial, Remote (Adafruit IO), and Syslog logging - enable any combination
  • Cloud Integration: Automatic data publishing to Adafruit IO for remote monitoring
  • Modular Design: Reusable library (ESP32_IoT_Utils) for easy project development
  • Battery Monitoring: Built-in battery voltage reading and reporting
  • Easy Configuration: Single-file configuration for sensors, intervals, and debug options

πŸ“‹ Current Hardware Configuration

Primary Setup (QtPy ESP32-S3)

  • Board: Adafruit QtPy ESP32-S3 with BFF Backpack
  • Sensor: HDC302x Temperature/Humidity Sensor (via STEMMA QT)
  • Power: Battery monitoring on A2 (via voltage divider)
  • Connectivity: WiFi to Adafruit IO

Typical Readings

  • Temperature (Β°F)
  • Humidity (%)
  • Battery Voltage (V)
  • Device status and diagnostics

πŸš€ Quick Start

Prerequisites

  1. Arduino IDE (1.8.x or 2.x) with ESP32 board support

  2. Required Libraries (install via Arduino Library Manager):

    • Adafruit IO Arduino
    • Adafruit HDC302x (for temp/humidity sensor)
    • ESP32 Arduino Core
  3. Adafruit IO Account (free tier works great)

    • Sign up at io.adafruit.com
    • Create a group called "esp32-weather" (or customize in code)
    • Note your username and key

Installation

  1. Clone this repository:

    git clone https://github.com/YOUR-USERNAME/ESP32-Sensor-Logger.git
  2. Install the ESP32_IoT_Utils library:

    • Copy library/ESP32_IoT_Utils/ folder to your Arduino libraries directory:
      • Windows: Documents\Arduino\libraries\
      • Mac: ~/Documents/Arduino/libraries/
      • Linux: ~/Arduino/libraries/
    • Restart Arduino IDE
  3. Create secrets file: Create a file at ~/.secrets.h (or customize path in code) with:

    #define WIFI_SSID "your-wifi-name"
    #define WIFI_PASSWORD "your-wifi-password"
    #define ADAFRUIT_AIO_USERNAME "your-aio-username"
    #define ADAFRUIT_AIO_KEY "your-aio-key"
  4. Upload the sketch:

    • Open SensorMonitor/SensorMonitor.ino
    • Select your board: Tools β†’ Board β†’ ESP32-S3 Dev Module (or your specific board)
    • Upload!

First Run

  1. Open Serial Monitor (115200 baud)
  2. Watch the startup sequence and first reading
  3. Check Adafruit IO to see your data arriving
  4. Device will enter deep sleep after publishing

βš™οΈ Configuration

All configuration is at the top of SensorMonitor.ino:

// Debug output options (enable any combination)
#define ENABLE_SERIAL_DEBUG true    // USB Serial
#define ENABLE_REMOTE_DEBUG true    // Adafruit IO logging
#define ENABLE_SYSLOG_DEBUG false   // Network syslog

// Timing
#define SLEEP_DURATION_SECONDS 300  // 5 minutes between readings

// Hardware
#define BATTERY_PIN A2
#define BATTERY_VOLTAGE_DIVIDER 2.0

// Adafruit IO
#define AIO_GROUP "esp32-weather"

πŸ“Š Adafruit IO Dashboard Setup

Create feeds in your Adafruit IO group:

  • esp32-weather.temperature - Temperature readings
  • esp32-weather.humidity - Humidity readings
  • esp32-weather.battery - Battery voltage
  • esp32-weather.debug-log - Debug messages (optional)

Add these feeds to a dashboard with appropriate visualizations (line charts, gauges, etc.).

πŸ”‹ Battery Life

With the default 5-minute reading interval:

  • 1000mAh battery: 5-6 days
  • 2000mAh battery: 10-12 days
  • 6000mAh (4Γ—AA): 30-40 days

See docs/power_saving_guide.md for optimization tips.

πŸ“ Project Structure

ESP32-Sensor-Logger/
β”œβ”€β”€ README.md                           # This file
β”œβ”€β”€ .gitignore                          # Git ignore rules
β”œβ”€β”€ SensorMonitor/                      # Main Arduino sketch
β”‚   └── SensorMonitor.ino
β”œβ”€β”€ library/                            # Reusable library
β”‚   └── ESP32_IoT_Utils/
β”‚       β”œβ”€β”€ ESP32_IoT_Utils.h
β”‚       β”œβ”€β”€ ESP32_IoT_Utils.cpp
β”‚       └── README.md                   # Library documentation
β”œβ”€β”€ docs/                               # Additional documentation
β”‚   β”œβ”€β”€ power_saving_guide.md          # Power optimization tips
β”‚   β”œβ”€β”€ REMOTE_DEBUG_FIX.md            # Remote logging setup
β”‚   └── SYSLOG_CRASH_FIX.md            # Syslog debugging notes
β”œβ”€β”€ hardware/                           # Hardware configurations
β”‚   └── README.md                      # Supported hardware guide
└── examples/                           # Future: other sensor configs

πŸ”§ ESP32_IoT_Utils Library

The included library provides reusable classes for common ESP32 IoT tasks:

  • DebugLogger: Multi-output debug logging (Serial, Remote, Syslog)
  • PowerManager: Battery monitoring and deep sleep management
  • WiFiManager: Simplified Adafruit IO connection with timeouts
  • DataPublisher: Easy data publishing to feeds
  • WatchdogManager: Hardware watchdog timer management
  • ErrorHandler: Automatic retry logic and error recovery

See library/ESP32_IoT_Utils/README.md for complete API documentation.

🎯 Roadmap: Multi-Sensor Support

Current Status

Single sensor configuration (HDC302x on QtPy ESP32-S3)

Planned Features

  • Configuration System: Identify board and sensor setup by CPU ID or config file
  • Sensor Abstraction: Common interface for different sensor types
  • Dynamic Feed Creation: Automatic feed setup based on available sensors
  • Multiple Board Support:
    • ESP32-S3 (current)
    • ESP32-C3 (planned)
    • ESP32-WROOM (planned)
  • Sensor Library: Support for various sensors:
    • Temperature/Humidity (HDC302x, DHT22, SHT31)
    • Air Quality (BME680, SGP30)
    • Light (TSL2591, VEML7700)
    • Soil Moisture (Capacitive sensors)
    • Motion (PIR)

Implementation Ideas

Option 1: CPU ID-based configuration

uint64_t chipID = ESP.getEfuseMac();
switch (chipID) {
  case 0x123456789ABC:  // Kitchen sensor
    sensorType = HDC302x;
    location = "kitchen";
    break;
  case 0x987654321DEF:  // Garden sensor
    sensorType = SOIL_MOISTURE;
    location = "garden";
    break;
}

Option 2: Configuration file

// config.h per device
#define DEVICE_NAME "kitchen-sensor"
#define SENSOR_TYPE HDC302X
#define HAS_BATTERY true
#define SLEEP_DURATION 300

Contributions and ideas welcome! See Contributing below.

πŸ› Troubleshooting

Common Issues

"Library not found" error

  • Ensure ESP32_IoT_Utils is in your Arduino libraries folder
  • Restart Arduino IDE

Sensor not detected

  • Check I2C connections (especially for STEMMA QT)
  • Verify sensor I2C address (0x44 for HDC302x)
  • Try different I2C bus (Wire vs Wire1)

WiFi connection fails

  • Check secrets file path and credentials
  • Increase WIFI_TIMEOUT_MS value
  • Check WiFi signal strength

Device keeps resetting

  • Watchdog timeout - add more watchdog.reset() calls
  • Increase WATCHDOG_TIMEOUT_SECONDS
  • Check power supply stability

Remote debug logs not appearing

  • Ensure ENABLE_REMOTE_DEBUG is true
  • Verify debug-log feed exists in Adafruit IO
  • Check that io.run() is being called
  • See docs/REMOTE_DEBUG_FIX.md

High power consumption

  • Verify I2C power-down is working: powerMgr.powerDownI2C(&Wire1)
  • Disable remote/syslog debugging in production
  • Increase sleep duration
  • See docs/power_saving_guide.md

πŸ“– Documentation

🀝 Contributing

Contributions are welcome! Here are some ways you can help:

  1. Add sensor support: Create examples for new sensors
  2. Test on different boards: Verify compatibility with other ESP32 variants
  3. Improve documentation: Clarify instructions or add tutorials
  4. Report issues: Found a bug? Open an issue!
  5. Optimize power: Share your battery optimization discoveries

Adding a New Sensor Configuration

  1. Copy SensorMonitor/SensorMonitor.ino to examples/YourSensorName/
  2. Modify sensor initialization and reading functions
  3. Update #define AIO_GROUP to match your setup
  4. Test thoroughly and document hardware requirements
  5. Submit a pull request!

πŸ“ License

MIT License - Feel free to use and modify for your projects!

πŸ™ Credits

Built with:

Hardware from:

πŸ“¬ Contact

Questions? Suggestions? Open an issue or start a discussion!


Happy Logging! πŸ“ŠπŸŒ‘οΈπŸ’Ύ

About

Battery-powered IoT sensor logging system for ESP32 with Adafruit IO

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages