我将Pigpio库与树莓派pi 3模型B +(Raspbian和python 2.7的最新版本)一起使用。在pi上运行uWSGI和nginx。我也在用烧瓶。
我对Pigpio守护程序有问题。如果我在启动后启动守护程序,
systemctl status pigpio
输出以下内容:
pigpiod.service - Daemon required to control GPIO pins via pigpio
Loaded: loaded (/lib/systemd/system/pigpiod.service; disabled; vendor preset: enabled)
Active: inactive (dead)
我对这一切还很陌生,但这似乎还不行。 之后,我在python中导入pipgio并连接到守护程序(我只是在尝试。没想到,它将起作用)。
pigpio.pi().connected
命令输出 True 。除了守护程序未处于活动状态之外,这似乎还可以。如果我键入pigpio.pi().stop()
,则该连接仍处于活动状态。我不了解这种行为。
我的问题是,如果我误解了图书馆的用法。
另外,这篇文章的真正原因是,我想控制GPIO。为此,我编写了以下代码。
import pigpio
from flask import Flask
from flask import render_template
from time import sleep
import RPi.GPIO as GPIO
import sys
from smbus2 import SMBus
import serial
@app.route("/forward/")
def forward():
try:
status = 0
endstatus = 1
while endstatus == 1:
response = "3"
if response == "2":
status = int(response)
elif response == "1":
status = int(response)
elif response == "3":
status = int(response)
# print(status)
if status == 3:
GPIO.output(Input3A, True)
GPIO.output(Input4A, True)
servo = 3
pi = pigpio.pi()
pi.set_mode(servo, pigpio.OUTPUT)
pi.set_servo_pulsewidth(servo, 1300)
if status == 1:
GPIO.output(Input3A, False)
GPIO.output(Input4A, False)
pi = pigpio.pi()
servo = 3
pi.set_servo_pulsewidth(servo, 1500)
pi.stop()
endstatus = 0
except KeyboardInterrupt:
pi = pigpio.pi()
all_off()
pi.stop()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
使用该代码,我只能运行一次。如果我单击相应的按钮再次执行try块,则什么也没有发生。之后,uWSGI-log输出:
(venv) root@raspberrypi:/home/pi/arc# tail /var/log/uwsgi/test_app_uwsgi.log
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/arc/movement.py", line 90, in forward
pi.set_mode(servo, pigpio.OUTPUT)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/pigpio.py", line 1340, in set_mode
return _u2i(_pigpio_command(self.sl, _PI_CMD_MODES, gpio, mode))
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/pigpio.py", line 989, in _pigpio_command
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
AttributeError: 'NoneType' object has no attribute 'send'
[pid: 321|app: 0|req: 2/2] 127.0.0.1 () {50 vars in 901 bytes} [Sun Jan 6 14:39:40 2019] GET /forward/ => generated 0 bytes in 170 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
和
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Did you start the pigpio daemon? E.g. sudo pigpiod
Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
我认为这可能是端口问题。但是我没有将Pigpio守护程序配置为侦听端口8889。以pigpiod -p 8889 start
开头似乎还不够。
如果我尝试使用以下命令sudo pigpio -l -n 127.0.0.1
启动守护程序,则uWSGI日志输出:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Did you start the pigpio daemon? E.g. sudo pigpiod
Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[pid: 320|app: 0|req: 2/2] 127.0.0.1 () {52 vars in 994 bytes} [Sun Jan 6 14:53:49 2019] GET / => generated 770 bytes in 5 msecs (HTTP/1.1 200) 2 headers in 80 bytes (1 switches on core 0)
[pid: 320|app: 0|req: 3/3] 127.0.0.1 () {48 vars in 846 bytes} [Sun Jan 6 14:59:50 2019] GET / => generated 766 bytes in 8 msecs (HTTP/1.1 200) 2 headers in 80 bytes (1 switches on core 0)
[pid: 320|app: 0|req: 4/4] 127.0.0.1 () {52 vars in 994 bytes} [Sun Jan 6 14:59:50 2019] GET / => generated 766 bytes in 8 msecs (HTTP/1.1 200) 2 headers in 80 bytes (1 switches on core 0)
/home/pi/arc/movement.py:54: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(Input1A, GPIO.OUT)
/home/pi/arc/movement.py:58: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(Input1B, GPIO.OUT)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Can't create callback thread.
Perhaps too many simultaneous pigpio connections.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Traceback (most recent call last):
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/arc/movement.py", line 90, in forward
pi.set_mode(servo, pigpio.OUTPUT)
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/pigpio.py", line 1340, in set_mode
return _u2i(_pigpio_command(self.sl, _PI_CMD_MODES, gpio, mode))
File "/home/pi/arc/venv/local/lib/python2.7/site-packages/pigpio.py", line 989, in _pigpio_command
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
AttributeError: 'NoneType' object has no attribute 'send'
[pid: 320|app: 0|req: 5/5] 127.0.0.1 () {50 vars in 901 bytes} [Sun Jan 6 15:02:38 2019] GET /forward/ => generated 0 bytes in 165 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
我做了很多研究,但是我被困住了。
我现在有更多信息。经过大量测试,我发现主要的问题是,我无法使用两次导入的Pigpio执行python脚本。该脚本将通过uwsgi,nginx和flask执行。这是网站上的一个简单按钮,可通过ajax重定向到pi上的脚本。 第一次执行后,脚本将执行应执行的操作。但是,如果我随后用Pigpio等执行另一个脚本,以此类推,则会出现以下错误:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Can't create callback thread.
Perhaps too many simultaneous pigpio connections.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Traceback (most recent call last):
File "motor_stopp.py", line 37, in <module>
all_off()
File "motor_stopp.py", line 21, in all_off
pi.set_mode(servo, pigpio.OUTPUT)
File "/usr/lib/python2.7/dist-packages/pigpio.py", line 1296, in set_mode
return _u2i(_pigpio_command(self.sl, _PI_CMD_MODES, gpio, mode))
File "/usr/lib/python2.7/dist-packages/pigpio.py", line 983, in _pigpio_command
sl.s.send(struct.pack('IIII', cmd, p1, p2, 0))
AttributeError: 'NoneType' object has no attribute 'send'
如果我使用kill -HUP pid
重新启动 uwsgi,则可以启动另一个python脚本。现在,我可以在代码块之后每次重新启动uwsgi。看来这可以解决我的问题。但这并不是真正的解决方案。
是不是uwsgi和pigpio使用相同的端口? 如果是这样,我如何更改其中一个库的端口?