# Example 5: Relay Control

## Experiment Overview

Control the on/off state of a relay via GPIO digital output, enabling switching of high-power devices (lights, motors, solenoid valves, etc.). Relays are the most commonly used actuators in IoT projects.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| RELAY | GPIO25 | Relay module (energize on HIGH) |

> Relay modules typically have optocoupler isolation, HIGH level signal energizes the relay (NO contact closes).

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "relay_01",
      "name": "Relay",
      "type": 12,
      "enabled": false,
      "pins": [25],
      "params": {
        "initialState": 0
      }
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario 1: Remote On/Off Control (Platform Trigger)

**Function**: Control relay on/off via cloud platform commands

#### 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 Relay Control`
   * **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 used as action parameters |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **HIGH Level** or **LOW Level**
   * **Target Peripheral**: Select `relay_01`

3. Click **Save** button

**Test Method**:

Send via cloud platform:

* `1` or `HIGH` → Relay energizes
* `0` or `LOW` → Relay de-energizes

***

### Scenario 2: Timed Switch (Timer Trigger)

**Function**: Automatically start device at 8 AM every day

#### 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**: `Relay Timed Control`
   * **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 **Timer Trigger** | Execute by time |
   | **Timer Mode** | Select **Daily Timer** | Fixed time every day |
   | **Time Point** | `08:00` | 8:00 AM |

**Step 3: Configure Action**

1. Click **Add Action** button

2. Fill in:
   * **Action Type**: Select **HIGH Level**
   * **Target Peripheral**: Select `relay_01`

3. Click **Save** button

> 💡 **Tip**: You can create another rule to turn off the relay at 6 PM (18:00)

## Notes

1. **Safety Voltage**: When controlling mains voltage (220V) with relays, always prioritize safety and ensure proper isolation
2. **Back EMF**: Relay coils generate reverse voltage when de-energized; modules typically have built-in flyback diodes
3. **Initial State**: Recommend `initialState: 0`, default off at power-on to prevent accidental energizing
4. **Current Capability**: ESP32 GPIO outputs 3.3V/12mA, requires transistor or module to drive relay
5. **Contact Rating**: Standard relay contacts rated at 10A/250VAC, use larger relays for higher loads
