pythonFlask和MPR121在树莓皮上的读数

2024-04-23 06:56:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着写一个小程序,用Adafruit的MPR121 python库读取MPR121的状态(用Raspberry pi2的电容触摸),这个状态可以通过一个简单的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方法(错误代码500)获取MPR121的状态。我怎样才能解决这个问题?你知道吗

回答:

 * 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 -

Tags: inpyselfappflasklibpackagesusr