单击按钮时,html文件不会加载包含python文件

时间:2017-02-26 07:25:35

标签: php python html

我连接到Raspberry Pi 2B。我有一个PHP文件的网页,其中包含两个名为ON和OFF的按钮。当我单击ON时,我的python文件将运行,当我单击OFF时,python文件将停止。

代码如下:

import sys
import serial
import os
import time
from datetime import datetime
from time import sleep
import RPi.GPIO as GPIO 
import Tkinter as tk
import tkFont

log = open("/home/pi/arduinolog.txt", "w")
ser = serial.Serial('/dev/ttyUSB0', 9600)

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
water=27
soln=22
light=24
count=0

def counterme():
   global count
   while count != 5:
      count = count + 1
      sleep(1)
      print str(count)

   if count >= 5:
        print "Good"
        count = 0


def loop():
    basa = ser.readline()
    mycode = str(basa)
    a = mycode.find("pH:")
    b = mycode.find("lx:")
    c = mycode.find("Analog value:")
    d = mycode.find("Voltage:")
    e = mycode.find("temp:")
    f = mycode.find("EC:")

    if a==0:
        a1 = float(mycode[a+3:a+8])
        print a1
        if a1<5:
            GPIO.setup(water,GPIO.OUT)
            GPIO.output(water,True) #Relay Open
            sleep(2)
            GPIO.output(water,False) #Relay Close

        if a1>7:
            GPIO.setup(soln,GPIO.OUT)
            GPIO.output(soln,True) #Relay Open
            sleep(2)
            GPIO.output(soln,False) #Relay Close

    if b==0:
        b1 = float(mycode[b+3:b+9])
        print b1
        if b1<1000:
            GPIO.setup(light,GPIO.OUT)
            GPIO.output(light,True) #Relay Open

        if b1>1000:
            GPIO.setup(light,GPIO.OUT)
            GPIO.output(light,True) #Relay Open            

    if f==0:
        f1= mycode[f+3:f+7]
        print f1
        if f1<0.8:
            GPIO.setup(water,GPIO.OUT)
            GPIO.output(water,True) #Relay Open
            sleep(2)
            GPIO.output(water,False) #Relay Close

        if f1>2.5:
            GPIO.setup(soln,GPIO.OUT)
            GPIO.output(soln,True) #Relay Open
            sleep(2)
            GPIO.output(soln,False) #Relay Close      

while True:
    loop()
    counterme()

下面的代码是我的PHP文件,其中包含ON和OFF按钮。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Automatic Control</title>
</head>

<body background="https://s13.postimg.org/amq6j590n/circuit_vector.png/">

<div data-role="header" data-position="fixed"><ul id="nav">
<li><a href="home.html">Dashboard</a></li>
<li class="dropdown"><a href="#" class="dropbtn">Events</a>
<div class="dropdown-content">
    <a href="pH.php">pH Sensor</a> 
    <a href="light.php">Light Sensor (LI)</a>
    <a href="temperature.php">Relative Humidity (RH)</a>
    <a href="temprobe.php">Water Temperature (WT)</a>
    <a href="conductivity.php" id="onlink">Electrical Conductivity (EC)</a>
</div>
</li>
<li class="dropdown"><a href="#" class="dropbtn"><I>Control</I></a>
<div class="dropdown-content">
    <a href="controlA.php">Manual Control</a> 
    <a href="controlB.php">Automatic Control</a>
</div>
</li>
<li><a href="gallery.html">View</a> </li>
<li><a href="contacts.html">About</a></li>
</ul></div>
<br><br>

<FORM>

<table width='100%'>
<tr align='center'><td>
<div style='border:2px solid; border-radius:30px;padding: 30px;width:300px;background-color:#F4F4F4;'>

</FORM>
<center><font size="5" color="#1717C9" face="Arial">
<B>AUTOMATIC CONTROL</B>
</font>

<font face="Arial">

<form action="" method="POST" name="form" style="display: inline;">

<input type="submit" id="on" value="ON">
<input type="hidden" name="ctl5" value="1">

</form>

<form action="" method="POST" name="form" style="display: inline;">
<input type="submit" id="off" value="OFF">
<input type="hidden" name="ctl5" value="0">
</form>

<?php
    $ctl5 = $_POST['ctl5'];
    $command1 = "sudo python /home/pi/automatic.py $ctl5";
    $result1 = shell_exec($command1);
    echo($result1);
?>


</font>
</center>
</div>
</div>
</font>
</body>
</html>

我有另一个PHP文件,它也运行一个python文件(打开和关闭继电器的状态并打印出继电器的状态),但加载页面只需要2-3秒。当我点击这些按钮时,它可以工作。

我的问题是,当我尝试加载此PHP文件(上面的代码)时,它不会加载。我认为这就像花费太多时间来初始化python文件一样。我该怎么办?

提前非常感谢你。

0 个答案:

没有答案