Advertisement
subnet

PHPSerial on Beaglebone Black

Aug 18th, 2014
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?
  2. /*
  3.  * using PHPSerial (https://github.com/Xowap/PHP-Serial) on Beaglebone Black to send ON/OFF to Arduino's LED
  4.  * and read LM35 sensor temperature. See Arduino code here: http://pastebin.com/J6fCNguP
  5.  * almost all code is by: http://paolosarteschi.altervista.org/wp/?p=375
  6.  * to connect Arduino & Beaglebone Black use:
  7.  * www.instructables.com/id/How-to-make-a-BeagleBone-and-an-Arduino-communicat/?ALLSTEPS
  8.  *
  9. */
  10.  
  11. error_reporting(E_ALL);
  12. ini_set("display_errors", 1);
  13. define("PORT","/dev/ttyO2");
  14. include "php_serial.class.php";
  15.  
  16. $serial = new phpSerial;
  17. $serial->deviceSet(PORT);
  18. $serial->confBaudRate(9600);
  19. $serial->confParity("none");
  20. $serial->confCharacterLength(8);
  21. $serial->confStopBits(1);
  22. $serial->confFlowControl("none");
  23. $serial->deviceOpen();
  24. $title = "";
  25.  
  26. global $temperatura, $statoled;
  27.  
  28.     $serial->sendMessage("r");
  29.     sleep(0.2);
  30.     $serialread = $serial->readPort();
  31.  
  32.     if($serialread==""){
  33.     //echo "errore - valori assenti";
  34.     header ('Location:' . $_SERVER['PHP_SELF']);
  35.     }
  36.  
  37.     elseif(strlen($serialread)!=3){
  38.     //echo "errore - valori non coerenti";
  39.     header ('Location:' . $_SERVER['PHP_SELF']);
  40.     }
  41.  
  42.     else{  
  43.     $statoled = substr($serialread,0,1);
  44.     $temperatura = substr($serialread, 1,2);
  45.  
  46.         if ($statoled == 1){
  47.         $title = "SPEGNI LED";
  48.         //echo $title;
  49.         }
  50.         else {
  51.         $title = "ACCENDI LED";
  52.         //echo $title;
  53.         }
  54.    
  55.     }
  56.  
  57.  if (isset($_GET['action'])) {
  58.  
  59.     if ($_GET['action'] == "on") {
  60.         $serial->sendMessage("a");
  61.         header ('Location:' . $_SERVER['PHP_SELF']);
  62.         }
  63.     else if ($_GET['action'] == "off") {
  64.         $serial->sendMessage("s");
  65.         header ('Location:' . $_SERVER['PHP_SELF']);
  66.         }
  67.  
  68.     $read = $serial->readPort();   
  69.  
  70. }
  71. ?>
  72. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  73. <html>
  74.  <head>
  75.   <title>TEST LED - <?=$title?></title>
  76.  </head>
  77.  
  78.  <body>
  79. <?
  80.  
  81.  
  82. if ($statoled == 1){
  83.  
  84. ?>
  85. <a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">
  86. <button style="color: red;width:250px"><h1>SPEGNI LED</h1></button></a>
  87. <?
  88. }
  89. else if ($statoled == 0){
  90.  
  91. ?>
  92. <a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">
  93. <button style="color: green;width:250px"><h1>ACCENDI LED</h1></button></a>
  94. <?
  95. }
  96.  
  97. ?>  
  98. <h2 style="font-family:arial;color: darkblue">
  99. Temperatura:
  100. <?
  101. echo "$temperatura&deg; C";
  102. ?>
  103. </h2>
  104.  </body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement