-2

I'm trying to write a one line python script to execute from linux terminal from a PHP document. The problem is that it has for loops and comments and i'm not sure how to make it into one line. Here's the script:

#!/usr/bin/env python

# Import required Python libraries
import RPi.GPIO as GPIO
import time

# Use BCM GPIO references instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# init list with pin numbers

pinList = [15]

# loop through pins and set mode and state to 'low'

for i in pinList: 
    GPIO.setwarnings(False)
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)

def trigger() :
        for i in pinList:
          GPIO.output(i, GPIO.HIGH)
          print "on"
#         GPIO.cleanup()
          break


try: 
    trigger()


except KeyboardInterrupt:
  print "  Quit" 
  # Reset GPIO settings
  GPIO.cleanup()

I'm running this from a PHP document that looks like the following:

<html>
<head>
<meta charset="UTF-8" />
  <link rel="stylesheet" type="text/css" href="css/style.css">
</head>


<?php
if (isset($_POST['LightON']))
{
exec("sudo python /home/pi/lighton.py");
}
if (isset($_POST['LightOFF']))
{
exec("sudo python /home/pi/lightoff.py");
}
?>

<form method="post">
<button class="btn" name="LightON">Light ON</button>&nbsp;
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form> 


</html>
9
  • 3
    Why does it have to be just one line? Commented Sep 20, 2015 at 20:53
  • How could it be one line? You can't just take 20 lines of viable code and turn it into one. Commented Sep 20, 2015 at 20:56
  • A comment from my previous post was saying it had to be. link @Jason Commented Sep 20, 2015 at 20:57
  • He said " haven't any idea writing to hardware pins. Write a local service (or single line python shell command(only startwith sudo)) , and send to shell or service. don't play with file permission" @Markus Meskanen Commented Sep 20, 2015 at 20:59
  • @AaronScottVigal I don't know about that, but I know you can't turn the script you have into one line. Well, you can using exec() and have it as a string, but don't take that route. You need something else than converting every script you have into one line. Commented Sep 20, 2015 at 21:01

1 Answer 1

1

That's a lot of code to do very little! You certainly don't need to keep the comments and if you're only setting one pin then you don't need the loops.

You may be able to get away with:

python -c "import RPi.GPIO as GPIO ; GPIO.setmode(GPIO.BCM) ; GPIO.setup(15, GPIO.OUT) ; GPIO.output(15, GPIO.HIGH)"

If you need to do anything more complicated than that then you should use a script or use something Flask to run the script as a web service. Better yet, use Python + Flask for the whole thing.

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

8 Comments

So do I run this inside of my php script like exec("THE_CODE_MENTIONED_ABOVE"); or do I just put the code inside the if() statement? @AlastairMcCormack
What if statement? Why don't you try it and see what happens? It's not going to set fire to anything! (Unless pin 15 is connected to a fuse!)
Lol I tried and it's not working. That code works when I put it in a python file and run it but it wont run from my php file. This is the if statement i'm talking about:
<html> <head> <title> Title </title> </head> <?php if (isset($_POST['LightON'])) { exec("sudo python on.py"); } if (isset($_POST['LightOFF'])) { exec("sudo python off.py"); } ?> <form method="post"> <button class="btn" name="LightON">Light ON</button>&nbsp; <button class="btn" name="LightOFF">Light OFF</button><br><br> </form> </html> @AlastairMcCormack
Rpi.GPIO often needs root privs. We may get way off topic here
|

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.