Go Down

Topic: Arduino PHP Communication (Read 5821 times) previous topic - next topic

Nurain

Hi,

I am new to Arduino and Programming in general.  I have got some code uploaded to the Arduino to control functions of two servo motors, i.e. FORWARD, BACKWARD, LEFT and RIGHT..  I want to use PHP code to send data to trigger these functions at the click of a button from a web browser, but have no idea how to get started..

Also I am using a WiShield from AsyncLabs to connect to my PC, this makes it significantly more confusing for me as I assume the HTML/PHP code will need to be uploaded to the Arduino to use the WiShields wireless/webserver feature..

Does anyone have any experience with this?.. I would really appreciate all the help I can get with this as it's currently driving me insane.  

Thanks.


Nur

qistoph

Hi Nur,

As far as I know the Arduino will not be able to run actual  PHP code.

You could either write an interface on a real webserver in PHP that forwards any commands to the Arduino. The commands could be forwarded to an interface on the WiShield, or the serial interface.

Or you could write pages on the Arduino.

I'm not really aware of the possibilities of the WiShield. If it has a built in webserver, you'll have to figure out if this webserver can interface with your code.
If it doesn't have a built in webserver, you'll have to write a simple webserver yourself.

Maybe you can ask a more specific question here, that way you'll get better answers.

Kind regards,
Chris

Ran Talbott

Check the Ajax examples in the Webduino library:  they may do what you want.

linusb

Here you go:

Code: [Select]
<?php
// Working code to control robot via web

// bugs: 1. when arduino is first powered up, the arduino source must be re-uploaded for it to work
//        

echo "The Guardino!";

if(isset(
$_POST['task']))
 
   &#123;
 
   $task$_POST['task'];
 
   // Switch stuff goes here
            
switch( $task 
 
               &#123; 
 
               case 'CLOCKWISE'
 
                         doClockwise(); 
 
                   break;
 
               case 'FORWARD'
 
                   doForward(); 
 
                   break;        
                
case 'CCLOCKWISE':
 
                     doCClockwise();
 
                     break;
 
               case 'REVERSE':
 
                     doReverse();
 
                     break;
 
               &#125;
 
   &#125;
 
else doForm(); 

function 
doForm()
 &
#123; 
//can use values here to control sending of email
       
$f "testlog.txt";
$str file_get_contents($f);
$numChar strlen($str);
$_date file_get_contents('./testlog.txt'NULLNULL,$numChar-27,10);
$_time file_get_contents('./testlog.txt'NULLNULL,$numChar-17,12);
$_value file_get_contents('./testlog.txt'NULLNULL,$numChar-5,4);
echo 
"Last Log Entry:   Date: $_date   Time: $_time    Value: $_value"       
// and here if trouble
?>




 <form method="post" action="">
<input type="submit" name="task" value="CLOCKWISE">
<input type="submit" name="task" value="FORWARD">
<input type="submit" name="task" value="CCLOCKWISE">
<input type="submit" name="task" value="REVERSE">
</form>
<br>
<table style="width:auto;"><tr><td><a href="http://picasaweb.google.com/lh/photo/hNWGzONBI_7ZRLpSuEiXeQ?feat=embedwebsite"><img src="http://lh6.ggpht.com/_0ykF70Y_1es/S0Cl-uBjeTI/AAAAAAAADHE/CDWCfA7z8rc/s400/arduino2.jpg" /></a></td></tr><tr><td style="font-family:arial,sans-serif; font-size:11px; text-align:right">From <a href="http://picasaweb.google.com/linusb1/Arduino?feat=embedwebsite">arduino</a></td></tr></table>
<br>
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/od6Ggg0gLks&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/od6Ggg0gLks&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
<br>
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/I-p_jI98zZU&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/I-p_jI98zZU&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

<script src="http://localhost/ArduinoProject/helloworld.js"></script>
     <script>
       var attributes = { code:'HelloWorld.class', archive:'/applet_HelloWorld/applet_HelloWorld.jar',  width:150, height:30} ;
       var parameters = {} ;
       deployJava.runApplet(attributes, parameters, '1.4');
</script>


<?php 
 
&#125;


function doClockwise()
 &
#123;
 
include "php_serial.class2.php";
 
$serial = new phpSerial;
 
$serial->deviceSet("COM4");
 
$serial->deviceOpen();
 
$serial->sendMessage('B');
 
$serial->deviceClose();
 echo 
"Clockwise Value = B Sent";
 
doform();
 &
#125;

 
function doForward()
 &
#123;
 
include "php_serial.class2.php";
 
$serial = new phpSerial;
 
$serial->deviceSet("COM4");
 
$serial->deviceOpen();
 
$serial->sendMessage("A");
 echo 
"Forward Value = A Sent";
 
$serial->deviceClose();
 
doform();
 &
#125;

function doCClockwise()
 &
#123;
 
include "php_serial.class2.php";
 
$serial = new phpSerial;
 
$serial->deviceSet("COM4");
 
$serial->deviceOpen();
 
$serial->sendMessage('D');
 
$serial->deviceClose();
 echo 
"Counter Clockwise Value = D Sent";
 
doform();
 &
#125;

 
function doReverse()
 &
#123;
 
include "php_serial.class2.php";
 
$serial = new phpSerial;
 
$serial->deviceSet("COM4");
 
$serial->deviceOpen();
 
$serial->sendMessage("S");
 
$serial->deviceClose();
 echo 
"Reverse Value = S Sent";
 
doform();
 &
#125;
 
?>


linusb

The cool bit is you can use a smart phone like a remote control through your phone's web browser.

http://www.youtube.com/user/linusb212#p/a/u/0/I-p_jI98zZU


Arduino Code:

Code: [Select]
// successfully Control robot via web

const int ledPin = 2;      // the pin that the LED is attached to
const int rightPIN=5;  //right servo
const int leftPIN=3;   //left servo
const int pingPin = 4;  //ping SERVO
const int avoidinch = 22;  //distance at which avoidance is initiatied

byte _control;  //value read through serial port
int leftforward=42;  //was 290
int rightforward=170;
int leftbackward=84;
int rightbackward=80;
 
void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode (leftPIN,OUTPUT);
  pinMode (rightPIN,OUTPUT);
}

