# Example 12: UART Serial Communication

## Experiment Overview

Implement serial communication between ESP32 and external devices via UART (Universal Asynchronous Receiver-Transmitter). ESP32 has 3 UART ports, FastBee supports configuring serial ports for debug output, Modbus communication or custom data exchange.

## Hardware Wiring

| UART Port | TX Pin | RX Pin | Purpose |
|-----------|--------|--------|---------|
| UART0 | GPIO1 | GPIO3 | USB debug output |
| UART1 | GPIO16 | GPIO17 | Modbus/External devices |
| UART2 | GPIO18 | GPIO19 | Extended serial port |

> UART0 is used for USB debug output by default, UART1/2 can be used to connect external devices.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "uart0",
      "name": "Serial0-Debug",
      "type": 1,
      "enabled": false,
      "pins": [1, 3],
      "params": {
        "baudRate": 115200,
        "dataBits": 8,
        "stopBits": 1,
        "parity": 0
      }
    },
    {
      "id": "uart1",
      "name": "Serial1-Modbus",
      "type": 1,
      "enabled": false,
      "pins": [16, 17],
      "params": {
        "baudRate": 9600,
        "dataBits": 8,
        "stopBits": 1,
        "parity": 0
      }
    }
  ]
}
```

### Parameter Description

| Parameter | Description | Options |
|-----------|-------------|---------|
| baudRate | Baud rate | 9600, 19200, 38400, 57600, 115200 |
| dataBits | Data bits | 7, 8 |
| stopBits | Stop bits | 1, 2 |
| parity | Parity | 0=None, 1=Odd, 2=Even |

## Peripheral Execution Linkage

### Scenario: Serial Data Send (Platform Trigger)

**Function**: Send data to external device via UART1 through cloud platform command

#### Web Interface Configuration Steps

**Step 1: Create Rule**

1. Click **Peripheral Execution** in the left menu
2. Click **<i class="fas fa-plus"></i> New Rule** button
3. Fill in basic configuration:
   * **Rule Name**: `Serial Send Data`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

1. Click **Add Trigger** button
2. Fill in trigger configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Platform Trigger** | Receive cloud platform commands |
   | **Operator** | Select **Set Mode** | Received data as action parameters |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **Serial Send**
   * **Target Peripheral**: Select `uart1`

3. Click **Save** button

**Test Method**:

Send any string via cloud platform, e.g.: `Hello UART`
Data will be sent through UART1's TX pin (GPIO16)

### UART Parameter Reference

| Parameter | Description | Options |
|-----------|-------------|---------|
| Baud Rate | Communication speed | 9600, 19200, 38400, 57600, 115200 |
| Data Bits | Number of data bits | 7, 8 |
| Stop Bits | Number of stop bits | 1, 2 |
| Parity | Parity method | None, Odd, Even |

## Notes

1. **UART0 Occupied**: UART0 (GPIO1/3) is used for USB debug, not recommended to reassign
2. **Pin Mapping**: ESP32 UART TX/RX can be mapped to any GPIO (except GPIO6-11)
3. **Voltage Matching**: ESP32 is 3.3V level, level shifting needed for 5V devices
4. **Modbus Configuration**: When used for Modbus, baud rate is typically 9600, must match slave device
5. **Buffer**: UART receive buffer defaults to 256 bytes, read promptly for large data volumes
6. **ESP32-S3**: S3's UART0 outputs via USB CDC, physical UART starts from UART1
