【问题标题】:PHP/HTML Communication for Raspberry Pi树莓派的 PHP/HTML 通信
【发布时间】:2018-03-05 06:46:31
【问题描述】:

我在这个编程领域非常新手,所以任何指导将不胜感激。我的 Rasp Pi 有一个摄像头流,我只是连接了一个伺服器来转动它。我将我的问题分为三个阶段。

1) 一个简单的两个按钮的 HTML GUI,带有一个用于相机度数的值框。

2) PHP 脚本每次在 html 中按下按钮时读取该值并将其写入文本文件。

3) 读取文本文件并相应移动相机的 Python 脚本。

我已经完成了第 3 步,即转动舵机。我还在 html 中制作了一个小的 GUI 作为第 1 步。我主要关心的是将客户端 html 页面链接到 php 服务器端。我的专业知识到此结束。我有一个网站托管在我的 Pi 上,所以我想要的只是能够通过一个简单的网页控制摄像机的位置。有没有一种简单的方法让 html 与 php 对话?如果我最初可以从 html 按钮获取数据,我可以编写一个简单的 php 代码将值写入文本文件。

确切地说,按钮不会传递实际数字,只是向 php 传递一个递增/递减信号,其中一个硬编码值随增加或减少(以度为单位的摄像机角度)。

这是我目前的代码。

<!doctype html>
<html>
<head>
    <title>Camera Control</title>
</head>
<body>
<h1 style="text-align: center;">Camera Control</h1>

<p style="text-align: center;">
    <input name="Leftbutton" type="button" value="Left" />&nbsp;&nbsp;
    <input maxlength="5" name="textbox" size="5" type="text" />&nbsp;&nbsp;
    <input name="Rightbutton" type="button" value="Right" />
</p>
</body>
</html>


<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = " **Some value from the button, text or number** \n";
fwrite($myfile, $txt);
fclose($myfile);
?>

我的目标是避免使用 JavaScript 或我不知道的语言,如果有一种简单易行的方法可以完成这项工作,那就太棒了。提前致谢。

【问题讨论】:

  • 搜索php form handling,因为没有发布任何内容。
  • 对我来说似乎是正确的想法,非常感谢!

标签: javascript php jquery python html


【解决方案1】:

下面这段代码应该是index.php:

<?php
  if($_GET['return'] == 'failed'){
    echo '<p>unable to execute command.<p>';
  }
?>

<style>
  #camera > input{
    margin-left 10px;
  }
</style>
<form id="camera" action="camera.php" method="post">
  <input type="submit" name="left" value="left">
  <input maxlength="5" name="textbox" size="5" type="text" />
  <input type="submit" name="right" value="right">
</form>

那么对于 PHP 来说是这样的:

<?php
  #camera.php

  if(isset($t = $_POST['textbox']) && isset($l = $_POST['left']) || isset($r = $_POST['right'])){
    if(isset($l){
      $v = "left";
    } elseif(isset($r)) {
      $v = "right";
    } else {
      header('Location: ./index.php?return="failed"');
      die();
    }

    if(!file_put_contents('data.txt', time() . "| $v:$t" . PHP_EOL)){
      header('Location: ./index.php?return="failed"');
      die();
    }

    header('Location: ./index.php');

    // if access to shell
    exec('python ./location/to/yourscript.py');
  }

?>

【讨论】:

    【解决方案2】:
    <html>
     <head>
      <title>PHP Test Page
      </title>
     </head>
    
    
     <body>
    <form method="post" action="temp.php">
    <input type="submit" value="Left" name="leftb">
    <input type="submit" value="Right" name="rightb">
    </form>  
    
    
    
     </body>
     </html>
    
    
    
    
    <?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    
    if ($_POST['leftb'])
    { echo "Left is pressed";
        $txt = -1;}
    
    
    else if ($_POST['rightb'])
    { echo "Right is pressed";
        $txt = 1;}
    
    
    fwrite($myfile, $txt);
    fclose($myfile);
    
    
    
    sleep(1);
    
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = 0;
    fwrite($myfile, $txt);
    
    fclose($myfile);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多