0

我使用名为“webiopi”的工具通过 Raspberry pi 和继电器板控制健康设备。此工具使用 Python 脚本连接网页上的按钮。我可以从我创建的按钮上的 GPIO 引脚读取状态(低/高)。

我想要的是在浏览器中显示脚本中的值。(如“温度测量”或“计算时间”)

有没有办法可以将 python 脚本的输出显示到网络浏览器?

    button = webiopi().createMacroButton("macro", "Normaal", "Prog1");
    $("#top").append(button)

<div id="content" align="center">
<td>    
  </td>
                            {{print here output from script.py}}
      <div id="top"></div>

   </div>
 </div>
4

2 回答 2

0

在这个论坛 [Wpio 论坛][1] https://groups.google.com/forum/#!topic/webiopi/DreW_74gm0o

给皮特杜达什这个问题的答案,我在这里复制

是的,这是可能的。您要做的是向您的 javascript 添加一个回调例程。首先,您在 javascript 中执行一些操作(通过按钮按下或计时器)并调用 python 宏。这一次,您在调用宏时指定一个回调例程。回调例程从宏接收数据,然后你可以用它做任何你想做的事情。

Javascript:

function getPythonResponse() {
// "sendData" is the name of the macro you specify in your script.py
// "[]" is an empty list.  If you want to send data for the macro to use, you would include that here
// "handlePythonResponse" is the name of the callback routine that will execute once the python macro is finished
webiopi().callMacro("sendData", [], handlePythonResponse);

}

var handlePythonResponse = function(macro, args, response) {
// "response" is the variable that holds the data from script.py
// Now we can apply those results to the webpage by assigning the value to the "pythonResult" id.  This is the element in your HTML where you want to print the info.
$("#pythonResult").text(response);

}

Python:

@webiopi.macro

定义发送数据():

HTML:

<div id="content" align="center">
<td></td>

<span id="pythonResult">{{print here output from script.py}}</span>

<div id="top"></div>

于 2016-04-15T18:44:02.373 回答
0

您可以使用 PHP 调用脚本并将输出回显到页面。您需要安装并启用 PHP,并且文件必须以.php. 如果你知道你正在使用的网络服务器,我可以给你一些额外的帮助。另请注意,脚本应将网页上所需的所有信息打印到标准输出中。

    button = webiopi().createMacroButton("macro", "Normaal", "Prog1");
    $("#top").append(button)

<div id="content" align="center">
<td>    
  </td>
      <?php exec("python myscript.py", $out); echo $out; ?>
      <div id="top"></div>

   </div>
 </div>
于 2016-04-15T12:22:30.487 回答