PHP GPIO Library


20 posts
by Pickley » Tue Jul 10, 2012 1:20 am
Just finished coding up a basic GPIO interface in PHP.

Here's the link if anyone is interested
https://github.com/pickley/PHP-GPIO

I'll be using it as a simple way to test out GPIO based devices such as controlling my Cybot (old make-it-yourself robot) via the PI.

I am unsure how it would work by using it in an webpage, I have't tested that yet. But technically it should work I think.

I hope it helps someone out.
Posts: 5
Joined: Thu Jul 05, 2012 8:58 am
by bircoe » Sun Aug 05, 2012 1:05 am
Hi, nice work, it seems to work quite well, however needs root/permissions to write to those files.

I want to use this as a front-end for an HAVAC control unit.

So far the only way I can get PHP to work is by using the gpio app included with WiringPi, calling it via exec... I don't really like this however your method file_put_contents('/sys/class/gpio/gpio'.$pinNo.'/value', $value); fails due to no permissions...

Obviously I don't want the apache process to run as root, any ideas how to handle this?
Posts: 33
Joined: Wed Jul 27, 2011 9:43 pm
by Pickley » Sun Aug 05, 2012 1:08 am
One option is for me to rewrite it to use the WiringPi lib, I think it's possible.
Posts: 5
Joined: Thu Jul 05, 2012 8:58 am
by bircoe » Sun Aug 05, 2012 1:12 am
You'd be a hero in the RPi world if you did!!!

The amount of potential that would open up is huge.

I'd have a go myself but have no idea where to start... any pointers? Guess I could start by looking at the python wrapper.
Posts: 33
Joined: Wed Jul 27, 2011 9:43 pm
by frak » Wed Aug 22, 2012 8:14 pm
There are now PHP bindings to the WiringPi library available on Github: https://github.com/WiringPi/WiringPi-PHP
Posts: 17
Joined: Sun Jul 22, 2012 9:33 pm
by Island Medic » Fri Aug 31, 2012 3:55 am
I have installed the two items WiringPi and WiringPi-php. I had a bit of a challenge getting the php module to install but sorted it out when I moved the wiringPi into the WiringPi-php directory.

When I manually activate the pins via

pi@raspberrypi ~ $ gpio mode 1 out
pi@raspberrypi ~ $ gpio write 1 1
pi@raspberrypi ~ $ gpio write 1 0

I am able to read the 3.3v using a multimeter.

Now when I try to do it via a php script I can't change the state of the pin.

<?php
ini_set("enable_dl","On");
include('wiringpi.php');
echo wiringpi::pinmode(1,"OUTPUT");
echo wiringpi::digitalwrite(1,1);
?>

When I run this script I get:


pi@raspberrypi ~/WiringPi-PHP $ php5-cgi test2.php
X-Powered-By: PHP/5.4.4-4
Content-type: text/html


I am not sure how to know if the php is actually working. Could you offer up some ideas on how I can trouble shoot this.

Thanks
Brad
Posts: 1
Joined: Thu Aug 30, 2012 2:42 am
by BlackJack » Fri Aug 31, 2012 8:26 am
@Island Medic: Try running the PHP script as root, i.e. put a ``sudo`` in front of your command line: ``sudo php5-cgi test2.php``
Code: Select all
while not self.asleep():
    sheep += 1
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
by gordon@drogon.net » Fri Aug 31, 2012 1:37 pm
bircoe wrote:Hi, nice work, it seems to work quite well, however needs root/permissions to write to those files.

I want to use this as a front-end for an HAVAC control unit.

So far the only way I can get PHP to work is by using the gpio app included with WiringPi, calling it via exec... I don't really like this however your method file_put_contents('/sys/class/gpio/gpio'.$pinNo.'/value', $value); fails due to no permissions...

Obviously I don't want the apache process to run as root, any ideas how to handle this?


