I have a Raspberry Pi 1B running Raspbian 10 (buster) with Python3, Apache2 and PHP installed, and rigged up with a strip of WS2812 LEDs.
I have a simple Python script (PixelTestOn.py, see below) that turns all the LEDs on. When using the following Terminal command, the LEDs all light up and the text within the script is displayed as expected:
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()
I also have a php script (PixelTestOn.php, see below) that should display some text, execute the PixelTestOn.py python script to turn on the LEDs, then display some more text. When using the following Terminal command, the HTML code is displayed, and the LEDs all light up and the text within the python script is displayed, all as expected:
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>
However, when I enter the following into the Chromium browser the html output (not code) is displayed and the text in the first print() statement at the start of the python code is displayed as expected, however, the LEDs do not light up and the text in the second print() statement at the end the python code is not displayed:
localhost/PixelTestOn.php
If I comment out all the statements between the 2 print() statements then the script is executed as expected in the browser.
All the files are stored in the /var/www/html directory, all have the owner www-data, and all have the access controls set to ‘Anyone’.
Here’s what I’ve tried:
- Using shell_exec(), exec() and system() in the php script
- Using different browsers (Echo, IE, Chrome) on my Windows 10 PC and Chrome on an Android tablet. Note that SSH is enabled on the Pi.
- Two different LED control libraries, rpi_ws281x and neopixel
- The Common Gateway Interface (CGI). I couldn’t get this to work with an html based form with a [Submit] button, when the button was pressed to enter the data an 'Internal Server Error' message was displayed in the browser.
My ultimate intention is to build an electronic cricket scoreboard based on a headless Raspberry Pi with individually addressable LEDs, that is accessed remotely via an Android tablet.
Update
Following Jay's comment I tried running ‘python3 PixelTestOn.py’ i.e. without ‘sudo’ in Terminal and got the following error message after the print statement ‘PixelTestOn started
’ was displayed:
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)
Seeing the ‘Permission denied’ statement I made a note of the permissions on file ‘/dev/mem’ then entered the following to change the access control to ‘Anyone’ for all 3 options.
sudo 777 /dev/mem
I then re-ran ‘python3 PixelTestOn.py’ in Terminal and, again after the print statement ‘PixelTestOn started
’ was displayed, got a similar error message but this time starting with:
Can’t open /dev/mem: Operation not permitted
I then changed the permissions on /dev/mem back to their original settings