1

I have this simple code: it takes Arduino serial data to a Raspberry Pi. On the Pi, I want to display the data on a browser in PHP.

<?php
  $fp = fopen('/dev/cu.wchusbserial1a12130','r+'); //use this for Mac Nano
  echo $fp."<br>";
  echo fread($fp, "10");
  fclose($fp);
?>

It works perfectly fine on the Mac server with a Nano or Uno. But once I load it onto my Pi server, and change the port to /dev/ttyUSB0, it does not work any more. The browser is just blank. Does it have something to do with the Pi permissions? Thanks.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tony
  • 155
  • 1
  • 3
  • 9
  • 2
    If the page is just blank it probably means you got a PHP error that was not shown. Try to add `error_reporting(E_ALL);ini_set('display_errors', 1);` at the top of your PHP page as described in [this post](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Mathieu VIALES Aug 27 '17 at 15:04
  • Thanks for the reply. – Tony Aug 27 '17 at 15:17
  • I will try to add the php error checking. But I doubt it is php error, why would it excute just fine on the Mac but not on the Pi? I suspect if it has to do with the USB port on the Pi? – Tony Aug 27 '17 at 15:19
  • Did you get a PHP error ? if so, what is it ? If not, it'll be hard (for me) to help you :-/ – Mathieu VIALES Aug 27 '17 at 15:20
  • Oh, yea. I have these three errors:Warning: fopen(/dev/ttyUSB0): failed to open stream: Permission denied in /var/www/html/diy.php on line 10 Warning: fread() expects parameter 1 to be resource, boolean given in /var/www/html/diy.php on line 14 Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/diy.php on line 16 – Tony Aug 27 '17 at 15:30
  • Aha!. as suspected, It is the permission issue, I sudo chmod 777 -R on /dev/ttyUSB0, it works! :) – Tony Aug 27 '17 at 15:43
  • You should create an answer and mark it as "answer" so this post gets closed. I'll turn my comment into an answer for posterity and to make the explanation clearer. – Mathieu VIALES Aug 27 '17 at 16:32
  • If your problem is solved, please mark my answer as "answer" or, if it did not help that much, make an answer yourself and mark it as "answer" :-) (thanks for the upvote btw) – Mathieu VIALES Aug 27 '17 at 19:21

1 Answers1

1

In PHP, when you get a completely blank page it often means that a fatal server error occurred, but that PHP was not allowed to report the error in plain text to the client (for security reasons). You can either change this in the php.ini (this will affect all of PHP) or by adding the below lines at the top of the PHP file that gives you a blank page.

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

Now for the failed to open stream: Permission denied it is a file system permission problem. The user that runs the web server does not have the permission to read the file. You can use the following command to give Apache permission to read your file sudo chmod -R 775 /dev/ttyUSB0. You can refer to this page for more information about the chmod command.

Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48