Pickley
Posts: 5
Joined: Thu Jul 05, 2012 8:58 am

PHP GPIO Library

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.

bircoe
Posts: 33
Joined: Wed Jul 27, 2011 9:43 pm

Re: PHP GPIO Library

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?

Pickley
Posts: 5
Joined: Thu Jul 05, 2012 8:58 am

Re: PHP GPIO Library

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.

bircoe
Posts: 33
Joined: Wed Jul 27, 2011 9:43 pm

Re: PHP GPIO Library

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.

frak
Posts: 17
Joined: Sun Jul 22, 2012 9:33 pm

Re: PHP GPIO Library

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

Island Medic
Posts: 1
Joined: Thu Aug 30, 2012 2:42 am

Re: PHP GPIO Library

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

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: PHP GPIO Library

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

User avatar
gordon@drogon.net
Posts: 1961
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: PHP GPIO Library

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/

janek
Posts: 8
Joined: Sun Aug 05, 2012 4:10 pm

Re: PHP GPIO Library

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?

bitbitbit
Posts: 30
Joined: Thu Aug 30, 2012 3:18 am

Re: PHP GPIO Library

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:

User avatar
gordon@drogon.net
Posts: 1961
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: PHP GPIO Library

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/

bitbitbit
Posts: 30
Joined: Thu Aug 30, 2012 3:18 am

Re: PHP GPIO Library

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:

User avatar
gordon@drogon.net
Posts: 1961
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: PHP GPIO Library

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/

mth3r
Posts: 2
Joined: Fri Nov 09, 2012 7:22 pm

Re: PHP GPIO Library

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?

mth3r
Posts: 2
Joined: Fri Nov 09, 2012 7:22 pm

Re: PHP GPIO Library

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.

arno_u_loginlux
Posts: 2
Joined: Fri Nov 23, 2012 10:01 pm

Re: PHP GPIO Library

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.

damianpeterson
Posts: 1
Joined: Wed Dec 05, 2012 7:50 am

Re: PHP GPIO Library

Wed Dec 05, 2012 7:52 am


magnified
Posts: 1
Joined: Thu Jan 31, 2013 1:06 am

Re: PHP GPIO Library

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.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: PHP GPIO Library

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'

Pickley
Posts: 5
Joined: Thu Jul 05, 2012 8:58 am

Re: PHP GPIO Library

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

calcinai
Posts: 1
Joined: Mon Jul 04, 2016 9:06 am

Re: PHP GPIO Library

Thu Jul 28, 2016 12:48 pm

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!

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 1 guest