Observatory
demo-kit/ is what simulates the device for the demo on the start page. The device itself does not need it.
demo-kit/ is what simulates the device for the demo on the start page. The device itself does not need it.
A Raspberry Pi stands with a telescope at a dark site, a cabin or a club field, behind a mobile router. It runs one Python program that points the mount, takes exposures and keeps a live stack of them. Two things talk to it, each from another network and each through its own Connectivitizer session:
Neither the Pi nor the PC nor the phone needs an open port or a public address, and none of them is ever in the same network as another.
The telescope is simulated. The program ships with a pretend mount and a pretend camera, so the example runs with nothing attached. Every frame is synthetic: an invented object of the same kind as the target (a spiral galaxy for M51, glowing gas for M42, a star cluster for M13, a ring for M57) with stars around it and fresh noise on every exposure. The names and coordinates of the four targets are real; the pictures are not pictures of them. Where a real mount and camera would be connected is marked in the code and below; that part is not implemented.
| Folder | What it is |
|---|---|
pi/observatory.py | The device: an HTTP and WebSocket server on 127.0.0.1, the simulator, the live stack, and the Connectivitizer agent that exposes the server as the service api. |
home/stacker.py | The program for the PC at home: a session to the device, one request for every new frame, the stack as files. |
ui/observatory.html | The observatory'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 observatory for the demo on the landing page (below). |
sudo apt install python3-numpy or the pip install below brings numpy.huelake-connectivitizer, in both requirements.txt), and from the web app a device key for the Pi, an API key for the PC, and one shared key that the Pi, the PC and the phone all hold.cd src/examples/observatory/pi
pip install numpy
python observatory.pyObservatory (simulator: every frame is synthetic) on http://127.0.0.1:8161
the screen: http://127.0.0.1:8161/.cz/ui.html
the state: http://127.0.0.1:8161/state
Local only: Connectivitizer is NOT started (no keys: fill in secrets.json, or set CZ_DEVICE_KEY and CZ_SHARED_KEY).Without keys it runs local only. Open the screen in a browser on the same machine, or ask it: curl http://127.0.0.1:8161/state, curl -X POST -d '{"target":"m42"}' http://127.0.0.1:8161/slew. It listens on the loopback address only.
The pretend mount starts on M51 and the pretend camera takes one exposure after the other, two seconds each. An exposure takes as long as it says, so a stack of 30 frames takes a minute.
| Option | Default | |
|---|---|---|
--port (or OBSERVATORY_PORT) | 8161 | The local port; 0 takes any free one. |
--slew-rate | 4 | Simulator: how many degrees a second the pretend mount turns. |
--tour-frames | 0 | Slew to the next target after this many frames; 0 stays on the target. |
--telescope | simulator | Anything else (the address of a mount) ends with "not implemented". |
On the same machine, without Connectivitizer, to see what it does:
cd src/examples/observatory/home
pip install numpy
python stacker.py --local http://127.0.0.1:8161Local: asking http://127.0.0.1:8161 without Connectivitizer.
stack 1: frame 8 added, 1 stacked, noise 207.9 counts, signal to noise 1.1
stack 1: frame 9 added, 2 stacked, noise 147.2 counts, signal to noise 1.6
stack 1: frame 10 added, 3 stacked, noise 119.9 counts, signal to noise 1.9It asks for the newest frame twice a second, adds every new one to a running average and writes the average after every frame to home/stacks/stack-<serial>.npy (the numbers, as float32) and stack-<serial>.png (stretched, to look at). When the observatory slews or its exposure or gain changes, a new stack with a new serial begins on both sides. --frames n stops after n frames, --out names another folder.
The noise of an average of n frames falls with the square root of n: the lines above show it at 208, 147 and 120 counts for one, two and three frames. The stacker estimates it from the picture itself, as the observatory does for its own stack.
On the Pi:
cd src/examples/observatory/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
.venv/bin/python observatory.pyOn the PC at home:
cd src/examples/observatory/home
python -m venv .venv && .venv/bin/pip install -r requirements.txt
cp secrets.example.json secrets.json # then fill it in
.venv/bin/python stacker.pyEach secrets.json (not committed, see the .gitignore next to it), or the same as environment variables:
secrets.json | Variable | |
|---|---|---|
deviceKey (Pi) | CZ_DEVICE_KEY | czd_...; web app: Credentials › Create credential › Device key |
apiKey (PC) | CZ_API_KEY | czk_...; web app: Credentials › Create credential |
sharedKey | CZ_SHARED_KEY | czs_...; web app: Credentials › Generate a shared key. The same one on the Pi, the PC and the phone. |
deviceName | CZ_DEVICE_NAME | How the device is called in the workspace. Default observatory, on both sides. |
The device appears in the workspace the first time it connects. The SDK forwards each stream of a session as plain TCP to 127.0.0.1:<port>, so the program's own server answers the stacker and the app; nothing else on the Pi is reachable. The stacker prints how its session is routed, direct (peer to peer) or e2e-relay, as its own endpoint established it. A frame of the simulator is 154 KB, one every two seconds at the default exposure.
In the Connectivitizer app on Android, signed in to the same workspace and holding the same shared key: pick the device. The app asks it for /.cz/ui.json, loads /.cz/ui.html through the session and shows it full screen, with the bar above it in the colours of the manifest. In the app the page gets its updates over a WebSocket (connectivitizer.webSocket("/events")); opened in a browser straight from the device it has no such bridge and asks /state every 0.6 seconds instead. An iPhone and a PC do not show device UIs yet (device UIs, "Not yet").
The page draws the live stack on a canvas from the numbers the device sends (GET /stack, 77 KB for the simulator's camera, once per frame); it holds no pictures of its own. The line Frame downlink says which frame a program last pulled with GET /frame/latest and how long ago: that is the stacker at home, seen from the phone. The chart shows the sky around the celestial pole, from declination +90° in the middle to −30° at the rim, with the targets and the place the mount points at; a tap on a target there or on its button slews to it.
All under the service api. Right ascension is in hours, declination in degrees.
| Request | Answer |
|---|---|
GET /state | { "t", "simulated", "mount": { "state": "tracking" | "slewing", "ra", "dec", "target": { "id", "name", "title", "kind", "ra", "dec" } }, "camera": { "exposureSeconds", "gain", "width", "height" }, "capture": { "running", "exposureProgress" (0 to 1), "sequence" }, "stack": { "serial", "frames", "integrationSeconds", "noise", "snr" }, "downlink": { "framesServed", "lastSequence", "lastSecondsAgo" } }. sequence counts the frames since the program started, serial the stacks. noise is in counts of the camera; noise and snr are estimates and 0 while the stack is empty. lastSecondsAgo is null until a frame was pulled. t is the device's clock in seconds since 1970. |
GET /targets | [ { "id", "name", "title", "kind", "ra", "dec" }, … ]. kind is spiral, nebula, cluster or ring. |
GET /frame/latest | The newest exposure, application/octet-stream: a head of 16 bytes, then the samples as uint16, row by row, red, green, blue. 404 while the stack has no frame (during a slew, say). With ?after=n: 204 while frame n is still the newest. |
GET /stack | The live stack, in the same form with samples of 8 bits: the average of all frames, the sky taken off, stretched for the screen. 404 while the stack has no frame. |
POST /slew | Body { "target": "m42" } or { "ra": 5.59, "dec": -5.4 }. Starts the slew and a new stack; answers the new state, as GET /state. 400 with { "error" } for an unknown target or a position outside 0 to 24 hours and -90 to 90 degrees. |
POST /capture | Body { "running": true | false, "exposureSeconds": 1 | 2 | 4 | 8, "gain": 0 to 300 }, each member optional. Another exposure or gain starts a new stack. Answers the new state; 400 with { "error" } for other values. |
WebSocket /events | { "type": "state", "state": { … } } (as GET /state) when it opens, once a second, four times a second during a slew, and at once when a slew starts or ends or the capture changes; { "type": "frame", "sequence", "stack": { … } } when a frame was added. Nothing is expected from the other side. |
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. |
The head of a picture, little endian: the four bytes CZFR; width and height (uint16 each); channels and bits per sample (uint8 each); the serial of the stack (uint16); a number (uint32), which is the frame's sequence for /frame/latest and the count of stacked frames for /stack.
The gain is in units of 0.1 dB, as many astronomy cameras count it. In the simulator a longer exposure collects more light for the same read noise, and a higher gain lowers the read noise a little.
The hardware is behind the classes Mount and Camera in observatory.py; the rest of the program (the stack, the estimates, the web server) only knows those two. SimulatedMount and SimulatedCamera implement them. RealMount and RealCamera are the places for a telescope: both raise "not implemented" and have never been run against hardware. Their comments describe what would have to be filled in:
picamera2 for a Raspberry Pi camera, or through INDI for an astronomy camera: set exposure time and gain, take the raw frame, bin it down to a size that travels well.Neither was written or tried. A real setup also needs what the simulator leaves out: limits and a meridian flip for the mount, exposures that are started ahead of time, and frames that are aligned to each other before they are averaged, because a real mount does not track perfectly. The stacker averages the frames as they come.
The landing page of the web app shows this page without any hardware (demo kit): ui/demo-data.js is the simulator once more, in JavaScript, answering the same requests and the /events socket with the same shapes. The sky comes from the same formulas in both files, and the demo's stack is a real average of its noisy frames. It differs in this: the demo opens on a stack of two frames, the pretend mount turns 20 degrees a second, the telescope moves on to the next target after 45 frames, and a pretend program pulls every frame within a second after it was taken.
cd src/connectivitizer
node scripts/build-demos.mjs --only observatory --out <folder> # writes <folder>/observatory/ui.html, which opens from the file systempi/observatory.py was run on a Windows PC with Python 3.12 and numpy 2.4, in simulator mode and local only. Every request in the table above was checked against it, with the answers for wrong input, the WebSocket (handshake, state and frame messages, close) and slews to all four targets and to a position given by coordinates.home/stacker.py was run with --local against that program on the same machine, over plain HTTP on the loopback address. It stacked the frames, followed a slew into a new stack and wrote its files; the lines under Run the stacker are from that run.