Skip to content

Advanced ESP32-C3 brightness control system with zero-maintenance dynamic IP discovery, mDNS hostname support, HTTP API, and comprehensive automation features for iMac displays

Notifications You must be signed in to change notification settings

doobidoo/iMacDimmerUbuntu

Repository files navigation

╔══════════════════════════════════════════════════════════════════════════════╗
β•‘                           iMac Dimmer Ubuntu v1.7.0                          β•‘
β•‘                                                                              β•‘
β•‘    πŸš€ Advanced ESP32-C3 Brightness Control with Dynamic IP Discovery         β•‘
β•‘                                                                              β•‘
β•‘    ✨ Features:                                                              β•‘
β•‘    β€’ Zero-maintenance network discovery   β€’ Professional web interface       β•‘
β•‘    β€’ mDNS hostname support (*.local)      β€’ Complete system integration      β•‘
β•‘    β€’ HTTP API with fallback mechanisms    β€’ Keyboard shortcuts & automation  β•‘
β•‘                                                                              β•‘
β•‘    πŸ”§ Hardware: ESP32-C3 SuperMini  |  🌐 Protocol: HTTP + mDNS + WiFi       β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

iMac Dimmer Ubuntu v1.7.0

A comprehensive solution for controlling iMac display brightness using ESP32-C3 with automatic network adaptation, multiple communication methods, and robust failover mechanisms.

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    WiFi     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    PWM    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Ubuntu PC     │◄───────────►│  ESP32-C3 Mini   │◄─────────►│  iMac Display   β”‚
β”‚                 β”‚             β”‚                  β”‚           β”‚   Brightness    β”‚
β”‚ β€’ Python Script β”‚             β”‚ β€’ Web Interface  β”‚           β”‚    Control      β”‚
β”‚ β€’ Keyboard      β”‚             β”‚ β€’ HTTP API       β”‚           β”‚                 β”‚
β”‚ β€’ Systemd       β”‚             β”‚ β€’ mDNS Service   β”‚           β”‚                 β”‚
β”‚ β€’ Web Browser   β”‚             β”‚ β€’ Version Track  β”‚           β”‚                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                               β”‚
         β”‚ HTTP API                      β”‚ imacdimmer.local
         β”‚ imacdisplay.py -s 70          β”‚ http://imacdimmer.local
         β”‚                               β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Key Features

🌐 Dynamic Network Discovery

  • mDNS hostname: Access via imacdimmer.local - no IP needed
  • Automatic IP discovery: Finds ESP32 even when network changes
  • Multi-layer fallback: MAC detection, network scanning, cached addresses
  • Zero maintenance: Works across router reboots and DHCP changes

πŸ”— Dual Communication Methods

  • HTTP-based control: Robust web API with /serial endpoint
  • Modern web interface: Real-time brightness control with presets
  • ESP32-C3 compatible: Bypasses bootloader serial communication issues
  • Version tracking: Firmware verification and update management

πŸŽ›οΈ Complete System Integration

  • Keyboard shortcuts: System-wide hotkey support
  • Systemd service: Background brightness restoration
  • Auto-dimmer: Automatic brightness reduction after idle time
  • Timer automation: Scheduled brightness adjustments
  • Configuration caching: Performance optimization with smart defaults

πŸ”§ Hardware Requirements

ESP32-C3 SuperMini Board

ESP32-C3 SuperMini Development Board

Required Components:

  • ESP32-C3 SuperMini Board (shown above)
  • PCI-E 6-pin extension cable or splitter (to intercept PWM signal)
  • USB cable for ESP32
  • Basic soldering equipment
  • Wire connectors (optional, for easier connections)

Hardware Setup:

  1. Access the LCD Driver Board

    • Open the iMac and locate the LCD driver board
    • Identify the PCI-E 6-pin cable connected to the LCD
  2. Intercept the PWM Signal

    • Cut the PWM wire on your extension cable (typically bottom right pin)
    • Connect the PWM wire to GPIO3 on the ESP32
    • Ensure proper grounding between ESP32 and iMac
  3. Connect the ESP32

    • Connect ESP32 to a USB port for power and programming
    • Use GPIO3 for PWM output (brightness control)
    • GPIO8 is used for status LED
  4. Additional Setup

    • Install mbpfan for fan control: sudo apt install mbpfan
    • This prevents fan issues when intercepting the PWM signal

Board Specifications:

  • Microcontroller: ESP32-C3 (160MHz, 320KB RAM)
  • Flash Memory: 4MB
  • WiFi: 802.11 b/g/n (2.4GHz)
  • USB: USB-C for programming and power
  • Size: Ultra-compact form factor
  • GPIO: 13 digital I/O pins
  • PWM Output: GPIO3 (10kHz, 8-bit resolution)

πŸ“‚ Project Structure

