API
Asynchronous client for the Wokwi Simulation API.
This class provides methods to connect to the Wokwi simulator, upload files, control simulations, and monitor serial output. It is designed to be asyncio-friendly and easy to use in Python scripts and applications. For a synchronous interface, see WokwiClientSync.
Initialize the WokwiClient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
API token for authentication (get from https://wokwi.com/dashboard/ci). |
required |
server
|
Optional[str]
|
Optional custom server URL. Defaults to the public Wokwi server. |
None
|
Source code in src/wokwi_client/client.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
connect() -> dict[str, Any]
async
¶
Connect to the Wokwi simulator server.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary with server information (e.g., version). |
Source code in src/wokwi_client/client.py
56 57 58 59 60 61 62 63 |
|
disconnect() -> None
async
¶
Disconnect from the Wokwi simulator server.
This also stops all active serial monitors.
Source code in src/wokwi_client/client.py
65 66 67 68 69 70 71 72 |
|
download(name: str) -> bytes
async
¶
Download a file from the simulator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the file to download. |
required |
Returns:
Type | Description |
---|---|
bytes
|
The downloaded file content as bytes. |
Source code in src/wokwi_client/client.py
99 100 101 102 103 104 105 106 107 108 109 110 |
|
download_file(name: str, local_path: Optional[Path] = None) -> None
async
¶
Download a file from the simulator and save it to a local path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the file to download. |
required |
local_path
|
Optional[Path]
|
The local path to save the downloaded file. If not provided, uses the name as the path. |
None
|
Source code in src/wokwi_client/client.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
gpio_list() -> list[str]
async
¶
Get a list of all GPIO pins available in the simulation.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: Example: ["esp32:GPIO0", "esp32:GPIO1", ...] |
Source code in src/wokwi_client/client.py
297 298 299 300 301 302 303 304 305 306 307 |
|
listen_pin(part: str, pin: str, listen: bool = True) -> None
async
¶
Start or stop listening for changes on a pin.
When enabled, "pin:change" events will be delivered via the transport's event mechanism.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
str
|
The part id. |
required |
pin
|
str
|
The pin name. |
required |
listen
|
bool
|
True to start listening, False to stop. |
True
|
Source code in src/wokwi_client/client.py
284 285 286 287 288 289 290 291 292 293 294 295 |
|
pause_simulation() -> None
async
¶
Pause the running simulation.
Source code in src/wokwi_client/client.py
165 166 167 168 169 |
|
read_framebuffer_png_bytes(id: str) -> bytes
async
¶
Return the current framebuffer as PNG bytes.
Source code in src/wokwi_client/client.py
319 320 321 |
|
read_pin(part: str, pin: str) -> PinReadMessage
async
¶
Read the current state of a pin.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
str
|
The part id (e.g. "uno"). |
required |
pin
|
str
|
The pin name (e.g. "A2"). |
required |
Source code in src/wokwi_client/client.py
274 275 276 277 278 279 280 281 282 |
|
restart_simulation(pause: bool = False) -> None
async
¶
Restart the simulation, optionally starting paused.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pause
|
bool
|
Whether to start the simulation paused (default: False). |
False
|
Source code in src/wokwi_client/client.py
196 197 198 199 200 201 202 203 |
|
resume_simulation(pause_after: Optional[int] = None) -> None
async
¶
Resume the simulation, optionally pausing after a given number of nanoseconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pause_after
|
Optional[int]
|
Number of nanoseconds to run before pausing again (optional). |
None
|
Source code in src/wokwi_client/client.py
171 172 173 174 175 176 177 178 |
|
save_framebuffer_png(id: str, path: Path, overwrite: bool = True) -> Path
async
¶
Save the current framebuffer as a PNG file.
Source code in src/wokwi_client/client.py
323 324 325 |
|
serial_monitor(callback: Callable[[bytes], Any]) -> asyncio.Task[None]
¶
Start monitoring the serial output in the background and invoke callback
for each line.
This method does not block: it creates and returns an asyncio.Task that runs until the transport is closed or the task is cancelled. The callback may be synchronous or async.
Example
task = client.serial_monitor(lambda line: print(line.decode(), end="")) ... do other async work ... task.cancel()
Source code in src/wokwi_client/client.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
serial_monitor_cat(decode_utf8: bool = True, errors: str = 'replace') -> None
async
¶
Print serial monitor output to stdout as it is received from the simulation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decode_utf8
|
bool
|
Whether to decode bytes as UTF-8. If False, prints raw bytes (default: True). |
True
|
errors
|
str
|
How to handle UTF-8 decoding errors. Options: 'strict', 'ignore', 'replace' (default: 'replace'). |
'replace'
|
Source code in src/wokwi_client/client.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
serial_write(data: Union[bytes, str, list[int]]) -> None
async
¶
Write data to the simulation serial monitor interface.
Source code in src/wokwi_client/client.py
267 268 269 |
|
set_control(part: str, control: str, value: Union[int, bool, float]) -> None
async
¶
Set a control value (e.g. simulate button press).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
str
|
Part id (e.g. "btn1"). |
required |
control
|
str
|
Control name (e.g. "pressed"). |
required |
value
|
Union[int, bool, float]
|
Control value to set (float). |
required |
Source code in src/wokwi_client/client.py
309 310 311 312 313 314 315 316 317 |
|
start_simulation(firmware: str, elf: Optional[str] = None, pause: bool = False, chips: list[str] = []) -> None
async
¶
Start a new simulation with the given parameters.
The firmware and ELF files must be uploaded to the simulator first using the
upload()
or upload_file()
methods.
The firmware file is required for the simulation to run.
The ELF file is optional and can speed up the simulation in some cases.
The optional chips
parameter can be used to load custom chips into the simulation.
For each custom chip, you need to upload two files:
- A JSON file with the chip definition, called <chip_name>.chip.json
.
- A binary file with the chip firmware, called <chip_name>.chip.bin
.
For example, to load the inverter
chip, you need to upload the inverter.chip.json
and inverter.chip.bin
files. Then you can pass ["inverter"]
to the chips
parameter,
and reference it in your diagram.json file by adding a part with the type chip-inverter
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
firmware
|
str
|
The firmware binary filename. |
required |
elf
|
Optional[str]
|
The ELF file filename (optional). |
None
|
pause
|
bool
|
Whether to start the simulation paused (default: False). |
False
|
chips
|
list[str]
|
List of custom chips to load into the simulation (default: empty list). |
[]
|
Source code in src/wokwi_client/client.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
stop_serial_monitors() -> None
¶
Stop all active serial monitor tasks.
This method cancels all tasks created by the serial_monitor method. After calling this method, all active serial monitors will stop receiving data.
Source code in src/wokwi_client/client.py
237 238 239 240 241 242 243 244 245 246 |
|
upload(name: str, content: bytes) -> None
async
¶
Upload a file to the simulator from bytes content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name to use for the uploaded file. |
required |
content
|
bytes
|
The file content as bytes. |
required |
Source code in src/wokwi_client/client.py
74 75 76 77 78 79 80 81 82 |
|
upload_file(filename: str, local_path: Optional[Path] = None) -> str
async
¶
Upload a local file to the simulator.
If you specify the local_path to the file flasher_args.json
(IDF flash information),
the contents of the file will be processed and the correct firmware file will be
uploaded instead, returning the firmware filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
The name to use for the uploaded file. |
required |
local_path
|
Optional[Path]
|
Optional path to the local file. If not provided, uses filename as the path. |
None
|
Returns: The filename of the uploaded file (useful for idf when uploading flasher_args.json).
Source code in src/wokwi_client/client.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
wait_until_simulation_time(seconds: float) -> None
async
¶
Pause and resume the simulation until the given simulation time (in seconds) is reached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seconds
|
float
|
The simulation time to wait for, in seconds. |
required |
Source code in src/wokwi_client/client.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
Synchronous client for the Wokwi Simulation API.
Design
• A private asyncio loop runs on a dedicated background thread.
• Public methods mirror the async API by submitting the underlying
coroutine calls onto that loop and waiting for results (blocking).
• Long-lived streamers (serial monitors) are scheduled on the loop and
tracked, so we can cancel & drain them on disconnect()
.
Source code in src/wokwi_client/client_sync.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
__getattr__(name: str) -> Any
¶
Delegate attribute access to the underlying async client.
If the attribute on WokwiClient
is a coroutine function, return a
sync wrapper that blocks until the coroutine completes.
Source code in src/wokwi_client/client_sync.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
connect() -> dict[str, Any]
¶
Connect to the simulator (blocking) and return server info.
Source code in src/wokwi_client/client_sync.py
94 95 96 |
|
serial_monitor(callback: Callable[[bytes], Any]) -> None
¶
Start monitoring the serial output in the background and invoke callback
for each line. Non-blocking. Runs until disconnect()
.
The callback may be sync or async. Exceptions raised by the callback are swallowed to keep the monitor alive (add your own logging as needed).
Source code in src/wokwi_client/client_sync.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
serial_monitor_cat(decode_utf8: bool = True, errors: str = 'replace') -> None
¶
Print serial monitor output in the background (non-blocking). Runs until disconnect()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decode_utf8
|
bool
|
Whether to decode bytes as UTF-8 (default True). |
True
|
errors
|
str
|
UTF-8 decoding error strategy ('strict'|'ignore'|'replace'). |
'replace'
|
Source code in src/wokwi_client/client_sync.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
stop_serial_monitors() -> None
¶
Stop all active serial monitor background tasks.
Source code in src/wokwi_client/client_sync.py
190 191 192 193 194 |
|