Created
January 26, 2022 22:22
-
-
Save Desdaemon/709d69c2b8d85facb9387e3d7ac9d93c 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
| #!/usr/bin/python3 | |
| # Source: https://support.particle.io/hc/en-us/articles/360045547634-How-can-I-set-up-my-Argon-or-Boron-via-USB- | |
| from shutil import which | |
| from urllib.request import urlopen | |
| from sys import exit as bail | |
| from subprocess import run | |
| from os import remove | |
| from re import compile as regex | |
| def particle(*cmd, check=True, **args): | |
| cmd = ["particle", *cmd] | |
| display = " ".join(cmd) | |
| print(f"$ {display}") | |
| return run(cmd, check=check, **args) | |
| def set_dfu(): | |
| return particle("usb", "dfu") | |
| def question(prompt): | |
| ans = None | |
| while ans != 'y' and ans != 'n': | |
| ans = input(f"{prompt} (y/n): ") | |
| return ans == 'y' | |
| id_pat = regex(r"Your device id is (.+)\nYour system firmware version is (.+)") | |
| def add(): | |
| if particle('whoami', check=False).returncode != 0: | |
| particle('login') | |
| if not question("Is your device flashing blue?"): | |
| particle('usb', 'start-listening') | |
| res = particle('serial', 'identify', check=False, capture_output=True) | |
| if res.returncode != 0: | |
| err = res.stderr.decode("utf-8") | |
| bail(f"`particle serial identify` failed: {err}") | |
| particle('serial', 'wifi') | |
| ident = res.stdout.decode('utf-8') | |
| dev_id, version = id_pat.search(ident).group(1, 2) | |
| if dev_id == 'undefined': | |
| bail("Error retrieving device id, please unplug your device and try again.") | |
| print(f"Device id: {dev_id}, version {version}") | |
| input("Press Enter when the device is breathing cyan...") | |
| particle('device', 'add', dev_id) | |
| name = str(input("Rename your device (leave blank to skip): ")) | |
| if name: | |
| out = particle('device', 'rename', dev_id, name, check=False, capture_output=True) | |
| if out.returncode != 0: | |
| err = out.stderr.decode('utf-8') | |
| print(f"Failed to rename device: {err}") | |
| particle('usb', 'setup-done', name or dev_id) | |
| print( | |
| """The device should now be visible from your app, | |
| if you connect to the same Wi-Fi network used in the setup.""") | |
| def prompt_reboot(): | |
| input("Press Enter when the device has rebooted...") | |
| if __name__ == '__main__': | |
| if which("particle") is None: | |
| bail( | |
| """Particle CLI could not be found on the path. | |
| Please install Particle CLI by following the instructions here: | |
| https://docs.particle.io/tutorials/developer-tools/cli/ | |
| or double-check that the `particle` command is visible on your path.""") | |
| print( | |
| """Particle Argon Setup Script | |
| During the process, the CLI may refer to a SETUP button. | |
| On the Argon, this is the MODE button. | |
| If setup fails, try unplugging and replugging the device.""") | |
| if not question("Is this your first time setting up? (Choose 'y' if you need to redo)"): | |
| add() | |
| else: | |
| set_dfu() | |
| particle("update") | |
| prompt_reboot() | |
| set_dfu() | |
| particle('flash', '--usb', 'tinker') | |
| fname = '__tmp__.bin' | |
| print(f"Downloading NCP firmware to {fname}...") | |
| with open(fname, 'wb') as file: | |
| res = urlopen( | |
| 'https://github.com/particle-iot/argon-ncp-firmware/releases/download/v0.0.5/argon-ncp-firmware-0.0.5-ota.bin') | |
| if res.status != 200: | |
| reason = res.reason or res.status | |
| bail(f"Failed to download NCP firmware: {reason}") | |
| file.write(res.read()) | |
| print("...Done") | |
| particle('flash', '--serial', fname) | |
| prompt_reboot() | |
| add() | |
| try: | |
| remove(fname) | |
| except: | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment