我正在编写一个小程序,它用Adafruit MPR121 python库读取via 121的状态( Raspberry PI 2的电容触摸),这种状态可以通过一个简单的get方法获得。
问题是,我无法创建它的读取线程:正如MPR121的例子所示,我必须至少使用一个while循环--或者只执行一个度量。示例:
import time
from flask import Flask
import Adafruit_MPR121.MPR121 as MPR121
#you have to give the static URL path and physical path on your machine
#after then you can reach some static files
# this is essential!!
app = Flask(__name__,static_url_path="/client", static_folder="/home/pi/asd/client")
cap = MPR121.MPR121()
#enabling some test issues
TEST = False
@app.route('/read')
def read():
if TEST:
return "asd"
else:
current_touched = cap.touched()
return current_touched
@app.route('/')
def root():
#This is the root path, just sent back a static webpage
return app.send_static_file('index.html')正如您所看到的,我将使用标准的get方法获得MPR121的状态(烧瓶应答errorCode 500)。我该如何解决这个问题?
答案:
* Serving Flask app "flask_main"
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
asd
[2018-04-09 21:48:02,189] ERROR in app: Exception on /read [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/alapitvany/flask_main.py", line 28, in read
current_touched = cap.touched()
File "/usr/local/lib/python2.7/dist-packages/Adafruit_MPR121/MPR121.py", line 179, in touched
t = self._i2c_retry(self._device.readU16LE, MPR121_TOUCHSTATUS_L)
AttributeError: 'MPR121' object has no attribute '_device'
192.168.1.175 - - [09/Apr/2018 21:48:02] "GET /read HTTP/1.1" 500 -发布于 2018-04-10 06:12:15
源MPR121库(MPR121/MPR121.py#L92)表示,在询问它是否已被访问之前,需要调用cap.begin()。这就是设置_device (在堆栈跟踪的底部)的地方。
https://stackoverflow.com/questions/49740741
复制相似问题