Link to home
Start Free TrialLog in
Avatar of dynamicOne
dynamicOne

asked on

PHP with Raspberry Pi

I am controlling my garage door using a Raspberry Pi. I want to add a magnetic switch to tell if the door is open or closed.

Any idea where to start?

Here is what I have so far:

<!DOCTYPE html>

<html lang="en">
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux (vers 6 November 2007), see www.w3.org">
  <meta charset="utf-8">

  <title>Garage Remote</title>
  <!--Begin code to open and close garage door-->

  <script type="text/javascript">

  window.onload = function(){

        buttonOff = document.getElementById('submitPressed');
        buttonOff.onClick = relayOff;
  }

  function relayOff(){

        hidden = document.getElementById("total");
        hidden.value = "pressed";

        form = document.getElementById("relay");
        form.method = "GET";
        form.action = "remote.php";
        form.submit();
  }
  </script><!--End code to open and close garage door-->
  <!--Begin code to color button when pressed-->

  <script type="text/javascript">
function setColor(btn,color){
   
    var property=document.getElementById(btn);
   if (window.getComputedStyle(property).backgroundColor == 'rgb(244, 113, 33)') {
      property.style.backgroundColor=color;
   }
    else {
      property.style.backgroundColor = "#2159F4";
    }
  }
  </script><!--End code to color button when pressed-->
</head>

<body>
  <form id="relay" action="remote.php" method="get">
    <input type="button" id="submitPressed" style=
    "border:1px solid #000; font-size:80px;" value="Open/Close"
    onclick="relayOff();setColor('submitPressed','#fff200');">
    <input type="hidden" name="total" id="total" value="">
  </form><!--Begin code to open and close garage door-->
  <?php

          exec("gpio -g mode 17 out");
          $state = $_GET["total"];

          if ($state == "off"){
                  exec("gpio -g write 17 1");
                  sleep(1);
                  exec("gpio -g write 17 0");
          }

  ?><!--End code to open and close garage door-->
  <!--Insert code for magnetic switch here.-->
</body>
</html>
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Where is the web server that is running PHP?
Avatar of dynamicOne
dynamicOne

ASKER

It is running on the Pi.
Interesting, didn't realize that Raspberry Pi could run all that.  I have never used PHP to control hardware.  I suggest you click on "Request Attention" above to get others to look at your question.
SOLUTION
Avatar of David
David
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On another note, please use the code tags when you post code in your posts as it makes it tons easier to read.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial