Due lampade, ESP32
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
Two people live apart, and a lamp stands in each home: an ESP32 with a ring of LEDs and a touch pad. Touch one lamp and the other one glows in your colour for a minute. The two chips talk to each other directly, ESP32 to ESP32, across two home networks; neither home needs an open port, a public address or a computer that stays on.
It is the idea of the lamps example for Raspberry Pis, built with nothing but two ESP32s. Both lamps run the same sketch, and each is one Connectivitizer device that is called and calls:
Connectivitizer.link("lamp-west", "api", 8081) puts the other lamp's web server on port 8081 of this chip, and Connectivitizer.post("lamp-west", "/glow", body, answer) sends the glow there. The lamp does that with its own device key; it has no API key.The sketch has run on a bare board and through Connectivitizer, without a ring or touch pad and with one lamp only. It compiles for the board esp32dev with PlatformIO (espressif32 6.12.0, Arduino core 2.0.17) against the library in ../../connectivitizer-esp32: 1,034,625 bytes of flash (78.9 % of the default 1.3 MB app partition) and 49,600 bytes of static RAM, and 1,034,633 bytes with -DCZ_MAX_SESSIONS=2. No ring and no touch pad have been connected, and two real lamps have not called each other yet. So the pins, the pulse widths for the WS2812B, the touch threshold and everything this page says about what happens between the two lamps are what the data sheets, the Arduino core and the library's code say, not what a test showed. The branch for version 3 of the Arduino core (rmtInit with a frequency, rmtWrite with a pin) has not been compiled. The page has only been run against the pretend lamp in ui/demo-data.js, in Chromium at phone sizes (360, 390 and 430 px wide) and with reduced motion, not in the app on a phone.
Tried on a board (November 2026). The sketch was flashed to an ESP32-S3 DevKit board (N16R8: 16 MB flash, 8 MB octal PSRAM) with nothing connected to it, built with PlatformIO (espressif32 6.12.0, Arduino core 2.0.17) against the library of this repository, and reached through a development platform in the same network with the TypeScript SDK, peer to peer and through the relay, both end-to-end encrypted. It ran with SIMULATE_RING and BOOT_BUTTON_IS_TOUCH set to true. /state, /history and the page answered with the same keys as the pretend lamp. A POST /glow from the calling program made the lamp call the other lamp itself (link() and post()): a small Node.js device stood in for lamp-west and received {"from":"lamp-east","color":…,"pattern":"pulse","via":"phone"}. With the other lamp offline the log says The glow did not arrive at lamp-west (status -1, DEVICE_NOT_FOUND) and the lamp carries on. The S3 compiles it, but its touch readings grow when touched, so the threshold logic is for the classic ESP32; the ESP32-C3 has no touch sensor and does not compile it.
| Folder | What it is |
|---|---|
ui/lamps-esp32.html | The lamp's screen: one self-contained page (about 21 KB), always dark. |
ui/manifest.json | What the device serves as /.cz/ui.json. |
ui/demo-data.js | A pretend lamp, for the demo on the landing page (demo kit). |
esp32/lamp/ | The Arduino sketch for both lamps, with the page built in (page.h). |
| Part | Notes |
|---|---|
| ESP32 development board | Any with the classic ESP32 (esp32dev). Other chips have other touch pins, and on the ESP32-S2 and S3 a touch makes the reading larger, not smaller. |
| LED ring, 16 × WS2812B (NeoPixel style) | Another count works: RING_LEDS. |
| 5 V supply for the ring | The data sheets give up to about 60 mA per LED at full white. RING_BRIGHTNESS is 0.4 as the sketch comes. |
| Level shifter for the data line, for example a 74AHCT125, and a 330 Ω resistor | The ESP32 gives 3.3 V; a WS2812B at 5 V asks for more on its data input. |
| Touch pad | A piece of copper foil or a metal part of the lamp, with one wire. |
The sketch makes the pulses for the ring itself, with the RMT unit of the ESP32, so the Connectivitizer library is the only one to install.
ESP32
┌─────────────┐
touch pad ─┤ GPIO4 (T0) │
│ GPIO13 ├── level shifter ─330Ω─ DIN of the ring
└─────────────┘| Part | Power | Signal |
|---|---|---|
| Ring | Its own 5 V supply; the minus of that supply goes to GND of the ESP32 as well | GPIO13 → level shifter → 330 Ω → DIN |
| Touch pad | none | one wire → GPIO4 (touch channel T0) |
The pins and the timing are constants at the top of lamp.ino. The sketch measures the untouched pad when it starts and counts a reading below 60 % of that as a touch (TOUCH_RATIO); keep the hand away while the lamp starts. A long wire or a large pad may need another ratio.
Two constants replace a part by something a bare DevKit board has: SIMULATE_RING (no ring; the on-board LED on GPIO2 is on while the lamp glows) and BOOT_BUTTON_IS_TOUCH (no touch pad; the BOOT button is the touch). Both are false as the sketch comes. While one is true, /state says "simulated": true and the page shows Simulated hardware at the bottom.
Both lamps belong to the same workspace. Each lamp needs:
In secrets.h | What for |
|---|---|
DEVICE_KEY | czd_…; web app: Credentials → Create credential → Device key, with "Its devices may open sessions to other devices" switched on. Without the switch the lamp can be reached but cannot call. |
SHARED_KEY | czs_…; Credentials → Generate a shared key. The same key on both lamps and on both phones: it is what encrypts every session end to end. |
DEVICE_NAME, OTHER_DEVICE | The names of this lamp and of the other one in the workspace. The second lamp gets them swapped. |
LAMP_COLOR | The colour this lamp sends when it is touched, #rrggbb. |
Give each lamp a device key of its own, so that one lamp can be taken out without touching the other.
cd src/examples/lamps-esp32/esp32/lamp
copy secrets.example.h secrets.h # fill it in; on the second lamp with the two names swappedPlatformIO (how it was compiled here): a platformio.ini beside a folder src that holds the files of the sketch.
[env:esp32dev]
platform = espressif32@6.12.0
framework = arduino
board = esp32dev
lib_deps = huelakesoftwaresolutions/Connectivitizer
build_flags = -DCZ_MAX_SESSIONS=2The sketch needs a version of the library that has link(), get() and post(). Here it was compiled against the library folder of this repository (lib_deps = symlink://<path to>/src/connectivitizer-esp32).
Arduino IDE: install the board package esp32 and the library Connectivitizer (Tools → Manage Libraries), open lamp.ino, choose ESP32 Dev Module, upload. The sketch has not been compiled with the Arduino IDE or arduino-cli.
The serial monitor (115200 baud) prints the address in your Wi-Fi and, once the device is online, Reachable through Connectivitizer. After a change to ui/lamps-esp32.html or ui/manifest.json, write page.h again and flash:
node make-page.mjsOn an ESP32 the library accepts one session at a time unless it is compiled with another limit (CZ_MAX_SESSIONS, library README): a second caller takes the place of the first. With the limit at one, a glow that arrives from the other lamp ends the session of a phone that is looking at this lamp, and the phone's next session ends the other lamp's. -DCZ_MAX_SESSIONS=2 in the platformio.ini above lets the phone and the other lamp be connected at the same time; each session costs memory, and the library's README gives its estimates. The session a lamp opens itself to the other lamp does not count against this limit. What the app shows when its session is replaced has not been tried.
A touch, or Send on the page, puts one glow into a queue. A task of its own takes it from there and calls Connectivitizer.post(), which waits for the answer for up to 15 seconds; loop() keeps drawing the ring and answering the web server meanwhile. The library opens the session to the other lamp with the first request and closes it when the port has not been used for half a minute, so the first glow after a pause takes a few seconds longer than the next ones. One glow is on its way at a time: a touch during that time is ignored, and the page gets 409.
The other lamp answers 200 with how long it glows. This lamp then shows the other ring as glowing on its page for that time. Any other outcome ends as "did not arrive" in the history: no answer within the time, a lamp that is not online, or a device key without the right to open sessions. While this lamp itself is not connected to the platform, a glow ends that way at once.
"Answered" on the page is the outcome of the last call to the other lamp. The sketch does not ask the platform which devices are online, and it does not call the other lamp just to see whether it is there.
In the Connectivitizer app (Android): link the app to the workspace, give it the shared key, pick your lamp. The app asks the device for /.cz/ui.json and shows the page it names.
In a browser in the same Wi-Fi: http://<address of the ESP32>/.cz/ui.html. The page then talks to the ESP32 directly; it is the same file.
Without hardware: the demo, with the pretend lamp.
cd src/connectivitizer
node scripts/build-demos.mjs --only lamps-esp32 --out <folder> # then open <folder>/lamps-esp32/ui.htmlTwo rings side by side, each with its sixteen LEDs: this lamp on the left, the other one on the right. A ring on the page lights up in the colour and the pattern of the glow, with the same formulas the sketch uses for the real ring. Below them: six colours, the four patterns (glow, pulse, heartbeat, wave) and the Send button; a tap on this lamp's ring on the page sends as well. Then what the lamps sent each other since this lamp started, with the time of day as the phone computes it from the age of each entry.
The page asks /state every 1.2 seconds and /history when /state says that something changed. With prefers-reduced-motion a glowing ring is a still picture, lit evenly.
Under the service name api, on port 80.
| Request | Answer |
|---|---|
GET /state | { "here": { "name", "color", "glow" }, "other": { "name", "glow", "reachable", "lastAnswerSecondsAgo", "lastStatus" }, "glowSeconds": 60, "patterns": ["glow", "pulse", "heartbeat", "wave"], "sending": false, "changes": 7, "simulated": false, "rssi": -58, "uptimeSeconds": 277920 }. A glow is null or { "color", "pattern", "seconds", "remainingSeconds" }. other.glow is what this lamp knows of the other one: the glow it delivered there, for as long as the other lamp said it lasts. other.reachable is true or false after the last call to the other lamp and null before the first; lastStatus is the HTTP status of that call, -1 without an answer. sending is true while a glow is on its way. changes goes up whenever the history changes. |
POST /glow from the phone | Body { "color": "#rrggbb", "pattern": "glow" | "pulse" | "heartbeat" | "wave" }, both optional. The lamp sends that glow to the other lamp. Answers at once with { "ok": true, "entry" }, the entry with "delivered": null; whether it arrived shows in the history. 409 { "error" } while the last glow is still on its way. |
POST /glow from the other lamp | Body { "from": "<its device name>", "color", "pattern", "via": "touch" | "phone" }. This lamp glows for 60 seconds (GLOW_SECONDS). Answers { "ok": true, "glowSeconds": 60 }. A from that is not OTHER_DEVICE is answered with 400. |
GET /history | { "received": 4, "sent": 2, "entries": [ { "id", "secondsAgo", "direction": "received" | "sent", "color", "pattern", "via": "touch" | "phone", "delivered": true | false | null }, … ] }. The last 30 glows, newest first. received and sent count since the lamp started; sent counts the glows that arrived. Kept in memory only. |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page. |
Wrong input is answered with 400 and { "error" }.
Whoever holds the shared key and may open sessions to the lamp in the workspace can post to /glow. The from check keeps a misconfigured lamp out; it is not a second lock.
ui/demo-data.js answers the same requests with the same members, with invented values, in the browser. These things are different from a real lamp, on purpose: