我正在使用一个系统,该系统应将我的树莓派GPIO引脚之一变为高电平约2秒钟。我将其分为2个不同的文件。 “网站”文件(名为app.py)和“ GPIO”文件(名为test.ty)。通过以下方式请求测试文件:

from flask import Flask, render_template
from test import open_door

app = Flask(__name__)

@app.route('/opendoor')
def openDoor():
    open_door()


if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')


test.py文件如下所示:

import RPi.GPIO as GPIO
import time

testPin = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(testPin, GPIO.OUT)

counter =0

def open_door():
    try:
        print ("Everthing is fine")
        while counter < 900000:
             print ("Everything is good")
             GPIO.output(testPin, GPIO.HIGH)

             counter += 1

    except:
        print ("Everything is oke!")

    finally:
        GPIO.cleanup()


我收到消息“一切都很好”和“一切都好!”但不是“一切都很好”的信息。在我看来,while循环不会执行。
有人知道为什么它不起作用吗?

最佳答案

counter不在另一个调用open_door()的文件的范围内,这就是为什么您看到“一切都很好!”的原因。因为未知变量是一个例外

09-30 14:35
查看更多