# Example 3B: Script Flowing Light

## Experiment Overview

Use FastBee's **Command Script Action** (`actionType=15`) to create LED flowing-light effects through a small declarative sequence of `PERIPH`, `DELAY`, and `LOG` commands.

## Hardware Wiring

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

## Method 1: Basic Sequential Flowing Light

### Peripheral Execution Rule Configuration

```json
{
  "id": "rule_cmd_flow_basic",
  "name": "Command Script - Basic Flowing Light",
  "enabled": false,
  "triggers": [
    { "triggerType": 1, "timerMode": 0, "intervalSec": 3600 }
  ],
  "actions": [
    {
      "targetPeriphId": "",
      "actionType": 15,
      "actionValue": "PERIPH cmd_led1 LOW\nDELAY 300\nPERIPH cmd_led1 HIGH\nPERIPH cmd_led2 LOW\nDELAY 300\nPERIPH cmd_led2 HIGH\nPERIPH cmd_led3 LOW\nDELAY 300\nPERIPH cmd_led3 HIGH\nPERIPH cmd_led4 LOW\nDELAY 300\nPERIPH cmd_led4 HIGH\nLOG Basic flowing light cycle complete",
      "useReceivedValue": false,
      "syncDelayMs": 0,
      "execMode": 0
    }
  ],
  "protocolType": 0,
  "scriptContent": "",
  "reportAfterExec": false
}
```

### Command Script Breakdown

```
PERIPH cmd_led1 LOW      # Turn on LED1 (active-low)
DELAY 300                # Delay 300ms
PERIPH cmd_led1 HIGH     # Turn off LED1 (active-high)
PERIPH cmd_led2 LOW      # Turn on LED2
DELAY 300                # Delay 300ms
PERIPH cmd_led2 HIGH     # Turn off LED2
... (repeat sequentially)
LOG Basic flowing light cycle complete    # Print log
```

### Execution Methods

* **Manual Run**: Keep the rule disabled and click **Run Once** in the Web interface. The disabled timer is only a valid placeholder because the current schema has no manual trigger type.
* **API Call**: `POST /api/periph-exec/run` with form field `id=rule_cmd_flow_basic`

***

## Method 2: Timer Auto-Cycle Flowing Light

### Peripheral Execution Rule Configuration

```json
{
  "id": "rule_cmd_flow_timer",
  "name": "Command Script - Timer Cycle Flowing Light",
  "enabled": true,
  "triggers": [
    { "triggerType": 1, "timerMode": 0, "intervalSec": 5 }
  ],
  "actions": [
    {
      "targetPeriphId": "",
      "actionType": 15,
      "actionValue": "PERIPH cmd_led1 LOW\nDELAY 300\nPERIPH cmd_led1 HIGH\nPERIPH cmd_led2 LOW\nDELAY 300\nPERIPH cmd_led2 HIGH\nPERIPH cmd_led3 LOW\nDELAY 300\nPERIPH cmd_led3 HIGH\nPERIPH cmd_led4 LOW\nDELAY 300\nPERIPH cmd_led4 HIGH",
      "useReceivedValue": false,
      "syncDelayMs": 0,
      "execMode": 0
    }
  ],
  "protocolType": 0,
  "scriptContent": "",
  "reportAfterExec": false
}
```

### Configuration Notes

* `intervalSec: 5` - Execute one cycle every 5 seconds; the script's four 300 ms delays control the LED timing
* No manual trigger needed; runs automatically after system startup

***

## Method 3: Bidirectional Flowing Light

### Peripheral Execution Rule Configuration

