[英]PHP/HTML Communication for Raspberry Pi
我是程序設計領域的新手,因此可以提供任何指導。 我的Rasp Pi有一個攝像頭流,我剛剛連接了一個伺服器以將其轉過來。 我將問題分為三個階段。
1)HTML GUI,用於簡單的兩個按鈕,並帶有用於攝影度的值框。
2)PHP腳本以每次在html中按下按鈕時讀取值並將其寫入文本文件。
3)讀取文本文件並相應移動相機的Python腳本。
我已經完成了第3步,即將伺服系統調轉。 我還制作了一個小的GUI,作為html中的第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" />
<input maxlength="5" name="textbox" size="5" type="text" />
<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或我不知道的語言,如果有一種簡單的簡便方法可以實現這一目標,那就太好了。 提前致謝。
下面的代碼應為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');
}
?>
<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.