# Example 1: Light First LED

## Experiment Overview

Control the on/off state of an LED via GPIO digital output, the introductory experiment for embedded development. The Puzhong development board has 4 LEDs (D1-D4), connected to GPIO15, GPIO2, GPIO0, GPIO4 respectively.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| D1 | GPIO15 | LED (active low) |
| D2 | GPIO2 | LED (active low) |
| D3 | GPIO0 | LED (active low) |
| D4 | GPIO4 | LED (active low) |

> The Puzhong board LEDs use common-anode wiring, GPIO outputs LOW to light the LED.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "led_d1",
      "name": "LED-D1",
      "type": 12,
      "enabled": false,
      "pins": [15],
      "params": {
        "initialState": 0
      }
    },
    {
      "id": "led_d2",
      "name": "LED-D2",
      "type": 12,
      "enabled": false,
      "pins": [2],
      "params": {
        "initialState": 0
      }
    },
    {
      "id": "led_d3",
      "name": "LED-D3",
      "type": 12,
      "enabled": false,
      "pins": [0],
      "params": {
        "initialState": 0
      }
    },
    {
      "id": "led_d4",
      "name": "LED-D4",
      "type": 12,
      "enabled": false,
      "pins": [4],
      "params": {
        "initialState": 0
      }
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario 1: Remote Control LED (Platform Trigger)

**Function**: Control the LED through MQTT commands from the cloud platform

#### 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**: `Remote Control LED-D1`
   * **Report Data**: ✅ Enable (report execution status to cloud platform)
   * **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 |
   | **Target Peripheral** | Leave empty or select `led_d1` | Peripheral receiving the command |
   | **Operator** | Select **Match Pattern** | Trigger when received data equals comparison value |
   | **Comparison Value** | `{"cmd":"on"}` | Command content to match |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in action configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Action Type** | Select **LOW Level** | GPIO output LOW, lights up LED |
   | **Target Peripheral** | Select `led_d1` | Peripheral ID to control |
   | **Execution Mode** | Select **Synchronous** | Wait for action to complete |

3. Click **Save** button

**Test Method**:

Send MQTT message via cloud platform:

```json
{"cmd":"on"}
```

***

### Scenario 2: Timed LED Blink (Timer Trigger)

**Function**: Make LED blink once per second

#### Web Interface Configuration Steps

**Step 1: Create Rule**

1. Click **<i class="fas fa-plus"></i> New Rule**
2. Fill in basic configuration:
   * **Rule Name**: `LED-D1 Blink`
   * **Report Data**: ❌ Disable (no need to report)
   * **Enabled**: ✅ Enable

**Step 2: Configure Trigger**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Timer Trigger** | Execute at time intervals |
   | **Timer Mode** | Select **Interval Timer** | Execute every X seconds |
   | **Interval Seconds** | `1` | Execute every 1 second |

**Step 3: Configure Actions (2 actions needed for blinking)**

**Action 1: Turn on LED**

1. Click **Add Action** button
2. Fill in:
   * **Action Type**: Select **LOW Level**
   * **Target Peripheral**: Select `led_d1`

**Action 2: Delay then turn off LED**

1. Click **Add Action** button again

2. Fill in:
   * **Action Type**: Select **Command Script**
   * **Script Content**:
     ```
     DELAY 500
     PERIPH led_d1 HIGH
     ```
   * **Execution Mode**: Select **Asynchronous**

3. Click **Save** button

**Execution Flow**:

```
Every 1 second
  ↓
Turn on LED (LOW level)
  ↓
Delay 500ms
  ↓
Turn off LED (HIGH level)
  ↓
Wait for next 1-second cycle
```

```json
{
  "id": "exec_led_d1",
  "name": "Remote Control LED-D1",
  "enabled": true,
  "triggers": [
    {
      "triggerType": 0,
      "triggerPeriphId": "led_d1",
      "operatorType": 0,
      "compareValue": ""
    }
  ],
  "actions": [
    {
      "targetPeriphId": "led_d1",
      "actionType": 1,
      "actionValue": ""
    }
  ]
}
```

## Notes

1. **GPIO0**: Must be pulled LOW during flashing, can be used as LED output during normal operation, but be careful during debugging
2. **GPIO2**: Some ESP32 modules have a built-in LED also connected to this pin
3. **Level Logic**: Puzhong board LEDs are active-low, opposite to the common active-high convention
4. **Current Limiting**: ESP32 GPIO outputs max 12mA, onboard LEDs already have current-limiting resistors
