Last active
July 14, 2017 22:42
-
-
Save bunchc/a148027524674e100dfb00902c17f964 to your computer and use it in GitHub Desktop.
Fan API
This file contains 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
import RPi.GPIO as io | |
from flask import Flask, jsonify | |
from flask_restful import Resource, Api | |
# Set our initial state | |
power_pin = 21 | |
fan_status = False | |
# Initialize GPIO, and ensure fan is off | |
io.setmode(io.BCM) | |
io.setup(power_pin, io.OUT) | |
io.output(power_pin, False) | |
# Setup a quick API | |
app = Flask(__name__) | |
api = Api(app) | |
@app.route("/status") | |
def status(): | |
return jsonify(io.input(power_pin)) | |
@app.route("/power/<state>") | |
def power(state): | |
if state.lower() == "on": | |
io.output(power_pin, True) | |
elif state.lower() == "off": | |
io.output(power_pin, False) | |
return jsonify(io.input(power_pin)) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=80, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment