PHP GPIO Library
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.
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.
Re: PHP GPIO Library
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?
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?
Re: PHP GPIO Library
One option is for me to rewrite it to use the WiringPi lib, I think it's possible.
Re: PHP GPIO Library
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.
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.
Re: PHP GPIO Library
There are now PHP bindings to the WiringPi library available on Github: https://github.com/WiringPi/WiringPi-PHP
-
- Posts: 1
- Joined: Thu Aug 30, 2012 2:42 am
Re: PHP GPIO Library
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
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
Re: PHP GPIO Library
@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
- gordon@drogon.net
- Posts: 2023
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: PHP GPIO Library
You can fix the permissions by using the gpio program in wiringPi to do the exports for you.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?
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
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/
Gordons projects: https://projects.drogon.net/
Re: PHP GPIO Library
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
does nothing, but works fine from ssh, any hints?
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");
Re: PHP GPIO Library
I had the same problem too..... test.php could not run for me....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.
Alsodoes nothing, but works fine from ssh, any hints?Code: Select all
exec("gpio write 1 1");




- gordon@drogon.net
- Posts: 2023
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: PHP GPIO Library
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...bitbitbit wrote:I had the same problem too..... test.php could not run for me....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.
Alsodoes nothing, but works fine from ssh, any hints?Code: Select all
exec("gpio write 1 1");
![]()
![]()
![]()
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/
Gordons projects: https://projects.drogon.net/
Re: PHP GPIO Library
I know how to control the GPIO pins throught the command line, like::gordon@drogon.net wrote: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...bitbitbit wrote:I had the same problem too..... test.php could not run for me....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.
Alsodoes nothing, but works fine from ssh, any hints?Code: Select all
exec("gpio write 1 1");
![]()
![]()
![]()
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
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??


and erm..... i just want to use the softpwm function in wiringpi.....


- gordon@drogon.net
- Posts: 2023
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: PHP GPIO Library
OK. One thing at a timebitbitbit 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??![]()
![]()
and erm..... i just want to use the softpwm function in wiringpi.....![]()

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/
Gordons projects: https://projects.drogon.net/
Re: PHP GPIO Library
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?
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?
Re: PHP GPIO Library
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 23, 2012 10:01 pm
Re: PHP GPIO Library
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.
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: 1
- Joined: Wed Dec 05, 2012 7:50 am
Re: PHP GPIO Library
https://github.com/ronanguilloux/php-gpio is awesome! Thank you!
Re: PHP GPIO Library
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.
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.
Re: PHP GPIO Library
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 

http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Re: PHP GPIO Library
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
https://github.com/aaronpearce/PHP-GPIO
WiringPi-PHP: https://github.com/WiringPi/WiringPi-PHP
ronanguilloux's fork: https://github.com/ronanguilloux/php-gpio
Re: PHP GPIO Library
I know I'm a bit late to the party, but I've been working on a gpio/general peripheral integration library in PHP. The implementation supports SPI, PWM and most other GPIO functions without using sysfs - so it's fast.
The code lets your project be event driven, more like the python implementations (gpio zero etc). I've also made an example web integration to display/change the pin states and functions in real time.
The core library: https://github.com/calcinai/phpi
The web socket demo: https://github.com/calcinai/phpi-websocket
Hopefully it's of use to someone!
The code lets your project be event driven, more like the python implementations (gpio zero etc). I've also made an example web integration to display/change the pin states and functions in real time.
The core library: https://github.com/calcinai/phpi
The web socket demo: https://github.com/calcinai/phpi-websocket
Hopefully it's of use to someone!
-
- Posts: 81
- Joined: Tue Jan 12, 2016 1:53 pm
Re: PHP GPIO Library
Sorry but your library is not working with model RPI 2B, error:calcinai wrote: ↑Thu Jul 28, 2016 12:48 pmI know I'm a bit late to the party, but I've been working on a gpio/general peripheral integration library in PHP. The implementation supports SPI, PWM and most other GPIO functions without using sysfs - so it's fast.
The code lets your project be event driven, more like the python implementations (gpio zero etc). I've also made an example web integration to display/change the pin states and functions in real time.
The core library: https://github.com/calcinai/phpi
The web socket demo: https://github.com/calcinai/phpi-websocket
Hopefully it's of use to someone!
Code: Select all
pi@raspberrypi:~/php-gpio-web/phpi-websocket $ php examples/server.php
PHP Fatal error: Uncaught Calcinai\PHPi\Exception\UnsupportedBoardException: Board revision [1a01041/BCM2835] is not (yet) supported. in /home/pi/php-gpio-web/phpi-websocket/vendor/calcinai/phpi/src/PHPi/Factory.php:72
Stack trace:
#0 /home/pi/php-gpio-web/phpi-websocket/examples/server.php(15): Calcinai\PHPi\Factory::create(Object(React\EventLoop\StreamSelectLoop))
#1 {main}
thrown in /home/pi/php-gpio-web/phpi-websocket/vendor/calcinai/phpi/src/PHPi/Factory.php on line 72
Re: PHP GPIO Library
It seems that your board has a non-standard revision number. I've added support manually for it, but will investigate further as to why this might be. Some of the earlier models had bits prepended to the revision in turbo mode, could this be the case?rpiuser2016 wrote: ↑Mon Oct 30, 2017 11:53 amSorry but your library is not working with model RPI 2B, error:Does any one knows how to access to GPIO pins using PHP?Code: Select all
pi@raspberrypi:~/php-gpio-web/phpi-websocket $ php examples/server.php PHP Fatal error: Uncaught Calcinai\PHPi\Exception\UnsupportedBoardException: Board revision [1a01041/BCM2835] is not (yet) supported. in /home/pi/php-gpio-web/phpi-websocket/vendor/calcinai/phpi/src/PHPi/Factory.php:72 Stack trace: #0 /home/pi/php-gpio-web/phpi-websocket/examples/server.php(15): Calcinai\PHPi\Factory::create(Object(React\EventLoop\StreamSelectLoop)) #1 {main} thrown in /home/pi/php-gpio-web/phpi-websocket/vendor/calcinai/phpi/src/PHPi/Factory.php on line 72
In the mean time, run a composer update on phpi-websocket and everything should work.
Re: PHP GPIO Library
Thanks rpdom, that's the conclusion I had come to. I've since found a couple of explanations of the changed format, I'll implement a more robust identification process today.