You can fix the permissions by using the gpio program in wiringPi to do the exports for you.

So, if wanting to use pins 17 an 18 for output and 4 for input, then:

Code: Select all
gpio export 17 out
gpio export 18 out
gpio export 4 in


Run the command gpio exports to check what's what.

then you can run your PHP program as the same user you used for the gpio program, as it changes ownership for you.

If writing your own stuff in php, or any language to use the GPIO via the /sys/class/gpio interface, then open the files once and cache the file-desriptior. Going through an open/read/close sequence is even slower.. not always possible in php behind apache though.

However also see the php bindings for wiringPi on github: https://github.com/wiringPi

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1543
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by janek » Sun Sep 02, 2012 4:52 pm
Does WiringPi-PHP works for anyone?

I've tried to use it, but it does nothing. Even included test.php is not working as it should for me.
Also
Code: Select all
exec("gpio write 1 1");

does nothing, but works fine from ssh, any hints?
Posts: 8
Joined: Sun Aug 05, 2012 4:10 pm
by bitbitbit » Tue Oct 16, 2012 9:55 am
janek wrote:Does WiringPi-PHP works for anyone?

I've tried to use it, but it does nothing. Even included test.php is not working as it should for me.
Also
Code: Select all
exec("gpio write 1 1");

does nothing, but works fine from ssh, any hints?


I had the same problem too..... test.php could not run for me.... :cry: :cry: :cry: :cry:
Posts: 30
Joined: Thu Aug 30, 2012 3:18 am
by gordon@drogon.net » Wed Oct 17, 2012 12:11 pm
bitbitbit wrote:
janek wrote:Does WiringPi-PHP works for anyone?

I've tried to use it, but it does nothing. Even included test.php is not working as it should for me.
Also
Code: Select all
exec("gpio write 1 1");

does nothing, but works fine from ssh, any hints?


I had the same problem too..... test.php could not run for me.... :cry: :cry: :cry: :cry:


I've not been in-touch with the chap who did the PHP bindings for wiringPi for a while now. I wonder if it's time I looked at it myself...

However, if you want to do it the "hard" way, then you can use the GPIO program (vi system ()) to export the pins, then open/read/write/close the files in /sys/class/gpio etc. It's probably fast enough for most needs without having native PHP functions to do it all.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1543
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by bitbitbit » Fri Oct 19, 2012 4:08 am
gordon@drogon.net wrote:
bitbitbit wrote:
janek wrote:Does WiringPi-PHP works for anyone?

I've tried to use it, but it does nothing. Even included test.php is not working as it should for me.
Also
Code: Select all
exec("gpio write 1 1");

does nothing, but works fine from ssh, any hints?


I had the same problem too..... test.php could not run for me.... :cry: :cry: :cry: :cry:


I've not been in-touch with the chap who did the PHP bindings for wiringPi for a while now. I wonder if it's time I looked at it myself...

However, if you want to do it the "hard" way, then you can use the GPIO program (vi system ()) to export the pins, then open/read/write/close the files in /sys/class/gpio etc. It's probably fast enough for most needs without having native PHP functions to do it all.

-Gordon


I know how to control the GPIO pins throught the command line, like::

echo out > /sys/class/gpio/gpio0/direction #set gpio 0 pin to output
echo 1 > /sys/class/gpio/gpio0/value #lights on at gpio 0
echo 0 > /sys/class/gpio/gpio0/value #lights off at gpio 0

but how can i include wiringpi library with it?? :shock: :shock:
and erm..... i just want to use the softpwm function in wiringpi..... :roll: :roll:
Posts: 30
Joined: Thu Aug 30, 2012 3:18 am
by gordon@drogon.net » Fri Oct 19, 2012 7:09 am
bitbitbit wrote:[
I know how to control the GPIO pins throught the command line, like::

echo out > /sys/class/gpio/gpio0/direction #set gpio 0 pin to output
echo 1 > /sys/class/gpio/gpio0/value #lights on at gpio 0
echo 0 > /sys/class/gpio/gpio0/value #lights off at gpio 0

but how can i include wiringpi library with it?? :shock: :shock:
and erm..... i just want to use the softpwm function in wiringpi..... :roll: :roll:


OK. One thing at a time :)

