Я хочу управлять GPIOS с помощью веб-сервера и датчиков.Я пытался настроить веб-сервер и свою собственную логику в разных потоках, но управление одним и тем же выводом GPIO не сработало.После того, как для GPIO PIN 22 установлено значение LOW, управление GPIO через веб-сервер больше не работает.
import RPi.GPIO as GPIO
import time
from flask import Flask, render_template, request
from multiprocessing import Process, Value, Lock
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
# Create a dictionary called pins to store the pin number, name, and pin state:
pins = {
22 : {'name' : 'GPIO 22', 'state' : GPIO.LOW},
18 : {'name' : 'GPIO 18', 'state' : GPIO.LOW}
}
# Set each pin as an output and make it low:
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
@app.route("/")
def main():
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
# Put the pin dictionary into the template data dictionary:
templateData = {
'pins' : pins
}
# Pass the template data into the template main.html and return it to the user
return render_template('main.html', **templateData)
# The function below is executed when someone requests a URL with the pin number and action in it:
@app.route("/<changePin>/<action>")
def action(changePin, action):
print "start"
# Convert the pin from the URL into an integer:
changePin = int(changePin)
# Get the device name for the pin being changed:
deviceName = pins[changePin]['name']
# If the action part of the URL is "on," execute the code indented below:
if action == "on":
# Set the pin high:
GPIO.output(changePin, GPIO.HIGH)
# Save the status message to be passed into the template:
message = "Turned " + deviceName + " on."
print "on"
if action == "off":
GPIO.output(changePin, GPIO.LOW)
message = "Turned " + deviceName + " off."
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
# Along with the pin dictionary, put the message into the template data dictionary:
templateData = {
'pins' : pins
}
return render_template('main.html', **templateData)
def start(l):
app.run(host='0.0.0.0', port=80, debug=True)
def record_loop(loop_on,l):
global Startzeitlicht
global Startzeitheizung
Startzeitlicht=0
Startzeitheizung=0
while True:
if GPIO.input(17) == 1:
GPIO.output(22, GPIO.HIGH)
Startzeitlicht=time.time()
if (time.time()-Startzeitlicht) > 5:
l.acquire()
print "dfdf"
GPIO.output(22, GPIO.LOW)
l.release()
print "bitte printe mich"
if __name__ == "__main__":
recording_on = Value('b', True)
lock = Lock()
p = Process(target=record_loop, args=(recording_on,lock))
p1 =Process(target=start, args=(lock,))
p1.start()
p.start()
#p.join()
#p1.joi
После того, как для GPIO PIN 22 установлено значение LOW, управление GPIO через веб-сервер больше не работает.