nahuavrilero
Posts: 17
Joined: Fri Jun 22, 2018 11:22 am

Stopping a python script from php

Tue Nov 27, 2018 11:19 pm

Hello.
I have a led changing color in an infinite loop and i need to stop it from a php page.
Is threre any way to do this?

Here are my python and php codes:

Code: Select all

<!DOCTYPE html>
<?php
header('refresh:10; url=lights.php')
?>

<html>
<title>Control de Luces</title>
<body background="wheat.png">

<br>
<form action="<?=$_SERVER['PHP_SELF']; ?>" method="post">

<button type="submit" style="height:150;width:350" name="lights" value="Switch"><font size="52">Luces</font></button>
<button type="submit" style="height:150;width:350" name="vent" value="Ventilador"><font size="52">Ventilador</font></button>
<button type="submit" style="height:150;width:350" name="rele3" value="Otro"><font size="52">Relé 3</font></button>

<br><br><br>
<font size="15"><font color="#F5ECCE"><u>LED:</u></font><br>

<button type="submit" style="height:150;width:350" name="rojo" value="Rojo"><font size="52">Led Rojo</font></button>
<button type="submit" style="height:150;width:350" name="verde" value="Verde"><font size="52">Led Verde</font></button>
<button type="submit" style="height:150;width:350" name="azul" value="Azul"><font size="52">Led Azul</font></button><br><br>
<br>
<button type="submit" style="height:150;width:350" name="random" value="random"><font size="52">Animación</font></button><br><br>

<?php

if (isset($_POST['lights']))
    {
         shell_exec("python3 /var/www/html/lights.py");
    }
else if (isset($_POST['vent']))
	{
		shell_exec("python3 /var/www/html/vent.py");
	}
else if (isset($_POST['rele3']))
        {
                shell_exec("python3 /var/www/html/rele3.py");
        }
else if (isset($_POST['rojo']))
    {
         shell_exec("python3 /var/www/html/rojo.py");
    }
else if (isset($_POST['verde']))
    {
         shell_exec("python3 /var/www/html/verde.py");
    }
else if (isset($_POST['azul']))
    {
         shell_exec("python3 /var/www/html/azul.py");
    }
else if (isset($_POST['off']))
    {
         shell_exec("python3 /var/www/html/alloff.py");
    }
else if (isset($_POST['random']))
    {
         shell_exec("python3 /var/www/html/ledrgb.py");
    }



?>
</form>

<form action="ledrgb.py/cerrar" method="post">
<button type="submit" style="height:150;width:350" name="apagar" value="true"><font size="42">Detener Animación</font></button><br><br>
</form>

<font face="Comic Sans MS" size="5" color="#009973"><b>

<?php

$statelight =  "<pre>".shell_exec("python3 /var/www/html/lightstate.py")."</pre>";
print $statelight;

$statevent = "<pre>".shell_exec("python3 /var/www/html/ventstate.py")."</pre>";
print $statevent;

$estado=  "<pre>".shell_exec("/usr/bin/python3 /var/www/html/relestate.py")."</pre>";
echo $estado;
?>
<hr>

<button type="submit" style="height:150;width:350" name="off" value="Switchoff"><font size="46">Apagar Todo</font></button>
<br>
<?php
echo shell_exec("php /var/www/html/temp.php");
?>
</font>
<br>
<a href="base.php"><font size="4"><b>Volver</b></font></a>
<div>
<div style="float:left" id="cont_4757ac49d574e272c6172255764ae404">
<script type="text/javascript" async src="https://www.meteored.com.ar/wid_loader/4757ac49d574e272c6172255764ae404">
</script></div>

</body>
</html>

Code: Select all

import pigpio
from time import sleep
pi=pigpio.pi()
pi.set_mode(17, pigpio.OUTPUT)
pi.set_mode(27, pigpio.OUTPUT)
pi.set_mode(22, pigpio.OUTPUT)

rojo=22
verde=27
azul=17

def cerrar(req, apagar):
       exit

def start(bR=0, bG=0, bB=0):
        
        while bR < 255:
                bR = bR + 1
                pi.set_PWM_dutycycle(rojo,bR)
                sleep(0.01)
        

def RtoY(bR=255, bG=0):
      
        while bG < 255:
                bG  = bG + 1
                pi.set_PWM_dutycycle(verde,bG)
                sleep(0.01)
        

def YtoG(bG=255, bR=255):
      
        while bR > 0:
                bR  = bR - 1
                pi.set_PWM_dutycycle(rojo,bR)
                sleep(0.01)
        
def GtoC(bG=255, bB=0):
       
        while bB < 255:
                bB  = bB + 1
                pi.set_PWM_dutycycle(azul,bB)
                sleep(0.01)
        
def CtoB(bG=255,bB=255):
        
        while bG > 0:
                bG  = bG - 1
                pi.set_PWM_dutycycle(verde,bG)
                sleep(0.01)
        
def BtoV(bR=0, bB=255):
        while bR < 255:
                bR  = bR + 1
                pi.set_PWM_dutycycle(rojo,bR)
                sleep(0.01)
        
def VtoW(bR=255,bG=0,bB=255):
        while bG < 255:
                bG  = bG + 1
                pi.set_PWM_dutycycle(verde,bG)
                sleep(0.01)
        
def WtoO(bR=255,bG=255,bB=255):
        while bR and bG and bB > 0:
                bR = bR - 1
                bG = bG - 1
                bB = bB - 1
                pi.set_PWM_dutycycle(rojo,bR)
                pi.set_PWM_dutycycle(verde,bG)
                pi.set_PWM_dutycycle(azul,bB)
                sleep(0.01)
        

while True:
        start()
        sleep(0.75)
        RtoY()
        sleep(0.75)
        YtoG()
        sleep(0.75)
        GtoC()
        sleep(0.75)
        CtoB()
        sleep(0.75)
        BtoV()
        sleep(0.75)
        VtoW()
        sleep(0.75)
        WtoO()
        sleep(0.75)
        

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: Stopping a python script from php

Wed Nov 28, 2018 5:44 am

i am learning as well you can use sys.argv to pass arguments or look about WEBIOPI

i was googling found
https://groups.google.com/forum/m/#!top ... FkRhMsRTQ8

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Stopping a python script from php

Wed Nov 28, 2018 4:11 pm

nahuavrilero wrote:
Tue Nov 27, 2018 11:19 pm
Hello.
I have a led changing color in an infinite loop and i need to stop it from a php page.
Is threre any way to do this?
Probably the most straightforward way to do this would be to have the php code either write a short file to a known location or delete a similar pre-existing file, and have the python script check for the existence of the file on each pass through the loop. Depending on the details of your application, you can have the python script exit when the 'signal' is 'raised' or enter another loop checking at intervals for it being 'lowered'.

nahuavrilero
Posts: 17
Joined: Fri Jun 22, 2018 11:22 am

Re: Stopping a python script from php

Thu Nov 29, 2018 1:01 pm

pfletch101 wrote:
Wed Nov 28, 2018 4:11 pm
nahuavrilero wrote:
Tue Nov 27, 2018 11:19 pm
Hello.
I have a led changing color in an infinite loop and i need to stop it from a php page.
Is threre any way to do this?
Probably the most straightforward way to do this would be to have the php code either write a short file to a known location or delete a similar pre-existing file, and have the python script check for the existence of the file on each pass through the loop. Depending on the details of your application, you can have the python script exit when the 'signal' is 'raised' or enter another loop checking at intervals for it being 'lowered'.
This is such a Good idea.
I'm gonna try it.
Thank you, very much. n.n

nahuavrilero
Posts: 17
Joined: Fri Jun 22, 2018 11:22 am

Re: Stopping a python script from php

Fri Nov 30, 2018 10:00 am

Ir worked.
But just from the terminal. Since OS.mknod("file") requires root permissions.
Is there a way to make it work from a PHP pagar without giving apache permissions todo execute sudo?

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Stopping a python script from php

Fri Nov 30, 2018 1:52 pm

nahuavrilero wrote:
Fri Nov 30, 2018 10:00 am
Ir worked.
But just from the terminal. Since OS.mknod("file") requires root permissions.
Is there a way to make it work from a PHP pagar without giving apache permissions todo execute sudo?
You need to give the apache user (or the relevant group) write privileges to the directory that you are creating the file in.

nahuavrilero
Posts: 17
Joined: Fri Jun 22, 2018 11:22 am

Re: Stopping a python script from php

Sat Dec 01, 2018 5:42 pm

Thanks, pfletch101.
That solved my problem.

To anyone working on something like this, my code is free to use, so you can borrow it.

Return to “Python”