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>
ProgrammingHome SecurityPersonal ElectronicsPHPRaspberry Pi

Avatar of undefined
Last Comment
noci

8/22/2022 - Mon
Dave Baldwin

Where is the web server that is running PHP?
ASKER
dynamicOne

It is running on the Pi.
Dave Baldwin

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
David

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Eddie Shipman

On another note, please use the code tags when you post code in your posts as it makes it tons easier to read.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.