0

首先感谢大家的建议。所以,我有一个名为 的 php,我有一个名为sys.php的 python 程序,button.py如果我按下其中一个按钮,网页就会打印出 GPIO 是低还是高。现在这很好用,现在我想达到那个,它会在不刷新网页的情况下获取信息,我该怎么做呢?我听说过 Ajax 和 JQuery,但我完全知道它是如何工作的。

这是 sys.php,其中一些片段是用匈牙利语编写的,因为我来自那里,希望它不会打扰您。

<html lang="hu">
<head>
<title>Project</title>
<meta charset="UTF-8">
<link rel="icon" href="logo.jpg" type="image/gif" sizes="16x16">
 <link rel="stylesheet" type="text/css" href="projekt.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>

</script>

</head>
<body>

<p class="small"><big>Project</big></p>
<p class="ex2">Choose GPIO </p>
<table style="width:95%">
  <tr>
    <td><form method="post"><input type="submit" value="GPIO 7" name="GPIO7"></form></td>
    <td><form method="post"><input type="submit" value="GPIO 8" name="GPIO8"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 10" name="GPIO10"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 11" name="GPIO11"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 12" name="GPIO12"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 13" name="GPIO13"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 15" name="GPIO15"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 16" name="GPIO16"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 18" name="GPIO18"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 19" name="GPIO19"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 21" name="GPIO21"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 22" name="GPIO22"></form></td>
        </tr>
        <tr>
        <td><form method="post"><input type="submit" value="GPIO 23" name="GPIO23"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 24" name="GPIO24"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 26" name="GPIO26"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 29" name="GPIO29"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 31" name="GPIO31"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 32" name="GPIO32"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 33" name="GPIO33"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 35" name="GPIO35"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 36" name="GPIO36"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 37" name="GPIO37"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 38" name="GPIO38"></form></td>
        <td><form method="post"><input type="submit" value="GPIO 40" name="GPIO40"></form></td>
        </tr>
</table>

<div id="demo">
<?php
                if(isset($_POST['GPIO7']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '7' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO8']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '8' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO10']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '10' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO11']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '11' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO12']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '12' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO13']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '13' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO15']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '15' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO16']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '16' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
 elseif(isset($_POST['GPIO18']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '18' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO19']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '19' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO21']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '21' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO22']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '22' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
 elseif(isset($_POST['GPIO23']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '23' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO24']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '24' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO26']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '26' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO27']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '27' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
 elseif(isset($_POST['GPIO28']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '28' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO29']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '29' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO31']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '31' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO32']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '32' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
    elseif(isset($_POST['GPIO33']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '33' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO35']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '35' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO36']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '36' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO37']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '37' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
                elseif(isset($_POST['GPIO38']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '38' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
       elseif(isset($_POST['GPIO40']))
        {
                $output=exec("sudo python /var/www/html/gomb.py '40' ");
        echo "<pre>A GPIO értéke: ";
                print_r($output);
        echo ".</pre>";
        }
?>
 </body>


</html>

这是蟒蛇:

#!/usr/bin/env python

import sys
import RPi.GPIO as GPIO

var=int(sys.argv[1])
GPIO.setmode(GPIO.BOARD)
GPIO.setup(var, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print(GPIO.input(var))
4

1 回答 1

0

我的建议是编写一个 javascript 函数,它异步地向网络服务器询问新信息。如果有的话,您可以操纵 DOM 以适应新信息。

为此,您的 php 代码需要位于单独的文件中,因为 AJAX 只能调用整个脚本。我们称它为“code.php”。

在 HTML 中,您需要一个地方来写入来自 javascript 的值。

HTML:

<p id="output"></p>

Javascript:

setInterval(myMethod, 1000);

function myMethod( )
{
  //this function will be called every second

  //now you can asynchronously ask the server
  $.ajax({ url: '/path/to/your/code.php',
         type: 'post',
         success: function(output) {
                      //this function is called when the php script finished executing
                      //'output' is your new data
                      //now you can write it into the HTML
                      document.getElementById('output').innerHTML = output;
                  }
  });
}

DOM 操作:https ://www.w3schools.com/js/js_htmldom.asp

阿贾克斯:https ://www.w3schools.com/jquery/ajax_ajax.asp

于 2019-12-20T14:12:48.780 回答