Skip to content

Instantly share code, notes, and snippets.

@cobrce
Last active November 8, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobrce/221a50ea80d52c9b8598d5d06a26c75f to your computer and use it in GitHub Desktop.
Save cobrce/221a50ea80d52c9b8598d5d06a26c75f to your computer and use it in GitHub Desktop.
Upload a hex file to RASPBERRY PI via FTP then flash it into AVR microcontroller with AVRDUDE (python 3.6)
from ftplib import FTP
from os import path
import spur # install it with PIP
address = "ip_address_of_raspberry_pi"
user_name = "user_name"
password = "password"
hex_file = r"absolute/relative_path_to_hex_file"
remote_hex_file = path.basename(hex_file)
eeprom_file = r"relative_path_to_eeprom_file"
remote_eeprom_file = path.basename(eeprom_file)
mcpu = "micro_controller" # example : atmega32
class MyFtp(FTP):
def __init__(self, host='', user='', passwd=''):
super().__init__(host=host, user=user, passwd=passwd)
def send_file(self,path,remote_name):
with open(path,"rb") as file:
return "successfully" in super().storbinary(f"STOR {remote_name}",file)
def program_over_ssh():
shell = spur.SshShell(address,user_name,password,missing_host_key=spur.ssh.MissingHostKey.accept)
result = shell.run(f"avrdude -p {mcpu} -c linuxgpio -U flash:w:{remote_hex_file}:i -U eeprom:w:{remote_eeprom_file}:i".split(" "))
print(result.output.decode('ascii'))
print(result.stderr_output.decode('ascii'))
input()
def main():
try :
session = MyFtp(address,user_name,password)
if session.send_file(hex_file,remote_hex_file) and session.send_file(eeprom_file,remote_eeprom_file):
print("Files sent, open ssh session")
program_over_ssh()
else:
print("Files not sent, programming aborted")
except Exception as e:
print("Exception occured")
repr(e)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment