Skip to content

MakerMylo/ESPID32

Repository files navigation

ESP32 PID Temperature Controller

Production-ready ESP32 firmware for multi-zone temperature control with PID controllers, SSR outputs, and a web-based interface.

Features

Core Functionality

  • Web Interface: ESP32-hosted web UI with live updates via WebSockets
  • Temperature Probes: Full CRUD for temperature probe configuration
  • PID Controllers: Multiple independent PID controllers with time-proportional SSR control
  • Auto-Tuning: Built-in PID auto-tuning algorithm
  • Wi-Fi Management: Automatic captive portal for Wi-Fi configuration
  • Persistent Storage: All configurations stored in NVS (Non-Volatile Storage)

Safety Features

  • SSR outputs default to OFF on boot and errors
  • Watchdog timer prevents system hangs
  • Automatic SSR shutdown if sensor becomes unavailable
  • Clear separation between low-voltage logic and SSR control

Hardware Requirements

  • ESP32 development board
  • DS18B20 temperature sensors (OneWire bus)
  • AC SSRs (Solid State Relays) for heater control
  • Appropriate power supplies and isolation

Software Requirements

  • PlatformIO IDE or PlatformIO CLI
  • ESP32 Arduino framework

Installation

Initial Setup (USB Required)

  1. Clone or download this repository
  2. Open in PlatformIO
  3. Connect your ESP32 via USB
  4. Build and upload:
    pio run -t upload
  5. Monitor serial output:
    pio device monitor

Production Updates (OTA Only)

Note: This firmware is configured for production use with OTA (Over-The-Air) updates only. USB access is not available in the production environment.

To update firmware via OTA:

  1. Find your device's IP address:

    • Check the web UI header (displays IP address)
    • Check serial monitor output: WiFi connected! IP: x.x.x.x
    • Use mDNS: http://pidtool.local (then check /api/ip endpoint)
  2. Update platformio.ini:

    • Set upload_port to your device's IP address (currently set to 10.0.0.128)
    • OTA password is Printer123 (configured in upload_flags)
  3. Build and upload:

    pio run -t upload
  4. The device will automatically reboot after a successful OTA update.

