THORNADOR
Posts: 19
Joined: Mon Mar 02, 2020 4:12 pm

Control LED

Wed Mar 11, 2020 10:38 am

Hi, I´m doing a web page for the control of one LED. I have seen two different possibilites for do it, but I don´t know which one is better (or if it works).
OPTION 1

Code: Select all

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Raspberry Pi WiFi Controlled LED</title>
</head>
       <body>
       <center><h1>Control LED using Raspberry Pi Webserver</h1>      
         <form method="get" action="index.php">                
            <input type="submit" style = "font-size: 14 pt" value="OFF" name="off">
            <input type="submit" style = "font-size: 14 pt" value="ON" name="on">
         </form>​​​
            </center>
	   </body>
</html>

<?php
    shell_exec("/usr/local/bin/gpio -g mode 27 out");
    if(isset($_GET['off']))
        {
            echo "LED is off";
            shell_exec("/usr/local/bin/gpio -g write 27 0");
        }
    else if(isset($_GET['on']))
        {
            echo "LED is on";
            shell_exec("/usr/local/bin/gpio -g write 27 1");
        }
?>
OPTION 2 (with Pyton)

Code: Select all

<html>
 <body>
  <!--GPIO--> 
  <form action="" method="post">
			<input type="submit" name="on" value="On">
			<input type="submit" name="offr" value="Off">
 </body>
</html>

<?php
  if ($_POST[on]) { 
   $a- exec("sudo python /var/www/html/on.py");
   echo $a;
  }
  if ($_POST[off]) { 
   $a- exec("sudo python /var/www/html/off.py");
   echo $a;
  }
?>

## on.py
#!usr/bin/env/ python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
GPIO.output(27, GPIO.HIGH)
GPIO.cleanup()

## off.py
#!usr/bin/env/ python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
GPIO.output(27, GPIO.LOW)
GPIO.cleanup()

JohnsUPS
Posts: 188
Joined: Fri Jul 06, 2018 2:13 am
Location: USA

Re: Control LED

Wed Mar 11, 2020 10:42 am


User avatar
neilgl
Posts: 2544
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Control LED

Wed Mar 11, 2020 11:07 am

I would go with (2) the python method. The first one uses wiringpi which is no longer supported unfortunately.

THORNADOR
Posts: 19
Joined: Mon Mar 02, 2020 4:12 pm

Re: Control LED

Wed Mar 11, 2020 11:59 am

neilgl wrote:
Wed Mar 11, 2020 11:07 am
I would go with (2) the python method. The first one uses wiringpi which is no longer supported unfortunately.
Thanks
And the code is well write, isn´t it?

Return to “General discussion”