PHP


13 posts
by brucethehoon » Fri Jun 01, 2012 8:52 pm
Howdy, all. I am using PHP to control / read from the GPIO pins.

Steps:
Install Apache 2
Install PHP 5
A big issue: You really aren't going to want to run Apache as root, but the base setup requires you to be root in order to manipulate the GPIOs. gpio-admin will export them to an area that allows non privileged users access.
Install gpio-admin (https://github.com/quick2wire/quick2wire-gpio-admin)
Add your web server user to the gpio group

Anode of LED is connected to +3v, then a 330 resistor, then to GPIO 4.

Sample blinky LED php script (output only)
Code: Select all
<?php
shell_exec("gpio-admin unexport 4"); // in the off chance I've left it exported  somewhere else
shell_exec("gpio-admin export 4"); // make GPIO 4 ready for use
shell_exec("echo out > /sys/devices/virtual/gpio/gpio4/direction"); // set GPIO 4 mode
shell_exec("echo 0 > /sys/devices/virtual/gpio/gpio4/value");// bring low - will sink LED current
sleep(1);
shell_exec("echo 1 > /sys/devices/virtual/gpio/gpio4/value");//bring high - will turn off LED
sleep(1);
shell_exec("echo 0 > /sys/devices/virtual/gpio/gpio4/value");
sleep(1);
shell_exec("echo 1 > /sys/devices/virtual/gpio/gpio4/value");
sleep(1);
shell_exec("gpio-admin unexport 4"); // unexport the pin.  Don't skip this step
?>


Hope this helps someone!
Posts: 6
Joined: Tue Jan 10, 2012 7:01 pm
by andyl » Fri Jun 01, 2012 9:26 pm
TBH you don't really need Apache. However Debian does make it hard to just install the PHP language without also installing apache.

You can quite easily just write and run PHP scripts just like you do python scripts. They do not have to be run via web access, or with the privileges of the web server user.

Also you are using shell_exec to write to the files to turn the LED on and off. This is pretty inefficient. You can do the same with PHP's inbuilt file operations.
Posts: 265
Joined: Tue Jan 10, 2012 11:05 am
by brucethehoon » Fri Jun 01, 2012 10:07 pm
For my purposes, I needed to have Apache running.

Very true on the file operations, I'll make those modifications. even file_get_contents and file_put_contents should be faster than what I've done here.

Thanks for the feedback!
Posts: 6
Joined: Tue Jan 10, 2012 7:01 pm
by gadgetoid » Sun Jun 03, 2012 10:07 pm
GordonDrogon's WiringPi should be wrappable for PHP. It has the same drawback: requires root privileges. But when I benched a properly wrapped library against shell-exec the difference in speed was immense.

You won't notice this when switching a few LEDs, but if you try to interface with anything more complicated then speed counts.

I wrapped it up for Ruby using SWIG, I believe the same can be done for PHP.
Posts: 74
Joined: Wed Mar 07, 2012 9:58 pm
by avtsuk » Tue Jun 05, 2012 7:30 pm
Has anyone experimented with polling some gpi with php?

It'd be nice if there was a whole php class for playing with the gpio's
Posts: 7
Joined: Sun Jun 03, 2012 9:20 pm
by AforAlex » Fri Jun 08, 2012 12:19 am
Hi, I was looking at this but really stuck with getting the GPIO-admin stuff installed, I'm new to Linux so this is probably where my problem lies.

I've been reading through the instructions and they have a few too many gaps in the instructions (I assume I am probably mean to download a file to somewhere and then extract it ... but I'm at a loss as to how or where I should do this). Any chance of a few pointers?

Many thanks
Posts: 72
Joined: Thu May 31, 2012 7:56 pm
by avtsuk » Sat Jun 09, 2012 9:02 pm
HI,

I've written some PHP which wiggles the GPIO's and talks to an LCD Display...

See my Pi time at : http://youtu.be/dxdBxfTwA04

