python Flask和MPR121阅读覆盆子PI

时间:2018-04-09 19:57:50

标签: python flask raspberry-pi

我正在尝试编写一个小程序,用Adafruit MPR121 python库读取MPR121的状态(使用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的状态(flask回答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 -

1 个答案:

答案 0 :(得分:1)

MPR121库(https://github.com/adafruit/Adafruit_Python_MPR121/blob/master/Adafruit_MPR121/MPR121.py#L92)的来源表示您在询问是否已被触摸之前需要致电cap.begin()。那个_device(在该堆栈跟踪的底部)被设置的地方。