【问题标题】:Passing a single variable from HTML to Python script using PHP使用 PHP 将单个变量从 HTML 传递到 Python 脚本
【发布时间】:2018-09-22 01:11:50
【问题描述】:

我正在尝试设置一个系统,当一个数字被输入到网页时,它会触发一个计时器并打开一个 GPIO 引脚RPI 几秒钟然后关闭。

我需要传递一个 数字 提交到 HTML 表单,到我的 python 脚本一起工作。

我以为我找到了解决方案,但我不确定为什么它不起作用。我正在使用 PHP7Pi zero W 上运行 apache2。

这是我在 /var/www/html/ 目录中的两个文件:

index.php:

<form action="index.php" method="post">
    <input type="text" name="seconds"/>
    <input type="submit" name="SubmitSeconds" /><br/>
</form>

<?php
if(isset($_POST['SubmitSeconds'])) {
    shell_exec('/usr/bin/python /var/www/html/script.py'.$seconds);
}
?>

脚本.py:

#!/usr/bin/python

from datetime import datetime
from threading import Timer
import RPi.GPIO as GPIO
import time, cgi, sys

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)

def GPIOTimer():
    GPIO.output(23, 1)
    time.sleep(10)
    GPIO.output(23, 0)

def ten_seconds():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute), second=(x.second+10), microsecond=0)

def five_mins():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+5), second=(x.second), microsecond=0)

def ten_mins():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+10), second=(x.second), microsecond=0)

def thirty_mins():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+30), second=(x.second), microsecond=0)

def one_hour():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour+1), minute=(x.minute), second=(x.second), microsecond=0)

def three_hours():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour+3), minute=(x.minute), second=(x.second), microsecond=0)

def TimerMain():
    global x
    global y
    global delta_t
    global secs
    global t
    delta_t=y-x
    secs = delta_t.seconds+1
    t = Timer(secs, GPIOTimer)
    t.start()

def TimerSequence(z):
    if (z==1):
        ten_seconds() 
    elif (z==2):
        five_mins
    elif (z==3):
        ten_mins()
    elif (z==4):
        thirty_mins()
    elif (z==5):
        one_hour()
    elif (z==6):
        three_hours()
    else:
        print("InputError")
    TimerMain()

result = int(sys.argv[1])
TimerSequence(result)

【问题讨论】:

    标签: php python html apache


    【解决方案1】:

    1) 你忘记声明变量“秒”

    <form action="index.php" method="post">
        <input type="text" name="seconds"/>
        <input type="submit" name="SubmitSeconds" /><br/>
    </form>
    
    <?php
    if(isset($_POST['SubmitSeconds'])) {
        $seconds = $_POST["seconds"];
        shell_exec('/usr/bin/python /var/www/html/script.py'.$seconds);
    }
    ?>
    

    【讨论】:

    • 谢谢!我添加了那条线,但由于某种原因,GPIO 引脚仍未被触发。无论如何我可以找出脚本是否正在运行或以某种方式调试进程?
    • 我建议你首先运行 python 脚本(分开)打印每个函数中的值,这样你就可以看到发生了什么。然后你知道 python 脚本可以工作,你可以从 php 调用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多