# ESP32 WiFi Serial Bridge

This project implements a transparent bridge between a hardware Serial (UART) port on an ESP32 and a TCP socket over WiFi. It includes a desktop Python script to discover the bridge and map the network connection to a local Virtual Serial Port (e.g., using `com0com` on Windows).

## Features

- **Automatic Discovery**: The desktop script finds ESP32 bridges on the local network via UDP broadcast.
- **Transparent Bridging**: Bidirectional data transfer between ESP32 UART and a TCP client.
- **Configurable**: WiFi, UART settings, and Port configuration via text files on the ESP32.
- **Robust**: Handles reconnections and errors gracefully.

## Project Structure

- `mpy/`: MicroPython firmware and configuration files for the ESP32.
- `desktop/`: Python script to run on your PC to interface with the bridge.

---

## 1. ESP32 Software Setup (MicroPython)

### Prerequisites
- ESP32 development board.
- [MicroPython firmware](https://micropython.org/download/esp32/) flashed to the specific device.
- A tool to upload files to ESP32 (e.g., [Thonny](https://thonny.org/), `ampy`, or `rshell`).

### Configuration Files

Create the following files in the `mpy/` folder (or edit the provided samples) and upload them to the root of your ESP32.

1.  **`wifi.config`** (WiFi Credentials)
    - Create a text file with two lines:
    ```text
    YOUR_WIFI_SSID
    YOUR_WIFI_PASSWORD
    ```

2.  **`uart.config`** (Hardware Configuration)
    - Key-value pairs for UART and TCP settings:
    ```ini
    UART_ID=2
    UART_BAUD=115200
    UART_TX=17
    UART_RX=16
    TCP_PORT=8888
    NEOPIXEL_PIN=48
    ```
    - Adjust `UART_TX`, `UART_RX`, and `NEOPIXEL_PIN` according to your board's pinout.

3.  **`webrepl_cfg.py`** (Optional Remote Access)
    - To enable WebREPL:
    ```python
    PASS = 'your_webrepl_password'
    ```

### Installation
1.  Upload `boot.py`, `main.py`, `wifi.config`, `uart.config`, and `webrepl_cfg.py` to the ESP32.
2.  Reset the board. The LED (if configured) will indicate connection status.

---

## 2. ESP32 Hardware Setup

### Prerequisites
- ESP32 S3 or similar board which runs micropython.
- (Optional, see connection details below) 12V to 5V DC Voltage convertor.

We need to connect the ESP32 to the controller SOC device (Arduino Nano) we want to control via UART. The ESP32 will act as a bridge between the device and the WiFi network. The onboard UART-USB chip will not work and the direct serial link WILL work fine.

If you want to use only a 12V voltage source, we can use a DC2DC Voltage convertor module as the one in the picture to provide 5V voltage to both the controller SOC and the ESP32.

The circuits we need are shown in the picture below.

![Circuit Diagram](img/hardware_connections.jpg)

Basicly, we need the following connections:
1. Arduino Nano D1 TX to ESP32 RX (PIN17)
2. Arduino Nano D0 RX to ESP32 TX (PIN16)
3. 12V Power+ to DC2DC Voltage convertor module IN+
4. GND to DC2DC Voltage convertor module IN-(GND), from Arduino Nano GND
5. DC2DC Voltage convertor module OUT 5V+ to Arduino Nano 5Vin
6. DC2DC Voltage convertor module OUT 5V+ to ESP32 5Vin
7. DC2DC Voltage convertor module OUT-(GND) to ESP32 GND

***Use only 12V power supply for the whole system, you don't want 2 USB power supplies in the same system.*** But as the ESP32 has diodes on USB Type-C connectors, it may be safe to plug in a USB 5V Power to ESP32 even when the 12V power supply is connected.

If you don't have a 12V to 5V DC converter, simply omit connections 3~7, but link 5V+ of Arduino Nano to 5V+ of ESP32, and GND of Arduino Nano to GND of ESP32. Plug in a USB 5V Power to any one of them to run the system.

Final wiring example:

![Wired SOCs](img/hardware_installation_sample.jpg)

---

## 3. Desktop Setup

### Prerequisites
- Python 3.x installed.
- **Virtual Serial Port Driver** (Windows):
    - Download and install [com0com](http://com0com.sourceforge.net/).
    - This allows you to create a pair of virtual COM ports (e.g., `COM20` <-> `CNCB0`).

### Installation
1.  Navigate to the `desktop/` directory.
2.  Install dependencies:
    ```bash
    pip install -r requirements.txt
    ```
    *(Note: `pyserial` is the main requirement)*

---

## 3. Usage

### A. Discovery Mode
If you don't know the IP address of your ESP32, run the script without arguments:

```bash
python desktop/bridge.py
```

**Output:**
```text
No serial port provided. Performing discovery only...
Searching for ESP32 Bridge (AppID: *)...
Found Bridge at 192.168.1.50: Port=8888, Magic=default

Discovery Successful:
  Target:      192.168.1.50:8888
  Target IP:   192.168.1.50
  Target Port: 8888
```

### B. Use TCP/telnet connection mode with APP (taking LaserGRBL as example)

1. Go to settings of the APP

![Go to LaserGRBL Settings](img/LaserGRBL_1_settings.png)

2. Find Connection mode and set to TCP/telnet

![Find Connection mode and set to TCP/telnet](img/LaserGRBL_2_telnet_mode.png)

3. Use the discovered IP and Port to connect

![Use the discovered IP and Port to connect](img/LaserGRBL_3_connect_telnet_tcp_port.png)

### C. Bridging to a Virtual Serial Port (com0com)

1.  **Setup com0com**:
    - Open `Setup Command Prompt` for com0com.
    - Create a pair: `install PortName=COM4 PortName=COM5`
    - Close the setup.
    - `COM4` is for this bridge script.
    - `COM5` is for your target application (e.g., Serial Monitor, flashing tool).
![com0com setup](img/com0com_setup.png)
    - If you can't find/error out at the virtual COMs in the following steps, please refer to the [trouble shooting section](#troubleshooting).

2.  **Run the Bridge**:
    Replace `COM4` with your chosen virtual port:

    ```bash
    python desktop/bridge.py COM4
    ```

    The script will automatically discover the ESP32 and start bridging.

    **Configuration Options:**
    - Specify Baud Rate: `python desktop/bridge.py COM4 --baud 115200`
    - Specify Target IP (skip discovery): `python desktop/bridge.py COM4 --target-ip 192.168.1.50`
    - Verbose Logging: `python desktop/bridge.py COM4 -v`

3.  **Connect your Application**:
    - Open your target application (e.g., PuTTY, Arduino IDE).
    - Connect to the **other end** of the virtual pair (e.g., `COM5` if you used `COM4` for the bridge). For the above example, connect to COM5 in your app such as LaserGRBL.

![connect to virtual com](img/LaserGRBL_4_connect_serial_COM.png)

    - The data will now flow:
      `App <-> COM5 <-> COM4 <-> bridge.py <-> WiFi <-> ESP32 <-> UART Device`

## Troubleshooting

- **No Bridge Found**: Ensure ESP32 is powered on and connected to the same WiFi network. Check `mpy/uart.config` for the correct `TCP_PORT`.
- **Serial Errors**: Check if `com0com` drivers are signed (or use the signed version). Verify baud rates match your device. If com0com doesn't work right in your Windows 10/11, try to disable Secure Boot in your UEFI/BIOS and reboot. Or, try other virtual serial solutions with proper signature for secure boot support.
- **Connection Drops**: The ESP32 server handles one client at a time. Ensure no other script is connected.

## LED Status Indicators

The ESP32 onboard NeoPixel LED (if configured) provides visual feedback:

- **Blue Blinking**: Initialization / Connecting to WiFi.
- **Red Blinking**: Error state (e.g., WiFi connection failed).
- **Orange Flash (Briefly every 5s)**: Ready, waiting for connection.
- **Green Flash (Briefly every 5s)**: Client Connected.

