[SOLVED] Ubuntu server + PHP + shellscript != Arduino output

I have a webcam running on my web server of a goldfish in a tank. I want to have a link on the webpage interact with the Arduino. One thing will be to open a hacked toy backhoe bucket in order to put food into the tank.

I am running a headless Ubuntu 12.04 server so i thought the easiest way to do this is with a shell script so i wrote:

#! /bin/bash
echo -n "1" > /dev/ttyACM0

That interacts with this sketch:

int feedPin = 13;

void setup(){
  Serial.begin(9600);
  pinMode(feedPin, OUTPUT);
}
void loop(){
  while (Serial.available() == 0);
  int val = Serial.read() - '0';
  
  if (val == 1) {
    Serial.println("Feeding the fish");
    digitalWrite(feedPin, HIGH);
    delay(1500)
    digitalWrite(feedPin, LOW);
  }
  else if (val ==0){
    Serial.println("Stopping");
    digitalWrite(ledPin, LOW);
  }
  else{
    Serial.println("invalid");
  }
}

For debugging right now all that does is turn on an LED and that is fine. I have a 100uf capacitor in between reset and ground to keep the connection alive (overkill but it works). When i run the shell script life is good and it words great.

So i wrote a simple PHP script that runs this shell script:

  <h3>Feeding the Fish</h3>

<?
exec('sh feed.sh');
?>

When i run that from the command line it works great. So i put a link on the website to run it for me:

<li><a href="feed.php">Feed the Fish</a></li>

when i click the link nothing happens =(

So i went back to the command line and checked and chmoded all the permission, and then ran # sudo -u www-data php feed.php to make sure apache could perform the task. It could not so i added www-data to the dialout goup using this:# usermod -a -G dialout www-data and now it it works fine.

Now i switch over to my browser and click the link and it works! So i reload the page and it works again! Then about a minute later i try and do a page reload and nothing happens. Igo back to the webpage and re-click the link. Nothing.

That makes me think that it has something with me running # sudo -u www-data php feed.php

which opens the connection and then it times out and apache cant re-open the connection.

any ideas?

Have you considered using the php serial function?
http://code.google.com/p/php-serial/

I know nothing of just about any programming language. I am the definition of a hobbyist. Therefor PHP is not a strong suit so bear with me please.

I took you suggestion and tried the PHP-serial with this code:

<h1>Feeding The Fish</h1>

<?php
deviceSet("/dev/ttyACM0"); 

// Then we need to open it 
$serial->deviceOpen(); 

// To write into 
$serial->sendMessage("1"); 

// If you want to change the configuration, the device must be closed 
$serial->deviceClose(); 

// We can change the baud rate 
$serial->confBaudRate(9600); 

?>

I pretty much just cut and paste off that website but it does not work.

when i bust down to the command line to try:

# php feed.php

i just get:

root@Wall-E:/var/www/nathanweber# php feed.php
<h1>Feeding The Fish</h1>

PHP Fatal error:  Call to undefined function deviceSet() in /var/www/nathanweber/feed.php on line 4

however that is the correct device and the script has permissions

????

You must install the package there to use those functions.

I have always used fopen and related functions. I used this for a test. It showed FAIL when the arduino is not inserted, and PASS when it is. It opens for read/write. Put this in your html output.

<?php
$my_com = fopen('/dev/ttyACM0','w+');

if($my_com == FALSE) echo "FAIL";
else echo "PASS";

fclose($my_com);
?>

http://php.net/manual/en/function.fopen.php

edit: You may want to consider disabling the usb reset signal if you do not want to reset your Arduino every time you connect with the webpage. Usually when you open the serial port, it resets the Arduino. Any SRAM variables will be lost.

That involves breaking a small trace between the "reset-en" pads on the Arduino circuit board. You can always enable it again by connecting the pads again (solder jump).

The down side is you will be required to press the reset button when the IDE starts the program upload to start the bootloader.

You must install the package there to use those functions.

What package? i have php5 installed, running, and working with owncloud.

Why would i want to disable the reset pin when i have a capacitor connecting ground to reset?

SurferTim:
I tried this in my browser.

<?php

$my_com = fopen('/dev/ttyACM0','w+');

if($my_com == FALSE) echo "FAIL";
else echo "PASS";

fclose($my_com);
?>

When i run from the command line

 # php test.php

I get a "pass"

When i link to the the php script from my browser with :

                <li><a href="test.php">Test</a></li>

I get a fail. This is very similar to the problems i was already having.

I saved this as test.php on my Apache server.

<html>
<body>
<center>
TEST

<?php
$my_com = fopen('/dev/ttyACM0','w+');

if($my_com == FALSE) echo "FAIL";
else echo "PASS";

fclose($my_com);
?>
</body>
</html>

Then I request the page from another computer, and it shows PASS.

edit: I forgot to mention one thing on my Ubuntu server. I had to allow group "other" read/write access to the Arduino.

cd /dev
sudo chown 777 ttyACM0

SOLVED!

Apache did not have suPHP pwers I installed using the follow process copied from ApacheMySQLPHP - Community Help Wiki

Installing suPHP
suPHP is a tool for executing PHP scripts with the permissions of their owners. It consists of an Apache module (mod_suphp) and a setuid root binary (suphp) that is called by the Apache module to change the uid of the process executing the PHP interpreter.

Note: suPHP enforces, security and helps avoid file permission problems under development environments with several users editing the site files, but it also demands more memory and CPU usage, which can degrade your server performance under certain circumstances.

To only install suPHP. use any method to install the package

# aptitude install libapache2-mod-suphp

Enable this module by doing

# sudo a2enmod suphp

then use a text editor such as "sudo nano" at the command line or "gksudo gedit" on the desktop to edit this file

sudo nano /etc/apache2/mods-available/php5.conf

or

gksu "gedit /etc/apache2/mods-available/php5.conf"

make a new empty line at the top of the content, then add

<Directory /usr/share>

make a new empty line at the bottom of the content, then add

> ``` > > > > save changes > > For security reasons we need to specify to suPHP what are the document paths allowed to execute scripts, use a text editor such as "sudo nano" at the command line or "gksudo gedit" on the desktop to edit this file > > > ``` > # sudo nano /etc/suphp/suphp.conf > ``` > > > > or > > > ``` > gksu "gedit /etc/suphp/suphp.conf > ``` > > > > find the value "docroot" and specify the document path of your site files, for example: > > docroot=/var/www/ > that value restrict script execution only to files inside "/var/www/" > > > docroot=/var/www/:${HOME}/public_html > that value restrict script execution only to files inside a custom home folder for each configured user inside "/var/www/:${HOME}/public_html" > > for this tutorial we are going to use this value > > > docroot=/home/user/public_html/ > which is the same Apache directory directive set before in this document > > save changes > > to restart Apache, type in your terminal > > > ``` > # sudo /etc/init.d/apache2 restart > ```

I am happy to hear you got it fixed.

I just did a fresh install of Ubuntu 11.10 as a LAMP server. Once I got the web server and php working, I added the dialout group to the www-data user (apache2 default user), rather than changing the permissions on /dev/ttyACM0. It also works good with fopen and the Arduino.

sudo usermod -a -G dialout www-data

Does anyone see a problem with that?

I had to do the same thing. I gave dialout permission to apache