-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (54 loc) · 2.19 KB
/
Copy pathmain.py
File metadata and controls
71 lines (54 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# SPDX-License-Identifier: MIT
# Copyright (C) 2025, CodeMagic LTD
import asyncio
import os
from pathlib import Path
import requests
from wokwi_client import GET_TOKEN_URL, WokwiClient
EXAMPLE_DIR = Path(__file__).parent
HELLO_WORLD_URL = "https://github.com/wokwi/esp-idf-hello-world/raw/refs/heads/main/bin"
FIRMWARE_FILES = {
"hello_world.bin": f"{HELLO_WORLD_URL}/hello_world.bin",
"hello_world.elf": f"{HELLO_WORLD_URL}/hello_world.elf",
}
SLEEP_TIME = int(os.getenv("WOKWI_SLEEP_TIME", "10"))
async def main() -> None:
token = os.getenv("WOKWI_CLI_TOKEN")
if not token:
raise SystemExit(
f"Set WOKWI_CLI_TOKEN in your environment. You can get it from {GET_TOKEN_URL}."
)
for filename, url in FIRMWARE_FILES.items():
if (EXAMPLE_DIR / filename).exists():
continue
print(f"Downloading {filename} from {url}")
response = requests.get(url)
response.raise_for_status()
with open(EXAMPLE_DIR / filename, "wb") as f:
f.write(response.content)
client = WokwiClient(token)
print(f"Wokwi client library version: {client.version}")
hello = await client.connect()
print("Connected to Wokwi Simulator, server version:", hello["version"])
# Upload the diagram and firmware files
await client.upload_file("diagram.json", EXAMPLE_DIR / "diagram.json")
await client.upload_file("hello_world.bin", EXAMPLE_DIR / "hello_world.bin")
await client.upload_file("hello_world.elf", EXAMPLE_DIR / "hello_world.elf")
# Start the simulation
await client.start_simulation(
firmware="hello_world.bin",
elf="hello_world.elf",
)
# Stream serial output for a few seconds
serial_task = asyncio.create_task(client.serial_monitor_cat())
# Alternative lambda version
# serial_task = client.serial_monitor(
# lambda line: print(line.decode("utf-8", errors="replace"), end="", flush=True)
# )
print(f"Simulation started, waiting for {SLEEP_TIME} seconds…")
await client.wait_until_simulation_time(SLEEP_TIME)
serial_task.cancel()
# Disconnect from the simulator
await client.disconnect()
if __name__ == "__main__":
asyncio.run(main())