# relay-proxy — Dingtian emulator + cloud proxy/watchdog (PoC) A NodeMCU v3 (ESP8266) that **is a Dingtian DT-R004 to the booth server** and, on top, a **proxy and watchdog toward a cloud service**. The booth's existing `dingtian` driver talks to it unchanged. It is the seed of the custom firmware for the real Dingtian board (ESP32); the IO layer is the only board-specific file. Why: park-2's booth PC died twice unnoticed. The device that already owns the barrier relay and the button inputs becomes the resilient core: it reports to the cloud independently of the PC, can power-cycle the PC, and keeps accepting commands from the cloud when the PC is dead. See `wiki/entities/dingtian-relay.md` and `wiki/decisions/cloud-service-saas.md`. ## What it emulates (the booth sees a DT-R004) | Surface | Behaviour | | --- | --- | | UDP 60000 binary | `FF AA s cmd pwLo pwHi …`: `00` status, `01` write (mask,set), `03` jog. Reply `FF AA s cmd relayByte inputByte`. Wrong `relay_pw` → dropped silently. | | UDP 60001 string | `00`, `1N`, `2N`, `1N*`. Password-less. Enabled at factory; `harden()` turns it off (`udp2.p=255`). | | `GET /api/v2/config.cgi` | The config JSON (relay_connect, input_link_relay, input_link_url). | | `POST /api/v2/config_set.cgi` | Applies + persists (LittleFS). Requires `"command":"setconfig"` like the real device. | | `GET /userset.cgi?u&old&u&new&` | Web-login rotation, `&0&` ok / `&2&` wrong old password. | | Input push | On input change, Digest-authenticated GET to the booth's `on_path`/`off_path`. | | `input_link_relay` | Factory ON (input auto-fires its relay) until the booth's precondition fix clears it. | Extra, PoC-only: `GET /` status text, `POST /poc/factory_reset`. ## Proxy / watchdog (toward `CLOUD_URL`) - Heartbeat every 10 s: IP, booth health, relay/input state, config flags. - Every input change is mirrored to the cloud (`/event`) with the booth push result. - Command poll every 2 s: `{"pulse":ch,"ms":500}`, `{"watchdog":false}`, `{"reboot_pc":true}`. - Watchdog: probes `BOOTH_HEALTH_URL` every 5 s; after 6 failures in a row it pulses relay 2 for 3 s (meant for an NC contact in series with the PC's mains, with BIOS AC Recovery = On), then backs off 10 min. The cloud can disable it. ## Pins (NodeMCU v3) — no wiring needed to start | Function | Pin | | --- | --- | | Relay 1 (barrier) | D1 / GPIO5, mirrored on the on-board LED | | Relay 2 (PC power) | D2 / GPIO4 | | Relay 3, 4 | D5 / GPIO14, D6 / GPIO12 | | Input 1 (button) | the on-board **FLASH** button (GPIO0), active LOW | | Input 2 | D7 / GPIO13, short to GND | ## Build / flash ``` cd firmware/relay-proxy copy include\secrets.h.example include\secrets.h # fill in; gitignored ..\.venv\Scripts\pio run -t upload ..\.venv\Scripts\pio device monitor ``` Cloud stand-in (throwaway): `node poc/cloud-standin.mjs` on a host the device can reach. ## Register it in the booth Devices → add **Dingtian relay controller**: host = the NodeMCU's IP (give it a DHCP reservation or set `STATIC_IP`; the booth only accepts pushes from the registered IP), channels 4, binary port 60000, string port 60001, HTTP port 80. On save the booth runs the normal flow: precondition fix (clears `input_link_relay`), `harden()` (random `relay_pw`, disables udp2, rotates the web login), and `configureInputPush()` (Digest creds + URLs). The serial log shows each step land. ## Limits of the PoC - Wi-Fi only; the real target has Ethernet. - HTTP calls block the loop briefly (~100 ms); fine for a demo, not for production. - The config JSON mirrors the keys the booth driver uses, not the full device schema. - Plain HTTP to the cloud stand-in; the real cloud link needs TLS.