douzai8285 2013-05-11 10:43
浏览 94
已采纳

Arduino和PHP通过Serial incomingbyte读取

I've got PHP script for control Arduino's diode via website, but I've got a problem.

My Arduino code is:

int green = 8;
int incomingbyte;

void setup()
{
  Serial.begin(9600);
  pinMode(green,OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
    incomingbyte = Serial.read();
  }
  if(incomingbyte == '0'){
  digitalWrite(green,HIGH);
  }
  if(incomingbyte == '1'){
  digitalWrite(green,LOW);
  }
}

My PHP code is:

<?php

error_reporting(E_ALL); 
ini_set("display_errors", 1);  

if (isset($_GET['action'])) {

    require("php_serial.class.php");

        $serial = new phpSerial();
        $serial->deviceSet("COM3");
        $serial->confBaudRate(9600);
        $serial->deviceOpen();

if ($_GET['action'] == "green1") {

        $serial->sendMessage("0");

} else if ($_GET['action'] == "green0") {

        $serial->sendMessage("1");
}

$serial->deviceClose();

}

And my HTML code:

<!DOCTYPE html>
<html>
<head>
<title>ARDUINO</title>
</head>
<body>

<h1> ARDUINO AND PHP COMMUNICATION </h1>

<a href="led.php?action=green1">ON</a></br>
<a href="led.php?action=green0">OFF</a></br>

</body>
</html>

I've got two problems:

  1. Arduino is getting only incomingbyte = 0, so I can turn diode on, but I cannot turn it off. I modified code to set incomingbyte = 1 to turn diode on, but it's not working also. So I think Arduino is getting only incomingbyte = 0.

  2. My website is shuting down after running script. When I click on "ON" or "OFF" script is running and I'm getting white (blank) site. What should I do to stay all the time on my HTML site?

  • 写回答

2条回答 默认 最新

  • douwei4370 2013-05-11 11:48
    关注

    re: 2 Add the html code under your php form handler - so everything is served from the same script, or use

    header() 
    

    to relocate back to the html page - but then you cannot output errors.

    EDIT so to do it the single file way:

    <?php
    // led.php code in here
    error_reporting(E_ALL); 
    ini_set("display_errors", 1);  
    
    if (isset($_GET['action'])) {
    // and so on ...
    
    
    
    ?>
    <!--// now show your html form regardless 
    of whether the form was submitted or not // -->
    <!DOCTYPE html>
    <html>
    <head>
    <title>ARDUINO</title>
    </head>
    <body>
    
    <h1> ARDUINO AND PHP COMMUNICATION </h1>
    
    <a href="?action=green1">ON</a></br>
    <a href="?action=green0">OFF</a></br>
    
    </body>
    </html>
    

    Edited to try and make the solution a bit clearer. Notice you do not have to add led.php to the links, they are submitted back to the same file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