Skip to content

Instantly share code, notes, and snippets.

@schedutron
Last active October 25, 2020 11:34
Show Gist options
  • Save schedutron/36a01e250a05a4916e9ac76bfb90c73f to your computer and use it in GitHub Desktop.
Save schedutron/36a01e250a05a4916e9ac76bfb90c73f to your computer and use it in GitHub Desktop.
Flask server to receive commands via /switch endpoint and send them via USB to CircuitPlayground Express
import serial
from flask import Flask, Response
# /dev/ttyACM0 is the USB interface to CPX on my Pi. It might be different for you,
# there are many resources on the web to help you find the correct interface.
SERIAL_PATH = '/dev/ttyACM0'
BAUDRATE = 115200
TIMEOUT = 0.01
SER = serial.Serial(SERIAL_PATH, baudrate=BAUDRATE, timeout=TIMEOUT)
app = Flask(__name__)
@app.route('/switch/<command>')
def switch(command):
try:
send_serial_cmd(command)
return Response(status=200)
except Exception:
return Response(status=500)
def send_serial_cmd(command):
out = bytes(command, 'utf-8') + b'\n'
SER.write(out)
SER.flush()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment