Raspberry PI GPIO PHP web page only works from Raspberry PI's

Viewed 20

head scratcher here. I have a login page and a home page on a Raspberry pi 3+. I have another 3+ and a Zero W in my house doing numerous things. On the one with the web pages, you login through a local sql database then get a home page of my pool pump and the water solar panel for the pool. There you can turn the pool pump on and off and the water solar panel. This is a mixture of PHP and HTML using GPIO to tun things on and off. If I RDP into the Raspberry PI and run the pages they work fine. Things turn on then off when clicked upon. If I goto one of the other Raspberry PI's and run the pages they work. But if I run it from a PC on the same network no joy, pages display but no GPIO is run when one of the two buttons are clicked. All the folders are open to read, write and execute. Not sure why it does not work. I even copied gpio into the script directory where the home.php resides. Below is the code once you login and see temps and status of pumps. When you click on a button it sets the pump on or off and changes the image from on to off or vice versa. As stated it works on the pi but not through a non pi browser.

<html>
<!-- Pump-temp.php - Gets pool and panel temp and allows you to turn the pumps on and off. -->
<!-- 20170911 - Determined relay's were on in low state.  Change icons and wiring. -->
<!-- 20170914 - New pump relay High to activate not low. -->
<!-- 20220115 - Adding High and Low temps-->
<!-- 22020916 - Added /user/bin/ to all GPIO commands for remote exection to work -->

    <body>
        <?php
        // Getting the temps - pump-temp.php 8/26/2017
        // $result is the array from all temp files, $p# positions for string pull $t#,$ht# and $lt# temps from the string
        // Actual temp file
        $myfile = fopen("/var/www/html/temp.txt","r");
        $result = fgets($myfile);
        fclose($myfile);
        $p1=strpos($result, ',');
        $p2=strpos($result, ',', $p1+1);
        $p3=strpos($result, ']');
        $t1=substr($result, 1, $p1-1);
        $t2=substr($result, $p1+2, ($p2-1)-($p1+1));
        $t3=substr($result, $p2+2, ($p3-1)-($p2+1));

        // High temps
        $hmyfile = fopen("/var/www/html/htemp.txt","r");
        $hresult = fgets($hmyfile);
        fclose($hmyfile);
        $hp1=strpos($hresult, ',');
        $hp2=strpos($hresult, ',', $hp1+1);
        $hp3=strpos($hresult, ']');
        $ht1=substr($hresult, 1, $hp1-1);
        $ht2=substr($hresult, $hp1+2, ($hp2-1)-($hp1+1));
        $ht3=substr($hresult, $hp2+2, ($hp3-1)-($hp2+1));

        // Low temps
        $lmyfile = fopen("/var/www/html/ltemp.txt","r");
        $lresult = fgets($lmyfile);
        fclose($lmyfile);
        $lp1=strpos($lresult, ',');
        $lp2=strpos($lresult, ',', $lp1+1);
        $lp3=strpos($lresult, ']');
        $lt1=substr($lresult, 1, $lp1-1);
        $lt2=substr($lresult, $lp1+2, ($lp2-1)-($lp1+1));
        $lt3=substr($lresult, $lp2+2, ($lp3-1)-($lp2+1));

        // setting gpio pins
        exec("sudo gpio -g mode 17 out"); 
        exec("sudo gpio -g mode 27 out");
        exec("sudo gpio -g mode 22 out");
        exec("sudo gpio -g read 17 ", $rReturn1, $errcode1 );
        exec("sudo gpio -g read 22 ", $rReturn2, $errcode2 );
        ?>
        <table border="0" frame="void" align="center">
            <form method="POST">
                <?php
                
                if(isset($_POST["P1"])) {
                    $overidefile = fopen("/home/pi/overide.txt","w");
                    if( $rReturn1[0] == "0") {
                        $rReturn1[0] = "1";
                    } else {
                        $rReturn1[0] = "0";
                    }
                    exec ("sudo gpio -g write 17 ".$rReturn1[0] );
                    fwrite($overidefile,$rReturn1[0]);
                    fclose($overidefile);
                }
                if(isset($_POST["P2"])) {
                    $overidefile = fopen("/home/pi/overide.txt","w");
                    if( $rReturn2[0] == "0") {
                        $rReturn2[0] = "1";
                        exec ("sudo gpio -g write 27 ".$rReturn2[0] );
                        sleep(2);
                        exec ("sudo gpio -g write 22 ".$rReturn2[0] );
                    } else {
                        $rReturn2[0] = "0";
                        exec ("sudo gpio -g write 22 ".$rReturn2[0] );
                        sleep(2);
                        exec ("sudo gpio -g write 27 ".$rReturn2[0] );
                    }
                    fwrite($overidefile,$rReturn2[0]);
                    fclose($overidefile);
                }
                exec ("sudo gpio -g read 17 ", $rReturn1, $errcode1 );
                exec ("sudo gpio -g read 22 ", $rReturn2, $errcode2 );
                ?>

                <tr>
                    <td width="40" height="25" valign="middle" align="right">
                    <?php echo date("H:i");?>
                    </td>
                    <td width="75" valign="middle" align="center"><b>High</b></td>
                    <td width="75" valign="middle" align="center"><b>Actual</b></td>
                    <td width="75" valign="middle" align="center"><b>Low</b></td>
                </tr>
                <tr>
                    <td width="40" valign="middle" align="right"><b>Outside</b></td>
                    <td width="75" height="25" valign="middle" align="center">
                    <?php
                    echo number_format($ht1,2);
                    ?>
                    </td>
                    <td width="75" height="25" valign="middle" align="center"><b>
                    <?php
                    echo number_format($t1,2);
                    ?></b>
                    </td>
                    <td width="75" height="25" valign="middle" align="center">
                    <?php
                    echo number_format($lt1,2);
                    ?>
                    </td>
                </tr>
                <tr>
                    <td width="40" valign="middle" align="right"><b>Pool</b></td>
                    <td width="75" height="25" valign="middle" align="center">
                    <?php
                    echo number_format($ht2,2);
                    ?>
                    </td>
                    <td width="75" height="25" valign="middle" align="center"><b>
                    <?php
                    echo number_format($t2,2);
                    ?></b>
                    </td>
                    <td width="75" height="25" valign="middle" align="center">
                    <?php
                    echo number_format($lt2,2);
                    ?>
                    </td>
                </tr>
                <tr>
                    <td width="40" valign="middle" align="right"><b>Panel</b></td>
                    <td width="75" height="25" valign="middle" align="center">
                    <?php
                    echo number_format($ht3,2);
                    ?>
                    </td>
                    <td width="75" height="25" valign="middle" align="center"><b>
                    <?php
                    echo number_format($t3,2);
                    ?></b>
                    </td>
                    <td width="75" height="25" valign="middle" align="center">
                    <?php
                    echo number_format($lt3,2);
                    ?>
                    </td>
                </tr>
                <tr>
                    <td width="40"></td>
                    <td width="75" height="25" valign="middle" align="center"><b>Pool</b></td>
                    <td width="75"></td>
                    <td width="75" height="25" valign="middle" align="center"><b>Panel</b><td>
                </tr>
                <tr>
                    <td width="40" height="50" align="center"><b>Pump</b></td>
                    <td width="75" height="50" align="center">
                    <?php
                    if ( $rReturn1[0] == "0") {
                        echo '  <input type="image" name="P1" src="../images/Off_32.png" height="32" width="32" value="Submit">';
                    } else {
                        echo '  <input type="image" name="P1" src="../images/On_32.png" height="32" width="32" value="Submit">';
                    }
                    ?>
                    </td>
                    <td width="75"></td>
                    <td width="75" height="50" align="center">
                    <?php
                    if ( $rReturn2[0] == "0") {
                        echo '  <input type="image" name="P2" src="../images/On_32.png" height="32" width="32" value="Submit">';
                    } else {
                        echo '  <input type="image" name="P2" src="../images/Off_32.png" height="32" width="32" value="Submit">';
                    }
                    ?>
                    </td>

                </tr>
            </form>
        </table>    
    </body>

</html>
0 Answers
Related