|
| 1 | +# DCP recipes |
| 2 | + |
| 3 | +Ready-to-flash skeletons for the five protocol shapes you'll most |
| 4 | +commonly want. Each one teaches a different slice of DCP and runs on |
| 5 | +an ESP32-class MCU. Hardware is cheap enough that you can buy the |
| 6 | +whole bench for ~¥40. |
| 7 | + |
| 8 | +| Recipe | What you'll learn | Approx. hardware cost | |
| 9 | +|---|---|---| |
| 10 | +| [`relay_switch`](#relay_switch) | bool param · idempotent · capability · non-idempotent pulse | ¥5 (5V relay module) | |
| 11 | +| [`sensor_dht22`](#sensor_dht22) | typed read intent with units · unsolicited event push · device-tracked config | ¥10 (DHT22) | |
| 12 | +| [`stepper_motor`](#stepper_motor) | int + duration params · `dry_run` as preview · non-idempotent move | ¥10 (28BYJ-48 + ULN2003) | |
| 13 | +| [`encoder_input`](#encoder_input) | **event-only device** — zero intents | ¥3 (KY-040) | |
| 14 | +| [`door_lock`](#door_lock) | capability + `dry_run` as **real safety story** · string return types · state-change events | ¥10 (SG90 servo) | |
| 15 | + |
| 16 | +Each recipe is a pair: a manifest (`examples/<name>_manifest.yaml`) |
| 17 | +and a firmware sketch (`firmware/esp32/examples/<name>/<name>.ino`). |
| 18 | +All five cross-compile clean on the full |
| 19 | +[ESP family build matrix](../README.md#cross-compile-clean-across-the-esp-family-xtensa--risc-v--esp8266). |
| 20 | + |
| 21 | +## Quick start (any recipe) |
| 22 | + |
| 23 | +```bash |
| 24 | +# 1. Wire your hardware per the comments at the top of the .ino |
| 25 | +# 2. Flash: |
| 26 | +arduino-cli compile --upload -p COM5 --fqbn esp32:esp32:esp32 \ |
| 27 | + --library firmware/esp32 firmware/esp32/examples/relay |
| 28 | +# 3. Run the bridge: |
| 29 | +dcp serve examples/relay_manifest.yaml --serial COM5 |
| 30 | +# 4. Point your MCP client (Claude Desktop / Ollama / etc.) at dcp. |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## relay_switch |
| 36 | + |
| 37 | +A single-channel 5V relay (door buzzer, appliance switch, fan). |
| 38 | + |
| 39 | +- 3 intents: `set_relay(state)`, `read_relay()`, `pulse(duration)` |
| 40 | +- The `set_relay` intent is **idempotent** — setting "on" twice still |
| 41 | + leaves it on. `pulse` is **non-idempotent** (two calls = two pulses) |
| 42 | + and the manifest declares that explicitly. |
| 43 | +- Both `set_relay` and `pulse` are gated on `relay.write`; `read_relay` |
| 44 | + only needs `relay.read`. A read-only session token cannot switch the |
| 45 | + appliance. |
| 46 | + |
| 47 | +**Files**: [`examples/relay_manifest.yaml`](../examples/relay_manifest.yaml) · |
| 48 | +[`firmware/esp32/examples/relay/relay.ino`](../firmware/esp32/examples/relay/relay.ino) |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## sensor_dht22 |
| 53 | + |
| 54 | +A DHT22 temperature + humidity sensor that PUSHES events when |
| 55 | +temperature crosses a configurable threshold — no polling needed |
| 56 | +from the LLM. |
| 57 | + |
| 58 | +- 2 read intents (`read_temperature`, `read_humidity`) with explicit |
| 59 | + `unit: celsius` / `unit: percent` declared in the manifest, so the |
| 60 | + LLM tool schema surfaces them and stops asking "Fahrenheit?" |
| 61 | +- 1 write intent (`set_alert_threshold`) that mutates device state |
| 62 | + used by the event loop |
| 63 | +- 1 event (`threshold_exceeded`) emitted on rising edge of "above |
| 64 | + threshold," with payload `{temperature, threshold}` |
| 65 | + |
| 66 | +The DHT22 driver call is stubbed (`read_dht_*()` returns mock data) so |
| 67 | +the example runs headlessly during compile-test. Drop in your |
| 68 | +favourite library (e.g. Adafruit DHT) at those two function bodies. |
| 69 | + |
| 70 | +**Files**: [`examples/sensor_dht22_manifest.yaml`](../examples/sensor_dht22_manifest.yaml) · |
| 71 | +[`firmware/esp32/examples/sensor_dht22/sensor_dht22.ino`](../firmware/esp32/examples/sensor_dht22/sensor_dht22.ino) |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## stepper_motor |
| 76 | + |
| 77 | +A 28BYJ-48 stepper driven by a ULN2003 board — curtain, valve, dial, |
| 78 | +small actuator, etc. |
| 79 | + |
| 80 | +- 3 intents: `step(direction, count, speed_rpm)`, `read_position()`, |
| 81 | + `home()` |
| 82 | +- This is where **dry_run gets genuinely useful**: an LLM can ask |
| 83 | + "what step position would this move end at?" and get |
| 84 | + `{would_move_to: 2700, from: 1234}` without spinning the shaft. |
| 85 | + Saves wear, time, and lets the LLM reason about reachability. |
| 86 | +- `step` and `home` are **non-idempotent** (each call advances state). |
| 87 | + The manifest declares that explicitly so the LLM knows not to retry. |
| 88 | +- Half-step coil sequence implemented inline; coils released after |
| 89 | + every move to avoid overheating. |
| 90 | + |
| 91 | +**Files**: [`examples/stepper_manifest.yaml`](../examples/stepper_manifest.yaml) · |
| 92 | +[`firmware/esp32/examples/stepper/stepper.ino`](../firmware/esp32/examples/stepper/stepper.ino) |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## encoder_input |
| 97 | + |
| 98 | +A KY-040 rotary encoder with integrated push button. |
| 99 | + |
| 100 | +This is the canonical **"the device has no intents"** pattern. The |
| 101 | +LLM does not command the encoder; it gets notified when the user |
| 102 | +turns or clicks. The `intents:` list in the manifest is literally |
| 103 | +empty, the bindings array in the firmware is literally empty, and the |
| 104 | +firmware only ever calls `send_event()`. |
| 105 | + |
| 106 | +Useful for: volume knobs, menu wheels, RPM counters, manual override |
| 107 | +inputs — anything where the LLM should react to user input but never |
| 108 | +drive that input itself. |
| 109 | + |
| 110 | +- 3 events: `encoder_turned(delta, position)`, `button_pressed`, |
| 111 | + `button_long_press(held_ms)` |
| 112 | +- Polled in `loop()` with 5 ms debounce on CLK and 20 ms on the |
| 113 | + button; long-press fires after 1 s of continuous press. |
| 114 | + |
| 115 | +**Files**: [`examples/encoder_manifest.yaml`](../examples/encoder_manifest.yaml) · |
| 116 | +[`firmware/esp32/examples/encoder/encoder.ino`](../firmware/esp32/examples/encoder/encoder.ino) |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## door_lock |
| 121 | + |
| 122 | +The capability + dry_run safety story made concrete: a servo (or |
| 123 | +solenoid) actuated door lock. |
| 124 | + |
| 125 | +- The capability name is **`lock.admin`**, not `lock.write` — the |
| 126 | + manifest itself signals "this is dangerous." A reviewer reading the |
| 127 | + manifest immediately sees that this intent is in a separate trust |
| 128 | + tier from "read the temperature." |
| 129 | +- `dry_run` is the safety primitive in action: an LLM can be |
| 130 | + ALLOWED to call `unlock` with `__dry_run__=true` (cheap, |
| 131 | + reversible — returns "would transition locked → unlocked") but the |
| 132 | + REAL unlock requires the `lock.admin` capability, which the |
| 133 | + operator typically mints out of band with a short TTL: |
| 134 | + |
| 135 | + ```bash |
| 136 | + dcp token mint --caps lock.read,lock.admin --ttl 300 |
| 137 | + ``` |
| 138 | + |
| 139 | +- The Bridge in the recommended `dcp serve --grant lock.read` |
| 140 | + configuration grants **only read** by default. Admin must be |
| 141 | + presented as a signed token per call. |
| 142 | +- A `state_changed(from, to)` event fires on any transition, |
| 143 | + regardless of cause — so manual key turns are observable too. |
| 144 | + |
| 145 | +**Files**: [`examples/door_lock_manifest.yaml`](../examples/door_lock_manifest.yaml) · |
| 146 | +[`firmware/esp32/examples/door_lock/door_lock.ino`](../firmware/esp32/examples/door_lock/door_lock.ino) |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## Adding your own recipe |
| 151 | + |
| 152 | +The protocol is the same regardless of device. Use one of the above |
| 153 | +as your starting template, change the manifest to declare your |
| 154 | +intents and events, and replace the firmware handlers with your |
| 155 | +hardware-specific code. The full add-a-feature walkthrough is in |
| 156 | +[`ADDING_FEATURES.md`](ADDING_FEATURES.md). |
| 157 | + |
| 158 | +If you build a recipe for a piece of hardware that isn't covered |
| 159 | +here (BME280, NeoPixel strip, ultrasonic sensor, GPS module, |
| 160 | +PCA9685, ADS1115…), please open a PR — the cookbook grows by |
| 161 | +contribution. |
| 162 | + |
| 163 | +## Footprint stays flat |
| 164 | + |
| 165 | +Adding a recipe doesn't grow the DCP layer. All five recipes above |
| 166 | +land at the same ~290 KB / ~22.5 KB on ESP32-WROOM-32 — the |
| 167 | +variation is in the example sketch's own logic, not in any per-recipe |
| 168 | +protocol cost. The intent table is `switch(intent_id) → handler`, and |
| 169 | +each intent adds one row and one function. There is no plugin loader, |
| 170 | +no runtime registration, no per-handler dispatcher overhead. |
0 commit comments