void loop()
{
   // check if data has been sent from the computer:
  if (Serial.available())
   {
    // read the most recent byte (which will be from 0 to 255):
    _control = Serial.read();
    // set the brightness of the LED:
       if (_control=='A') _forward();
       else if (_control=='B') _clockwise();
       else if (_control=='D') _cclockwise();
       else if (_control=='S') _reverse();
       else delay(5000);
    }  // end serialavailable loop

   // blink LED in rapid fashion to signal no serial signal
  digitalWrite(ledPin, HIGH);
  delay(50);
  digitalWrite(ledPin, LOW);
  delay(100);
}  


void _forward()
{
 analogWrite (leftPIN,leftforward);  
 analogWrite (rightPIN,rightforward);
 delay(1000);
 digitalWrite (leftPIN,LOW);  
 digitalWrite (rightPIN,LOW);
}

void _clockwise() //confirmed
{
 analogWrite (leftPIN,leftforward);  
 analogWrite (rightPIN,rightbackward);
   delay(1000);
 digitalWrite (leftPIN,LOW);  
 digitalWrite (rightPIN,LOW);
}

void _cclockwise()
{
 analogWrite (leftPIN,leftbackward);  
 analogWrite (rightPIN,rightforward);
   delay(1000);
 digitalWrite (leftPIN,LOW);  
 digitalWrite (rightPIN,LOW);
}

void _reverse()
{
 analogWrite (leftPIN,leftbackward);  
 analogWrite (rightPIN,rightbackward);
   delay(1000);
 digitalWrite (leftPIN,LOW);  
 digitalWrite (rightPIN,LOW);
}

zoomkat

I don't have an Iphone, but I think a friend was able to operate my servo pan/tilt cam below from her Iphone. I think the video also updated, but the Iphone was in a bar with a weak signal so not sure how well it worked.

http://web.comporium.net/~shb/wc2000-PT-script.htm
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

linusb

Hey ZoomKat, I stumbled across your site on my iphone the other day while doing some robot surfing.  Very cool!

zoomkat

Quote
Hey ZoomKat, I stumbled across your site on my iphone the other day while doing some robot surfing.  Very cool!

