【问题标题】:Raspi / Problem running script to toggle GPIO over web with a webpageRaspi /问题运行脚本以通过网页在Web上切换GPIO
【发布时间】:2021-07-26 04:13:33
【问题描述】:

我正在尝试通过在网络上切换 RaspberryPi 上的 GPIO 引脚来控制 LED 灯。

index.html(下图)有两个按钮来运行函数,这将触发两个脚本(lighton.cgi 和 lightoff.cgi)直接控制 GPIO。我验证了将打开或关闭 LED 的脚本。但是不知何故,当我单击 index.html 上的按钮时,它不会打开或关闭 LED。

我的 index.html 中是否存在语法错误?还是 html 约定发生了变化?有人可以帮帮我吗?

index.html 代码:

<html>
<head>
</head>
<body>
<button onclick="lighton()">Turn On</button>
<br><br>
<button onclick="lightoff()">Turn Off</button>
<script>
var xmlhttp;
xmlhttp=new XMLHttpRequest();
function lighton()
    {
        xmlhttp.open("GET","cgi-bin/lighton.cgi",true);
        xmlhttp.send();
    }
function lightoff()
    {
        xmlhttp.open("GET","cgi-bin/lightoff.cgi",true);
        xmlhttp.send();
    }
</script>
</body>
</html>

lighton.cgi 代码:

#!/bin/bash
gpio -g write 10 1

[b]lightoff.cgi 代码:[/b]

#!/bin/bash
gpio -g write 10 0

我基本上按照下面的 MJRoBot 教程进行操作: https://www.hackster.io/mjrobot/iot-controlling-a-raspberry-pi-robot-over-internet-6988d4#toc-step-1--bill-of-material-1

【问题讨论】:

    标签: html raspberry-pi iot


    【解决方案1】:

    您的 html 中至少缺少一个开头 &lt;scipt&gt;

    <html>
    <head>
    </head>
    <body>
    <button onclick="lighton()">Turn On</button>
    <br><br>
    <button onclick="lightoff()">Turn Off</button>
    <script>
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    function lighton()
        {
            xmlhttp.open("GET","cgi-bin/lighton.cgi",true);
            xmlhttp.send();
        }
    function lightoff()
        {
            xmlhttp.open("GET","cgi-bin/lightoff.cgi",true);
            xmlhttp.send();
        }
    </script>
    </body>
    </html>
    

    这是否行不通,检查控制台 (F12) 以查看您的 javascript 是否抛出任何错误,或者将 ajax 发送到您的 raspi。

    【讨论】:

    • 对不起,我没有完全复制。
    • 您应该尝试浏览器调试器以查看问题所在。尝试在您的 xmlhttp.open() 命令中使用完整地址,包括您的 raspies ip 或主机名 - 例如xmlhttp.open("GET","https://raspie:8000/cgi-bin/lightoff.cgi",true); 或任何您的 ip/端口,否则只有在 raspi 本身上运行 webbrowser 而不是网络中的任何其他计算机时它才会起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2019-10-19
    相关资源
    最近更新 更多