Jump to content

php fopen serial port access denied


Go to solution Solved by kicken,

Recommended Posts

Hello,

 

I am using php to write to Arduino via a serial port (/dev/ttyACM0). The problem that I ran into is that I keep getting an Access Denied error in the log for the port. If I chmod the port, I can then write to it, but that only lasts until I make a change to the Arduino code or reset the device or computer. I tried adding myself to the dialout group and the uucp group like some sites suggested, but I still can not write to the port unless I chmod it first. I can also write to the port using python without any errors. Is their a way to force php to always allow me to write to that port?

 

Here is the php code

<?php

$verz="1.0";
$comPort = "/dev/ttyACM0"; /*change to correct com port */

	if (isset($_POST["rcmd"])) {
		$rcmd = $_POST["rcmd"];
		switch ($rcmd) {
			case "ON":
				$fp = fopen($comPort, "w") or die("Can't Open Port");
				fwrite($fp, "1");
				sleep(1);
				fclose($fp);
				break;
			case "OFF":
				$fp = fopen($comPort, "w");
				fwrite($fp, "0");
				sleep(1);
				fclose($fp);
				break;
		}
	}
?>
<html>
<body>
	
<center><h1>Arduino from PHP Example</h1><b>Version <?php echo $verz; ?></b></center>
<form method="post" action="index.php">
<input type="submit" value="ON" name="rcmd">
<input type="submit" value="OFF" name="rcmd">
</form>
</body>
</html>

and here is the error

[Tue Oct 22 14:19:10 2013] [error] [client 127.0.0.1] PHP Warning:  fopen(/dev/ttyACM0): failed to open stream: Permission denied in /var/www/arduino/index.php on line 10, referer: http://127.0.0.1/arduino/index.php

Any help would be appreciated,

Thanks

Link to comment
https://forums.phpfreaks.com/topic/283198-php-fopen-serial-port-access-denied/
Share on other sites

  • Solution

What are the permissions and owner/group for /dev/ttyACM0? What groups are you a member of? Can you run the necessary chmod with PHP before opening the file?

 

Note that if you are running this via apache which your error message indicates, you need to consider the user that apache is executing the script as, not what your login user is when determining the permissions and groups. For example, you'd need to add apache's user to the dialup / uucp groups, not yourself.

 

kicken

 

Note that if you are running this via apache which your error message indicates, you need to consider the user that apache is executing the script as, not what your login user is when determining the permissions and groups. For example, you'd need to add apache's user to the dialup / uucp groups, not yourself.

 

THANK YOU!!!!

 

It was so simple but I over looked it.

 

I had tried adding the user apache to the group but was getting a user error. Then I ran ps aux | egrep '(apache|httpd)' which showed me that the user was www-data. Then I added www-data to the group dialout using sudo usermod -a -G dialout www-data and after I logged out and back in it works!

 

Edited by CyborgHamster
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.