Simple PHP-Web-Gpio example switching leds



  • As a first project I implemented an old application with which one can switch 2 leds, connected to gpio ports, on and off. The application is straight forwards, using only PHP and PHP-CGI.
    First you have to install PHP and PHP-CGI (see openwrt-wiki)

    opkg update
    opkg install php5 php5-cgi
    

    Notice the /etc/php.ini file afterwards.
    You also need to add the line:

    list interpreter ".php=/usr/bin/php-cgi"
    

    to the 'main' section of the /etc/config/uhhtpd file

    Restart your webserver with

    /etc/init.d/uhttpd restart
    

    and now you're up and running for using PHP

    I used gpiocfg instead of fast-gpio mainly because I didn't know of the existence of fast-gpio. In the first few lines of the PHP file you can select the gpio pins to use. Start using the webpage calling Omega-abcd.local/php/SwitchLeds.php in your browser.

    The PHP file is:
    SwitchLeds.php
    Place this file in /www/php, otherwise you need to change the above URL.
    Have fun!



  • @Rinus-van-Weert Great! Thanks for the example!



  • Hi Rinus,

    Thanks for the example. I got stuck as follows: php5 installed OK, but messages came back from opkg "Unknown package 'php5-cgi'." and "cannot install php5-cgi". There is no file php-cgi in the directory /usr/bin. Do you have a suggestion?

    Thank you.



  • Any update on this? My first project and it crashed as in the previous comment.

    Off to find another project to try, I guess. :) Bo


  • administrators

    @Yale-E-Goldman @Bo-Barry

    Try updating to the latest firmware, running opkg update and then trying opkg install php5 php5-cgi again.
    Worked on my end.



  • Thanks for the pointers here @Rinus-van-Weert! I've managed to get PHP up and running without any fuss at all. Only last thing I'd add is whilst you're in modifying uhttpd you might want set up a secondary site.

    So in /etc/config/uhttpd I've added the following below the main config definition

    ...
    
    config uhttpd 'myapp'
            list listen_http '0.0.0.0:4000'
            list listen_http '[::]:4000'
            list interpreter '.php=/usr/bin/php-cgi'
            option home '/www/myapp'
            option cgi_prefix '/cgi-bin'
            option index_page 'index.php'
            option error_page '/oopsie.html'
    ...
    

    The benifit (for me) being that, if you wanted to, you can access this site from the wider world without exposing the console.

    Happy Hacking all :P



  • @Bo-Barry got it loaded, now don't know to 'run it'. First time PHP.



  • @Rinus-van-Weert thanks for the example.
    find an small error on this line: "to the 'main' section of the /etc/config/uhhtpd file" => uhhtpd = uhttpd

    @Bo Barry you can run the program in your brouwser, http:\ipadres.for.onion\php\SwithcLeds.php
    (if you put the script in www/php as SwitchLeds.php)

    I have also a relay expansion and make 2 functions for this:

    function initRelay( ){ // init Relay expansion
        $command = 'relay-exp -i';
    	exec( $command ); //init 
    }
    
    function writeRelay( $pen, $value){ 
    	if ( is_numeric( $value) )	// check if value is a number, could also be empty, if so write value tot output pin
    	{	$command = 'relay-exp ' .$pen .($value == 1 ?  ' 1' : ' 0');
    		$res = exec( $command );
    		echo $res;
    	}
    	return $value;
    }
    
    

    but there is no way to get the status of the relays. you only now the status when init.



  • @Lazar-Demin Hi Lazar, After opkg update, php5-cgi installed and I was able to run PHP srcipts, like SwitchLeds and others. Thanks for the help. I had recently done the opkg update, so how often does it need to be run?



  • @Yale-E.-Goldman Package indexes are usually updated daily, so it's recommended that you do an opkg update before each opkg install.



  • @Boken-Lin @Rinus-van-Weert How can I install the "SwitchLeds.php" file directly to the onion omega? Thanks for the awesome example, I have been looking at making a project like this, but I had no idea whatsoever on what to do.
    Thanks,
    Kyle



  • Hi, I have made a class that use this functions to control the Onion
    php_onion

    I found an other interesting topic here that use an other approach



  • @Kyle I use pscp from putty with flag -scp
    Use

    >PSCP.exe -scp -pw {onionpasw} source\* root@{onion.IP}:/www/php
    

    to copy source to php directory



  • @Kyle from your Omega, you can also do a wget to get the file

    wget --no-check-certificate https://community.onion.io/uploads/files/1446719911755-switchleds.php


  • @Anco-van-der-Kolk
    @Fred-Blais
    Thanks guys! will do!


Log in to reply
 

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.