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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
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
65 66 67 68 69 70 71 72 | |
disconnect() -> None
async
¶
Disconnect from the Wokwi simulator server.
This also stops all active serial monitors.
Source code in src/wokwi_client/client.py
74 75 76 77 78 79 80 81 | |
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
119 120 121 122 123 124 125 126 127 128 129 130 | |
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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
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
324 325 326 327 328 329 330 331 332 333 334 | |
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
311 312 313 314 315 316 317 318 319 320 321 322 | |
pause_simulation() -> None
async
¶
Pause the running simulation.
Source code in src/wokwi_client/client.py
192 193 194 195 196 | |
read_framebuffer_png_bytes(id: str) -> bytes
async
¶
Return the current framebuffer as PNG bytes.
Source code in src/wokwi_client/client.py
373 374 375 | |
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
301 302 303 304 305 306 307 308 309 | |
read_vcd() -> VCDData
async
¶
Read logic analyzer data as VCD (Value Change Dump).
Returns the captured signals from the logic analyzer in VCD format, which can be viewed in tools like PulseView or GTKWave.
Returns:
| Type | Description |
|---|---|
VCDData
|
VCD data containing the vcd string, channel_count, and sample_count. |
Raises:
| Type | Description |
|---|---|
WokwiError
|
If no logic analyzer is present in the diagram. |
Source code in src/wokwi_client/client.py
381 382 383 384 385 386 387 388 389 390 391 392 393 | |
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
223 224 225 226 227 228 229 230 | |
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
198 199 200 201 202 203 204 205 | |
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
377 378 379 | |
save_vcd(path: Path, overwrite: bool = True) -> VCDData
async
¶
Save logic analyzer VCD data to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Destination file path. |
required |
overwrite
|
bool
|
Overwrite existing file (default True). |
True
|
Returns:
| Type | Description |
|---|---|
VCDData
|
VCD data containing the vcd string, channel_count, and sample_count. |
Raises:
| Type | Description |
|---|---|
WokwiError
|
If file exists and overwrite=False, or if no logic analyzer is present in the diagram. |
Source code in src/wokwi_client/client.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
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
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
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
294 295 296 | |
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
336 337 338 339 340 341 342 343 344 | |
start_simulation(firmware: str | list[FlashSection] | None = None, elf: Optional[str] = None, pause: bool = False, chips: list[str] = [], flash_size: Optional[int] = None) -> 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.
For ESP-IDF projects, use upload_idf_firmware() to upload firmware sections
from flasher_args.json, then pass the result's firmware and flash_size here.
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 | list[FlashSection] | None
|
Either a firmware filename (str) or a list of FlashSection objects (from upload_idf_firmware). |
None
|
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). |
[]
|
flash_size
|
Optional[int]
|
Flash size in megabytes (optional, typically from IdfFirmwareUploadResult). |
None
|
Source code in src/wokwi_client/client.py
147 148 149 150 151 152 153 154 155 156 157 158 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 188 189 190 | |
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
264 265 266 267 268 269 270 271 272 273 | |
touch_event(part: str, x: float, y: float, event: str, release_after: Optional[int] = None) -> None
async
¶
Send a touch event to a part with a touchscreen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
part
|
str
|
Part id (e.g. "lcd1"). |
required |
x
|
float
|
X coordinate (touch controller coordinates). |
required |
y
|
float
|
Y coordinate (touch controller coordinates). |
required |
event
|
str
|
Touch event type: "press", "release", or "move". |
required |
release_after
|
Optional[int]
|
For "press" events, automatically release after this many nanoseconds of simulation time (optional). |
None
|
Source code in src/wokwi_client/client.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
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
83 84 85 86 87 88 89 90 91 | |
upload_file(filename: str, local_path: Optional[Path] = None) -> str
async
¶
Upload a local file to the simulator.
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.
Source code in src/wokwi_client/client.py
93 94 95 96 97 98 99 100 101 102 103 | |
upload_idf_firmware(flasher_args_path: str | Path) -> IdfFirmwareUploadResult
async
¶
Upload ESP-IDF firmware from a flasher_args.json file.
Reads flasher_args.json, uploads each flash section (bootloader, partition table, app) individually, and returns the section metadata needed for start_simulation().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flasher_args_path
|
str | Path
|
Path to the flasher_args.json file. |
required |
Returns: An IdfFirmwareUploadResult with firmware sections and optional flash_size.
Source code in src/wokwi_client/client.py
105 106 107 108 109 110 111 112 113 114 115 116 117 | |
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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
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 | |