flask gpio test
This commit is contained in:
parent
98bb854612
commit
7c6e38e1c6
@ -23,50 +23,50 @@ for pin in pins_in:
|
|||||||
|
|
||||||
@kamera_server.route("/")
|
@kamera_server.route("/")
|
||||||
def main():
|
def main():
|
||||||
# For each pin, read the pin state and store it in the pins dictionary:
|
# For each pin, read the pin state and store it in the pins dictionary:
|
||||||
for pin in pins:
|
for pin in pins:
|
||||||
pins[pin]['state'] = GPIO.input(pin)
|
pins[pin]['state'] = GPIO.input(pin)
|
||||||
for pin in pins_in:
|
for pin in pins_in:
|
||||||
pins[pin]['state'] = GPIO.input(pin)
|
pins[pin]['state'] = GPIO.input(pin)
|
||||||
|
|
||||||
# Put the pin dictionary into the template data dictionary:
|
# Put the pin dictionary into the template data dictionary:
|
||||||
templateData = {
|
templateData = {
|
||||||
'pins' : pins,
|
'pins' : pins,
|
||||||
'pins_in' : pins_in
|
'pins_in' : pins_in
|
||||||
}
|
}
|
||||||
# Pass the template data into the template main.html and return it to the user
|
# Pass the template data into the template main.html and return it to the user
|
||||||
return render_template('main.html', **templateData)
|
return render_template('main.html', **templateData)
|
||||||
|
|
||||||
# The function below is executed when someone requests a URL with the pin number and action in it:
|
# The function below is executed when someone requests a URL with the pin number and action in it:
|
||||||
@kamera_server.route("/<changePin>/<action>")
|
@kamera_server.route("/<changePin>/<action>")
|
||||||
def action(changePin, action):
|
def action(changePin, action):
|
||||||
# Convert the pin from the URL into an integer:
|
# Convert the pin from the URL into an integer:
|
||||||
changePin = int(changePin)
|
changePin = int(changePin)
|
||||||
# Get the device name for the pin being changed:
|
# Get the device name for the pin being changed:
|
||||||
deviceName = pins[changePin]['name']
|
deviceName = pins[changePin]['name']
|
||||||
# If the action part of the URL is "on," execute the code indented below:
|
# If the action part of the URL is "on," execute the code indented below:
|
||||||
if action == "on":
|
if action == "on":
|
||||||
# Set the pin high:
|
# Set the pin high:
|
||||||
GPIO.output(changePin, GPIO.HIGH)
|
GPIO.output(changePin, GPIO.HIGH)
|
||||||
# Save the status message to be passed into the template:
|
# Save the status message to be passed into the template:
|
||||||
message = "Turned " + deviceName + " on."
|
message = "Turned " + deviceName + " on."
|
||||||
if action == "off":
|
if action == "off":
|
||||||
GPIO.output(changePin, GPIO.LOW)
|
GPIO.output(changePin, GPIO.LOW)
|
||||||
message = "Turned " + deviceName + " off."
|
message = "Turned " + deviceName + " off."
|
||||||
|
|
||||||
# For each pin, read the pin state and store it in the pins dictionary:
|
# For each pin, read the pin state and store it in the pins dictionary:
|
||||||
for pin in pins:
|
for pin in pins:
|
||||||
pins[pin]['state'] = GPIO.input(pin)
|
pins[pin]['state'] = GPIO.input(pin)
|
||||||
for pin in pins_in:
|
for pin in pins_in:
|
||||||
pins[pin]['state'] = GPIO.input(pin)
|
pins[pin]['state'] = GPIO.input(pin)
|
||||||
|
|
||||||
# Along with the pin dictionary, put the message into the template data dictionary:
|
# Along with the pin dictionary, put the message into the template data dictionary:
|
||||||
templateData = {
|
templateData = {
|
||||||
'pins' : pins,
|
'pins' : pins,
|
||||||
'pins_in' : pins_in
|
'pins_in' : pins_in
|
||||||
}
|
}
|
||||||
|
|
||||||
return render_template('main.html', **templateData)
|
return render_template('main.html', **templateData)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
kamera_server.run(host='0.0.0.0')
|
kamera_server.run(host='0.0.0.0')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user