About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Register or Sign In

I2C bus works only with direc start in development area/ but not when compiled programm is started by PHPskript

276 Views - Created 20/02/2020


Posted by:
Thomas Schmieder

Last edited: 20/02/2020

Users Avatar

I have developped a program for I2C bus and IO PI plus. This program works correctly when started in the developers area. When started the compiled program by a php file the message below pops up

The PHP file is shown here:


echo shell_exec('/home/pi/Documents/tom/tom/Debug/tom');


The relevant C++ code is here for I2C bus is here

const char *fileName = "/dev/i2c-1"; // change to /dev/i2c-0 if you are using a revision 0002 or 0003 model B

void write_byte_data(char address, char reg, char value) {
    if ((i2cbus = open(fileName, O_RDWR)) < 0) {
        printf(".Failed to open i2c port for write\n");    This message pops up when started by the PHP file!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    if (ioctl(i2cbus, I2C_SLAVE, address) < 0) {
        printf("..Failed to write to i2c port for write\n");

    buf[0] = reg;
    buf[1] = value;

    if ((write(i2cbus, buf, 2)) != 2) {
        printf("...Failed to write to i2c device for write\n");


Can somebody please explain/I think it is a problem with the userrights of the Raspb and can please exactly what to program in Linux

and also please how parameters can be checked in Linux. PS program also dont work when using root rights (sudo -i)

Thank you for help Tom

PS im using a Raspberry 3  B    /////// the parameter O_RDWR is no where declared...is this correct?


Posted by:

Users Avatar

United Kingdom

Users Twitter  Users Website  

Hello Thomas

The problem you are having is probably a permissions issue where PHP does not have permission to access the I2C bus.

Try adding www-data to the i2c group with the following command.

sudo adduser www-data i2c

You may also have to run the following command to give read/write permissions to /dev/i2c-1.

sudo chmod g+rw /dev/i2c-1

O_RDWR is an argument flag for the open system call.  You can find out more about how open works on this Linux manual page.

AB Electronics UK Tech Support


Posted by:
Thomas Schmieder

Users Avatar

Vielen Dank an AB...funktioniert!


Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.