Last active
January 14, 2024 23:56
-
-
Save chriscummings/683d2a7b8f8a7a6b0c859324ec94e6e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import paramiko | |
from dotenv import load_dotenv | |
# Sketch params: | |
sketch_destination = "/home/chris/demeter" | |
sketch_files = [ | |
"demeter.ino", | |
"demeter.h", | |
"demeter.py", | |
".env" | |
] | |
sketch_libraries = [ | |
"OneWire", | |
"DallasTemperature", | |
"Adafruit SSD1306", | |
"WatchDog" | |
] | |
# Pi params: | |
path_to_arduino_cli = "/home/chris/bin/arduino-cli" | |
path_to_flash_script = "/home/chris/flash.py" | |
#========================================================= | |
# Set creds. | |
load_dotenv() | |
PI_USER = os.getenv("PI_USER") | |
PI_PASS = os.getenv("PI_PASS") | |
PI_PORT = int(os.getenv("PI_PORT")) | |
PI_IP = os.getenv("PI_IP") | |
paramiko.util.log_to_file("ssh.log") | |
# SFTP | |
transport = paramiko.Transport((PI_IP, PI_PORT)) | |
transport.connect(None, PI_USER, PI_PASS) | |
sftp = paramiko.SFTPClient.from_transport(transport) | |
# SSH | |
ssh = paramiko.client.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
ssh.connect(PI_IP, port=PI_PORT, username=PI_USER, password=PI_PASS) | |
# Ensure parent directory. | |
stdin, stdout, stderr = ssh.exec_command("mkdir "+sketch_destination) | |
output = " ".join(stdout.readlines()) # Waits for command to finish | |
# Upload sketch source files to RaspberryPi | |
for f in sketch_files: | |
local_path = os.path.abspath(f) | |
dest_path = os.path.join(sketch_destination, f) | |
sftp.put(local_path, dest_path) | |
# Install sketch libraries to Pi. | |
stdin, stdout, stderr = ssh.exec_command(path_to_arduino_cli+" lib list") | |
output = " ".join(stdout.readlines()) # Waits for command to finish | |
for l in sketch_libraries: | |
print("Checking library: "+l) | |
if l in output: | |
print(l+" already installed.") | |
else: | |
print("Installing library: "+l) | |
stdin, stdout, stderr = ssh.exec_command(path_to_arduino_cli+" lib install \""+l+"\"") | |
stdout.channel.set_combine_stderr(True) | |
output = "\n".join(stdout.readlines()) # Waits for command to finish | |
print(output) | |
print("--Handing over control to flash script--") | |
# Compile & flash. | |
stdin, stdout, stderr = ssh.exec_command("python3 "+path_to_flash_script+" "+sketch_destination) | |
stdout.channel.set_combine_stderr(True) | |
output = "".join(stdout.readlines()) # Waits for command to finish | |
print(output) | |
# Cleanup | |
if ssh: | |
ssh.close() | |
del ssh, stdin, stdout, stderr | |
if sftp: sftp.close() | |
if transport: transport.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment