How to pass variables from php to python and vice versa using json

0

Someone knows why only one part of a program is executed in python, when it is called by one in php, my codes are as follows

<?php
      $x=$_GET['velocidad'];
       if(isset($_GET['der'])){
             $y=1;
             $z=0;


         }elseif(isset($_GET['izq'])){
              $z=1;
              $y=0;

           }

$data = array("$x", "$y", "$z");
$resultado=shell_exec("python3 /var/www/html/motorpwm.py " . escapeshellarg(json_encode($data)));
 $resultData = json_decode($resultado, true);
 var_dump($resultData);
?>

PYTHON CODE

import RPi.GPIO as GPIO
import sys, json
import time

try:
    data = json.loads(sys.argv[1])
except:
    print ("ERROR")
    sys.exit(1)
resultado = {'status': 'Yes!'}
print (json.dumps(data))
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 100)
pwm.start(0)
GPIO.setup(11, GPIO.OUT)  
pwm2 = GPIO.PWM(11, 100)
pwm2.start(0)
p3=0
p1=1
p2=0
while p3!=2:
        if p1==1:
                pwm.ChangeDutyCycle(100)
                pwm2.ChangeDutyCycle(0)
                time.sleep(1)
                p3=2
        elif p2==1:
                pwm.ChangeDutyCycle(0)
                pwm2.ChangeDutyCycle(100)

pwm.stop()
GPIO.cleanup()

the variables p1, p2, and the 100 that is in the if conditions, I want to replace them with the data obtained by json, so that is my other problem, how to handle or break down the variables obtained by php and use them separately. but the most important problem is that the rest of the program is not executed in python, the only thing that runs is from import rpi.GPIO as GPIO, to print (json.dumps (data)), the rest simply does not run, try to change the order of the code and return "null" to my php program.

just to clarify: -version of php = 5.6 -I use python3 -I use apache2 and html next to php -The objective of the code is to control the direction and speed of an engine -the python code if it works because if I remove the json part, and I run the program from the command terminal if it works.

this is the code it works with if it runs from the command window

import RPi.GPIO as GPIO
import sys
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 100)
pwm.start(0)
GPIO.setup(11, GPIO.OUT)  
pwm2 = GPIO.PWM(11, 100)
pwm2.start(0)
vel=float(sys.argv[1])
p1=int(sys.argv[2])
p2=int(sys.argv[3])
p3=0
print(vel)
print(p1)
print(p2)
while p3!=2:
        if p1==1:
                pwm.ChangeDutyCycle(vel)
                pwm2.ChangeDutyCycle(0)
                time.sleep(1)
                p3=2
        elif p2==1:
                pwm.ChangeDutyCycle(0)
                pwm2.ChangeDutyCycle(1)

print("fin")
pwm.stop()
GPIO.cleanup()
    
asked by esencia 01.10.2017 в 20:47
source

0 answers