# Using PlatformIO with Docker and VS Code for ESP32 Development

This guide explains how to set up a workflow where:
- VS Code is installed on your local workstation.
- Source code and PlatformIO are inside a Docker container running on a remote machine.
- The ESP32 device is physically connected to your local computer.

## Is this possible?
Yes, it is possible! You can develop for ESP32 using PlatformIO in Docker on a remote machine, while connecting the ESP32 board to your local workstation. The key is to forward the ESP32's serial port from your local machine to the Docker container on the remote machine.

## Steps to Set Up

### 1. Prepare Your Remote Machine
- Ensure Docker is installed and running on the remote machine.
- Clone your project repository (with PlatformIO sources) to the remote machine.

### 2. Create a Docker Image with PlatformIO
- Use a Dockerfile (see `.devcontainer/Dockerfile` or create your own) that installs PlatformIO CLI and any required dependencies.
- Build the Docker image:
  ```sh
  docker build -t platformio-dev .
  ```

### 3. Share Source Code
- Mount your project directory into the Docker container using Docker volumes:
  ```sh
  docker run -it --name pio-dev -v /path/to/project:/workspace easylinux/platformio-dev:1.0
  ```

### 4. Forward the ESP32 Serial Port
- **Option 1: Use `socat` to forward the serial port over SSH**
  - On your local machine, install `socat`.
  - Find your ESP32 serial device (e.g., `/dev/ttyUSB0`).
  - Forward the serial port to the remote machine:
    ```sh
    socat TCP-LISTEN:12345,reuseaddr,fork FILE:/dev/ttyUSB0,raw,echo=0
    ```
  - On the remote machine (in Docker or before entering the container), forward the TCP port to a virtual serial device:
    ```sh
    socat -d -d PTY,link=/tmp/ttyESP32,raw TCP:your.local.ip.address:12345
    ```
  - In PlatformIO, use `/tmp/ttyESP32` as the upload port.

- **Option 2: Use VS Code Remote Development**
  - Use the "Remote - SSH" extension to open the remote folder in VS Code.
  - Use the "Remote - Containers" extension to develop inside the Docker container.
  - Use the "Serial Port Forwarding" feature (if available) to forward the ESP32 port.

### 5. Configure PlatformIO
- In your `platformio.ini`, set the upload port to the forwarded device (e.g., `/tmp/ttyESP32`).
- Example:
  ```ini
  upload_port = /tmp/ttyESP32
  ```

### 6. Develop and Upload
- Edit code in VS Code (locally or via remote extensions).
- Build and upload firmware using PlatformIO in Docker.

## Notes
- Serial port forwarding may introduce some latency.
- For debugging, ensure the forwarded port supports bidirectional communication.
- You may need to adjust permissions for the serial device.

## References
- [PlatformIO Docs: Remote Development](https://docs.platformio.org/en/latest/plus/pio-remote.html)
- [VS Code Remote Development](https://code.visualstudio.com/docs/remote/remote-overview)
- [Socat Serial Port Forwarding](https://stackoverflow.com/questions/39636742/forward-serial-port-over-tcp)

---
This setup allows you to keep your development environment and sources on a remote machine, while still programming and debugging your ESP32 connected locally.