-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflutter_server.py
42 lines (33 loc) · 1.02 KB
/
flutter_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Flask, request, jsonify
import json
import RPi.GPIO as GPIO
def toggle_pin(pin, toggle=True):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.OUT)
state = GPIO.input(pin)
if toggle and state == 0:
GPIO.output(pin,GPIO.HIGH)
elif toggle:
GPIO.output(pin,GPIO.LOW)
return int(not state)
app = Flask(__name__)
def return_text(text):
response = json.dumps({"status": text})
return str(response)
@app.route("/status/", methods=["POST"])
def get_status():
if request.method == "POST":
json_request = request.get_json()
pin = json_request['pin']
status = toggle_pin(pin, False)
return return_text(status), 200
@app.route("/toggle/", methods=["POST"])
def toggle():
if request.method == "POST":
json_request = request.get_json()
pin = json_request['pin']
toggle_pin(pin, True)
return return_text('Toggled..'), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', debug = True)