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.

8
  • 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 Commented Aug 27, 2017 at 15:04
  • Thanks for the reply. Commented Aug 27, 2017 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? Commented Aug 27, 2017 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 :-/ Commented Aug 27, 2017 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 Commented Aug 27, 2017 at 15:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.