OTA Configuration:

  • Hostname: pidtool (accessible via http://pidtool.local)
  • Password: Printer123
  • Port: 3232 (ArduinoOTA default)

First-Time Setup

Wi-Fi Configuration

On first boot, if no Wi-Fi credentials are stored:

  1. The ESP32 will create an Access Point:
    • SSID: PrintboxConfig
    • Password: Printer123
  2. Connect to this network from your device
  3. A captive portal will automatically open (or navigate to 192.168.4.1)
  4. Select your Wi-Fi network and enter credentials
  5. The device will reboot and connect to your network
  6. Access the web interface at the IP address shown in serial monitor

Adding Temperature Probes

  1. Navigate to the "Temperature Probes" tab
  2. Click "Add Probe"
  3. Configure:
    • Name: User-friendly identifier
    • GPIO Pin: Pin connected to OneWire bus (typically GPIO 4)
    • Pull Mode: None/Pull-up/Pull-down (usually None for OneWire)
    • Enabled: Enable/disable this probe

Note: Currently, all probes share the same OneWire bus on GPIO 4. For multiple independent buses, modify sensors.cpp.

Configuring PID Controllers

  1. Navigate to the "PID Controllers" tab
  2. Click "Add PID Controller"
  3. Configure:
    • Name: Identifier for this controller
    • Temperature Probe: Select the probe to read from
    • Setpoint: Target temperature (°C)
    • Kp, Ki, Kd: PID constants (start with defaults or use auto-tune)
    • Output GPIO Pin: Pin connected to SSR control input
    • Output Pull Mode: Usually None for SSR control
    • Control Window: Time window for time-proportional control (1-5 seconds, default 2000ms)
    • Enabled: Enable/disable this controller

PID Auto-Tuning

  1. Configure a PID controller with a probe and setpoint
  2. Ensure the heater is connected and safe to operate
  3. Click "Tune" on the PID controller
  4. The system will:
    • Heat the system to create oscillations
    • Measure oscillation characteristics
    • Calculate optimal PID constants using Ziegler-Nichols method
    • Save the tuned values automatically
  5. Tuning typically takes 2-10 minutes depending on system response

Warning: Auto-tuning will actively control the heater. Ensure proper safety measures are in place.

Architecture

Module Structure

src/
├── main.cpp           # Main entry point, initialization, and loop
├── storage.h/cpp      # NVS-based persistent storage
├── wifi_manager.h/cpp # Wi-Fi connection and captive portal
├── sensors.h/cpp     # Temperature probe management
├── pid_controller.h/cpp # PID control and auto-tuning
└── web_ui.h/cpp      # Web server and UI

Key Design Decisions

  1. Time-Proportional Control: SSRs are controlled using time-proportional output within a configurable window (1-5 seconds). This avoids high-frequency PWM that can damage SSRs and provides smooth control.

  2. Persistent Storage: All configurations use ESP32's Preferences (NVS) library for reliable non-volatile storage.

  3. Safety-First Design:

    • All SSR outputs initialize to OFF
    • Watchdog timer prevents system hangs
    • Sensor validation prevents heater operation with bad readings
    • Clear separation between control logic and output hardware
  4. Modular Architecture: Each subsystem is self-contained with clear interfaces, making maintenance and extension easier.

  5. WebSocket Updates: Live data updates use WebSockets for efficient real-time communication without polling overhead.

API Endpoints

  • GET / - Main web interface
  • GET /api/probes - List all temperature probes
  • POST /api/probe - Create/update probe
  • DELETE /api/probe?id=X - Delete probe
  • GET /api/pids - List all PID controllers
  • POST /api/pid - Create/update PID controller
  • DELETE /api/pid?id=X - Delete PID controller
  • GET /api/data - Get current temperature readings and PID states
  • POST /api/tune?id=X&action=start - Start auto-tuning
  • WebSocket /ws - Live updates (JSON messages)

Safety Considerations

  1. Electrical Safety:

    • Ensure proper isolation between low-voltage ESP32 and high-voltage AC circuits
    • Use appropriate SSRs rated for your load
    • Follow local electrical codes
  2. Thermal Safety:

    • Implement external thermal fuses/cutoffs if required
    • Monitor system behavior during auto-tuning
    • Set reasonable temperature limits in your application
  3. Software Safety:

    • Watchdog timer resets system if it hangs
    • Sensor validation prevents operation with bad readings
    • SSR defaults to OFF on all error conditions

Troubleshooting

Wi-Fi Won't Connect

  • Check serial monitor for connection errors
  • Verify credentials are correct
  • Ensure 2.4GHz Wi-Fi (ESP32 doesn't support 5GHz)
  • Try factory reset (clear NVS) if credentials are corrupted

Temperature Readings Invalid

  • Verify OneWire bus wiring (data line needs pull-up resistor, typically 4.7kΩ)
  • Check GPIO pin configuration matches hardware
  • Ensure DS18B20 sensors are properly powered
  • Verify sensor addresses if using multiple sensors

PID Not Controlling

  • Verify probe is assigned and enabled
  • Check SSR wiring and GPIO configuration
  • Ensure PID controller is enabled
  • Verify setpoint is reasonable
  • Check that probe readings are valid

Auto-Tuning Fails

  • Ensure heater is properly connected
  • Verify probe is reading correctly
  • Check that system can actually heat up (thermal mass, insulation)
  • Increase tuning timeout if system is slow to respond

OTA Update Issues

  • Finding Device IP:
    • Check web UI header (displays IP address)
    • Use mDNS: http://pidtool.local then check /api/ip endpoint
    • Check serial monitor output: WiFi connected! IP: x.x.x.x
  • Update Fails:
    • Verify device IP is correct in platformio.ini
    • Ensure device is on the same network
    • Check password is correct (Printer123)
    • Verify device is responding: curl http://<IP>/api/ip
    • OTA uploads can take 60+ seconds - be patient
  • Device Not Responding After OTA:
    • Wait 10-15 seconds for device to reboot
    • Check serial monitor for boot messages
    • Verify WiFi connection is stable

Limitations & Future Enhancements

  1. OneWire Bus: Currently all probes share GPIO 4. Future: Support multiple independent OneWire buses.

  2. Sensor Types: Currently optimized for DS18B20. Future: Support other sensor types (analog, I2C, etc.).

  3. PID Tuning: Current implementation uses simplified Ziegler-Nichols. Future: More sophisticated tuning algorithms.

  4. Web UI: Basic but functional. Future: Enhanced visualization, graphs, logging.

License

This firmware is provided as-is for use in embedded temperature control applications.

Contributing

When modifying this code:

  • Maintain safety features (SSR defaults, watchdog, sensor validation)
  • Keep modular structure
  • Test thoroughly before deployment
  • Document any hardware-specific assumptions

About

ESP32 Based PID Control

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published