通过PHP运行c程序(树莓派)


Running c-program through PHP (raspberry Pi)

在我的第三个if语句中,当我按下按钮时,我想在树莓派上运行一个C程序。我不知道该怎么做。

现在,C程序只会点亮一个LED,如果我只是运行编译后的程序,它就会工作,但我不知道如何通过PHP来完成。

我读过一些使用"exec"的地方,但我不知道如何做到这一点(我对编程相对陌生)。

<html>
<head>
</head>
<body>

<form method=GET action="index.php">
<input name="button" type="submit" value="Turnon">
<input name="button" type="submit" value="Turnoff">
<input name="button" type="submit" value="ON">
<input name="button" type="submit" value="OFF">
</form>

<?php

if ($_GET["button"] == "Turnon")
   {
    system ( "gpio mode 28 out" );
    system ( "gpio write 28 1" );
   };
if ($_GET["button"] == "Turnoff")
   {
    system ( "gpio mode 28 out" );
    system ( "gpio write 28 0" );
   };
if ($_GET["button"] == "ON")
   {
    system("sudo /home/pi/var/www/blink.exe >/dev/null 2>/dev/null & ");
   };
if ($_GET["button"] == "OFF")
   {
    system ( "gpio mode 28 out" );
    system ( "gpio write 28 0" );
   };
?>

</body>
</html>

您检查过PHP中的shellexec()命令吗?

从PHP文档

string exec ( string $command [, array &$output [, int &$return_var ]] )

在您的代码中:

<?php
exec("gpio mode 28 out", $output);
print_r($output);

$output包含脚本的结果