Its experimental for another project..
Posts: 7
Joined: Sun Jun 03, 2012 9:20 pm
by AforAlex » Sun Jun 10, 2012 12:01 am
This is what i have done so far, webcam pointing at an LED that you can then turn on and off :-)

http://thingswatihavedonewithmyraspberr ... pot.co.uk/

My goal is to connect it up to an RC car that can then be driven about.

My problem now is my electronic skills, not really sure at the moment how to connect it up to drive a motor. I'm sure the internet will show me how eventually. Any help in the mean time greatly appriciated.
Posts: 72
Joined: Thu May 31, 2012 7:56 pm
by avtsuk » Sun Jun 10, 2012 10:43 am
What sort of motor are you hoping to drive?

A. Washing machine motor :-) or a stepper motor?

I guess a stepper motor, but its actually easier to control a washing machine motor..

You're better off using a motor controller and interfacing to that, the next consideration is how you want to talk to the controller, what sort of stepper motor you are using and finding a controller that fits both requirements. Then its connecting up and programming.

Sounds interesting.

Can the Pi take to the sky and control a rc helicopter.. :-)
Posts: 7
Joined: Sun Jun 03, 2012 9:20 pm
by AforAlex » Sun Jun 10, 2012 10:04 pm
Remote control car, so couple of options as i see it:

- Interface with the RC controller to send the radio signal to the car
- Control the servos in the RC car
- Directly drive the motors of the RC car

At this stage I am still learning and trying to find out how to do any of the above, my electronic skills are basic and so far limited to making an LED switch on and off, but I don't see how that is so different from switching a motor on and off ....

my feeling is some way of driving the servo's would be the simplest method of control as would mean the Raspberry pi would just need to be connected in two places (Steering and drive). But i am still new to this. So guess in answer to your question it would be a stepping motor that I would like to control.

Does anyone know of a good beginners guide? My problem at the moment i that I do not know what i don't know, once i know it then it will all seem so simple, but until i know it i'm clueless, so any pointers to books/websites/other resources are greatly appreciated.

thanks
Posts: 72
Joined: Thu May 31, 2012 7:56 pm
by brucethehoon » Wed Jun 13, 2012 12:41 am
AforAlex wrote:Hi, I was looking at this but really stuck with getting the GPIO-admin stuff installed, I'm new to Linux so this is probably where my problem lies.

I've been reading through the instructions and they have a few too many gaps in the instructions (I assume I am probably mean to download a file to somewhere and then extract it ... but I'm at a loss as to how or where I should do this). Any chance of a few pointers?

Many thanks


Sorry! for some reason I didn't get notified on this post.

Install GIT:

Code: Select all
sudo bash
apt-get install git-core


next, install the gpio-admin using GIT:

Code: Select all
git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd gpio-admin
make
make install


that should do it.

So sorry for not replying sooner. If this board has private messaging, please feel free to message me if I don't reply.

It's worth noting that others were right. This is NOT the best way to hit the GPIOs. Heck, PHP isn't a great start in the first place. The thing is that for my very specific purposes, it was the best choice. I didn't have to learn a new language variant just to turn on an LED or check a GPIO. If (for some reason) you are in the same boat, I'm happy to help. I've little doubt that someone will come up with a PHP mod that will do it FAR better, and soon.
Posts: 6
Joined: Tue Jan 10, 2012 7:01 pm
by Cyberspice » Thu Sep 13, 2012 4:06 pm
Hi,

I'm an embedded software developer. I also happen to be a PHP internals developer too. I'm the maintainer of the Direct IO (Serial and low level file system IO) extension for PHP. I've started work on a GPIO extension so watch this space :-)
Posts: 2
Joined: Thu Jul 05, 2012 10:06 pm
by pyromagi » Fri Jan 03, 2014 10:22 am
How long should we watch this space for? :D
I would like a simple php driven page that alter the value of a pin and checks status of all pins.

Kind regards
Posts: 9
Joined: Wed Dec 25, 2013 1:39 am