1

I know these type of "what's the best way" questions are frowned upon on StackExchange, but I will try to fit it within the guidelines. I have tried several methods and can't figure out which is the best to do this.

The Setup

I have a Pi (LAMP) networked to multiple Arduinos over I2C. I want to be able to click a button on a web page and operate a lights or take readings on the Arduino.

Progress

So far, I have found out how to use PHP to call change pins.

    //set the gpio's mode to output     
    system("gpio mode ".$pin." out");
    //reading pin's status
    exec ("gpio read ".$pin, $status, $return );
    //set the gpio to high/low
    if ($status[0] == "0" ) { $status[0] = "1"; }
    else if ($status[0] == "1" ) { $status[0] = "0"; }
    system("gpio write ".$pin." ".$status[0] );
    //reading pin's status
    exec ("gpio read ".$pin, $status, $return );
    //print it to the client on the response
    echo($status[0]);   
}

This works in changing the pins on the Pi itself.

Problem

I cannot translate this into an I2C call. I've tried to call a python script that uses SMBus, but I haven't been successful at getting it working. I've also looked at using Wiring Pi, but apparently there is an issue with calling data back into PHP.

I could use Javascript if this would be the best way. I've looked at using AJAX to make calls, and even considered if Node would be the best way. I am weakest with Javascript, but would learn if there is a developed library to achieve this goal.

In short, the question is what libraries or methods are there to execute I2C functions from a web page?

  • May or may not be useful to you as it is C centric, but there's a quick mention of PHP: raspberrypi.stackexchange.com/questions/37689/… Briefly, I don't think anyone has implemented this properly in PHP. WRT system/exec style stuff, I doubt there is really "an issue with calling data back into PHP" if you do it properly, but you should play around with that using something else as the methodology will be same regardless of the command. I think you are looking for this: php.net/manual/en/function.popen.php (using the r mode to read from the command). – goldilocks Jun 26 '16 at 19:26
  • @goldilocks I don't mind using C as well. It seems like PHP is a bit clunky in this method though (calling all languages). The more I look into this it seems like Node is the most developed in this area. – pekasus Jun 26 '16 at 23:24
  • 1
    I can't say. I know Raspbian ships with an IoT tool called node-red, which presumably means there are some js facilities for working with the GPIOs -- but I don't know if that extents to full I2C functionality or not. All of this is built on native C, so that's the most foolproof but long-winded approach ;) Beyond that python should be very trustworthy as it's definitely the most popular interface on the pi. I still think you should give popen() a spin though; I'm not a PHP user but that's a wrapper on a standard C function. – goldilocks Jun 26 '16 at 23:53
  • Meaning, if you are using that gpio tool then it's more or less a fork/exec with a pipe to read or (not and) write from/to. – goldilocks Jun 26 '16 at 23:55
  • I've created an example using WebSockets, for a library in pure PHP which can achieve this. All of the peripheral registers can be mapped natively, so all that is lacking is the protocol spec. github.com/calcinai/phpi-websocket – calcinai Sep 12 '16 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.