You can manipulate the GPIO from the command line with the gpio command, so the above becomes:

gpio -g mode 0 out # Set gpio pin 0 to output (one of the I2C pins)
gpio -g write 0 1 # Write 1 to gpio pin 0

So at that level you could crudely use php's system() command to call the gpio program.

To take that into PHP, you could firstly use the gpio's export function to export the GPIO pins:

gpio export 0

then you can open and write the /sys/class/gpio/... files from your PHP program.

Right now if you want to use the softPwm stuff you need to write it in C though and the LEDs/PWM only works while the program is runnig - it's no good for transient things like a CGI script called from a web page - you/someone cound write a daemon type application to maintain them though.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1543
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by mth3r » Fri Nov 09, 2012 7:25 pm
I have been able to get the wiringpi::digitalRead() functions to work, but not the digitalWrite().

I can change the status of the various pins via gpio -g write 18 1 and have a php page using "digitalRead" show the change, but I can't change the gpio status directly from a php page. Anyone else have similar issues?
Posts: 2
Joined: Fri Nov 09, 2012 7:22 pm
by mth3r » Fri Nov 09, 2012 8:16 pm
mth3r wrote:I have been able to get the wiringpi::digitalRead() functions to work, but not the digitalWrite().

I can change the status of the various pins via gpio -g write 18 1 and have a php page using "digitalRead" show the change, but I can't change the gpio status directly from a php page. Anyone else have similar issues?



Never mind. I made the wiringpi.ini file as stated in the readme and it worked fine.
Posts: 2
Joined: Fri Nov 09, 2012 7:22 pm
by arno_u_loginlux » Tue Dec 04, 2012 9:57 pm
Many thanks to @pickley for having shared this!

For my own pleasure & personal needs, I forked & largely revamped this nice piece of script into a Composer-friendly, PSR-0 compatible library, with some unit tests & some sensors-dedicated classes. Makes it easier to integrate some GPIO write/read operations into a larger PHP project (weather station, temperature logger, etc.) of your own.

It's free, it's small, and it's here : https://github.com/ronanguilloux/php-gpio

Hope it'll help.
Posts: 2
Joined: Fri Nov 23, 2012 10:01 pm
by damianpeterson » Wed Dec 05, 2012 7:52 am
Posts: 1
Joined: Wed Dec 05, 2012 7:50 am
by magnified » Thu Jan 31, 2013 1:09 am
Hey all, fairly new to the whole Linux/Raspberry Pi world - but so far so good..

I'm having problems with using the php-gpio library calls from within my PHP page. I've written some code to activate a few pins, when I run it from the webpage, nothing happens... If I run it from the CLI with sudo, everything works as expected.

Is there a permission I need to adjust? Something simple I'm overlooking?

Any help you could provide would be great... Cheers.
M.
Posts: 1
Joined: Thu Jan 31, 2013 1:06 am
by -rst- » Thu Jan 31, 2013 2:32 pm
Are you running the python script using sudo from the PHP too? Some of the GPIO functions need that... That php-gpio git page has detailed instructions on permissions :roll:
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 900
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland
by Pickley » Mon Feb 04, 2013 1:28 am
Updated link to my GPIO library is below, but please use WiringPi-PHP or ronanguilloux's fork of mine as it is much better.

https://github.com/aaronpearce/PHP-GPIO

WiringPi-PHP: https://github.com/WiringPi/WiringPi-PHP

ronanguilloux's fork: https://github.com/ronanguilloux/php-gpio
Posts: 5
Joined: Thu Jul 05, 2012 8:58 am