Duas lâmpadas
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
demo-kit/ é o que simula o dispositivo para a demo na página inicial. O próprio dispositivo não precisa dele.
Two people live apart, and a lamp stands in each home: a Raspberry Pi with a ring of LEDs and a touch sensor. Touch one lamp and the other one glows in your colour for a minute. The two lamps talk to each other, Pi to Pi, across two home networks that are never the same; neither home needs an open port or a public address.
Each Pi runs the same Python program and is one Connectivitizer device that is called and calls:
The phone page of a lamp shows both lamps side by side with the distance between them, sends a glow in a colour of your choice with one of four patterns (glow, pulse, heartbeat, wave), says whether the other lamp is in reach, and keeps a quiet history of the day.
The hardware is simulated. The program ships with a simulator: a glow is a line on the console and a touch is the Enter key, so the example runs with nothing attached. The code for real LEDs and a real touch sensor is there, but it has never been run (below).
| Folder | What it is |
|---|---|
pi/lamp.py | One lamp: an HTTP and WebSocket server on 127.0.0.1 (standard library only), the hardware behind the class Lamp, and the Connectivitizer agent that exposes the server as the service api and opens the sessions to the other lamp. |
pi/lamp.example.json | Who this lamp is and who the other one is. |
ui/lamps.html | The lamp's screen: one self-contained page. |
ui/manifest.json | What the device answers at /.cz/ui.json: the name of the screen and the colours of the app's bar. |
ui/demo-data.js | A pretend lamp for the demo on the landing page (below). |
cd src/examples/lamps/pi
python lamp.pyLamp 'lamp-north' (simulator) on http://127.0.0.1:8151, belongs with 'lamp-south'
the screen: http://127.0.0.1:8151/.cz/ui.html
the state: http://127.0.0.1:8151/state
Local only: Connectivitizer is NOT started (no keys: fill in secrets.json, or set CZ_DEVICE_KEY and CZ_SHARED_KEY).
Glows sent from here do not arrive anywhere.
[simulator] press Enter to touch the lamp.Without keys it runs local only and listens on the loopback address only. Open the screen in a browser on the same machine, or ask it: curl http://127.0.0.1:8151/state. A lamp on its own has nowhere to send a glow: every glow sent from it ends as "did not arrive".
To see two lamps talk without Connectivitizer, run the program twice on one machine and point each at the other. The glows then go by plain HTTP on the loopback address: no session, no encryption. This is for trying the program, not a way to connect two homes.
python lamp.py --port 8151 --local-peer http://127.0.0.1:8152
python lamp.py --port 8152 --local-peer http://127.0.0.1:8151 --config lamp-south.json # here and other swapped| Option | Default | |
|---|---|---|
--config | lamp.json next to the program | Who this lamp and the other lamp are. Without the file: the two lamps of lamp.example.json. |
--port (or LAMP_PORT) | 8151 | The local port; 0 takes any free one. |
--lamp | simulator | gpio for real LEDs and a touch sensor (not tested). |
--touch-every | never | Simulator: the lamp is touched by itself every so many seconds. |
--led-pin, --led-count, --touch-pin | 18, 16, 17 | For --lamp gpio. |
--local-peer | The address of a second program on the same machine, instead of Connectivitizer. |
lamp.json (copy lamp.example.json; not committed) names both lamps. The second Pi gets the same file with here and other swapped.
{
"here": { "name": "lamp-north", "label": "Alex", "place": "Northhaven", "latitude": 54.0, "longitude": 10.0, "color": "#ffb45e" },
"other": { "name": "lamp-south", "label": "Sam", "place": "Southbrook", "latitude": 48.5, "longitude": 11.5, "color": "#8fb8ff" },
"glowSeconds": 60
}| Member | |
|---|---|
name | The device name of the lamp in the workspace. here.name is what this program connects as; other.name is the device it opens sessions to. |
label, place | What the page shows. |
latitude, longitude | Only used for the distance on the page (great circle, whole kilometres). Rounded values are enough; leave them out and the page shows no distance. |
color | The colour this lamp sends when it is touched, #rrggbb. |
glowSeconds | How long this lamp glows when a glow arrives. Default 60. |
What the lamps sent each other is kept in history-<name>.json next to the program (the last 200 glows), so it is still there after a restart.
Both lamps belong to the same workspace. Each Pi needs two things:
secrets.json | Variable | What for |
|---|---|---|
deviceKey | CZ_DEVICE_KEY | czd_...; web app: Credentials › Create a device key, with "Its devices may open sessions to other devices" switched on. With it and the name from lamp.json the lamp is a device: the phone and the other lamp can open sessions to it, and it opens sessions to the other lamp. |
sharedKey | CZ_SHARED_KEY | czs_...; web app: Credentials › Generate a shared key. The same key on both lamps and on both phones: it is what encrypts every session end to end. |
Give each Pi a device key of its own, so that one lamp can be taken out without touching the other. One device key for both lamps works as well.
cd src/examples/lamps/pi
python -m venv .venv && .venv/bin/pip install -r requirements.txt # .venv\Scripts\pip on Windows
cp secrets.example.json secrets.json # then fill it in
cp lamp.example.json lamp.json # then fill it in; swapped on the second Pi
.venv/bin/python lamp.pysecrets.json and lamp.json are not committed (see pi/.gitignore). With a device key that does not have "Its devices may open sessions to other devices" switched on, the lamp can be reached but cannot send: the platform refuses its sessions, and every glow sent from it ends as not delivered with FORBIDDEN.
How a glow travels: the program runs one SDK agent, the device. On a touch the agent opens a session to the device named in other.name for the service api (agent.connect), over the connection to the platform it keeps anyway. The program posts the glow to /glow there, and keeps the session for the next glows; a session that was not used for two minutes is closed. The SDK looks for a direct path between the two homes first and uses the relay otherwise; the page says "directly" or "through the relay" as the sending lamp's SDK reported it for that session (session.info.mode). Every 20 seconds the program asks the platform for the devices its device key may reach (agent.list_devices), and the page shows the other lamp as "in reach" when it is listed as online.
On the receiving side the SDK forwards each stream of a session as plain TCP to 127.0.0.1:<port>, so the program's own server answers both the phone and the other lamp; nothing else on the Pi is reachable.
In the Connectivitizer app on Android, signed in to the same workspace and holding the same shared key: pick your lamp. The app asks it for /.cz/ui.json, loads /.cz/ui.html through the session and shows it full screen. In the app the page gets its updates over a WebSocket (connectivitizer.webSocket("/events")); opened in a browser straight from the device it asks /state and /history every second and a half instead. An iPhone does not show device UIs yet (device UIs, "Not yet").
Sending from the page asks the lamp at home to send the glow: the phone never talks to the other lamp. Tapping the lamp on the page does the same as the button.
All under the service api.
| Request | Answer |
|---|---|
GET /state | { "t", "here": { "name", "label", "place", "color", "glow" }, "other": { "name", "label", "place", "color", "glow", "reachable", "path" }, "distanceKm", "glowSeconds", "patterns": [ … ], "hardware": "simulator" | "gpio", "link": "connectivitizer" | "local" | "none" }. 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, false, or null while it is not known. other.path is "direct" or "relayed" for the last glow that arrived, null before that. t is the device's clock in seconds since 1970. |
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 still with "delivered": null; whether it arrived follows as an event and in the history. |
POST /glow from the other lamp | Body { "from": "<its device name>", "color", "pattern", "via": "touch" | "phone" }. This lamp glows. Answers { "ok": true, "entry", "glowSeconds" }. A from that is not the lamp named in lamp.json is answered with 400. |
GET /history | { "today": { "received", "sent", "lastReceived", "lastSent" }, "entries": [ { "id", "t", "direction": "received" | "sent", "color", "pattern", "via": "touch" | "phone", "delivered": true | false | null }, … ] }. The last 30 glows, newest first. today counts from the device's local midnight, and counts a sent glow only once it has arrived; lastReceived and lastSent are times of the last such glow, which may be before today, or null. |
WebSocket /events | { "type": "state", "state" } when it opens, when a glow has ended and when the other lamp comes into or goes out of reach; { "type": "glow", "entry", "state" } when a glow is received or sent; { "type": "delivered", "entry", "state" } when a sent glow has arrived, or with "error" (an SDK error code such as DEVICE_OFFLINE, or one of the program's own: NO_LINK for a lamp without a way to the other one, NOT_CONNECTED while the lamp itself is not connected to the platform, REFUSED when the other lamp did not answer 200, FAILED for anything else) when it has not. Nothing is expected from the page. |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page, read from ui/ on every request, so the page can be edited while the program runs. |
Wrong input is answered with 400 and { "error" }.
Whoever holds the shared key and may open sessions to the lamp in the workspace (with an API key, with a device key that has "Its devices may open sessions to other devices" switched on, or signed in to the app) can post to /glow. The from check keeps a misconfigured lamp out; it is not a second lock.
The hardware is behind the class Lamp in lamp.py: show(color, pattern, seconds), close(), and on_touch, which the hardware calls. SimulatedLamp implements it and is the one that has been run.
GpioLamp (--lamp gpio) is written from the documentation of the two libraries it uses and has never been run, because no Raspberry Pi with LEDs was at hand:
rpi_ws281x drives the LEDs (pip install rpi_ws281x), data line on GPIO 18. The library needs root for that pin.gpiozero (part of Raspberry Pi OS) reads the touch sensor on GPIO 17, a module whose output goes high while it is touched.It draws the light 30 times a second with the same brightness formulas as the page. Expect to correct it on the first run with real parts.
The landing page of the web app shows this page without any hardware (demo kit): ui/demo-data.js answers the same requests and the /events socket with the same shapes. The pretend other lamp sends a glow six seconds after the page opens and then every 15 to 25 seconds, takes every glow sent to it after about a second, and is always in reach. A glow lasts 16 seconds there instead of 60. "Directly" in the demo is pretend data like everything else in it.
cd src/connectivitizer
node scripts/build-demos.mjs --only lamps --out <folder> # writes <folder>/lamps/ui.html, which opens from the file system--local-peer, one of them with --touch-every. Checked against it: GET /state, GET /history, POST /glow from the phone side and from the other program, the answers to wrong input, the WebSocket (handshake, the three kinds of events, ping, close), the manifest and the page, a glow from one program arriving at the other and ending after its time, and a glow from a lamp on its own ending as not delivered.NOT_CONNECTED).GpioLamp); through Connectivitizer. No session was opened between two lamps or from a phone to a lamp, neither direct nor relayed, and the two programs have not run in two different networks. Whether a lamp opens its session to the other lamp with its device key, and what a device key without the right to open sessions leads to, has therefore not been seen with this program. The page has not been seen in the app with a lamp.