2

I am a hobbyist with limited knowledge of html, javascript, php. I set up a Raspberry Pi microcomputer as a Web server. As a starting point I want to turn on a LED that is connected to the Pi with a button on a Web page. I have the following code that works (name of html file is min.php):

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
    <body>
    LED Control:
    <form method="get" action="min.php">
        <input type="submit" value="ON" name="on">
    </form>
    <?php
    $setmode7 = system("gpio mode 7 out");
    if(isset($_GET['on'])){
        $gpio_on = system("gpio write 7 1");
        echo "LED is on";
    }
    else {
        echo "LED is off";
    }
    ?>
    </body>
</html>

Now I want to rewrite the code with an ajax function so that the page does not reload when the button is clicked and here I have problems. I looked at a lot of the posted examples but I just can't get over the hump. I changed the html code as follows:

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
<button type="button" onclick="LED_On()">LED On</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script>
<script>
function LED_On(){
    $.ajax({
        url:"LED_On.php",
        type:"GET",
        data:"on"
    });
}
</script>
</body>
</html>

The LED_On.php file has the following code and is stored in the same directory as the html file:

<?php
if(isset($_GET['on'])){
     $setmode7 = system("gpio mode 7 out");
     $gpio_on = system("gpio write 7 1");
     echo "LED is on";
     }
     else {
        echo "LED is off";
    }
?>

Clicking the button does not turn on the LED. Any help is appreciated.

3
  • done any basic debugging, like checkin if the ajax code actually sent an HTTP request to the server? checked if it invoked your script? checked if the system() calls did anything? Commented Aug 23, 2016 at 21:25
  • data:{on:"true"} Commented Aug 23, 2016 at 21:29
  • might be <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script> needs to be <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> Commented Aug 23, 2016 at 21:29

1 Answer 1

2

Check your code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script>

<?script> should be changed to </script>

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.