Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
accessing bibliopixel script via web
#1
I am using the BiblioPixel python library along with a raspberry pi b+ to control 60 feet of rgb led lpd8806 light strips. So far so good. It's all up and working without a hitch. However, I now want to control the lights via a web page served by the pi. I installed apache (Server version: Apache/2.2.22 (Debian)) and php (PHP 5.4.36-0+deb7u3 (cli)) and the pi can now serve up php web pages and I have much of my color chooser/controller funtional.

The trouble is when trying to get my python script to control the lights when run via the web server.

The web server root is /var/www and the BiblioPixel library is at /var/www/BiblioPixel-master/
I've made a very simple python script to control the leds

/var/www/BiblioPixel-master/pcolor.py:
Code:
#!/usr/bin/env python

import sys                
from bibliopixel.drivers.LPD8806 import *
from bibliopixel import *
import bibliopixel.colors as colors

r = sys.argv[1]
g = sys.argv[2]
b = sys.argv[3]

print r

driver = DriverLPD8806(480, c_order=ChannelOrder.GRB, use_py_spi=True, dev="/dev/spidev0.0", SPISpeed=4)
led = LEDStrip(driver)
led.all_off()
led.fillRGB(int(r),int(g),int(b))
led.update()

I call this script from a php script /var/www/pick_test.php:

Code:
<?php
$rval = 100;
$gval = 100;
$bval = 100;
$command = escapeshellcmd('./BiblioPixel-master/pcolor.py ' . $rval . ' ' . $gval . ' ' . $bval);
$output = shell_exec($command . ' 2>&1');
echo $output;
?>

I can run command line with no problem
/var/www $ php -f pick_test.php
The lights turn on with the proper RGB values I set in the php variables $rval, $gval and $bval

When I access via the webpage (my pi ip is 192.168.1.94)
http://192.168.1.94/pick_test.php
the php script is properly served and the pcolor.py script is run (it returns the "print r" value from the python script to the webpage) but it fails where the driver is called with the follow error returned to the webpage:

Code:
ERROR - spi_driver_base - Cannot find SPI device. Please see https://github.com/maniacallabs/bibliopixel/wiki/SPI-Setup for details. 100 Traceback (most recent call last): File "./BiblioPixel-master/pcolor.py", line 16, in driver = DriverLPD8806(480, c_order=ChannelOrder.GRB, use_py_spi=True, dev="/dev/spidev0.0", SPISpeed=4) File "/var/www/BiblioPixel-master/bibliopixel/drivers/LPD8806.py", line 7, in __init__ super(DriverLPD8806, self).__init__(num, c_order = c_order, use_py_spi = use_py_spi, dev = dev, SPISpeed = SPISpeed) File "/var/www/BiblioPixel-master/bibliopixel/drivers/spi_driver_base.py", line 41, in __init__ self._bootstrapSPIDev() File "/var/www/BiblioPixel-master/bibliopixel/drivers/spi_driver_base.py", line 73, in _bootstrapSPIDev raise IOError(error) IOError: Cannot find SPI device. Please see https://github.com/maniacallabs/bibliopixel/wiki/SPI-Setup for details.

Long story short. Works command line, but same files fail when called via webserver.
Can anyone give me a push in the right direction?

Thanks!
Jason

The BiblioPixel library is fantastic, btw.
#2
PHP is a little off my knowledge base but I'm guessing it's because SPI requires sudo or root to access the device. When you are running the PHP script directly, you probably have sufficient privileges. But when running via the web server it gets run with the access rights of the server, which is likely in a different user group all together. You need to make sure that your server runs with sufficient privileges. The safest way is going to be to add SPI access rights to the user group that the server is running as.

I've done the whole web control thing before using python bottle and think I had to do something like this... but it was years ago. I will look into what I did further tonight. But hopefully my suggestion above will work.
#3
Excellent advice!

For future thread readers...

My webserver runs as user: www-data and group: www-data (more /etc/apache2/envvars).
To access /dev/spidev0.* you must be a member of the group "spi".
I added membership to gpio in case I need it in the future, but it wouldn't be needed for the BiblioPixel library.

Code:
usermod -a -G spi www-data
usermod -a -G gpio www-data

will add user www-data to the spi and gpio groups. It didn't work until I rebooted.

It is very exciting to choose a color from the color wheel on my phone and have 60 feet of tray ceiling led lights change color!!!

Thanks Adam!
#4
Awesome! Thanks so much for the extra detail on what you did Smile
Be sure to post about your project on the Show and Tell when you finish... we'd love to see it in action!
#5
Wow, this is exactly what I'm looking for!! Thanks for clarifying in detail how to get it to work with PHP and please do post details about your project Smile
#6
Awesome! Yes, I'm finishing up stage one of my project, then I'll post some details.
#7
Thanks. I've gotten it to work by clicking a link with a simple exec('python /path/to/file.py') PHP script, now to make it more interactive. Initially I thought of making my server through something like CherryPy, which basically is a python script that can generate a webserver to host whatever content manipulatable through python. I'm new to python however (just started this weekend), so I'll see how far I'm willing to go for this project.


Forum Jump:


Users browsing this thread: 1 Guest(s)