```json
{
  "id": "rule_cmd_flow_bidirectional",
  "name": "Command Script - Bidirectional Flowing Light",
  "enabled": true,
  "triggers": [
    { "triggerType": 1, "timerMode": 0, "intervalSec": 5 }
  ],
  "actions": [
    {
      "targetPeriphId": "",
      "actionType": 15,
      "actionValue": "PERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nPERIPH cmd_led2 LOW\nDELAY 200\nPERIPH cmd_led2 HIGH\nPERIPH cmd_led3 LOW\nDELAY 200\nPERIPH cmd_led3 HIGH\nPERIPH cmd_led4 LOW\nDELAY 200\nPERIPH cmd_led4 HIGH\nPERIPH cmd_led3 LOW\nDELAY 200\nPERIPH cmd_led3 HIGH\nPERIPH cmd_led2 LOW\nDELAY 200\nPERIPH cmd_led2 HIGH",
      "useReceivedValue": false,
      "syncDelayMs": 0,
      "execMode": 0
    }
  ],
  "protocolType": 0,
  "scriptContent": "",
  "reportAfterExec": false
}
```

### Execution Sequence

```
LED1 → LED2 → LED3 → LED4 → LED3 → LED2 → (cycle)
```

Achieves a "ping-pong" back-and-forth effect.

***

## Method 4: Accumulative Brightening Flowing Light

### Peripheral Execution Rule Configuration

```json
{
  "id": "rule_cmd_flow_accumulate",
  "name": "Command Script - Accumulative Flowing Light",
  "enabled": true,
  "triggers": [
    { "triggerType": 1, "timerMode": 0, "intervalSec": 5 }
  ],
  "actions": [
    {
      "targetPeriphId": "",
      "actionType": 15,
      "actionValue": "PERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led2 LOW\nDELAY 200\nPERIPH cmd_led3 LOW\nDELAY 200\nPERIPH cmd_led4 LOW\nDELAY 500\nPERIPH cmd_led1 HIGH\nPERIPH cmd_led2 HIGH\nPERIPH cmd_led3 HIGH\nPERIPH cmd_led4 HIGH",
      "useReceivedValue": false,
      "syncDelayMs": 0,
      "execMode": 0
    }
  ],
  "protocolType": 0,
  "scriptContent": "",
  "reportAfterExec": false
}
```

### Effect Description

* LEDs turn on sequentially without turning off
* After all 4 LEDs are on, delay 500ms
* Then all turn off, start next cycle

***

## Method 5: Random MQTT Telemetry (with LEDs Reset)

### Peripheral Execution Rule Configuration

```json
{
  "id": "rule_cmd_flow_random",
  "name": "Command Script - Random Flowing Light",
  "enabled": true,
  "triggers": [
    { "triggerType": 1, "timerMode": 0, "intervalSec": 5 }
  ],
  "actions": [
    {
      "targetPeriphId": "",
      "actionType": 15,
      "actionValue": "PERIPH cmd_led1 HIGH\nPERIPH cmd_led2 HIGH\nPERIPH cmd_led3 HIGH\nPERIPH cmd_led4 HIGH\nMQTT 0 [{\"id\":\"flow_state\",\"value\":\"RANDOM(1,4)\"}]\nLOG Random flowing light triggered",
      "useReceivedValue": false,
      "syncDelayMs": 0,
      "execMode": 0
    }
  ],
  "protocolType": 0,
  "scriptContent": "",
  "reportAfterExec": false
}
```

### Description

* Resets all four LEDs to HIGH, then uses `RANDOM(1,4)` in the MQTT payload
* `RANDOM()` expansion is supported in `MQTT` and `LOG` commands; it does not dynamically rewrite peripheral IDs
* Requires the MQTT feature to be active and connected; otherwise the MQTT command is skipped

***

## Command Script Syntax Reference

### Supported Commands

| Command | Format | Description |
|---------|--------|-------------|
| **PERIPH** | `PERIPH <id> <action> [value]` | Control peripheral |
| **DELAY** | `DELAY <ms>` | Delay (milliseconds) |
| **LOG** | `LOG <message>` | Print log |
| **MQTT** | `MQTT <topicIndex> <payload>` | Publish through a configured MQTT topic by zero-based index |

### PERIPH Sub-commands

