Particle Command Line Interface
Contents
Particle Command Line Interface#
The Particle Argon is a microcontroller board similar to an Arduino in concept but with extended wifi and cloud support.
The Particle command line interface provides is a javascript based library exposing much of the Particle device functionality. This notebook shows how the command line interface can be use within Google Colab notebook.
Installation of the Particle command line interface#
%%capture
!bash <( curl -sL https://particle.io/install-cli )
Login to Particle#
import getpass
import subprocess
particle_cli = "/root/bin/particle"
username = getpass.getpass(prompt="Username: ")
password = getpass.getpass(prompt="Password: ")
process = subprocess.run([particle_cli, "login",
"--username", username,
"--password", password],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process.stderr.decode("utf-8"):
print(process.stderr.decode("utf-8"))
else:
print(f"Successfully logged in to Particle Device Cloud as {username}")
process = subprocess.run([particle_cli, "list"], stdout=subprocess.PIPE)
print(process.stdout.decode("utf-8"))
Username: ··········
Password: ··········
Successfully logged in to Particle Device Cloud as kantor.1@nd.edu
jck_argon_01 [e00fce68eaceb1faa7cf7193] (Argon) is online
Functions:
int digitalread (String args)
int digitalwrite (String args)
int analogread (String args)
int analogwrite (String args)
Demonstrations#
Flashing Tinker firmware#
Tinker is the default firmware that ships with Particle devices. The following cell restores the device to the factory default by flashing tinker.
device_name = "jck_argon_01"
process = subprocess.run([particle_cli, "flash", device_name, "tinker"], stdout=subprocess.PIPE)
print(process.stdout.decode("utf-8"))
attempting to flash firmware to your device jck_argon_01
Flash device OK: Update started
Flash success!
Toggle on-board led#
import time
import os
import subprocess
led = "D7"
# digital write
def digitalwrite(device_name, pin, value):
process = subprocess.run([particle_cli, "call", device_name, "digitalwrite",
f"{pin},{value}"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return process.stdout.decode("utf-8")
# loop and toggle
for k in range(0, 5):
digitalwrite(device_name, led, "HIGH")
time.sleep(0.5)
digitalwrite(device_name, led, "LOW")
Reading Grove Light Sensor V1.2#
The Grove Light Sensor V1.2 to pin
import time
import os
import subprocess
import re
device_name = "jck_argon_01"
light_sensor = "A2"
def read_ansi(byte_str):
"""Decode a byte string and remove any ANSI control codes."""
return re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', byte_str.decode("utf-8"))
# analog read
def analogread(device_name, pin):
process = subprocess.run([particle_cli, "call", device_name, "analogread", f"{pin}"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return int(read_ansi(process.stdout))
analogread(device_name, light_sensor)
3895