User avatar
Mettauk
Posts: 235
Joined: Mon Dec 10, 2012 12:40 pm
Location: Zarg

Reading GPIO states via web interface - php

Wed Jan 17, 2018 5:31 pm

I have the following code in a php page that will switch a relay on or off and tells me when its done.
But, when I open the page I can't tell the current state (high or low, 0 or 1). Can someone guide me how to include a call to read and display current state of a GPIO?

Code: Select all

...
<p><form method="get" action="switch.php">
                <input type="submit" value="ON" name="on">
                <input type="submit" value="OFF" name="off">
        </form>
<?php

        $setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out");
        if(isset($_GET['on'])){
                $gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 1");
                echo "Heater is on";
        }
As humans we have been the same for a very very long time, technology changes how we do... not who we are as people.

User avatar
rpdom
Posts: 14979
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Reading GPIO states via web interface - php

Wed Jan 17, 2018 6:56 pm

You can get the current value like this

Code: Select all

$status = shell_exec("/usr/local/bin/gpio -g read 17");
$status will now contain 1 (on) or 0 (off).

User avatar
Mettauk
Posts: 235
Joined: Mon Dec 10, 2012 12:40 pm
Location: Zarg

Re: Reading GPIO states via web interface - php

Wed Jan 17, 2018 7:27 pm

Thank you, it works sort of now, I am trying to use that result for something like...

Code: Select all

$status = shell_exec("/usr/local/bin/gpio -g read 17");
        if $status = 0 ;
                echo "WOW its O";
        else if $status = 1 ;
                echo "OH no its 1";
I Which does not work but I don't know the correct method

EDIT... found it sort of I think but it does not seem to update on a page refresh or reload with no form data?

Code: Select all

$status = shell_exec("/usr/local/bin/gpio -g read 17");

        if ($status = "0") {
                echo "WOW its O";
        }
        else if ($status = "1") {
                echo "OH no its 1";
        }
Am I confusing High and Low with 0 and 1 which indicate in or out? it seems to be stuck showing 1 with the GPIO is high or low? (Relay open or closed)
I have a different css script that updates an html page on refresh or click to show a differing button colour as

Code: Select all

        		#gpio17.HIGH {
        				background-color: Red;
        				color: White;
        		}
        		
        		#gpio17.LOW {
        				background-color: Gray;
        				color: Black;
        		}
but this is not in php and i'm getting very confused. :(
As humans we have been the same for a very very long time, technology changes how we do... not who we are as people.

Return to “Troubleshooting”