Arduino to Pi to PHP - 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.

Related

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.

sage install set up on debian server, help setting up correct DOT_SAGE permissions

i just installed sage 6.2 from source on a debian server, then..
1) command sage -c 'print (3+5)'
WORKS as expected from terminal
FAILS from php script exec("sage -c 'print (3+5)'", $output[]);
throws the error
Error: environment variable $HOME is not set.
Error setting environment variables by sourcing '/mysoft/sage-6.2/src/bin/sage-env';...
2) partial fix:
set export HOME='/var/www/junk' in sage-env file
3) now
LINE command sage -c 'print (3+5)' FAILS from terminal log shows.. "...OSError: [Errno 13] Permission denied: '/var/www/junk/.sage/db'..."
PHP script exec("sage -c 'print (3+5)'", $output[]); WORKS as expected
so it seems i can make sage work thru command line or thru php script but not both at same time. depending on if i comment out
export HOME='/var/www/junk' or not..
moreover the real trouble is that with export HOME='/var/www/junk' the entire server does not copy or back up correctly throwing errors about permissions on the .sage directory.. and making all server backups useless [specifically using google cloud computing snapshots to back up entire server, but also throws errors even when taring the www directory ]...
ultimately i want to be able to able to run the php exec version of sage AND be able to back up and copy snapshots of server with no errors.. all help is MUCH appreciated as this is driving me crazy....

Possible to open Terminal with PHP?

So I've got a Mac and I am trying to create a localhost webpage that will open the terminal and start a minecraft server. That way my brother can be on the server while I am not on the computer.
Problem is, no matter what I try, I can't get the terminal to open.
So, here's the code (just in a simple localhost/test.php file:
<?php
exec('osascript -e \'launch the application "Terminal"\'',$out);
system('osascript -e \'tell application "Terminal" do script "cd ~/Desktop/Bukkit/;" & "./start_server.command"\'',$out);
echo $out;
?>
As you see, I was trying to do it with applescript, the second line works only if the terminal is already open, which is why I want the first line to open the terminal.
So, is there anyway to make this happen?

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

FTP in PHP script and command line fails with 553, but Filezilla transfers OK

I'm getting a weird FTP failure in a PHP script. I've recreated the failure just using command line FTP, so I think I can eliminate PHP from my enquiries.
Using command line FTP in Ubuntu 10.10, I can connect to my FTP server, log in, list directories, etc. However, when I try to transfer a file from my local machine to the FTP server using "put" I get a "553 Could not create file" error. I get the same error in active and passive modes. Transfer is set to binary.
When I run FileZilla on the same machine and connect to the same FTP server using the same credentials, and try to transfer the same file to the same destination directory, the transfer works just fine, after defaulting to binary and passive mode.
FWIW, the PHP script (which is a simple ftp_connect, ftp_login and ftp_put) using the same details fails with "Illegal PORT command", although I suspect this is just PHP's interpretation of the same error I get from command line FTP. Again, this error occurs in both active and passive FTP modes.
FileZilla and ftp command probably starts in a different directories. You can setup starting remote directory in FileZilla and you have to go to the same location from command line.
Try using pwd command after logging in from command line client, it will show current working directory, compare that with the one FileZilla is using and then go to the correct directory with a cd command.

Categories