# Example 9: External Interrupt

## Experiment Overview

Detect external signal changes (button press, sensor trigger, etc.) via GPIO interrupts, immediately triggering events when rising edge, falling edge or level change is detected. Compared to polling, interrupts respond faster and do not consume CPU resources.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|-----------------|
| KEY1 | GPIO14 | Button/Sensor (falling edge trigger) |

> Any interrupt-capable GPIO pin can be used, almost all ESP32 GPIOs support interrupts.

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "int_falling",
      "name": "Falling Edge Interrupt",
      "type": 19,
      "enabled": false,
      "pins": [14],
      "params": {}
    },
    {
      "id": "int_rising",
      "name": "Rising Edge Interrupt",
      "type": 18,
      "enabled": false,
      "pins": [27],
      "params": {}
    },
    {
      "id": "int_change",
      "name": "Level Change Interrupt",
      "type": 20,
      "enabled": false,
      "pins": [26],
      "params": {}
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario: Interrupt Triggered Alarm (Event Trigger)

**Function**: When falling edge interrupt is detected (button press), buzzer sounds for 0.5 seconds

#### Web Interface Configuration Steps

**Step 1: Ensure buzzer peripheral is configured**

* Peripheral ID: `buzzer_gpio`
* Type: GPIO Output

**Step 2: 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**: `Interrupt Alarm`
   * **Report Data**: ✅ Enable
   * **Enabled**: ✅ Enable

**Step 3: Configure Trigger (Event Trigger)**

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

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Event Trigger** | Respond to system events |
   | **Event Type** | `100` | GPIO interrupt event |
   | **Event Source** | `int_falling` | Falling edge interrupt peripheral ID |

**Step 4: Configure Actions (3 actions needed)**

**Action 1: Buzzer ON**

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

**Action 2: Delay 500ms**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **Delay**
   * **Action Params**: `500` (milliseconds)

**Action 3: Buzzer OFF**

1. Click **Add Action** button a third time

2. Fill in:
   * **Action Type**: Select **LOW Level**
   * **Target Peripheral**: Select `buzzer_gpio`

3. Click **Save** button

**Execution Flow**:

```
Button pressed (falling edge)
  ↓
Trigger interrupt event
  ↓
Buzzer ON (HIGH)
  ↓
Delay 500ms
  ↓
Buzzer OFF (LOW)
```

### Interrupt Type Reference

| Interrupt Type | Trigger Condition | Use Case | Type Value |
|---------------|-------------------|----------|------------|
| Rising Edge | LOW→HIGH change | Button release, sensor trigger | 18 |
| Falling Edge | HIGH→LOW change | Button press, sensor trigger | 19 |
| Level Change | Any level change | Monitor signal changes | 20 |

## Notes

1. **Debouncing**: Mechanical buttons produce bounce, FastBee has built-in 50ms debounce protection
2. **Interrupt Safety**: Time-consuming operations cannot be executed in interrupt callbacks, FastBee uses event queue for async processing
3. **Pin Restrictions**: GPIO6-11 (SPI Flash) cannot be used for interrupts
4. **Level Requirement**: ESP32 GPIO is 3.3V level, 5V signals need voltage division
5. **Wake-up Feature**: Some pins (RTC GPIO) support deep sleep interrupt wake-up