Thanks for the report! I need to find which brouser enabled cell phones it will and will not work with (aka, an el cheapo cell phone I can afford).
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Nurain

Hi linusb.

Thank you so much for that.  Can you tell me, how do you connect to the arduino?.. usb?.. I know to upload the Arduino code .pde file to the Arduino, can you give me some info on how to use the php file?..  Will apache xampp as a server do?..

Thanks again.


Nur

linusb

#9
Jan 24, 2010, 01:48 am Last Edit: Jan 24, 2010, 01:50 am by linusb212 Reason: 1
Yes, my Arduino is connected through USB port.  I'm waiting on a Bluesmirf wireless modem that will allow the robot to be controlled wireless.  

I downloaded the WampServer package which is free and includes Apache, php, and mySQL.

If you want to write/modify php code, I suggest you download the Eclipse IDE.  It's also free and seems to be a great platform.  I'm not a programmer but was able to figure out the interface easily enough and get things to working.  With that php code above, it seems to me you can just copy/paste into notepad and save it as test.php and then open the file in your browser.  

It seems to work doing it like that for me just now...but I don't have my php server running so I don't quite understand how that works since php is a server side application.

But anyway, download WampServer and Eclipse IDE.  Run Apache from the WampServer, you'll have to do some configuring to your router settings if you have one.  Configure the root directory for Apache and place the test.php file in there.

Then you can put your computer's IP address into the browser and should show you a directory of your root folder with the php and any other files you placed in there.  Click the php file  and it should display a web page.  If you use the exact code from above, it should display 4 buttons that when clicked will each send a different byte of data over the serial port to be read by your arduino.  There will also be displayed a picture of my robot and a couple youtube videos.

Good luck.

Nurain

Thank you so much linusb, can't express how grateful I am for you help.

I'll give this a try in a few days.



Nur

Nurain

#11
Jan 24, 2010, 05:39 pm Last Edit: Jan 24, 2010, 05:39 pm by nurain Reason: 1
Hi linusb,

Got a couple errors when I test it..



Warning: include(php_serial.class2.php) [function.include]: failed to open stream: No such file or directory in E:\xampp\htdocs\phptest\phpcode.php on line 74

Warning: include() [function.include]: Failed opening 'php_serial.class2.php' for inclusion (include_path='.;E:\xampp\php\PEAR') in E:\xampp\htdocs\phptest\phpcode.php on line 74

Fatal error: Class 'phpSerial' not found in E:\xampp\htdocs\phptest\phpcode.php on line 75



Looks like I am missing 'php_serial.class2.php'  :(..

linusb

Oh yeah, that's important.:)

It seems too big for me to paste into here.  Download it here:
http://www.phpclasses.org/browse/file/17926.html

Copy and Paste that into either Eclipse or notepad and save it as php_serial.class2.php in the same folder as your other php file

Make sure your original php code references the correct serial port that your arduino is on.  Mine is Com4.  

Nurain

#13
Jan 24, 2010, 05:56 pm Last Edit: Jan 24, 2010, 06:11 pm by nurain Reason: 1
Hi mate.. Yeh I did that.  The errors went away.  Unfortunately, when I press the buttons, my robot doesn't react.  Any idea what I am doing wrong?..  

My robot is connected to the computer using a USB cable connected to the Arduino.

And yeah, my com port is also com4 so don't think that's it.


[edit]About the testlog.txt file, is it meant to be an empty text file where logs are saved? or something else?.. [/edit]

linusb

You'll want to try to troubleshoot things and see where the problem is then.

The one thing I should have told you is that you'll need to go into your Arduino code and look at the pin assignments I have for the servo motors.  I'm sure your servos aren't connected to the same pins so you'll need to modify the arduino code so that it is specific to your configuration.

Also, I modified my servos for continuous rotation.  I just experimented with values to use to get the wheels to turn in the proper direction.  Those values in my code may very well not work with your servos but they should at the least move somehow through that php code.

If all that doesn't work I'd ask:

Have you successfully read serial data on your arduino to do something simple like light up an LED or anything?

When you press the buttons...do you get the status messages appearing above the buttons "Forward signal sent" or something along those lines?  That would indicate that the php code is working correctly?

Did you install WampServer?

Go Up