diff --git a/README.md b/README.md index db87a13..78bd3d5 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ You can run the whole laptop side with fake data and no hardware. These commands ### One command for the real unit -When the unit is on the network, the launcher runs the whole laptop side: Mosquitto with `server/mosquitto.conf`, the logger, and the dashboard on `http://127.0.0.1:8080`. It waits for the unit, opens the dashboard in Live mode and shows a status screen (broker, logger session, unit online, last status, message rates, laptop clock offset). +When the unit is on the network, the launcher runs the whole laptop side: Mosquitto with `server/mosquitto.conf`, the broker beacon the unit uses to [find the laptop](docs/firmware.md#finding-the-broker), the logger, and the dashboard on `http://127.0.0.1:8080`. It waits for the unit, opens the dashboard in Live mode and shows a status screen (broker, logger session, unit online, last status, message rates, laptop clock offset). ```powershell python -m invigil start diff --git a/docs/contract.md b/docs/contract.md index 959ef53..2acbc6d 100644 --- a/docs/contract.md +++ b/docs/contract.md @@ -59,6 +59,8 @@ The topic keeps the `{node_id}` level so more units can be added in Phase 2 with The unit publishes over plain MQTT on port 1883. The dashboard runs in a browser, which cannot open a plain MQTT socket, so it subscribes to `invigil/node/+/+` over WebSockets on port 9001 (see `server/mosquitto.conf`). Topics and payloads are the same on both. +The unit learns the broker's address from a UDP beacon the launcher broadcasts on port 18830. It is not an MQTT message and carries no readings. See [Finding the broker](firmware.md#finding-the-broker). + ## Proposed Not implemented. Nothing publishes this topic yet, `server/contract.py` rejects it, and the dashboard only shows detections from its Demo source. Agree on it here before building the Python side. diff --git a/docs/firmware.md b/docs/firmware.md index 574ed91..e518ce1 100644 --- a/docs/firmware.md +++ b/docs/firmware.md @@ -2,7 +2,7 @@ The firmware for sensor unit N1: a Raspberry Pi Pico 2 W (RP2350 + CYW43439) with an nRF24L01+PA+LNA on SPI0. -**Status: version 0.2.0 runs on the real unit N1.** On the first hardware run (2026-10-26, version 0.1.0) every self-check passed, the unit published BLE, spectrum and status messages, and the logger rejected none of them ([first boot report](results/2026-10-26-first-boot/report.md)). The BTstack byte order is checked and correct ([byte-order report](results/2026-10-26-byte-order/report.md)). The timing values are still starting guesses, and the [To measure](#to-measure) section lists what is left to check on the board. +**Status: version 0.3.0 runs on the real unit N1.** It found the broker by beacon on a phone hotspot on 2026-11-17 ([report](results/2026-11-17-broker-beacon/report.md)); the nRF24L01 did not answer on that run, so the spectrum path was last checked on 0.2.0. On the first hardware run (2026-10-26, version 0.1.0) every self-check passed, the unit published BLE, spectrum and status messages, and the logger rejected none of them ([first boot report](results/2026-10-26-first-boot/report.md)). The BTstack byte order is checked and correct ([byte-order report](results/2026-10-26-byte-order/report.md)). The timing values are still starting guesses, and the [To measure](#to-measure) section lists what is left to check on the board. - Toolchain: the official Pico SDK **2.3.1** in C with CMake, Arm GNU Toolchain **15.2.Rel1**, picotool **2.3.1**, built with the Raspberry Pi Pico VS Code extension. No Arduino, no Arduino-Pico core, no PlatformIO, no ESP code. - Board: `pico2_w`. @@ -20,19 +20,43 @@ The unit repeats one cycle, as the [MQTT contract](contract.md) requires: The cycle only runs while Wi-Fi is up, the clock is set by SNTP and MQTT is connected. Nothing is published before the clock is set, because the contract rejects unsynced timestamps. Reconnecting happens between cycles, never inside a scan window. +## Finding the broker + +The unit does not need the laptop's IP address. On a phone hotspot the laptop gets a different address every time, so the unit finds the broker by itself (since version 0.3.0). + +**The beacon.** While the launcher runs (`invigil start`), it broadcasts one small UDP datagram a second to port **18830** on every network the laptop is on: + +``` +invigil broker 1883 52133b7ca99633f6 +``` + +`1883` is the MQTT port. The last field is a signature: the first 16 hex characters of HMAC-SHA256, with the hash salt as the key and `invigil broker ` as the message. The unit computes the same thing from the address the datagram came from. So a device that copies the beacon cannot point the unit at itself, and a laptop with another salt is ignored. `invigil/beacon.py` sends it, `firmware/portable/beacon.c` checks it, and CI compares the two. + +**What the unit does.** + +1. After Wi-Fi is up and the clock is set, it listens for a beacon and connects to the sender. The log says `mqtt: connecting to 10.139.204.170:1883 (beacon) as invigil-N1`. +2. If no beacon arrives within 5 s, it tries `MQTT_HOST` from `secrets.h` if one is set, else the last address a beacon came from. If it has neither, it keeps listening and says so once. +3. If the connection drops, it does the same again, with the usual backoff. +4. While connected, a beacon from another address means the broker has moved (the laptop got a new address, or another laptop took over). The unit waits until the old address has missed 3 beacons, drops the old connection and connects to the new address. With two laptops broadcasting at once, it stays with the first one it heard. + +**Scan windows.** Listening is receive only: a broadcast is never acknowledged or answered, so a beacon that arrives during a scan window makes the unit transmit nothing. Every connect, reconnect and move happens between cycles, as before. + +**If the unit never finds the broker.** `mqtt: no broker beacon on UDP port 18830 yet` means no beacon reached the unit. Check that the launcher is running (its status screen has a `Beacon` line), that the laptop and the unit are on the same network, and that the network does not keep its devices apart (some hotspots and guest networks do, often called client or AP isolation). `ignored N beacons with a wrong signature` means `HASH_SALT` in `secrets.h` and `hash_salt` in `server/config.toml` differ. + ## Modules | File | Job | | --- | --- | | `src/config.h` | Pins, timing constants (each marked as a guess to measure), the debug flag. Includes `secrets.h` | | `src/wifi_sta.c` | Wi-Fi station (WPA2 personal, country Egypt), reconnect with backoff (1 s doubling to 30 s), SNTP clock, prints the Wi-Fi channel and RSSI | -| `src/mqtt_link.c` | lwIP MQTT, QoS 0, client ID `invigil-N1`, reconnect with backoff, waits when the output buffer is full | +| `src/mqtt_link.c` | lwIP MQTT, QoS 0, client ID `invigil-N1`, chooses the broker (beacon, then the `MQTT_HOST` fallback), reconnect with backoff, waits when the output buffer is full | +| `src/broker_find.c` | Listens for the launcher's beacon on UDP port 18830 and keeps the sender's address. Receive only | | `src/ble_scan.c` | BTstack observer, passive scan, duplicate filtering off, boot self-check with the hash test vector, buffer of 1024 readings | | `src/nrf_scan.c` | nRF24L01 on SPI0 at 4 MHz, receive only, RPD hits per channel per window, sweep timing | | `src/main.c` | The cycle, watchdog (8 s), LED, 10 s summary on USB serial | | `src/lwipopts.h`, `src/btstack_config.h` | lwIP and BTstack settings | -| `portable/` | Code that needs no Pico SDK, tested on a PC: SHA-256 and address hashing, manufacturer ID parsing, JSON payloads, spectrum counting, the reading buffer, and the receive-only nRF24L01+ driver | -| `tests/` | Host tests for `portable/`, and `check_contract.py`, which runs sample payloads through `server/contract.py` | +| `portable/` | Code that needs no Pico SDK, tested on a PC: SHA-256 and address hashing, manufacturer ID parsing, JSON payloads, spectrum counting, the reading buffer, the beacon check, and the receive-only nRF24L01+ driver | +| `tests/` | Host tests for `portable/`, and `check_contract.py`, which runs sample payloads through `server/contract.py` and compares the beacons with `invigil/beacon.py` | ### Privacy and receive-only rules in the code @@ -70,10 +94,10 @@ Then fill it in: | Field | Value | | --- | --- | | `WIFI_SSID`, `WIFI_PASSWORD` | The hall Wi-Fi. WPA2 personal only | -| `MQTT_HOST` | The laptop's IPv4 address (run `ipconfig` on the laptop). Not a host name | -| `MQTT_PORT` | `1883` | +| `MQTT_HOST` | Optional fallback, used only when no [beacon](#finding-the-broker) is heard. `""` for none, or the laptop's IPv4 address (not a host name) on a network where it is fixed | +| `MQTT_PORT` | `1883`. The port for `MQTT_HOST`. With the beacon, the port comes from the beacon | | `MQTT_USER`, `MQTT_PASSWORD` | The same login as `server/config.toml` and `server/mosquitto.passwd` | -| `HASH_SALT` | The same value as `hash_salt` in `server/config.toml`. The unit will not publish BLE readings while it is empty | +| `HASH_SALT` | The same value as `hash_salt` in `server/config.toml`. The unit will not publish BLE readings while it is empty. The beacon is signed with it too | | `NODE_ID` | `"N1"` | | `SNTP_SERVER` | `"pool.ntp.org"`, or the laptop's IP address if the hall network has no internet. Then run an NTP server on the laptop | @@ -172,7 +196,7 @@ Every 10 s the unit prints one line of `key=value` pairs. These are the numbers | `send_overruns` | Send windows that needed more than 500 ms to drain. The unit waited longer instead of scanning | | `tx_in_scan` | Frames lwIP handed to the radio during scan windows. Should be 0 | | `sweeps_discarded` | Sweeps thrown away because lwIP sent a frame during them | -| `wifi`, `mqtt` | Connection state | +| `wifi`, `mqtt` | Connection state. `mqtt=finding-broker` means the unit is listening for the [beacon](#finding-the-broker) | `ble_no_rssi`, `ble_invalid` and `SELF-CHECK FAILED` only appear when they apply. @@ -191,6 +215,8 @@ The contract says the unit must not scan the 2.4 GHz band while it transmits ove | SNTP | One request an hour, plus a DNS lookup, whenever the lwIP timer fires | Rarely | | Wi-Fi chip firmware | 802.11 acks for frames it receives, and power-save frames if power saving is on. lwIP does not see these. Power saving is off (`WIFI_PM_MODE` is `CYW43_NONE_PM`) because the default mode sends a power-save frame about 200 ms after the last packet, which would fall inside every scan window | 802.11 acks only, when something arrives | | Reconnecting | Wi-Fi and MQTT reconnects only start between cycles, and the cycle stops while either is down | No | +| Broker beacon | The unit only listens. A broadcast is not acknowledged at the Wi-Fi level and lwIP sends no reply to it | No | +| Broker moved | The old connection is dropped with one TCP reset between cycles, not a normal close, which would keep resending to an address that may be gone | No | | Bluetooth | Passive scan, so the Bluetooth side never transmits | No | | nRF24L01 | Receive only, auto-ack off | No | diff --git a/docs/results/2026-11-17-broker-beacon/report.md b/docs/results/2026-11-17-broker-beacon/report.md new file mode 100644 index 0000000..72b0327 --- /dev/null +++ b/docs/results/2026-11-17-broker-beacon/report.md @@ -0,0 +1,55 @@ +# Finding the broker by beacon: first run on a phone hotspot + +Date: 2026-11-17. Unit N1, firmware 0.3.0. Network: an Android phone hotspot, 2.4 GHz, channel 6. The laptop's address came from the hotspot's DHCP. `MQTT_HOST` in `secrets.h` still held an address from the old network (192.168.8.2), so it could not have been what connected. + +## Result + +- The unit heard the launcher's beacon and connected to the laptop about 1 s after the clock was set. The hotspot passes broadcasts between its devices, so the beacon is enough here. +- `invigil status` showed the unit online with firmware 0.3.0, about 78 BLE readings a second, and no rejected messages. +- **The nRF24L01 self-check failed on this run** (no answer on SPI0), so no spectrum messages were sent. This is a wiring or power fault on the bench, not part of this change: the radio code is the same as in 0.2.0. The spectrum path on 0.3.0 is still to be checked once the radio answers. +- `tx_in_scan` was 2 in the two summaries that contain a clock request (one at boot, one with `sntp: clock corrected`) and 0 in the others. That matches the SNTP row in [What can transmit during a scan window](../../firmware.md#what-can-transmit-during-a-scan-window). The beacon listener added no transmissions. + +Not tested yet: the laptop changing address while the unit runs, and two laptops broadcasting at once. Both are covered by host tests only. + +## Serial log + +The network name is replaced by an example. + +``` +Invigil unit N1, firmware 0.3.0, Pico SDK 2.3.1 +last reset: not a watchdog timeout +self-checks: + PASS node ID "N1" + PASS hash test vector: got 41239c0be85e, expected 41239c0be85e + PASS hash salt: set + FAIL nRF24L01: no answer on SPI0 (register read-back failed). Check wiring and power. + BLE keeps running, no spectrum messages will be sent + PASS broker beacon listener (UDP port 18830) +timing: scan 1000 ms, send up to 500 ms, quiet gap 50 ms, status every 10000 ms +mqtt: the broker is found by its beacon on UDP port 18830, fallback 192.168.8.2:1883 +wifi: connecting to "phone-hotspot" +wifi: connected to "phone-hotspot", IP 10.139.204.195 +wifi: channel 6 (2437 MHz, nRF channels 27-47), RSSI -62 dBm +sntp: asking pool.ntp.org for the time +sntp: clock set to 2026-11-17 07:17:10 UTC +mqtt: connecting to 10.139.204.170:1883 (beacon) as invigil-N1 +mqtt: connected +summary t=13s windows=5 sweeps_per_window=0 sweep_us=0 sweep_us_max=0 ble_captured=498 ble_published=498 ble_msgs=29 ble_dropped=0 ble_buffered=0 spectrum_sent=0 spectrum_dropped=0 status_sent=2 send_ms_max=85 mqtt_busy=0 send_overruns=0 tx_in_scan=2 sweeps_discarded=0 wifi=up mqtt=connected SELF-CHECK FAILED: nrf +summary t=23s windows=9 sweeps_per_window=0 sweep_us=0 sweep_us_max=0 ble_captured=853 ble_published=853 ble_msgs=50 ble_dropped=0 ble_buffered=0 spectrum_sent=0 spectrum_dropped=0 status_sent=1 send_ms_max=64 mqtt_busy=0 send_overruns=0 tx_in_scan=0 sweeps_discarded=0 wifi=up mqtt=connected SELF-CHECK FAILED: nrf +summary t=33s windows=9 sweeps_per_window=0 sweep_us=0 sweep_us_max=0 ble_captured=888 ble_published=888 ble_msgs=51 ble_dropped=0 ble_buffered=0 spectrum_sent=0 spectrum_dropped=0 status_sent=0 send_ms_max=107 mqtt_busy=0 send_overruns=0 tx_in_scan=0 sweeps_discarded=0 wifi=up mqtt=connected SELF-CHECK FAILED: nrf +sntp: clock corrected by +140 ms +summary t=44s windows=10 sweeps_per_window=0 sweep_us=0 sweep_us_max=0 ble_captured=906 ble_published=906 ble_msgs=51 ble_dropped=0 ble_buffered=0 spectrum_sent=0 spectrum_dropped=0 status_sent=2 send_ms_max=68 mqtt_busy=0 send_overruns=0 tx_in_scan=2 sweeps_discarded=0 wifi=up mqtt=connected SELF-CHECK FAILED: nrf +``` + +## Status screen + +``` +Broker running at localhost:1883 (started by the launcher, PID 46048) +Beacon broadcasting on UDP port 18830 (PID 42988) +Logger data\raw\2026-11-17-live-091645 (PID 38604) +Dashboard http://127.0.0.1:8080 (PID 19172) +Unit N1 online +Last status uptime 1m 03s, Wi-Fi RSSI -53 dBm, fw 0.3.0, 6 s ago +Readings ble 77.6/s, spectrum 0.0/s, status 0.1/s, rejected messages 0 +Laptop clock 388 ms behind pool.ntp.org. WARNING: more than 100 ms off, latency results are not valid. +``` diff --git a/docs/setup.md b/docs/setup.md index be0c2c3..bad678d 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -80,7 +80,7 @@ The unit connects to the laptop over Wi-Fi, so the broker must listen on the net The config opens two listeners: plain MQTT on port 1883 for the unit and the Python tools, and WebSockets on port 9001 for the [dashboard](../README.md#dashboard), because a browser cannot open a plain MQTT socket. The same login applies to both. -3. Put the same username and password in `server/config.toml` and in the firmware secrets. Point the unit at the laptop's IP address (find it with `ipconfig`, or `ip addr` on Linux). +3. Put the same username and password in `server/config.toml` and in the firmware secrets. The unit finds the laptop by itself through the launcher's beacon (see [Finding the broker](firmware.md#finding-the-broker)), so its IP address is not needed. Without the launcher, set `MQTT_HOST` in the firmware secrets to the laptop's IP address (find it with `ipconfig`, or `ip addr` on Linux). 4. If Windows Firewall asks, allow Mosquitto on private networks. 5. Start the logger as above. @@ -94,7 +94,7 @@ python -m invigil start | Command | What it does | | --- | --- | -| `start` | Starts Mosquitto with `server/mosquitto.conf`, the logger (session `live-