Briefkasten
Der Ordner demo-kit/ simuliert das Gerät für die Demo auf der Startseite. Auf dem Gerät wird er nicht benötigt.
Der Ordner demo-kit/ simuliert das Gerät für die Demo auf der Startseite. Auf dem Gerät wird er nicht benötigt.
The mailbox stands at the end of the drive, or the gate at the far side of the yard, out of reach of the Wi-Fi of the house. An ESP32 with a reed switch sits there, on a network of its own, a mobile router for example. A second ESP32 indoors has a light. When the mailbox opens, the one outside calls the one inside, and the light glows until someone clears the flag: with the button next to it, or from the phone. Neither network needs an open port or a public address, and no computer runs in between.
Two sketches, two Connectivitizer devices:
esp32/sensor) watches the lid and calls. Connectivitizer.link("mailbox-flag", "api", 8081) puts the indicator's web server on port 8081 of the sensor's chip, and Connectivitizer.post("mailbox-flag", "/event", body, answer) reports an opening there. The sensor does that with its own device key; it has no API key.esp32/indicator) is only called: by the sensor and by the phone. It shows the flag light and keeps what happened.Both bring their own screen, a device UI that the Connectivitizer app shows, so the phone can open either. The indicator's page is the one on the landing page.
The sensor stays awake. It keeps its Wi-Fi connection and its connection to the platform all the time and never sleeps, so it needs a power supply that is always there. It is not a battery design, and this example says nothing about how long a battery would last.
The sketches have run on a bare board and through Connectivitizer, one at a time. Both compile for the board esp32dev with PlatformIO (espressif32 6.12.0, Arduino core 2.0.17) against the library in ../../connectivitizer-esp32:
| Sketch | Flash | Share of the default 1.3 MB app partition | Static RAM |
|---|---|---|---|
sensor | 991,381 bytes | 75.6 % | 46,060 bytes |
indicator | 1,015,981 bytes | 77.5 % | 46,976 bytes |
indicator with -DCZ_MAX_SESSIONS=2 | 1,015,989 bytes | 77.5 % | 46,976 bytes |
No switch, light or button has been connected, and the two have not yet run on two boards at the same time. So the pins and everything this page says about what happens between the two devices are what the Arduino core and the library's code say, not what a test showed. The branch for version 3 of the Arduino core in the indicator (ledcAttachChannel) has not been compiled. The indicator's page has only been run against the pretend device in ui/demo-data.js, in Chromium at phone sizes (360, 390 and 430 px wide) and with reduced motion; the sensor's page has only been run against one made-up answer to /state, at 360 px. Neither has been seen in the app on a phone.
Tried on a board (November 2026). Each 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. The indicator answered /state, /history, POST /event and POST /clear with the same keys as the pretend device. The sensor ran with PIN_LID = 0 and LID_OPEN_LEVEL = LOW (the BOOT button as the lid, as the sketch suggests for a bare board); each opening and closing reached a Node.js stand-in for mailbox-flag as POST /event with "from":"mailbox", and the sensor logged The indicator answered. On the S3 the pins had to change: GPIO 22 to 25 do not exist there, and on modules with octal PSRAM GPIO 26 to 37 belong to the flash and the PSRAM, so a sketch that drives one of them stops the chip. Here PIN_FLAG moved to 15 and PIN_BUTTON to 0.
| Folder | What it is |
|---|---|
ui/mailbox.html | The indicator's screen: one self-contained page (about 15 KB), always light. |
ui/manifest.json | What the indicator serves as /.cz/ui.json. |
ui/demo-data.js | A pretend indicator with a pretend sensor, for the demo on the landing page (demo kit). |
ui/sensor.html, ui/sensor-manifest.json | The sensor's screen (about 5 KB) and its manifest. |
esp32/sensor/ | The sketch for the ESP32 at the mailbox, with its page built in (page.h). |
esp32/indicator/ | The sketch for the ESP32 indoors, with its page built in (page.h). |
| Where | Part | Notes |
|---|---|---|
| Mailbox | ESP32 development board | Any with the classic ESP32 (esp32dev). |
| Mailbox | Reed switch (normally open) and a magnet | The magnet on the lid, so that it holds the switch closed while the lid is closed. A tilt switch works the same way. |
| Mailbox | A power supply that is always there | See above: the sensor does not sleep. |
| Mailbox | Wi-Fi with a way to the internet | A mobile router, or the far edge of another Wi-Fi. |
| Indoors | ESP32 development board | As above. |
| Indoors | An LED with a 220 Ω resistor | The flag light. |
| Indoors | A push button | Clears the flag. |
The Connectivitizer library is the only one to install for either sketch.
Sensor, at the mailbox Indicator, indoors
┌─────────────┐ ┌─────────────┐
GND ─reed──┤ GPIO27 │ │ GPIO25 ├─220Ω─ LED ─ GND
└─────────────┘ GND ─btn───┤ GPIO27 │
└─────────────┘| Device | Part | Signal |
|---|---|---|
| Sensor | Reed switch | GPIO27 → switch → GND. The pin's own pull-up is used: the pin reads LOW while the lid is closed and HIGH while it is open. For a switch that is closed while the lid is open, set LID_OPEN_LEVEL to LOW. |
| Indicator | LED | GPIO25 → 220 Ω → LED → GND. For a light that is on with the pin low, set FLAG_ACTIVE_HIGH to false. |
| Indicator | Button | GPIO27 → button → GND. The pin's own pull-up is used. |
The pins are constants at the top of sensor.ino and indicator.ino.
Two bare DevKit boards are enough to try it. On the sensor, PIN_LID = 0 and LID_OPEN_LEVEL = LOW make the BOOT button the lid: pressed is open. On the indicator, PIN_FLAG = 2 is the on-board LED of most boards and PIN_BUTTON = 0 its BOOT button.
Both devices belong to the same workspace.
In secrets.h | Sensor | Indicator |
|---|---|---|
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 sensor can be reached but cannot call. | A device key of its own. It does not need the switch: the indicator calls nobody. |
SHARED_KEY | czs_…; Credentials → Generate a shared key. | The same key. It also goes into the app; it is what encrypts every session end to end. |
DEVICE_NAME | mailbox | mailbox-flag |
| The other device | INDICATOR_DEVICE: mailbox-flag | SENSOR_DEVICE: mailbox |
cd src/examples/mailbox/esp32/sensor # and the same in ..\indicator
copy secrets.example.h secrets.h # fill it inPlatformIO (how they were compiled here): for each sketch 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
; for the indicator, so that the phone and the sensor can be connected at the same time:
; build_flags = -DCZ_MAX_SESSIONS=2The sensor needs a version of the library that has link() and post(). Here both were 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 the sketch, choose ESP32 Dev Module, upload. The sketches have not been compiled with the Arduino IDE or arduino-cli.
The serial monitor (115200 baud) prints the address in the Wi-Fi and, once the device is online, Reachable through Connectivitizer. The sensor also prints every lid movement and the outcome of every call. After a change to a page or its manifest, write that sketch's 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 call from the sensor ends the session of a phone that is looking at the indicator. -DCZ_MAX_SESSIONS=2 for the indicator lets both be connected at the same time; each session costs memory, and the library's README gives its estimates. The sensor is only called by the phone, so one is enough there; the session it opens itself does not count against the limit. What the app shows when its session is replaced has not been tried.
The sensor reads the switch every 20 ms and takes a change as real when three readings in a row agree. An opening is counted at the moment the lid leaves its closed place. A lid that is open when the sensor starts is not counted.
The sensor calls the indicator when there is something to say:
CHECK_IN_SECONDS): a check-in, so that the indicator can tell a quiet mailbox from a sensor that is gone.Every call is one POST /event with the number of openings since the last call that was answered, the place of the lid and the sensor's Wi-Fi signal. A task of its own makes the call and waits for the answer for up to 15 seconds; loop() keeps watching the lid meanwhile. The library opens the session with the first request and closes it when the port has not been used for half a minute, so a call after a quiet time opens a session first, which takes a few seconds.
A call without the answer 200 is tried again after 15 seconds (RETRY_SECONDS), for as long as it takes; while the sensor itself is not connected to the platform, it waits. The openings stay counted on the sensor until then, so an hour without a connection ends with one call that says how often the mailbox was opened in that hour, not when. The count is kept in memory: a sensor that restarts before it got through loses it.
On the indicator an opening raises the flag. The light swells and fades once in three seconds and is never dark while the flag is up. The flag and the number of openings since it was cleared are stored in flash and survive a restart; the list of what happened, and the time the flag went up, do not. The indicator shows the sensor as late when it has not called for 2.5 of its check-in intervals, 25 minutes as the sketches come.
In the Connectivitizer app (Android): link the app to the workspace, give it the shared key, pick mailbox-flag or mailbox. The app asks the device for /.cz/ui.json and shows the page it names.
In a browser in the same Wi-Fi as the device: 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 of the indicator's page, with the pretend device.
cd src/connectivitizer
node scripts/build-demos.mjs --only mailbox --out <folder> # then open <folder>/mailbox/ui.htmlThe indicator's page draws a mailbox whose flag goes up with the real one, with a postmark that counts the openings since the flag was cleared. Below it: Clear the flag, what the sensor last said (the lid, the time of its last call, its Wi-Fi signal, and a warning when it is late or has never called), and the list of openings and clearings with the time of day as the phone computes it from the age of each entry. The page asks /state every 1.5 seconds and /history when /state says that something changed.
The sensor's page is a status sheet: the lid, the openings since the sensor started, how many of them are not delivered yet, the last call that was answered, and what the indicator answered about its flag.
Both under the service name api, on port 80.
| Request | Answer |
|---|---|
GET /state | { "name": "mailbox-flag", "flag": true, "flagSecondsAgo": 754, "opened": 2, "sensor": { "name": "mailbox", "lastSeenSecondsAgo": 31, "lid": "closed", "rssi": -72, "checkInSeconds": 600, "late": false }, "changes": 9, "rssi": -55, "uptimeSeconds": 528000 }. flagSecondsAgo is null while the flag is down, and after a restart with the flag up. opened counts the openings since the flag was cleared. The members of sensor are what its last call said; lastSeenSecondsAgo, lid, rssi and checkInSeconds are null until it has called since the indicator started. changes goes up whenever the history or the lid changes. |
POST /event from the sensor | Body { "from": "<its device name>", "opened": 1, "lid": "open" | "closed", "rssi": -72, "checkInSeconds": 600 }. opened of 1 or more raises the flag; 0 is a check-in or a lid that closed. Answers { "ok": true, "flag": true | false }. A from that is not SENSOR_DEVICE is answered with 400. |
POST /clear | Any body. Puts the flag down. Answers like /state. Does nothing while the flag is down. |
GET /history | { "entries": [ { "id", "secondsAgo", "kind": "opened" | "cleared", "via": "sensor" | "button" | "phone", "count" }, … ] }. The last 30, newest first. count is the number of openings in that call, 0 for a clearing. Kept in memory only. |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page. |
Whoever holds the shared key and may open sessions to the indicator in the workspace can post to /event and /clear. The from check keeps a misconfigured sensor out; it is not a second lock. POST /clear is recorded as "phone", whoever sent it.
| Request | Answer |
|---|---|
GET /state | { "name": "mailbox", "indicator": "mailbox-flag", "lid": "closed", "opens": 3, "unsent": 0, "calling": false, "lastDeliveredSecondsAgo": 125, "lastStatus": 200, "indicatorFlag": true, "checkInSeconds": 600, "rssi": -72, "uptimeSeconds": 90061 }. opens counts since the sensor started; unsent are the openings the indicator has not confirmed. lastStatus is the HTTP status of the last call, -1 without an answer, null before the first. indicatorFlag is what the indicator answered last, null before that. |
GET /.cz/ui.json, GET /.cz/ui.html | The manifest and the page. |
ui/demo-data.js answers the indicator's requests with the same members, with invented values, in the browser. These things are different from a real mailbox, on purpose: