我正在使用树莓派构建自动浇水系统,遇到了一些问题,当我在IDE中运行代码无限次运行时,我想在一段时间后停止运行,如果您可以建议我如何我可以使用Web服务器自动运行它。这是我的剧本。
import RPi.GPIO as GPIO
import time
timeout = time.time() + 60*1
water_pump = 20
moisture_sensor = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(moisture_sensor, GPIO.IN)
GPIO.setup(water_pump,GPIO.OUT)
def callback(moisture_sensor):
if GPIO.input(moisture_sensor):
GPIO.output(water_pump, GPIO.LOW)
else:
GPIO.output(water_pump, GPIO.HIGH)
GPIO.add_event_detect(moisture_sensor, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(moisture_sensor, callback)
答案 0 :(得分:0)
我正在使用Flask框架通过WEB控制GPIO。 这是个小例子:
import RPi.GPIO as GPIO
import time
from flask import Flask
timeout = time.time() + 60*1
app = Flask(__name__)
water_pump = 20
moisture_sensor = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(moisture_sensor, GPIO.IN)
GPIO.setup(water_pump,GPIO.OUT)
def callback(moisture_sensor):
if GPIO.input(moisture_sensor):
GPIO.output(water_pump, GPIO.LOW)
else:
GPIO.output(water_pump, GPIO.HIGH)
@app.route('/start')
def start():
GPIO.output(water_pump, GPIO.HIGH)
@app.route('/stop')
def stop():
GPIO.output(water_pump, GPIO.LOW)
GPIO.add_event_detect(moisture_sensor, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(moisture_sensor, callback)
app.run()