我有一个运行Raspbian 10(破坏者)的Raspberry Pi 1B,安装了Python3,Apache2和PHP,并安装了WS2812 LED灯带。
我有一个简单的Python脚本(PixelTestOn.py,请参见下文),该脚本可以打开所有LED。当使用以下“终端”命令时,所有LED均将点亮,并且脚本中的文本将按预期显示:
sudo python3 PixelTestOn.py*
PixelTestOn.py
#! /usr/bin/env python3
print("PixelTestOn started<br>")
from rpi_ws281x import PixelStrip, Color
# LED strip configuration:
LED_COUNT = 30 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
# LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 40 # Set to 0 for darkest and 255 for brightest
LED_INVERT = True # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
# Create NeoPixel object with appropriate configuration.
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
# Intialize the library (must be called once before other functions).
strip.begin()
# Switch all pixels on
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(255, 255, 255))
strip.show()
print("PixelTestOn finished")
exit()
我也有一个php脚本(PixelTestOn.php,请参见下文),该脚本应显示一些文本,执行PixelTestOn.py python脚本以打开LED,然后显示更多文本。使用以下终端命令时,将显示HTML代码,并且所有LED均点亮,并且显示python脚本中的文本,均符合预期:
sudo php PixelTestOn.php
PixelTestOn.php
<html>
<head>
<title>Pixel Test</title>
<h3>Test to turn LEDs on</h3>
<p>Should see all LEDs light up</p>
<p></p>
<p>About to call shell_exec() (5)</p>
<?PHP
echo shell_exec("python PixelTestOn.py");
?>
<p>Returned from shell_exec()</p>
</head>
但是,当我在Chromium浏览器中输入以下内容时,将显示html输出(非代码),并且按预期方式显示python代码开头的第一个print()语句中的文本,但是,LED却没有亮起,并且第二个print()语句中的文本最后不显示python代码:
localhost/PixelTestOn.php
如果我注释掉2个print()语句之间的所有语句,则该脚本将按预期在浏览器中执行。
[所有文件都存储在/ var / www / html目录中,所有文件都有所有者www-data,并且所有访问控制都设置为'Anyone'。
这是我尝试过的:
[我的最终意图是基于无头Raspberry Pi并通过可单独寻址的LED来构建电子板球记分牌,可通过Android平板电脑进行远程访问。
更新按照Jay的评论,我尝试在终端中运行“ python3 PixelTestOn.py”(即不使用“ sudo”),并在显示“ PixelTestOn启动”打印语句后收到以下错误消息:
Can’t open /dev/mem: Permission denied
Traceback (most recent call last):
File “PixelTestOn.py”, line 24, in <module>
strip.begin()
File “/home/pi/.local/lib/python3.7/site-packages/rpi_ws281x/rpi_ws281x.py”, line 130, in begin
raise RuntimeError(‘ws2811_init failed with code {0} ({1})’.format(resp, str_resp))
RuntimeError: ws2811_init failed with code -5 (mmap() failed)
[看到“权限被拒绝”的语句,我记下了文件“ / dev / mem”的权限,然后输入以下内容将所有3个选项的访问控制都更改为“任何人”。
sudo 777 /dev/mem
然后我在终端中重新运行python3 PixelTestOn.py,在再次显示打印语句'PixelTestOn启动后,收到了类似的错误消息,但这一次是从以下开始的:
Can’t open /dev/mem: Operation not permitted
然后我将/ dev / mem的权限更改回其原始设置
欢迎使用堆栈溢出。
根据下面的链接,我看到您无法避免以root用户身份运行python脚本-https://github.com/jgarff/rpi_ws281x/issues/155#issuecomment-370939228
因此,您需要确定以下内容-
echo shell_exec("sudo python PixelTestOn.py"); # Note the 'sudo' prefix
此外,当您通过Chromium或Firefox等浏览器运行PHP时,请求必须通过Web服务器(Apache,NginX等)传输。因此,您的PHP脚本将作为Web用户而不是root用户执行。
假设您的问题,您的网络用户是www-data
,请在/etc/sudoers
文件中添加以下文字-
www-data ALL=(ALL) NOPASSWD: ALL
这应该可以解决您的问题。