我在树莓上。 我有一个C编译程序,它返回一些带有值的行:即
printf("Input BP : CF \n");
我有一个带php的Web服务器,该服务器在特定的URL上执行C程序,我想显示C程序返回的行。 我成功在Windows上执行了某些操作,但在Rspi上却无法正常工作
这是PHP来源:
else if ($cmd=='temp2') {
echo exec("sudo ./spa5");
}
else if ($cmd=='err') {
exec("sudo ./spa5",$last_line,$retval); / // $last_line = system("./spa5 2>&1", $retval);
//$last_line = system("./spa5 ", $retval);
// print_r ($last_line)."</BR> ";
// echo ($retval)."</BR> ";
while( list(,$row) = each($last_line) ) {
echo $row. "<br />";
}
有些错误,但是在哪里? 我已经在WebServer Apache和NGINX上进行了测试
答案 0 :(得分:0)
我在获得C程序时遇到了类似的问题-它正在控制Raspberry上的RF发射器-与php exec一起工作(我正在使用nginx)。
我使用了基于以下 wireless sockets raspberry tutorial 的C程序
可能存在身份验证问题,但是我只能通过执行以下步骤来解决。没有Python,我无法执行php,但是既没有结果,也没有错误。
我做了一个弯路,并使用Python脚本运行了C程序,现在它可以工作了:
Python脚本由用户(属于sudo组)拥有:users,权限744
#!/usr/bin/python3.5
import subprocess
import sys
from subprocess import call
if len(sys.argv) != 2:
print('invalid numbers of arguments. Script will be terminated.')
else:
print('used argument:', sys.argv[1])
subprocess.call(["/path/to/myC_Program", sys.argv[1]]);
要测试Python脚本,您可以使用以下代码代替subprocess.call
# subprocess.check_call(["/path/to/myC_Program 1"]); this returns "no such file"
# subprocess.check_call(["/path/to/myC_Program", "1"]);this returns the echo of the C-program"`
with
sudo visudo
我加了
www-data ALL=(ALL) NOPASSWD: /path/to/myC_Program, NOPASSWD: /path/to/mypythonscript.py
我的php文件“ light_via_browser.php”(由root拥有,权限644)使用参数“ temp”调用Python脚本:
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=2, maximum-scale=6.0, minimum-scale=2">
<title>Light Control</title>
</head>
<script>
</script>
<style>
div[type="normal"] {
clear: left;
width: 32vw;
height: 1vw;
background-color:transparent;
}
div[type="inp_left1"] {
height:11vw;
width: 11vw;
padding: 0vw;
float:left;
margin-left: 4vw;
display:inline;
border: 1px solid silver;
background-color:grey;
}
div[type="inp_left2"] {
height:11vw;
width: 11vw;
padding: 0vw;
float:left;
margin-left: 1vw;
display:inline;
border: 1px solid silver;
background-color:grey;
}
input[type="submit"] {
width: 6vw;
font-size: 4vw;
margin-left: 2.5vw;
margin-top: 2vw;
padding: 1vw 1vw;
font-family: Roboto, sans-serif;
font-weight: 400;
color: teal;
border: 1px solid silver;
background-image: linear-gradient(to top, gainsboro 0%, white 90%);
border-radius: 2vw;
}
</style>
<body>
<h3>Lighting Control:</h3>
<form style='width: 48vw' method="get" action="light_via_browser.php" >
<div type="inp_left1">
<input type="submit" value=1 name="one" >
</div>
<div type="inp_left2">
<input type="submit" value=2 name="two">
</div>
<div type="inp_left2">
<input type="submit" value=3 name="three">
</div>
<div type='normal' style='position: relative'>
</div>
<div type="inp_left1">
<input type="submit" value=4 name="four">
</div>
<div type="inp_left2">
<input type="submit" value=5 name="five">
</div>
<div type="inp_left2">
<input type="submit" value=6 name="six">
</div>
<div type='normal' style='position: relative'>
</div>
<div type="inp_left1">
<input type="submit" value=7 name="seven">
</div>
<div type="inp_left2">
<input type="submit" value=8 name="eight">
</div>
<div type="inp_left2">
<input type="submit" value=9 name="nine">
</div>
<div type='normal' style='position: relative'>
</div>
</form>
<?php
if(isset($_GET['one'])){
$temp = (string)$_GET['one'];
}else if(isset($_GET['two'])){
$temp = (string)$_GET['two'];
}else if(isset($_GET['three'])){
$temp = (string)$_GET['three'];
}else if(isset($_GET['four'])){
$temp = (string)$_GET['four'];
}else if(isset($_GET['five'])){
$temp = (string)$_GET['five'];
}else if(isset($_GET['six'])){
$temp = (string)$_GET['six'];
}else if(isset($_GET['seven'])){
$temp = (string)$_GET['seven'];
}else if(isset($_GET['eight'])){
$temp = (string)$_GET['eight'];
}else if(isset($_GET['nine'])){
$temp = (string)$_GET['nine'];
}
switch ($temp) {
case 9:
$gpio_cmd = "whoami";
break;
default;
$gpio_cmd = "sudo python3.5 /path/to/mypythonscript.py $temp";
}
exec($gpio_cmd,$output,$status);
if (0 === $status) {
$result = sizeof($output);
switch ($temp) {
case 9:
$msg = "just a test:";
echo "<a>$msg</a><br>";
echo "current user is <br>'$output[0]'<br>";
break;
default;
switch ($output[0]) {
case 3092898:
$lamp = "entree";
break;
case 3092900:
$lamp = "shop";
break;
case 3092904:
$lamp = "rear";
break;
case 8344209:
$lamp = "garden";
break;
case 5084072:
$lamp = "Num";
break;
case 15631240:
$lamp = "NA";
break;
case 15631250:
$lamp = "rear";
break;
case 15631256:
$lamp = "shop";
break;
default;
}
$msg = "$output[0]: $lamp";
echo "$msg<br>";
}
} else {
echo "Command failed with status: $status";
}
?>
</body>
</html>
$ result返回数组$ output的长度。 可以操纵/显示该数组中的所有项目。
在浏览器屏幕截图下方,显示按钮和两个变量作为按下按钮后的返回结果:
您可以尝试一下,我很好奇这种方法是否也解决了您的问题。 干杯。