我有一个应用程序,我开发了一个应用程序,用来控制网络上的东西,烧瓶和蓝图上的Raspberry Pi。我在客户端有一个android应用程序,它发送http请求和覆盆子pi作为服务器应答。我有一个场景,我通过设置一个引脚来控制风扇,然后改变它的占空比来控制它的速度。在我改变速度后,我让应用程序返回一个带有当前占空比的json消息,如果粉丝在旋转或不旋转(旋转部分无用,我知道:))。问题是,我不能在每次重新调用刚刚报告状态的函数(带有“/”路径的函数)时重置每个变量的占空比。我想不出一种方法可以使它在不将文件保存的情况下简单快速地完成,并且我不想将文件复杂化(如果可能)。保存可变的Python蓝图烧瓶
这是主要应用程序的代码,其中有粉丝注册为蓝图。
from flask import Flask
from scen3 import scen3_api
from scen2 import scen2_api
app = Flask(__name__)
app.register_blueprint(scen3_api, url_prefix='/scenariu/3')
app.register_blueprint(scen2_api, url_prefix='/scenariu/2')
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
这是包含情景2东西的第二个脚本的代码。基本上它有两个功能,在设置返回“/”路径上的占空比和旋转状态的引脚并通过“/ PWMValue /”路径上的参数更改PWM值。
import RPi.GPIO as GPIO
from flask import Flask, render_template, request, jsonify, Blueprint
scen2_api= Blueprint('scen2_api', __name__)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
GPIO.setup(16,GPIO.OUT)
GPIO.output(16,GPIO.LOW)
dutyCycle=0
spin=0
p = GPIO.PWM(12, 500)
p.start(float(dutyCycle))
#spin=0
@scen2_api.route("/")
def main():
spin=0
if dutyCycle != 0.0:
spin=1
return jsonify(state=str(spin),pwmvalue=str(dutyCycle))
@scen2_api.route("/PWMValue/<action>")
def action(action):
dutyCycle=float(action)
p.ChangeDutyCycle(float(dutyCycle))
return jsonify(state=str(spin),pwmvalue=str(dutyCycle))
每次我用报告函数调用函数时,它会返回0占空比和旋转。我怎样才能解决这个问题,但仍然使这个行为一样优选不使用文件?谢谢:)