| Sub-command | Format | Description |
|-------------|--------|-------------|
| HIGH | `PERIPH <id> HIGH` | Output HIGH |
| LOW | `PERIPH <id> LOW` | Output LOW |
| PWM | `PERIPH <id> PWM <value>` | Set 8-bit PWM duty (0-255) |
| BLINK | `PERIPH <id> BLINK <interval_ms>` | Blink |
| BREATHE | `PERIPH <id> BREATHE <cycle_ms>` | Start breathing effect |
| STOP | `PERIPH <id> STOP` | Stop the target's active effect |

### Command Separator

* Use `\n` (newline) to separate multiple commands
* Must be escaped as `\\n` in JSON

***

## Capability Boundary

Command scripts are designed for short, deterministic sequences. They do not provide variables, conditions, or loops. Use rule triggers and multiple actions for conditions and linkage; use a timer trigger to repeat a sequence.

***

## Notes

1. **Command Length Limit**: One command script is limited to 1024 characters and 50 commands
2. **DELAY Accuracy**: Actual delay may have ±10ms error
3. **GPIO0 Limitation**: Do not pull GPIO0 LOW during boot, otherwise enters download mode
4. **Delay Limit**: A single `DELAY` is limited to 10 seconds and total delay to 30 seconds; execution times out after 35 seconds
5. **Trigger Frequency**: Timer interval should be greater than total script execution time to avoid queue buildup
6. **Log Output**: Use LOG command to track script execution progress for debugging
7. **Error Handling**: If peripheral ID in PERIPH command does not exist, it will be skipped and execution continues

***

## Extended Exercises

### Exercise 1: Breathing Light Effect

Use PWM commands to achieve LED breathing effect:

```json
{
  "actionValue": "PERIPH cmd_led1 PWM 0\nDELAY 50\nPERIPH cmd_led1 PWM 512\nDELAY 50\nPERIPH cmd_led1 PWM 1024\nDELAY 50\nPERIPH cmd_led1 PWM 2048\nDELAY 50\nPERIPH cmd_led1 PWM 4095\nDELAY 50\nPERIPH cmd_led1 PWM 2048\nDELAY 50\nPERIPH cmd_led1 PWM 1024\nDELAY 50\nPERIPH cmd_led1 PWM 512\nDELAY 50\nPERIPH cmd_led1 PWM 0"
}
```

### Exercise 2: Traffic Light Simulation

Use 3 LEDs to simulate red/yellow/green traffic light:

```json
{
  "actionValue": "PERIPH led_red LOW\nDELAY 3000\nPERIPH led_red HIGH\nPERIPH led_yellow LOW\nDELAY 1000\nPERIPH led_yellow HIGH\nPERIPH led_green LOW\nDELAY 3000\nPERIPH led_green HIGH\nLOG Traffic light cycle complete"
}
```

### Exercise 3: Morse Code

Use LED to send SOS signal:

```json
{
  "actionValue": "PERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nDELAY 200\nPERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nDELAY 200\nPERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nDELAY 600\nPERIPH cmd_led1 LOW\nDELAY 600\nPERIPH cmd_led1 HIGH\nDELAY 600\nPERIPH cmd_led1 LOW\nDELAY 600\nPERIPH cmd_led1 HIGH\nDELAY 600\nPERIPH cmd_led1 LOW\nDELAY 600\nPERIPH cmd_led1 HIGH\nDELAY 600\nPERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nDELAY 200\nPERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nDELAY 200\nPERIPH cmd_led1 LOW\nDELAY 200\nPERIPH cmd_led1 HIGH\nLOG SOS complete"
}
```

***

## Related Documents

* [Example 3: LED Flowing Light](https://www.fastbee.cn/doc/en/device/v2/examples/03-led-flowing.html)
* [Script Action Documentation](https://www.fastbee.cn/doc/en/device/v2/periph-exec/script-actions.html)
* [Peripheral Execution Configuration Guide](https://www.fastbee.cn/doc/en/device/v2/periph-exec/periph-exec-configuration-guide.html)
* [Script Command Reference](https://www.fastbee.cn/doc/en/device/v2/periph-exec/script-guide.html)

***

**Tip**: Save hardware-control rules as disabled first, run them once for verification, and only then enable their timer or event trigger.
