-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduino_remote.py
More file actions
122 lines (93 loc) · 3.92 KB
/
arduino_remote.py
File metadata and controls
122 lines (93 loc) · 3.92 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import subprocess
from fabric import Connection
# Configuration variables
ARDUINO_PATH = "/path/to/your/arduino/sketches"
ARDUINO_CLI = "/path/to/arduino-cli"
SSH_CMD = "ssh your-vps ssh -p 2222 -i /path/to/.ssh/solveit your-user@localhost"
LIB_PATH = "/path/to/Arduino/libraries"
PORT = ''
FQBN = ''
def detect_board() -> tuple[str, str]:
"""Detect connected Arduino board and return its port and FQBN.
Queries the remote workstation via SSH to list connected Arduino boards,
then parses the output to find the port (e.g., /dev/ttyUSB0) and
Fully Qualified Board Name (e.g., arduino:avr:uno).
Also updates the global PORT and FQBN variables as a side effect.
Returns:
A tuple of (port, fqbn) strings. Empty strings if no board detected.
"""
global PORT, FQBN
result = subprocess.check_output(
f'{SSH_CMD} "{ARDUINO_CLI} board list"',
shell=True,
text=True
)
lines = result.strip().split('\n')
if len(lines) > 1:
parts = lines[1].split()
for part in parts:
if part.startswith('/dev/'):
PORT = part
elif part.count(':') >= 2:
FQBN = part
print(f"Detected: {PORT} {FQBN}")
return PORT, FQBN
def create_sketch(name: str, code: str) -> None:
"""Create an Arduino sketch file locally in /tmp.
Writes the provided code to a .ino file that can then be sent
to the remote workstation for compilation and upload.
Args:
name: Sketch name (used as filename, without .ino extension)
code: The Arduino C++ code to write to the file
"""
with open(f'/tmp/{name}.ino', 'w') as f:
f.write(code)
print(f"Created /tmp/{name}.ino")
def send_sketch(name: str) -> None:
"""Transfer a sketch from /tmp to the remote workstation.
Creates the sketch directory on the remote workstation if needed,
then copies the local .ino file via SSH.
Args:
name: Sketch name (matching the local file in /tmp)
"""
os.system(f'{SSH_CMD} "mkdir -p {ARDUINO_PATH}/{name}"')
os.system(f'cat /tmp/{name}.ino | {SSH_CMD} "bash -c \'cat > {ARDUINO_PATH}/{name}/{name}.ino\'"')
print(f"Sent {name}.ino to workstation")
def compile_sketch(name: str) -> None:
"""Compile an Arduino sketch on the remote workstation.
Args:
name: Sketch name (must already exist on the workstation)
"""
os.system(f'{SSH_CMD} "{ARDUINO_CLI} compile --fqbn {FQBN} {ARDUINO_PATH}/{name}/{name}.ino"')
print(f"Compiled {name}.ino")
vps = Connection('your-vps-user@your-vps-hostname', connect_kwargs={"key_filename": "/path/to/.ssh/id_ed25519"})
workstation = Connection('your-workstation-user@localhost', port=2222, gateway=vps)
def install_lib(lib_name: str) -> bool:
"""Install an Arduino library on the remote workstation.
Args:
lib_name: Name of the library to install (e.g., "Servo", "FastLED")
Returns:
True if installation succeeded, False otherwise
"""
result = workstation.run(f'{ARDUINO_CLI} lib install "{lib_name}"', warn=True)
if result.ok:
print(f"✓ Installed library: {lib_name}")
else:
print(f"✗ Failed to install: {lib_name}")
return result.ok
def compile_lib_sketch(name: str) -> None:
"""Compile an Arduino sketch that uses external libraries.
Like compile_sketch, but includes the custom library path.
Args:
name: Sketch name (must already exist on the workstation)
"""
os.system(f'{SSH_CMD} "{ARDUINO_CLI} --libraries {LIB_PATH} compile --fqbn {FQBN} {ARDUINO_PATH}/{name}/{name}.ino"')
print(f"Compiled {name}.ino")
def upload_sketch(name: str) -> None:
"""Upload a compiled sketch to the connected Arduino board.
Args:
name: Sketch name (must be compiled on the workstation already)
"""
os.system(f'{SSH_CMD} "{ARDUINO_CLI} upload -p {PORT} --fqbn {FQBN} {ARDUINO_PATH}/{name}/{name}.ino"')
print(f"Uploaded {name}.ino")