iMacDimmerUbuntu/
β”œβ”€β”€ src/
β”‚   └── main.cpp                    # ESP32 firmware v1.7.0
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ imacdisplay_http.py        # Smart discovery Python script
β”‚   β”œβ”€β”€ auto_dimmer.py             # Automatic idle-time brightness dimmer
β”‚   β”œβ”€β”€ test_auto_dimmer.py        # Auto-dimmer testing suite
β”‚   β”œβ”€β”€ ping_test.py               # Network connectivity tests
β”‚   └── hybrid_test.py             # Communication diagnostics
β”œβ”€β”€ systemd/
β”‚   β”œβ”€β”€ brightness.service         # System service configuration
β”‚   └── auto-dimmer.service        # Auto-dimmer service configuration
β”œβ”€β”€ DYNAMIC_IP_SOLUTION.md         # Detailed technical documentation
β”œβ”€β”€ final_install.sh               # Automated installation script
β”œβ”€β”€ install_auto_dimmer.sh         # Auto-dimmer installation script
β”œβ”€β”€ platformio.ini                 # PlatformIO build configuration
└── README.md                      # This file

πŸš€ Quick Start

One-Command Installation

# Clone and install everything automatically
git clone https://github.com/doobidoo/iMacDimmerUbuntu.git
cd iMacDimmerUbuntu

# Set up WiFi credentials
cp .env.example .env
# Edit .env with your WiFi credentials

./final_install.sh

The installation script will:

  • βœ… Test ESP32 connectivity
  • βœ… Configure optimal communication method
  • βœ… Install system scripts with dynamic discovery
  • βœ… Set up systemd service
  • βœ… Verify complete functionality

πŸ“‹ Manual Installation Steps

1. Flash ESP32 Firmware

# Set up WiFi credentials first
cp .env.example .env
# Edit .env with your WiFi SSID and password

# Build and upload v1.7.0 firmware
~/.platformio/penv/bin/platformio run --target upload

2. Configure Network Access

# Option A: Use mDNS hostname (recommended)
python3 scripts/imacdisplay_http.py --ip imacdimmer.local

# Option B: Auto-discover ESP32
python3 scripts/imacdisplay_http.py --discover

# Option C: Manual IP configuration
python3 scripts/imacdisplay_http.py --ip 192.168.1.100

3. Install System Integration

# Install system script
sudo cp scripts/imacdisplay_http.py /usr/local/bin/imacdisplay.py
sudo chmod +x /usr/local/bin/imacdisplay.py

# Install and start service
sudo cp systemd/brightness.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable brightness.service
sudo systemctl start brightness.service

🎯 Usage Examples

Command Line Interface

# Brightness control
imacdisplay.py -s 70          # Set to 70%
imacdisplay.py -g             # Get current brightness
imacdisplay.py -i 10          # Increase by 10%
imacdisplay.py -d 10          # Decrease by 10%

# System diagnostics
imacdisplay.py -v             # Get firmware version
imacdisplay.py --ping         # Test ESP32 connectivity
imacdisplay.py --discover     # Find and save ESP32 location

# Network configuration
imacdisplay.py --ip imacdimmer.local    # Use hostname
imacdisplay.py --ip 192.168.1.27        # Use specific IP

Web Interface

🌐 Access Methods πŸŽ›οΈ Interface Features
β€’ http://imacdimmer.local (recommended)
β€’ http://[ESP32-IP-ADDRESS]
β€’ Auto-discovery enabled
β€’ 🎚️ Real-time brightness slider
β€’ 🎯 Quick preset buttons (5%, 20%, 50%, 70%, 100%)
β€’ πŸ“Š System information display
β€’ πŸ“‘ WiFi status and signal strength
β€’ πŸ”„ Firmware version verification

Keyboard Shortcuts

Configure in your desktop environment:

Action Command
Brightness Up imacdisplay.py -i 10
Brightness Down imacdisplay.py -d 10
Preset Dim imacdisplay.py -s 20
Preset Bright imacdisplay.py -s 80

Auto-Dimmer (Idle Time Control)

Automatically dims the display after a period of inactivity:

# Install and test auto-dimmer
./install_auto_dimmer.sh

# Test functionality safely
python3 scripts/test_auto_dimmer.py

# Manual control options
auto_dimmer.py --minutes 10 --level 5    # Dim to 5% after 10 minutes
auto_dimmer.py --status                   # Show current status
auto_dimmer.py --test                     # Test idle detection

# Enable as system service
sudo systemctl enable auto-dimmer.service
sudo systemctl start auto-dimmer.service

Auto-Dimmer Features:

  • πŸ• Configurable idle timeout (default: 10 minutes)
  • πŸŒ™ Safe minimum brightness (default: 5%, never completely dark)
  • πŸ‘‹ Activity detection (restores brightness when user returns)
  • πŸ’Ύ Configuration persistence (remembers settings)
  • πŸ”„ Graceful recovery (restores brightness on shutdown)

