Ajout prise en charge ESP-Display

This commit is contained in:
Serge NOEL
2025-12-01 13:53:54 +01:00
parent bcd88909b7
commit ae375b8fe2
26 changed files with 3945 additions and 1017 deletions

465
README.md
View File

@@ -1,77 +1,65 @@
# 🚂 Locomotive Test Bench
A comprehensive testing platform for model/scale locomotives using ESP32 (D1 Mini ESP32) and LM18200 H-Bridge motor driver. This system supports both **DC Analog** and **DCC Digital** control modes with a responsive web interface.
A comprehensive testing platform for model/scale locomotives using **ESP32-2432S028R** (ESP32 with ILI9341 touchscreen) and motor driver circuits. This system supports both **DC Analog** and **DCC Digital** control modes with an intuitive touchscreen interface.
## Features
## Features
### Control Modes
- **DC Analog Mode**: Traditional PWM-based speed control with bidirectional operation
- **DCC Digital Mode**: Full DCC protocol support for digital model locomotives
- 128-step speed control
- Function control (F0-F12)
- Function control (F0-F28 capable)
- Short and long address support (1-10239)
### WiFi Capabilities
- **Access Point Mode**: Create a standalone WiFi network
- **Client Mode**: Connect to existing WiFi networks
- Automatic reconnection in client mode
- Runtime WiFi configuration via web interface
### Track Configuration
- **2-Rail Mode**: Standard two-rail DC/DCC operation
- **3-Rail Mode**: Center rail configuration with relay switching
### Web Interface
- Responsive Bootstrap-based design
- Real-time status monitoring
- Speed control with visual slider
- Direction control (forward/reverse)
- Emergency stop button
- DCC address configuration
- Function button controls (F0-F12) for DCC mode
- WiFi settings management
- Mobile-friendly design
### Touchscreen Interface
- **320x240 ILI9341 TFT Display** with resistive touch
- Power ON/OFF control with visual indicators
- Mode switching (DCC/Analog) with automatic power-off safety
- Interactive speed slider (0-100%)
- Direction control (Forward/Reverse)
- Rail configuration selector (2-rail/3-rail)
- Real-time status display
- Persistent settings (saved to ESP32 NVS)
## Hardware Requirements
### Safety Features
- **Automatic power-off** when switching between DCC and Analog modes
- Emergency stop via power button
- Configuration persistence across reboots
### Components
- **ESP32 D1 Mini** (or compatible ESP32 board)
- **LM18200 H-Bridge Motor Driver**
- **2x WS2812 RGB LEDs** (for status indication)
## 🔧 Hardware Requirements
### Main Components
- **ESP32-2432S028R Module** (ESP32 with built-in ILI9341 touchscreen)
- **Motor Driver** (LM18200, L298N, or similar)
- **DCC Booster Circuit** (for DCC mode)
- **Relay Module** (5V single-channel for 2-rail/3-rail switching)
- **Power Supply**: Suitable for your locomotive scale (typically 12-18V)
- Model locomotive (DC or DCC compatible)
### ESP32-2432S028R Module Specifications
- **MCU**: ESP32-WROOM-32
- **Display**: 2.8" ILI9341 TFT (320x240)
- **Touch**: XPT2046 Resistive Touch Controller
- **Built-in**: USB-C, MicroSD slot, RGB LED
### Pin Connections
#### LM18200 Motor Driver (DC Analog Mode)
| LM18200 Pin | ESP32 Pin | Description |
|-------------|-----------|-------------|
| PWM | GPIO 25 | PWM speed control |
| DIR | GPIO 26 | Direction control |
| BRAKE | GPIO 27 | Brake control (active low) |
| OUT1 | - | Motor terminal 1 |
| OUT2 | - | Motor terminal 2 |
| Vcc | 5V | Logic power |
| GND | GND | Ground |
See **[WIRING_ESP32-2432S028R.md](WIRING_ESP32-2432S028R.md)** for complete wiring diagrams and connection details.
#### DCC Signal Output
| Signal | ESP32 Pin | Description |
|--------|-----------|-------------|
| DCC A | GPIO 32 | DCC Signal A |
| DCC B | GPIO 33 | DCC Signal B (inverted) |
#### Quick Pin Reference
| Function | ESP32 GPIO | Connected To |
|----------|-----------|--------------|
| PWM/DCC_A | 18 | LM18200 PWM Input (dual purpose) |
| DIR/DCC_B | 19 | LM18200 DIR Input (dual purpose) |
| Motor Brake | 23 | LM18200 Brake Input |
| Relay Control | 4 | Relay Module Signal |
| TFT/Touch | 2,12-15,21,22 | Built-in (no wiring needed) |
#### Status LEDs (WS2812)
| LED | ESP32 Pin | Function | Colors |
|-----|-----------|----------|---------|
| Data | GPIO 4 | LED strip data | - |
| LED 0 | - | Power status | Green=ON, Red=OFF |
| LED 1 | - | Mode indicator | Blue=DCC, Yellow=Analog |
**Note**: DCC signals require appropriate signal conditioning and booster circuitry for track connection.
### Wiring Diagram Notes
1. Connect LM18200 motor outputs to track or locomotive
2. Ensure proper power supply voltage for your scale
3. DCC mode requires additional booster circuit (not included in basic schematic)
4. Use appropriate heat sinking for LM18200
## Software Setup
## 📦 Software Setup
### Prerequisites
- [Visual Studio Code](https://code.visualstudio.com/)
@@ -81,142 +69,139 @@ A comprehensive testing platform for model/scale locomotives using ESP32 (D1 Min
1. **Clone or download this project**
```bash
cd /your/projects/folder
git clone <repository-url>
cd LocomotiveTestBench
cd DCC-Bench
```
2. **Open in VS Code**
- Open VS Code
- File → Open Folder → Select `LocomotiveTestBench` folder
- File → Open Folder → Select `DCC-Bench` folder
3. **Download Bootstrap files for offline use**
3. **Install Dependencies**
- PlatformIO will automatically download required libraries:
- TFT_eSPI (Display driver)
- XPT2046_Touchscreen (Touch controller)
- ArduinoJson (Configuration)
- DCCpp (DCC protocol)
4. **Build the project**
```bash
cd data
chmod +x download_bootstrap.sh
./download_bootstrap.sh
pio run
```
Or download manually:
- [Bootstrap CSS](https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css) → `data/css/bootstrap.min.css`
- [Bootstrap JS](https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js) → `data/js/bootstrap.bundle.min.js`
4. **Upload Filesystem (LittleFS)**
- Click PlatformIO icon in sidebar
- Under PROJECT TASKS → Upload Filesystem Image
- Wait for upload to complete
5. **Upload to ESP32-2432S028R**
```bash
pio run --target upload
```
5. **Build the project**
- Select "Build" under PROJECT TASKS
6. **Upload to ESP32**
- Connect ESP32 via USB
- Click "Upload" under PROJECT TASKS
- Wait for upload to complete
7. **Monitor Serial Output** (optional)
- Click "Monitor" to see debug output
6. **Monitor Serial Output** (optional)
```bash
pio device monitor
```
- Default baud rate: 115200
## Configuration
## 🎮 Usage
### First Time Setup
### First Power-On
1. **Power on the ESP32**
- Default mode: Access Point
- SSID: `LocoTestBench`
- Password: `12345678`
1. **Connect USB-C cable** to ESP32-2432S028R
2. **Display initializes** - you should see the touchscreen UI
3. **Default state**:
- Power: OFF
- Mode: DC Analog
- Rails: 2-Rail
- Speed: 0%
2. **Connect to WiFi**
- Use phone/computer to connect to `LocoTestBench` network
- Default IP: `192.168.4.1`
### Basic Operation
3. **Access Web Interface**
- Open browser: `http://192.168.4.1`
- You should see the Locomotive Test Bench interface
#### Power Control
- Tap **[POWER]** button to toggle power ON/OFF
- Green = ON, Red = OFF
- Power must be ON for motor/DCC output
### WiFi Configuration
#### Mode Selection
- Tap **[MODE]** button to switch between DCC and DC Analog
- **⚠️ IMPORTANT**: Power automatically turns OFF when changing modes
- Cyan = DCC mode, Yellow = DC Analog mode
#### Access Point Mode (Default)
- Creates standalone network
- Default SSID: `LocoTestBench`
- Default Password: `12345678`
- IP Address: `192.168.4.1`
#### Rail Configuration
- Tap **[RAILS]** button to switch between 2-Rail and 3-Rail
- Relay activates in 3-Rail mode
- Can be changed while power is on
#### Client Mode
1. Open web interface
2. Expand "WiFi Configuration"
3. Select "Client (Connect to Network)"
4. Enter your network SSID and password
5. Click "Save & Restart"
6. Device will restart and connect to your network
7. Check serial monitor for assigned IP address
#### Speed Control
- Use the **horizontal slider** to adjust speed (0-100%)
- Drag the white knob or tap anywhere on the slider
- Real-time speed updates to motor/DCC controller
## Usage Guide
#### Direction Control
- Tap **[DIR]** button to toggle Forward/Reverse
- FWD = Forward, REV = Reverse
- Changes immediately if power is on
### DC Analog Mode
### Status Bar
The bottom status bar shows:
- Current power state
- Active mode (DCC/DC)
- Rail configuration (2-Rail/3-Rail)
- DCC address (if in DCC mode)
- Current speed and direction
1. **Select Mode**
- Click "DC Analog" button in Control Mode section
## ⚙️ Configuration
2. **Set Speed**
- Use slider to adjust speed (0-100%)
- Speed shown in large display
### Settings Persistence
All settings are automatically saved to ESP32's Non-Volatile Storage (NVS):
- Power state
- Mode selection (DCC/Analog)
- Rail configuration (2-rail/3-rail)
- Speed value
- Direction
- DCC address
- DCC functions
3. **Change Direction**
- Click "🔄 Reverse" button to toggle direction
- Arrow indicator shows current direction (→ forward, ← reverse)
Settings persist across power cycles and reboots.
4. **Emergency Stop**
- Click "⏹ STOP" button to immediately stop locomotive
### DCC Address Configuration
To change the DCC locomotive address:
1. Edit `src/main.cpp` or add a UI element
2. Default address is **3** (configurable in code)
3. Supports addresses 1-10239 (short and long addresses)
### DCC Digital Mode
### Touch Calibration
If touch response is inaccurate, adjust calibration in `include/TouchscreenUI.h`:
```cpp
#define TS_MIN_X 200 // Adjust if needed
#define TS_MAX_X 3700 // Adjust if needed
#define TS_MIN_Y 200 // Adjust if needed
#define TS_MAX_Y 3750 // Adjust if needed
```
1. **Select Mode**
- Click "DCC Digital" button in Control Mode section
- DCC sections will appear
2. **Set Locomotive Address**
- Enter DCC address (1-10239)
- Click "Set" button
- Address is saved to memory
3. **Control Speed**
- Use slider to adjust speed (0-100%)
- Direction control works same as analog mode
4. **DCC Functions**
- Function buttons (F0-F12) appear in DCC mode
- Click button to toggle function ON/OFF
- Active functions shown in darker color
## Pin Customization
## 📝 Pin Customization
To change pin assignments, edit these files:
### Motor Controller Pins
Edit `include/MotorController.h`:
```cpp
#define MOTOR_PWM_PIN 25 // Change as needed
#define MOTOR_DIR_PIN 26 // Change as needed
#define MOTOR_BRAKE_PIN 27 // Change as needed
#define MOTOR_PWM_PIN 18 // PWM speed control
#define MOTOR_DIR_PIN 19 // Direction control
#define MOTOR_BRAKE_PIN 23 // Brake control
```
### DCC Output Pins
Edit `include/DCCGenerator.h`:
```cpp
#define DCC_PIN_A 32 // Change as needed
#define DCC_PIN_B 33 // Change as needed
#define DCC_PIN_A 17 // DCC Signal A
#define DCC_PIN_B 16 // DCC Signal B (inverted)
```
### LED Indicator Pins
Edit `include/LEDIndicator.h`:
### Relay Control Pin
Edit `include/RelayController.h`:
```cpp
#define LED_DATA_PIN 4 // WS2812 data pin
#define NUM_LEDS 2 // Number of LEDs
#define RELAY_PIN 4 // 2-rail/3-rail relay control
```
## API Documentation
## 📚 API Documentation
This project includes comprehensive API documentation using Doxygen.
@@ -260,107 +245,50 @@ LocomotiveTestBench/
│ │ ├── bootstrap.min.css # Bootstrap CSS (local)
│ │ └── style.css # Custom styles
│ └── js/
│ ├── bootstrap.bundle.min.js # Bootstrap JS (local)
│ └── app.js # Application JavaScript
├── include/ # Header files
│ ├── Config.h # Configuration management
│ ├── WiFiManager.h # WiFi connectivity
## 📂 Project Structure
```
DCC-Bench/
├── platformio.ini # PlatformIO configuration (ESP32-2432S028R)
├── README.md # This file
├── ESP32-2432S028R_MIGRATION.md # Migration details
├── WIRING_ESP32-2432S028R.md # Wiring guide
├── include/ # Header files
│ ├── Config.h # Configuration management (NVS)
│ ├── MotorController.h # DC motor control
│ ├── DCCGenerator.h # DCC signal generation
│ ├── LEDIndicator.h # WS2812 LED status indicators
│ └── WebServer.h # Web server & API
└── src/ # Source files
├── main.cpp # Main application
├── Config.cpp # Configuration implementation
├── WiFiManager.cpp # WiFi implementation
│ ├── DCCGenerator.h # DCC signal generation
│ ├── RelayController.h # 2-rail/3-rail relay control
│ └── TouchscreenUI.h # Touchscreen interface
└── src/ # Source files
├── main.cpp # Main application
├── Config.cpp # Configuration implementation
├── MotorController.cpp # Motor control implementation
├── DCCGenerator.cpp # DCC implementation
├── LEDIndicator.cpp # LED indicator implementation
└── WebServer.cpp # Web server implementation
├── DCCGenerator.cpp # DCC implementation
├── RelayController.cpp # Relay control implementation
└── TouchscreenUI.cpp # UI implementation
```
## API Reference
## 🔧 Troubleshooting
### REST API Endpoints
#### GET /api/status
Returns current system status
```json
{
"mode": "dcc",
"speed": 50,
"direction": 1,
"dccAddress": 3,
"ip": "192.168.4.1",
"wifiMode": "ap"
}
```
#### POST /api/mode
Set control mode
```json
{"mode": "dcc"} // or "analog"
```
#### POST /api/speed
Set speed and direction
```json
{"speed": 75, "direction": 1}
```
#### POST /api/dcc/address
Set DCC locomotive address
```json
{"address": 1234}
```
#### POST /api/dcc/function
Control DCC function
```json
{"function": 0, "state": true}
```
#### POST /api/wifi
Configure WiFi (triggers restart)
```json
{
"isAPMode": false,
"ssid": "YourNetwork",
"password": "YourPassword"
}
```
## Troubleshooting
### Cannot Connect to WiFi AP
- Verify ESP32 has power
- Check default SSID: `LocoTestBench`
- Default password: `12345678`
- Try restarting ESP32
### Web Interface Not Loading
- Verify correct IP address (check serial monitor)
- Try `http://192.168.4.1` in AP mode
- Check if LittleFS mounted successfully (serial output)
- Ensure filesystem was uploaded (Upload Filesystem Image)
- Clear browser cache and reload
- Try different browser
### Bootstrap/CSS Not Loading
- Verify Bootstrap files are downloaded to `data/css/` and `data/js/`
- Re-run `data/download_bootstrap.sh` script
- Upload filesystem image again
- Check browser console for 404 errors
### Display Issues
- **Blank screen**: Check USB power connection, verify 5V supply
- **Touch not responding**: Adjust touch calibration values in `TouchscreenUI.h`
- **Inverted display**: Change rotation in `TouchscreenUI::begin()`
- **Wrong colors**: Verify ILI9341 driver configuration in `platformio.ini`
### Motor Not Running (DC Mode)
- Check LM18200 connections
- Verify power supply is connected
- Check pin definitions match your wiring
- Use serial monitor to verify commands are received
- Verify mode is set to "DC Analog" (yellow button)
- Check power is ON (green button)
- Verify motor controller connections
- Check pin assignments match your wiring
- Use serial monitor to verify commands
### DCC Not Working
- Verify DCC pins are correctly connected
- DCC requires proper signal conditioning/booster
- Verify mode is set to "DCC" (cyan button)
- Check power is ON
- Verify DCC booster is connected and powered
- Check DCC signal with oscilloscope (GPIO 17, 16)
- Verify DCC address matches your locomotive
- Check locomotive is DCC-compatible
- Verify correct address is programmed in locomotive
@@ -417,22 +345,57 @@ This project is provided as-is for educational and hobbyist purposes.
## Credits
- ESP32 Arduino Core
- ESPAsyncWebServer library
- Bootstrap CSS framework
- ArduinoJson library
## Support
### Relay Not Switching
- Check relay module power (5V and GND)
- Verify GPIO 4 connection to relay signal pin
- Listen for relay click when toggling 2-rail/3-rail
- Test relay with multimeter (continuity test)
### Settings Not Saving
- Check serial monitor for NVS errors
- Try factory reset (clear NVS partition)
- Verify ESP32 flash has NVS partition
### Serial Monitor Shows Errors
- Check all #include statements resolved
- Verify all libraries installed via PlatformIO
- Check for pin conflicts
- Review error messages for specific issues
## 📋 Technical Specifications
### Software
- **Platform**: PlatformIO with Arduino framework
- **Libraries**:
- TFT_eSPI (Display driver)
- XPT2046_Touchscreen (Touch controller)
- ArduinoJson (Configuration)
- DCCpp (DCC protocol from Locoduino)
- **Storage**: ESP32 NVS (Non-Volatile Storage)
### Hardware Limits
- **PWM Frequency**: 20kHz (motor control)
- **DCC Timing**: NMRA standard compliant
- **Touch**: Resistive (pressure-sensitive)
- **Display**: 320x240 pixels, 65K colors
## 🤝 Support & Contributing
For issues, questions, or contributions:
- Check serial monitor output for debugging
- Verify hardware connections
- Review pin configurations
- Check serial monitor output for debugging (115200 baud)
- Verify hardware connections match pin assignments
- Review **[WIRING_ESP32-2432S028R.md](WIRING_ESP32-2432S028R.md)**
- Check **[ESP32-2432S028R_MIGRATION.md](ESP32-2432S028R_MIGRATION.md)** for migration details
- Test with known-good locomotive
## 📄 License
This project is open source. Check repository for license details.
---
**Version**: 1.0
**Last Updated**: November 2025
**Compatible Boards**: ESP32 D1 Mini, ESP32 DevKit, other ESP32 variants
**Framework**: Arduino for ESP32
**Version**: 2.0 (ESP32-2432S028R Edition)
**Last Updated**: December 2025
**Compatible Hardware**: ESP32-2432S028R (ESP32 with ILI9341 touchscreen)
**Framework**: Arduino for ESP32 via PlatformIO