我有一个Raspberry Pi 1B,运行Raspbian 10(buster),安装了Python3、Apache2和PHP,并装配了一条WS2812 LED
我有一个简单的Python脚本(PixelTestOn.py,见下文),可以打开所有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代码,指示灯全部亮起,并显示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()语句中的文本会按预期显示,但是,指示灯不会亮起,python代码结尾的第二个print()语句中的文本不会显示:
localhost/PixelTestOn.php
如果我注释掉两条print()语句之间的所有语句,那么脚本将按预期在浏览器中执行
所有文件都存储在/var/www/html目录中,所有文件都具有所有者www数据,并且所有文件的访问控制都设置为“任何人”
以下是我尝试过的:
我的最终目的是建立一个基于无头树莓圆周率的电子板球记分板,带有单独可寻址的LED,可通过Android平板电脑远程访问
更新
根据Jay的评论,我尝试运行“python3 PixelTestOn.py”,即在终端中不使用“sudo”,并在显示打印语句“PixelTestOn start
后收到以下错误消息:
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)
看到“Permission denied”语句,我记录了文件“/dev/mem”上的权限,然后输入以下内容,将所有3个选项的访问控制更改为“anywhere”
sudo 777 /dev/mem
然后,我在终端中重新运行了“python3 PixelTestOn.py”,在显示打印语句“PixelTestOn start
”后,再次收到类似的错误消息,但这次从以下内容开始:
Can’t open /dev/mem: Operation not permitted
然后,我将/dev/mem上的权限更改回其原始设置
欢迎使用堆栈溢出
根据下面的链接,我看到您无法避免以root身份运行python脚本- https://github.com/jgarff/rpi_ws281x/issues/155#issuecomment-370939228
所以你需要做下面的事情,当然-
此外,当您通过Chromium或Firefox等浏览器运行PHP时,请求必须通过web服务器(Apache、NginX等)传递。因此,PHP脚本将作为web用户而不是root用户执行
根据您的问题,假设您的web用户是
www-data
,请在/etc/sudoers
文件中添加以下文本,以允许www-data
作为sudo运行命令-这应该能解决你的问题
相关问题 更多 >
编程相关推荐