πŸ”§ Configuration

WiFi Credentials

WiFi credentials are now managed through environment variables for security:

  1. Create a .env file in the project root:

    cp .env.example .env
  2. Edit .env with your WiFi credentials:

    WIFI_SSID=YourWiFiNetwork
    WIFI_PASSWORD=YourWiFiPassword
    
  3. Build and flash - credentials are injected at build time:

    ~/.platformio/penv/bin/platformio run --target upload

Note: The .env file is gitignored to prevent accidental credential exposure.

Network Discovery Methods

The system automatically tries (in order):

  1. mDNS Hostname: imacdimmer.local
  2. Cached Address: Last known working connection
  3. ARP Table Scan: ESP32 MAC address detection
  4. Network Discovery: Intelligent local network scanning
  5. Manual Configuration: User-specified addresses

πŸ” Security Considerations

WiFi Credentials Management

  • βœ… Credentials stored in .env file (not in source code)
  • βœ… Environment variables injected at build time
  • βœ… .env file excluded from git repository
  • βœ… Example template provided (.env.example)

Important: If you've previously committed credentials to git, change your WiFi password immediately as git history retains old commits.

πŸ›‘οΈ Safety & Reliability Features

Hardware Safety

  • βœ… Minimum brightness enforcement (5%)
  • βœ… Safe startup brightness (70%)
  • βœ… PWM output protection
  • βœ… Status LED feedback

Network Resilience

  • βœ… Automatic WiFi reconnection
  • βœ… mDNS service registration
  • βœ… HTTP communication redundancy
  • βœ… Configuration caching and recovery

System Integration

  • βœ… Systemd service with auto-restart
  • βœ… Background brightness restoration
  • βœ… Non-blocking communication timeouts
  • βœ… Graceful degradation on failures

πŸ” Troubleshooting

Network Connectivity

# Test ESP32 discovery
python3 scripts/hybrid_test.py

# Test hostname resolution
ping imacdimmer.local

# Manual connectivity test
curl http://imacdimmer.local/version

System Service

# Check service status
systemctl status brightness.service

# View service logs
journalctl -u brightness.service -f

# Restart service
sudo systemctl restart brightness.service

Firmware Issues

# Check web interface
curl http://imacdimmer.local/version

# Verify firmware version
python3 scripts/imacdisplay_http.py -v

# Re-flash firmware if needed
~/.platformio/penv/bin/platformio run --target upload

🌐 Network Compatibility

βœ… Fully Compatible

  • Home networks with standard routers
  • Networks with mDNS/Bonjour support
  • Standard DHCP configurations
  • Multi-VLAN setups with local access

⚠️ Limited Compatibility

  • Corporate networks with mDNS blocked
  • Networks with restricted ARP access
  • Very strict firewall configurations

Workaround: Use manual IP configuration:

imacdisplay.py --ip [actual-esp32-ip]

πŸš€ Advanced Features

API Endpoints

The ESP32 provides a RESTful API:

# Version information
GET /version

# WiFi and system status
GET /wifistatus

# Serial command emulation
GET /serial?cmd=version
GET /serial?cmd=get
GET /serial?cmd=50

# LED control
GET /led?pin=8&state=1

# Direct brightness control
GET /brightness?level=128

Configuration Management

Settings are automatically cached in ~/.config/imacdisplay.conf:

{
  "esp32_ip": "imacdimmer.local",
  "last_brightness": 70
}

πŸ“Š Technical Specifications

Component Specification
Microcontroller ESP32-C3 (160MHz, 320KB RAM)
WiFi 802.11 b/g/n, 2.4GHz
PWM Output GPIO3, 10kHz frequency, 8-bit resolution
Communication HTTP/1.1, mDNS, WebSocket ready
Power USB-C, 3.3V operation
Flash Memory 4MB with OTA support

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly with python3 scripts/hybrid_test.py
  5. Submit a pull request

πŸ“„ License

This project is open source. See the repository for license details.

πŸ“š Hardware References

Wiring Diagrams & Guides

Safety Notes

  • Always disconnect power before working on hardware
  • Use proper grounding to prevent static damage
  • The PWM signal is typically 3.3V compatible
  • Test connections before final assembly

πŸŽ‰ Acknowledgments

  • ESP32 Community: For comprehensive hardware support
  • PlatformIO: For excellent development environment
  • mDNS/Avahi: For network service discovery
  • mbpfan Project: For iMac fan control solution

πŸ”— Links

Built with ❀️ for the ESP32 and open source communities

About

Advanced ESP32-C3 brightness control system with zero-maintenance dynamic IP discovery, mDNS hostname support, HTTP API, and comprehensive automation features for iMac displays

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •