简体   繁体   English

Raspberry Pi的PHP / HTML通讯

[英]PHP/HTML Communication for Raspberry Pi

I am very novice in this field of programing so any guidance would be appreciated. 我是程序设计领域的新手,因此可以提供任何指导。 I have a camera stream for my Rasp Pi and I just attached a servo to turn it around. 我的Rasp Pi有一个摄像头流,我刚刚连接了一个伺服器以将其转过来。 I have broken down my problem into three stages. 我将问题分为三个阶段。

1) HTML GUI for a simple two button, with a value box for camera degrees. 1)HTML GUI,用于简单的两个按钮,并带有用于摄影度的值框。

2) PHP scrip to read the value each time a button is pressed in html and write it down to a text file. 2)PHP脚本以每次在html中按下按钮时读取值并将其写入文本文件。

3) Python script that reads the text file and moves the camera accordingly. 3)读取文本文件并相应移动相机的Python脚本。

I have completed step 3 which is to turn the servo around. 我已经完成了第3步,即将伺服系统调转。 I also made a small GUI as step 1 in html. 我还制作了一个小的GUI,作为html中的第1步。 My major concern is to link a client side html page to a php server side. 我主要关心的是将客户端html页面链接到php服务器端。 My expertise end right here. 我的专长到此结束。 I have a website hosted on my Pi so all I want is to be able to control the cameras position through a simple web page. 我在Pi上托管了一个网站,因此我所希望的就是能够通过一个简单的网页控制摄像机的位置。 Is there a simple way for html to talk to php? 有没有简单的方法可以让html与php对话? I can write a simple php code to write value to a text file if I can initially get data from the html button. 如果最初可以从html按钮获取数据,我可以编写一个简单的php代码将值写入文本文件。

And to be exact, the button wont be passing an actual number, just an increment/decrement signal to the php where a hardcoded value with increase or decrease(Camera angle in degrees). 确切地说,按钮不会传递实际的数字,只是传递给php的增量/减量信号,而其中的硬编码值会增加或减少(相机角度,以度为单位)。

Here is my code so far. 到目前为止,这是我的代码。

<!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>

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);
?>

My goal is to avoid JavaScript or languages that I don't know, if there is a simple easy way I can get this done would be amazing. 我的目标是避免使用JavaScript或我不知道的语言,如果有一种简单的简便方法可以实现这一目标,那就太好了。 Thanks in advance. 提前致谢。

This code below should be index.php: 下面的代码应为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>

Then for the PHP something like this: 然后对于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');
  }

?>
<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);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM