Set a Pixelblaze pattern and brightness over WebSocket
TL;DR. Pixelblaze takes JSON on WebSocket port 81. brightness 0.5 matches the slider and is not saved. activeProgramId switches a pattern and is saved.
This is a commercial product. AgentOnStage isn't paid for this listing.
Commercial productLED/VideoIntermediatePixelblazeWebSocket
What it does
ElectroMage's Pixelblaze Websockets API page says a Pixelblaze is controlled by a WebSocket on port 81. JSON frames and binary frames control and edit patterns. The page names three clients that already use it: the web app the board serves at its own IP address, Firestorm, an open-source React app that controls many boards at once, shows which patterns are on which board, launches a pattern on every board that has it, sequences a playlist, and clones a board, and pixelblaze-client, a Python project for one or more boards.
Exported variables are how a pattern takes live numbers. On a V1 board, which the page dates as pre-2019, a variable that the pattern uses but does not initialize can be set, and it defaults to 0 if nothing else sets it. If the pattern assigns that variable itself, the page says the assignment overwrites it every time and setVars has no effect. On V2 and on V3 Standard or Pico, a top-level variable is exported and given a default:
export var myVar = 2
A JSON text frame sets variables on the pattern that is running:
{"setVars": {"myVar": 3.14159, "var2": 42}}
{"getVars": true} reads them back. The reply is a vars object. The page says that reply shows the state only after the last pixel of the current frame has been render()ed.
{"listPrograms": true} lists the patterns stored on the board. The reply is binary, split across frames. Byte 0 is 0x07. Byte 1 is a start or end flag, 0x01 or 0x04, or 0x05 when the list fits in one frame. The rest of the frame is a tab-separated list of ids and names. The page says a client should concatenate frames until the end flag 0x04 is set. A pattern id identifies a pattern on that board. The page's example is 7MuJmcy4FZbs9jGbB. The same .epe file imported onto two boards gets a different id on each board. After it is created or imported, the id stays put on that board across edits.
{"activeProgramId": "<id>"} loads a stored pattern. The page says that change is saved and survives a restart. Brightness is a separate frame, a number from 0.0 to 1.0, the same control as the brightness slider. {"brightness": 0.5} is the example. The page says this value is not saved between restarts.
UI controls defined in the pattern are a second path. The page says sliders and color pickers are supported. A control's handler runs when the pattern activates and when the control is used. {"getControls": "<id>"} reads controls for that pattern id, including a pattern that is not the active one. The sample reply nests the controls under the id: hsvPickerMyColor is [0.77, 0.84, 1] and sliderMySlider is 0.4431. setControls writes the active pattern. A slider is 0.0 to 1.0. An HSV or RGB picker is an array of three numbers, also 0.0 to 1.0. Those values are not stored by default. The page says that is so the flash is not worn out. To store them, wait after the last change and send the same object with "save": true.
Set WLED pixel props over the JSON API is a different controller. WLED takes an HTTP POST to /json/state. This page is Pixelblaze's WebSocket on port 81.
How to set it up
- Open a WebSocket to the board's IP on port 81. The page says JSON text frames and binary frames are both used. Start with
{"getVars": true}or{"listPrograms": true}before you write anything. - On a V2 or V3 pattern, export the variables you intend to drive, with a default, as in
export var myVar = 2. On a V1 pattern, leave the variable uninitialized ifsetVarsshould stick. The page says an assignment inside the pattern overwritessetVars. - Send
{"setVars": {"myVar": 3.14159}}while that pattern is active. Read it back with{"getVars": true}after a frame has rendered. The page says the reply is the state after the last pixel'srender(). - Switch patterns with
{"activeProgramId": "<id>"}, using an id from the program list. The page says that choice is saved across a restart. Set{"brightness": 0.5}for the slider. The page says brightness is not saved across a restart. - Leave
"save": trueoff asetControlsframe unless you mean to write flash. The page says the default is not to persist, and that a save should wait until the control has stopped moving.
Source: Pixelblaze Websockets API.
FAQ
Does brightness survive a power cycle?
The page says activeProgramId is saved and persists if the device restarts. Brightness uses the same range as the slider, 0.0 to 1.0, and the page says it will not be saved between restarts. Control values also stay unsaved until a frame includes "save": true.
Can setVars drive a variable the pattern assigns itself?
On V2 and V3 the variable has to be exported. On V1, the page says a variable the pattern sets on its own is overwritten each time, and setVars will not take effect. A V1 variable that is used and not initialized defaults to 0 and can be set.
Is the pattern list JSON?
listPrograms is answered with binary frames. The page says byte 0 is 0x07, the next byte flags the start or the end, and the rest is a tab-separated list of ids and names. A one-frame list uses 0x05. Keep reading until the end flag 0x04.
Also on this rig
Original source: https://electromage.com/docs/websockets-api/