Serial port access denied - php

I am running an Apache web server on a Raspberry Pi. I have some Python code which executes in the index.php file as follows:
<?php
system("python /home/pi/python/foobar.py")
?>
The python script opens a serial port, like so:
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
When I run the python script from the command line on the Raspberry Pi, it works perfectly. However, when I browse the site on another computer, I get this error message:
[Errno 13] Permission denied: '/dev/ttyUSB0'
I have done some research and found that most people who run into this error do so because the user doesn't belong to the dialout group. When using the Raspberry Pi, I do belong to it, and I assume that on another computer, I don't.
In summary, how do I get permission to access a serial port on a Raspberry Pi server?

Related

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.
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.

Accessing X Display through Apache

I am trying to send keypresses to a Raspberry Pi running Raspdbian from from a web page hosted on an Apache server at /var/www.
I'm using xdotool, which can send keypresses using commands like this:
xdotool key C
I made a python script xdotool.py that sends a few keypresses like that:
#!/usr/bin/env python
import os
print 'sending HI'
os.system("export DISPLAY=:0")
os.system("xdotool key H")
os.system("xdotool key I")
I am SSH'd into the Raspberry as user Pi, and running this script from terminal works fine and sends the keystrokes "HI" to the Raspberry.
However, I want to be able to do this through a web page. I've made a php-script that runs python scripts and displays the terminal output in the browser, but it does not work with the xdotool.py script. I have given the script permission to be viewed/changed/executed by all users.
I've also changed the Apache settings in /etc/apache2/envvars and changed the user from www-data to pi. The user pi should have all necessary permissions.
The script does run, and "sending HI" is displayed in the browser. But it doesn't send the keystrokes. The error I get from error.log is this:
Error: Can't open display: (null)
Failed creating new xdo instance
Any ideas how I can accomplish this? If there's a way to give everyone permission to do everything, that wouldn't be a problem seeing that this server will only run on a local network with no internet access.

Accessing shell script on remote server

So first of all I have 2 Servers:
Raspberry pi
Linux Debian server
The pi has a script that displays the current Temperature in a room.
This is the script :
#!/bin/bash
i2cset -y 1 0x48 0xEE
dte=$(date +%Y-%m-%d)
tme=$(date +%H:%M:%S)
hexraw=$(i2cget -y 1 0x48 0xAA w)
msb=$(echo ${hexraw:4:2})
lsb=$(echo ${hexraw:2:1})
dec=$(printf "%d\n" "0x$msb$lsb")
temp=$(echo "scale=1; $dec/16" | bc)
echo $temp
exit
Displaying the temperature on the pi's website works fine.
But then I have another script on the linux server that takes the value of the pi's script:
#!/bin/bash
x=$(ssh pi#172.16.248.210 sudo /home/pi/CurrTemp;)
echo $x
This works too. But when I want to display the Temp on my Linux server website(with shell_exec()) it doesn't work. The var_dump is empty.
PHP:
I've tried to write it with a "sudo".
I've changed the script owner to root.
ssh authentication does not work since the web server can't (and should) access your ssh keys.
You have to options (where the latter is definitely preferred!):
Create another ssh key for the web server on the Debian server and add to allowed_keys on the PI. You may restrict the key to allow only that command.
Create a little web service on the PI and output the temperature in plain text, xml, json or whatever. The use file_get_contents() to obtain the file over HTTP, like this using a plain text file:
temperature.php on raspberry pi:
<?php
echo shell_exec('/home/pi/CurrTemp');
temperature.php on Debian:
<?php
$temperature = file_get_contents('http://172.16.248.210/temperature.php');
echo $temperature;

Running imagesnap in PHP with Mac Server

I am trying to develop a small site to take snapshots of my house using a webcam connected to to my mac mini at home. The mac mini is running OS X Server. The Webcam is a Logitech HD Pro Webcam C920 (USB connected). It is based on PHP and HTML5 (no other code besides CSS).
My development machine is a MacBook Pro using the internal iSight camera. It is not running OS X server; I manually enabled apache and PHP. While developing the site on my laptop, everything worked flawlessly. When I installed it on my mac mini the imagesnap program turns on the camera but never finishes; I have to manually kill the process, and the snapshot is never created.
The following error is logged:
imagesnap[2363]: *** QTCaptureSession warning: Session received the following error while decompressing video: Error Domain=NSOSStatusErrorDomain Code=-12903 "The operation couldn’t be completed. (OSStatus error -12903.)". Make sure that the formats of all video outputs are properly configured.
This is only when running it from the site directly. If I run the same program from the command-line, it works just fine... even running it as the _www user!
sudo -u _www bin/imagesnap images/snapshot_$DATE.jpg
This is the code that does the call in PHP:
<?php
if(isset($_POST['submit'])) {
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
}
?>
I was unable to figure out how to write the date to the output filename (I'm a noob with PHP), so I used a bash script to do it for me (I'm better with BASH):
#!/bin/bash
DATE=`date +"%m-%d-%y_%T"`
bin/imagesnap -q -w 3 images/snapshot_$DATE.jpg
I've also tried exec(), passthru(), and shell_exec() all with the same results.
Finally, I've also tried the following: Create test.php, put code:
<?php
system('bin/run_imagesnap.sh', $return_value);
if ($return_value) {
echo "The command failed.";
}
?>
and ran it on the command-line as the _www user using the php-cli:
sudo -u _www php test.php
and it also worked just fine (created the snapshot file and did not give error).
I'm thinking maybe it has something to do with the OS X server app, but google searches return nothing.
I'm stumped. Why does it break when I run the same exact code on the browser but works fine on the command-line using the same exact tools and user?
Unknown if this is the correct way, but I ended up using sudo to run the script.
visudo
and then give the _www user passwordless root access to the script:
_www ALL=NOPASSWD: /path/to/script
and now it works. There's always more than one way to skin a cat..

Raspberry Pi unable to execute script by PHP

I'm playing with Raspberry Pi and anm Arduino shield in order to run a script via Apache/PHP. This script simple blink a LED. I have already tested the script via shell and it works fine, with the command
/root/arduPi/blink_test
I'm able to see my LED blinking. So I made the same thing via Apache PHP with this short PHP script
<?php
if(isset($_GET['cmd'])){
echo '/root/arduPi/'.$_GET['cmd'];
exec('/root/arduPi/'.$_GET['cmd']);
}
?>
but nothing happen and no error has been displayed.
I tested the PHP code with
<?php
phpinfo();
?>
and it's fine. How can I fix this problem?
I had the same issue a while back it was because Apache does not have permission to access certain devices on the Pi. I fixed this by getting rid of the need to be root to access these devices. HERE Is my post about this same issue the fix was to setup sudo as passwordless.
THIS is what I used to accomplish setting up sudo as passwordless. You should then be able to execute the script as follows exec('sudo /root/arduPi/'.$_GET['cmd']);

Categories