Impresión 3D
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
demo-kit/ es lo que simula el dispositivo para la demostración en la página de inicio. El dispositivo en sí no lo necesita.
A Raspberry Pi sits next to a 3D printer and runs one Python program. The program answers what the print job is doing, how hot nozzle and bed are and what the layers look like, takes pause, resume and cancel, and brings its own screen: a device UI that the Connectivitizer app shows on the phone. The page draws the part layer by layer as it grows. Neither the Pi nor the phone needs an open port or a public address.
The printer is simulated. The program ships with a simulator, a pretend print job that advances in real time, so the example runs with nothing attached. Where a real printer would be connected is marked in the code and below; that part is not implemented.
| Folder | What it is |
|---|---|
pi/printer_companion.py | The device: an HTTP and WebSocket server on 127.0.0.1 (standard library only), the simulator, and the Connectivitizer agent that exposes the server as the service api. |
ui/printer.html | The printer'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 printer for the demo on the landing page (below). |
pip install -r pi/requirements.txt, which is huelake-connectivitizer), a device key and a shared key from the web app.cd src/examples/printer/pi
python printer_companion.pyPrinter companion (simulator) on http://127.0.0.1:8137
the screen: http://127.0.0.1:8137/.cz/ui.html
the job: http://127.0.0.1:8137/job
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:8137/job, curl -X POST http://127.0.0.1:8137/job/pause. It listens on the loopback address only.
A pretend print takes 15 minutes; 20 seconds after it is done or cancelled the next one starts (a vase, a tower, a gear, in turn). The heaters start cold and the pretend print does not wait for them.
| Option | Default | |
|---|---|---|
--port (or PRINTER_PORT) | 8137 | The local port; 0 takes any free one. |
--print-seconds | 900 | How long one pretend print takes. |
--restart-seconds | 20 | The pause before the next pretend print. |
--printer | simulator | Anything else (a serial port) ends with "not implemented". |
cd src/examples/printer/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 printer_companion.pysecrets.json (not committed, see pi/.gitignore), or the same as environment variables:
secrets.json | Variable | |
|---|---|---|
deviceKey | CZ_DEVICE_KEY | czd_...; web app: Credentials › Create credential › Device key |
sharedKey | CZ_SHARED_KEY | czs_...; web app: Credentials › Generate a shared key. The phone has the same one. |
deviceName | CZ_DEVICE_NAME | How the device is called in the workspace. Default printer-pi. |
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 app; 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 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 /job and /temps once a second instead. An iPhone and a PC do not show device UIs yet (device UIs, "Not yet").
Cancel has to be held for a good second, until the ring is full, so that a stray tap does not end a print.
All under the service api.
| Request | Answer |
|---|---|
GET /job | { "state": "printing" | "paused" | "idle" | "done" | "cancelled", "file", "progress" (0 to 1), "layer", "layers", "elapsedSeconds", "remainingSeconds", "filamentUsedMm", "speedPercent" }. layer counts from 1. The simulator is never idle. |
GET /temps | { "t", "nozzle": { "actual", "target" }, "bed": { "actual", "target" }, "history": [ { "t", "nozzle", "bed" }, … ] }. Degrees Celsius; a target of 0 means the heater is off. history is the last 10 minutes, one entry every 5 seconds, oldest first. t is the device's clock in seconds since 1970. |
GET /toolpath?layer=n | { "layer", "layers", "z", "polylines": [ [x0, y0, x1, y1, …], … ] }: the outline of one layer. x and y from 0 to 1 across the bed, z the height of the layer in the same unit. Without layer: the current one. 404 for a layer the job does not have. Under 1 KB for most layers, up to 3 KB for the gear. |
POST /job/pause, POST /job/resume, POST /job/cancel | The new job, as GET /job. 409 with { "error", "job" } when the job is not in a state for it (resume while printing, say). |
WebSocket /events | { "type": "state", "state", "job" } when it opens and on every change of the state; { "type": "progress", "t", "job", "temps": { "nozzle", "bed" } } about once a second. 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. |
The page asks for the outlines one layer at a time, three requests at once, and at most about 120 layers of a job (every second or third one of a taller print).
The printer is behind the class Printer in printer_companion.py; the web server only knows that class. SimulatedPrinter implements it. SerialPrinter is the place for a real one: it raises "not implemented" and has never been run against a printer. Its comment describes the two ways to fill it in:
pyserial: the program sends the file to the printer line by line, reads the temperatures with M105, takes the layer from the slicer's ;LAYER: comments and the outlines from the extruding G1 moves of the file.Neither was written or tried. In particular the simulator's cancel only changes a number, while a real one has to stop the motion and switch the heaters off.
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. It differs in speed only: a whole print in three and a half minutes, starting at 35 %, the next one six seconds after the end. The outlines of the three parts come from the same formulas in both files.
cd src/connectivitizer
node scripts/build-demos.mjs --only printer --out <folder> # writes <folder>/printer/ui.html, which opens from the file systemdone, and the page in a browser talking to it (polling).