Example 3B: Command Script Flowing Light
About 4 min
Example 3B: Command Script Flowing Light
Experiment Overview
Use FastBee's Command Script Action (ACTION_SCRIPT) to achieve LED flowing light effects through PERIPH and DELAY command sequences. Unlike JavaScript scripts, command scripts use declarative command sequences that are more intuitive and readable.
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
{
"id": "rule_cmd_flow_basic",
"name": "Command Script - Basic Flowing Light",
"enabled": true,
"triggers": [
{ "type": "manual" }
],
"actions": [
{
"type": 15,
"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",
"execMode": 0
}
]
}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 logExecution Methods
- Manual Trigger: Manually trigger the rule via API or Web interface
- API Call:
POST /api/periph-exec/rules/rule_cmd_flow_basic/trigger
Method 2: Timer Auto-Cycle Flowing Light
Peripheral Execution Rule Configuration
{
"id": "rule_cmd_flow_timer",
"name": "Command Script - Timer Cycle Flowing Light",
"enabled": true,
"triggers": [
{
"type": "timer",
"params": {
"interval": 1500,
"repeat": -1
}
}
],
"actions": [
{
"type": 15,
"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",
"execMode": 0
}
]
}Configuration Notes
interval: 1500- Execute one cycle every 1.5 seconds (4 LEDs × 300ms + delay margin)repeat: -1- Infinite loop- No manual trigger needed; runs automatically after system startup
Method 3: Bidirectional Flowing Light
Peripheral Execution Rule Configuration
{
"id": "rule_cmd_flow_bidirectional",
"name": "Command Script - Bidirectional Flowing Light",
"enabled": true,
"triggers": [
{
"type": "timer",
"params": {
"interval": 2500,
"repeat": -1
}
}
],
"actions": [
{
"type": 15,
"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",
"execMode": 0
}
]
}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
{
"id": "rule_cmd_flow_accumulate",
"name": "Command Script - Accumulative Flowing Light",
"enabled": true,
"triggers": [
{
"type": "timer",
"params": {
"interval": 2000,
"repeat": -1
}
}
],
"actions": [
{
"type": 15,
"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",
"execMode": 0
}
]
}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 Flowing Light (with MQTT Command)
Peripheral Execution Rule Configuration
{
"id": "rule_cmd_flow_random",
"name": "Command Script - Random Flowing Light",
"enabled": true,
"triggers": [
{
"type": "timer",
"params": {
"interval": 500,
"repeat": -1
}
}
],
"actions": [
{
"type": 15,
"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",
"execMode": 0
},
{
"type": 10,
"actionType": 10,
"targetPeriphId": "cmd_led{{received.value}}",
"actionValue": "LOW",
"useReceivedValue": false,
"execMode": 0
}
]
}Description
- Uses
RANDOM(1,4)to generate random number - Reports currently lit LED number via MQTT
- Randomly lights one of the LEDs
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 <qos> <payload> | Send MQTT message |
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 PWM value (0-4095) |
| BLINK | PERIPH <id> BLINK <interval_ms> | Blink |
| COLOR | PERIPH <id> COLOR <#RRGGBB> | NeoPixel color |
Command Separator
- Use
\n(newline) to separate multiple commands - Must be escaped as
\\nin JSON
Command Script vs JavaScript Script Comparison
| Feature | Command Script (ACTION_SCRIPT) | JavaScript Script |
|---|---|---|
| Syntax | Declarative command sequence | Programming language |
| Readability | ⭐⭐⭐⭐⭐ Very intuitive | ⭐⭐⭐ Requires programming knowledge |
| Flexibility | ⭐⭐⭐ Fixed command set | ⭐⭐⭐⭐⭐ Completely free |
| Performance | ⭐⭐⭐⭐ Lightweight parsing | ⭐⭐⭐ Requires JS engine |
| Use Case | Simple timing control, debugging | Complex logic, computation |
| Variable Support | ❌ Not supported | ✅ Supports getVar/setVar |
| Conditional | ❌ Not supported | ✅ Supports if/else |
| Loop | ❌ Not supported (use timer instead) | ✅ Supports for/while |
Selection Recommendations
- ✅ Use Command Script: Simple timing, beginners, rapid prototyping, peripheral debugging
- ✅ Use JavaScript Script: Complex logic, state machines, dynamic computation, conditional branching
Notes
- Command Length Limit: Single actionValue recommended not to exceed 512 characters
- DELAY Accuracy: Actual delay may have ±10ms error
- GPIO0 Limitation: Do not pull GPIO0 LOW during boot, otherwise enters download mode
- Blocking Execution: Command scripts execute synchronously; DELAY blocks subsequent commands
- Trigger Frequency: Timer interval should be greater than total script execution time to avoid queue buildup
- Log Output: Use LOG command to track script execution progress for debugging
- 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:
{
"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:
{
"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:
{
"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 (JavaScript Script)
- Script Action Documentation
- Peripheral Execution Configuration Guide
- Script Command Reference
Tip: Command scripts are ideal for quick verification of peripheral functions and simple timing control. For more complex logic, refer to 03-led-flowing.md for JavaScript script implementation.
