Tony
Tony

Reputation: 155

Arduino to Pi to PHP

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.

Upvotes: 1

Views: 132

Answers (1)

Mathieu VIALES
Mathieu VIALES

Reputation: 4772

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.

Upvotes: 1

Related Questions