@
but0n 你好,参照你的项目给了我很大的帮助;最近在做一个扫描器的项目,遇到了一个问题就是 python 和 html 之间的通信建立不起来,代码如下:
#红外传感器
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,
GPIO.IN, pull_up_down=GPIO.PUD_UP)
#16 进制库
import binascii
#FLASK 框架
from flask import Flask, url_for, render_template, jsonify
#实例化串口
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout = None) #一直等待
app = Flask(__name__)
@
app.route('/')
def index():
return render_template('index.html')
@
app.route("/json/")
def scan(channel):
def order_list():
a = '7e 00 08 01 00 02 01 ab cd' #触发扫描器的指令
a_list = []
for i in a.split():
a_list.append(binascii.a2b_hex(i)) #转为 16 进制
return a_list
ser.write(order_list()) #触发扫描
scandata = ser.readline() #读取数据
data = {"zhipinming": "brian","age": scandata,"has_car": False}
return jsonify(data=data)
#18 引脚的中断和边缘检测,发生改变时调用 scan 函数
#红外感应一次,扫描器工作一次
GPIO.add_event_detect(18, GPIO.FALLING, callback=scan)
if __name__ == '__main__':
app.run()
运行后提示错误:
Traceback (most recent call last):
File "/home/pi/Desktop/tgpscan/
scan.py", line 35, in scan
return jsonify(data=data)
File "/usr/lib/python2.7/dist-packages/flask/
json.py", line 234, in jsonify
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] \
File "/usr/lib/python2.7/dist-packages/werkzeug/
local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/lib/python2.7/dist-packages/werkzeug/
local.py", line 297, in _get_current_object
return self.__local()
File "/usr/lib/python2.7/dist-packages/flask/
globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context