Advertisement
Guest User

Arduino read/write serial with PHP

a guest
Mar 9th, 2011
2,880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. //PHP Code
  2.  <?php
  3. require("php_serial.class.php");
  4. if (isset($_GET['action']))
  5. {
  6. $serial = new phpSerial;
  7. $serial->deviceSet("/dev/ttyACM0");
  8. $serial->confBaudRate(115200);
  9. $serial->deviceOpen();
  10.     if ($_GET['action'] == "greenon")
  11.     {
  12.     $serial->sendMessage("0");
  13.     $done = false;
  14.     $line = "";
  15.         while (!$done)
  16.         {
  17.         $read = $serial->readPort();
  18.             for ($i = 0; $i < strlen($read); $i++)
  19.             {
  20.                 if ($read[$i] == "\n")
  21.                 {
  22.                 $done = true;
  23.                 echo "Response from Arduino: ".$line."\n";
  24.                 $line = "";
  25.                 }
  26.                 else
  27.                 {
  28.                 $line .= $read[$i];
  29.                 }
  30.             }
  31.         }
  32.     }    
  33.     else if ($_GET['action'] == "greenoff")
  34.     {
  35.     $serial->sendMessage("1\r");
  36.     }
  37.     else if ($_GET['action'] == "redon")
  38.     {
  39.     $serial->sendMessage("2\r");
  40.     $read = $serial->readPort();
  41.     echo $read;
  42.     }
  43.     else if ($_GET['action'] == "redoff")
  44.     {
  45.     $serial->sendMessage("3\r");
  46.     }
  47. $serial->deviceClose();
  48. }
  49.  
  50.  
  51. ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  52. "http://www.w3.org/TR/html4/loose.dtd">
  53. <html>
  54. <head>
  55. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  56. <title>Arduino Project 1: Serial LED Control</title>
  57. </head>
  58. <body>
  59.  
  60. <h1>Arduino Project 1: Serial LED Control</h1>
  61. <p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenon" ?>">
  62. Click here to turn pin13 on.</a></p>
  63. <p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenoff" ?>">
  64. Click here to turn pin13 off.</a></p>
  65. <p><a href="<?=$_SERVER['PHP_SELF'] . "?action=redon" ?>">
  66. Click here to turn the RED LED on.</a></p>
  67. <p><a href="<?=$_SERVER['PHP_SELF'] . "?action=redoff" ?>">
  68. Click here to turn the RED LED off.</a></p>
  69. </body>
  70. </html>
  71.  
  72. /* ##Arduino code
  73. int ledPin13 = 13; // the pin that the green LED is attached to
  74. int ledPin12 = 12; // the pin that the red LED is attached to
  75. int relay = 8;
  76. int incomingByte;      // a variable to read incoming serial data into
  77. void setup()
  78. {
  79.    
  80. Serial.begin(115200); // initialize serial communication
  81.  
  82. pinMode(ledPin13, OUTPUT);  // initialize the green LED pin as an output
  83. pinMode(ledPin12, OUTPUT);  // initialize the red LED pin as an output
  84. pinMode(relay, OUTPUT);
  85.  }
  86.  void loop() {
  87.    digitalWrite(relay, HIGH);
  88.    // see if there's incoming serial data:
  89.    if (Serial.available() > 0) {
  90.      
  91.      incomingByte = Serial.read(); // read the oldest byte in the serial buffer
  92. //Preform the code to switch on or off the leds
  93.     if (incomingByte == '0') {
  94.  
  95.     digitalWrite(ledPin13, HIGH); //If the serial data is 0 turn red LED on
  96. delay(100);
  97. Serial.println("0");
  98. }
  99.    if (incomingByte == '1') {
  100.    digitalWrite(ledPin13, LOW); //If the serial data is 1 turn red LED off
  101.  Serial.println("1");
  102.  }
  103.  
  104.      if (incomingByte == '2') {
  105.     digitalWrite(ledPin12, HIGH); //If the serial data is 2 turn green LED on
  106. Serial.println("2");
  107. }
  108.    if (incomingByte == '3') {
  109.    digitalWrite(ledPin12, LOW); //If the serial data is 3 turn green LED off
  110.  Serial.println("3");
  111.  }
  112.  
  113.    }
  114.  }
  115.  
  116. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement