av8tor
Posts: 13
Joined: Wed Mar 12, 2014 12:33 am

Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sat Mar 29, 2014 8:58 pm

I'm building an application on the Raspberry that required more I/O pins then the GPIO offered. So using i2c and the MCP23017 IC I'm having partial success.
I can execute the i2cset commands from the command line and they work perfectly, example:

Code: Select all

To set up GPB0-7 as outputs:
sudo i2cset -y 1 0x20 0x01 0x00 
To turn on GPB0:
sudo i2cset -y 1 0x20 0x15 0x01
The problem comes in when I try to use the same i2cset commands in my PHP program. I have the Raspberry running apache2 and php5.

Code: Select all

<?php
system ('sudo i2cset -y 1 0x20 0x01 0x00'); /* set GPB0-7 to output pins */
system ('sudo i2cset -y 1 0x20 0x15 0x01'); /* turn on GPB0 */
?>
Nothing happens when I run the code, no errors, nothing... I'm thinking since i2cset must be run with sudo that is it possible its a permission problem? Regardless what the problem is, how can I execute i2cset commands from PHP?
Thanks
George

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sun Mar 30, 2014 1:08 am

sudo is always a problem because of the delay. The obvious solution is to either run your program as root or change the I2C calls so that a semi-normal user can run the program.

execs from python are asynchronous.

Here are a few things to try.

Set the ownership and mode of i2cset and i2cget. Change the owner to root and set the mode to 4755. Now anyone can use the program.
or
Change the mode of /dev/i2c-0 and /dev/i2c-1 with chmod o+rw /dev/i2c-0
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

av8tor
Posts: 13
Joined: Wed Mar 12, 2014 12:33 am

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sun Mar 30, 2014 11:38 am

Richard-TX wrote:sudo is always a problem because of the delay. The obvious solution is to either run your program as root or change the I2C calls so that a semi-normal user can run the program.

Set the ownership and mode of i2cset and i2cget. Change the owner to root and set the mode to 4755. Now anyone can use the program.
or
Change the mode of /dev/i2c-0 and /dev/i2c-1 with chmod o+rw /dev/i2c-0
Thanks for your suggestions:
I tired the chmod of the i2c-0 and i2c-1 files, that didn't help.

The php program is run on the apache2 webserver, if I wanted to allow the php program to run as root does that mean I would have to allow apache2 run as root? If so how do I do that.
I realize from a security point of view that's not a good idea, however this webserver is not connected to the "outside" world, this application is only used inside my home....

George

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sun Mar 30, 2014 12:40 pm

I sort of forgot about the apache part.

In the case of apache you should set i2cget and i2cset as suid root programs. That leaves the system pretty secure.

What may be a problem is where the executables are. There is server-root and document-root and cgi-bin to consider.

A lot will depend on how your apache server is configured.

Then there are other issues such as the non-synchronous execs within Python/Perl/php. What I have seen happen in Python and php is that the exec of the external program occurs, but the Python script continues on without waiting for the command to return. As a result, either output is lost or the second of 3rd exec does not occur as Python has exited thus killing the child processes before they have a chance to do their assigned tasks.

So figure out what sort of exec call you have to make so that Python will wait for the child process to exit and return.

Check your apache logs. They should tell you something.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sun Mar 30, 2014 12:45 pm

In addition to the path to the executables, the devices that the executeables may also be a factor. You can create copies of the /dev/i2c-* devices anywhere on the system. mknod is the tool you need to do it.

If you post a copy of your program as well as your sites-enabled/site file I will duplicate your setup and see what the issue is and how to fix it.

I will also need a list of enabled plugins/modules
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sun Mar 30, 2014 1:53 pm

Ok.

I found your problem and have a few things for you to do.

login as root and execute the following

Code: Select all

chmod 4755 /usr/sbin/i2cdetect /usr/sbin/i2cset /usr/sbin/i2cget /usr/sbin/i2cdump
use the follwing php script.

Code: Select all

  <?php
    system ('/usr/sbin/i2cdetect -y 1'); /* set GPB0-7 to output pins */
    system ('/usr/sbin/i2cget -y 1 0x20'); /* set GPB0-7 to output pins */
    system ('/usr/sbin/i2cget -y 1 0x44'); /* set GPB0-7 to output pins */
    system ('/usr/sbin/i2cdump -y 1 0x20'); /* set GPB0-7 to output pins */
    /* system ('sudo i2cset -y 1 0x20 0x01 0x00'); /* set GPB0-7 to output pins */
    /*system ('sudo i2cset -y 1 0x20 0x15 0x01'); /* turn on GPB0 */
    ?>
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Sun Mar 30, 2014 2:11 pm

Please note that messages to stderr are not printed to the browser.

To get those messages you have to redirect stderr to stdout like this:

system ('/usr/sbin/i2cget -y 1 0x20 2>&1');

If you had done that with your original program you would have see this:

Code: Select all

sudo: no tty present and no askpass program specified 
Sudo needs both and that is why sudo will not work. su would not work either.

removing sudo and wIthout the full pathname to the executable the message would have been:

Code: Select all

sh: 1: i2cset: not found 
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

snowboarder
Posts: 1
Joined: Mon Mar 31, 2014 7:16 pm

Re: Raspberry, PHP, I2C, and the MCP23017 Port Expander

Mon Mar 31, 2014 7:18 pm

Just curious... why not write your python script and call the python script from your php application?

Return to “General discussion”