douzai8285
douzai8285
2013-05-11 10:43

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 douwei4370 8年前

    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.

    点赞 评论 复制链接分享
  • dongwu5801 dongwu5801 8年前

    hey i just figured out two important changes for ur code....

    1>change the

    $serial->sendMessage("0");
    

    to

    $serial->sendMessage('0');
    

    and same for to send '1'.

    2>include the sleep command.here like this

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

    the sleep command inserts the delay. when the serial port is opened by php in the

    $serial->deviceopen();
    

    command the arduino gets autoresetted. thus when php executes the next commands which arduino will not be able to recieve them and hence may not be able to take the action. 2 is the best delay as 1 would be very short and other more than 2 are long. use the php and html code in the same file as in the above post and name it as "name.php" without the " ". this worked for me ....

    点赞 评论 复制链接分享