-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>
Bruh
  • 11
  • 1
  • 1
  • 5
  • 3
    Why does it have to be just one line? – Jason Sep 20 '15 at 20:53
  • *How* could it be one line? You can't just take 20 lines of viable code and turn it into one. – Markus Meskanen Sep 20 '15 at 20:56
  • A comment from my previous post was saying it had to be. [link](http://stackoverflow.com/questions/32598420/cant-execute-python-script-from-php-document) @Jason – Bruh Sep 20 '15 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 – Bruh Sep 20 '15 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. – Markus Meskanen Sep 20 '15 at 21:01
  • Well I'm trying to use exec() in a PHP document to run this file @Markus Meskanen – Bruh Sep 20 '15 at 21:02
  • Even if I use exec() it still doesn't work @MarkusMeskanen – Bruh Sep 20 '15 at 21:12
  • in the comment you got it says the shell command should be one line (i.e. short), not the python script you execute :) – Pynchia Sep 20 '15 at 21:23
  • So what do I do to fix it? Sorry, I'm new and still trying to learn @Pynchia – Bruh Sep 20 '15 at 21:25

1 Answers1

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.

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
  • 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 – Bruh Sep 20 '15 at 21:49
  • 1
    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!) – Alastair McCormack Sep 20 '15 at 21:54
  • 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: – Bruh Sep 20 '15 at 22:03
  • Title  

    @AlastairMcCormack

    – Bruh Sep 20 '15 at 22:05
  • Rpi.GPIO often needs root privs. We may get way off topic here – Alastair McCormack Sep 20 '15 at 22:07
  • Your code is unreadable in a comment. Update your question if it's pertinent. – Alastair McCormack Sep 20 '15 at 22:08
  • Please help me. Lol i've been trying to figure this out for days and I need help. Do you by chance have an email address that we can talk over? @AlastairMcCormack – Bruh Sep 20 '15 at 22:08
  • I added an answer to your other post. – Jason Sep 20 '15 at 22:24