Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Python Flask server to receive API calls from a Smartthings hub. Based on examples at:
import RPi.GPIO as GPIO
from flask import Flask, jsonify
import time
# create the flask server app
app = Flask(__name__)
# set the mode on the GPIO pins to be based on counting the pins via
# the the layout:
1 3 5 7 9 11 13 15 17 19 ... (and so on)
2 4 6 8 10 12 14 16 18 20 ... (and so on)
# Configuration for devices, incoming via REST call as type/thingname/action
# The lookup is then mapped to GPIO output to carryout the task
things = {
'door': [{'name': 'door1', 'gpio': {'activate':11}},
{'name': 'door2', 'gpio': {'activate':12}}]
# Initialize pins
for k,val in things.items():
for t in val:
for k,g in t['gpio'].items():
# Run the action via GPIO output
def thing_do(f,action):
if action in f['gpio']:
GPIO.output(f['gpio'][action], GPIO.HIGH)
GPIO.output(f['gpio'][action], GPIO.LOW)
# response code
return 200
# response code
return 400
def status():
print("answering /status request")
# response code
return 'online', 200
def action(ttype,thing, action):
# response code
res = 400
if ttype in things:
for f in things[ttype]:
if thing == f['name'] or thing == "all":
# run an action based on lookup
res = thing_do(f,action)
return jsonify({'res':res}), res
# start the flask app
if __name__ == "__main__":'', port=82, debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.