Interaction with PHP and Python

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
robcj07
 
Posts: 9
Joined: Fri Jul 14, 2017 6:44 pm

Interaction with PHP and Python

Post by robcj07 »

Hello,
I'm trying to turn on a led through a web browser with PHP and Python. Next are the codes of my php program and the python one.

Code: Select all

PHP:
<!DOCTYPE html>
<html lang = .es.>
        <head>
                <title>Control remoto</title>
        </head>
        <body>
                <?php
                        if(isset($_GET["prender"])){
                                exec("python prender.py");}
                        if(isset($_GET["apagar"])){
                                exec("python apagar.py");}
                ?>
                <form method = "get">
                        <button type = "button" onclick="location.href='on-off.php?prender=true'">Prender</button>
                        <button type = "button" onclick="location.href='on-off.php?apagar=true'">Apagar</button>
                </form>
        </body>
</html>

Python:
import Adafruit_BBIO.GPIO as GPIO

GPIO.setup("USR3", GPIO.OUT)
GPIO.output("USR3", GPIO.HIGH)
PHP is working fine on the Beagle and lighttpd is also running just fine but I'm having a hard time making those two work together. The python program is functional and the php program to me is fairly straightforward but apparently it isn't. Thanks.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Interaction with PHP and Python

Post by drewfustini »

One issue could be permissions as Adafruit_BBIO by default requires it to be run as a root in order to write to files in /sys.

In order to replicate this issue:
What directory should I put the PHP script in?
What URL path should I access in browser to run the script?

Also, I'd to know more about the system software running on your BeagleBone:
uname -a
cat /etc/dogtag
cat /etc/debian_version
cat /sys/devices/platform/bone_capemgr/slots
cat /proc/cmdline

User avatar
robcj07
 
Posts: 9
Joined: Fri Jul 14, 2017 6:44 pm

Re: Interaction with PHP and Python

Post by robcj07 »

Regarding your comment about python needing to be run as root I made this little change. Still doesn't work. I'm using the web server lighttpd and it requires all the scrpits to be in /var/www as it says so on its welcome page "The DocumentRoot, which is the directory under which all your HTML files should exist, is set to /var/www.", however, it is actually /var/www/html

Code: Select all

<?php
       if(isset($_GET["prender"])){
               exec("python /var/www/html/prender.py");}
       if(isset($_GET["apagar"])){
               exec("python /var/www/html/apagar.py");}
?>
This is the information about my OS

Code: Select all

root@beaglebone:~# uname -a
Linux beaglebone 4.4.30-ti-r64 #1 SMP Fri Nov 4 21:23:33 UTC 2016 armv7l GNU/Linux

root@beaglebone:~# cat /etc/dogtag
BeagleBoard.org Debian Image 2016-11-06

root@beaglebone:~# cat /etc/debian_version
8.8

root@beaglebone:~# cat /sys/devices/platform/bone_capemgr/slots
 0: PF----  -1
 1: PF----  -1
 2: PF----  -1
 3: PF----  -1
 4: P-O-L-   0 Override Board Name,00A0,Override Manuf,cape-universaln

root@beaglebone:~# cat /proc/cmdline
console=tty0 console=ttyO0,115200n8 capemgr.enable_partno=BB-UART1,BB-UART2,BB-UART4, BB-UART5 root=/dev/mmcblk0p1 rootfstype=ext4 rootwait coherent_pool=1M quiet cape_universal=enable
Finally, The URL is http://localhost/myscripts.php. By writing localhost automatically directs the brower to the directory where the html or php files exists.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Interaction with PHP and Python

Post by drewfustini »

Thanks. Please paste the contents of:
prender.py
apagar.py

User avatar
robcj07
 
Posts: 9
Joined: Fri Jul 14, 2017 6:44 pm

Re: Interaction with PHP and Python

Post by robcj07 »

prender.py

Code: Select all

#!/usr/bin/env python
import Adafruit_BBIO.GPIO as GPIO

GPIO.setup("USR3", GPIO.OUT)
GPIO.output("USR3", GPIO.HIGH)
After some research I also found out I had to made those programs executables with. Still no dice:
chmod +x /var/www/html/prender.py
apagar.py is the same as prender.py but changing GPIO.HIGH for GPIO.LOW

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Interaction with PHP and Python

Post by drewfustini »

On Debian 8.8 (2017-06-18) image, I had to install these PHP packages:
sudo apt-get install php5-common libapache2-mod-php5 php5-cli
sudo systemctl restart apache2

I see this in the apache2 error log:
Traceback (most recent call last):
File "/home/debian/apagar.py", line 2, in <module>
import Adafruit_BBIO.GPIO as GPIO
File "build/bdist.linux-armv7l/egg/Adafruit_BBIO/GPIO.py", line 7, in <module>
File "build/bdist.linux-armv7l/egg/Adafruit_BBIO/GPIO.py", line 4, in __bootstrap__
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 954, in resource_filename
self, resource_name
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1651, in get_resource_filename
self._extract_resource(manager, self._eager_to_zip(name))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1681, in _extract_resource
self.egg_name, self._parts(zip_path)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1020, in get_cache_path
self.extraction_error()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1000, in extraction_error
raise err
pkg_resources.ExtractionError: Can't extract file(s) to egg cache

The following error occurred while trying to extract file(s) to the Python egg
cache:

[Errno 13] Permission denied: '/var/www/.python-eggs'

The Python egg cache directory is currently set to:

/var/www/.python-eggs

Perhaps your account does not have write access to this directory? You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: Interaction with PHP and Python

Post by drewfustini »

I now have the PHP page turning on and off the LED.

The key step was to add these lines to the sudoers file. Run the command "sudo visudo" and add these linse:
www-data ALL=(ALL) NOPASSWD: /home/debian/apagar.py
www-data ALL=(ALL) NOPASSWD: /home/debian/prender.py
/var/www/html/on-off.php:
<!DOCTYPE html>
<html lang = .es.>
<head>
<title>Control remoto</title>
</head>
<body>
<?php
if(isset($_GET["prender"])){
exec("sudo /home/debian/prender.py");}
if(isset($_GET["apagar"])){
exec("sudo /home/debian/apagar.py");}
?>
<form method = "get">
<button type = "button" onclick="location.href='on-off.php?prender=true'">Prender</button>
<button type = "button" onclick="location.href='on-off.php?apagar=true'">Apagar</button>
</form>
</body>
</html>

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”