我是自动化的泵,只有当土壤湿度值(从土壤湿度传感器获得)超过某一值时才开启。情况是这样的:
我尝试了以下操作,但得到了working outside of request context错误:
web_plants.py(which调用python soil_on.py脚本):
@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
tvalue= -1 #get value from dropdown
msg = ''
if request.method == "POST":
msg= "rating above 3"
os.system("python soil_on.py&")
templateData = template(text = msg) #display text using template()
return render_template('index.html', **templateData)soil_on.py(which从water类调用auto_Irrigation()函数:
import water
if __name__ == "__main__":
water.auto_Irrigation()water.py (有auto_Irrigation())
app = Flask(__name__)
def template(title = "HELLO!", text = ""):
templateDate = {
'text' : text,
'tvalues' : getTValues(),
'selected_tvalue' : -1
}
return templateDate
def getTValues():
return (10, 11, 15, 2, 1)
@app.route("/", methods=['POST', 'GET'])
def auto_Irrigation():
tvalue= -1 #get value from dropdown
# msg = ''
if request.method == "POST":
tvalue = int(request.form['tvalue'])
if tvalue> 3:
GPIO.output(40, GPIO.HIGH)
else:
GPIO.output(40, GPIO.LOW)
return render_template('index.html', **templateData)index.html
<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">
<select name= 'tvalue'>
{% for tvalue in tvalues %}
{% if selected_tvalue == tvalue %}
<option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
{% else %}
<option value="{{ tvalue }}" >{{ tvalue }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="Submit" />
</form>我不知道如何使这个功能成为一个背景任务。目前,这是为我工作,但该函数没有作为后台任务运行(因此它只检查值和打开/关闭泵):
index.html
<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">
<select name= 'tvalue'>
{% for tvalue in tvalues %}
{% if selected_tvalue == tvalue %}
<option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
{% else %}
<option value="{{ tvalue }}" >{{ tvalue }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="Submit" />
</form>为了这篇文章,web_plants.py:(simplified函数threshold()使它更容易理解)
def template(title = "HELLO!", text = ""):
templateDate = {
'text' : text,
'tvalues' : getTValues(),
'selected_tvalue' : -1
}
return templateDate
def getTValues():
return (10, 11, 15, 2, 1)
@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
tvalue= -1 #default value
msg = ''
if request.method == "POST":
tvalue = int(request.form['tvalue'])
if tvalue> 3:
msg= "rating above 3"
#generating template data
templateData = template(text = msg)
templateData['selected_tvalue'] = tvalue
return render_template('index.html', **templateData)发布于 2018-04-06 08:48:35
考虑使用芹菜,这是一个异步任务队列。继续运行长期运行的作业,获取状态更新(甚至是自定义更新)。
这些链接应该可以帮助您理解它是如何工作的:
https://github.com/miguelgrinberg/flask-celery-example
https://blog.miguelgrinberg.com/post/using-celery-with-flask
你甚至可以看看芹菜的文件。
这是如何实现您的函数的示例。你可以从那里继续前进。
/tasks.py
@celery.task
def my_task(self, threshold):
if sensor_val < threshold:
self.update_state(state="Sensor Value below threshold", meta={'sensor_val':sensor_val, 'threshold':threshold})
else:
self.update_state(state="Sensor Value past threshold. Please check.", meta={'sensor_val':sensor_val, 'threshold':threshold})
"""Do whatever you would like to do"""/views.py
def sensor_check(request):
if request.method == "POST":
threshold = request.POST['threshold']
from tasks import my_task
job = my_task.delay(threshold)
return HttpResponseRedirect(reverse("task_status")+"?job_id="+job.id)
def task_status(request):
if 'job_id' in request.GET:
job_id = request.GET['job_id']
job = AsyncResult(job_id)
data = job._get_task_meta()
return JsonResponse(data)
else:
JsonResponse("No job ID given")https://stackoverflow.com/questions/49688824
复制相似问题