dongyouzhui1969 2015-11-08 13:41
浏览 79
已采纳

用于控制Arduino的PHP站点在网页加载时发送代码

I am trying to create a domotica setup with my Arduino and my server. Before using the RF transmitter, I'm trying to control LEDs (setting them on/off) from my php webpage.

It all works perfectly, except for the fact that LED1 turns on when I'm loading/refreshing the webpage. My webpage contains the following code (which I adapted from https://www.lassiemarlowe.com/tutorials/power-led-bulbs-arduino-php-part-2/):

<html>
<head>
<title>Arduino Domotica Control Panel</title>

Some css code here...

<?php
switch($_POST)
{
    case isset($_POST['submitOn']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 1);
        fclose($fp);
        break;
    case isset($_POST['submitOff']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 2);
        fclose($fp);
        break;
    case isset($_POST['submitOn1']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 3);
        fclose($fp);
        break;
    case isset($_POST['submitOff1']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 4);
        fclose($fp);
        break;
    case isset($_POST['submitOn2']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 5);
        fclose($fp);
        break;
    case isset($_POST['submitOff2']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 6);
        fclose($fp);
        break;
    case isset($_POST['allon']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 7);
        fclose($fp);
        break;
    case isset($_POST['alloff']):
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, 8);
        fclose($fp);
        break;
}

?>
</head>


<body>

<h1>Control Panel</h1>


<form class="control-panel-frm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <input type='submit'  class="s3d turnOn" name='submitOn' value='LED 1 on'>
    <input type='submit' class="s3d switchoff" name='submitOff' value='LED 1 off'>
    <br><br>
    <input type='submit'  class="s3d turnOn" name='submitOn1'  value='LED 2 on'>
    <input type='submit'  class="s3d switchoff"  name='submitOff1' value='LED 2 off'>
    <br><br>
    <input type='submit'  class="s3d turnOn" name='submitOn2'  value='LED 3 on'>
    <input type='submit'  class="s3d switchoff"  name='submitOff2'  value='LED 3 off'>
    <br><br>
    <input type='submit' class="s3d turnOn"  name='allon' value='All LEDs on'>
    <input type='submit'  class="s3d switchoff"  name='alloff' value='All LEDs off'>

</form>

As stated before, the problem is that LED1 (corresponding to php post 'submitOn') turns on when I load/refresh the webpage. When checking the serial monitor, the Arduino receives a '1'.

What should I change in order to prevent my webpage from sending anything to the Arduino when it loads?

  • 写回答

1条回答 默认 最新

  • doujia7779 2015-11-08 14:40
    关注

    I think you understood switch wrong. Switch compares the value from the braces () with the value given by case. See the manual page for switch to get more clarification on this.

    Besides that you did not unterstood the transfer of data from a html form to a PHP application. You send LED 1 on as value for the form element submitOn.

    Try this:

    <?php
    $actions = [
        'submitOn' => 1,
        'submitOff' => 2, 
        //...
    ];
    
    if(!empty($_POST['action']) && array_key_exists($_POST['action'], $actions)){
        $fp = fopen("/dev/ttyUSB1", "w");
        fwrite($fp, $actions[$_POST['action']]);
        fclose($fp);
    }
    
    ?>
    <form class="control-panel-frm" method="post" action="">
        <input type="submit" class="s3d turnOn" name="action" value="submitOn">
        <input type="submit" class="s3d switchoff" name="action" value="submitOff">
    </form>
    

    Note that an empty action of a form sends the form data to the current page. See Two submit buttons in one form for the use of more than one submit button.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