Pages: [1]   Go Down
Author Topic: arduino+ethernet shield+php  (Read 7521 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am doing a 2 two communication between arduino and php server.
right now, i am using arduino as a client and successfully send data to my remote php server through ethernet shield.
but i am not sure how i can using php control arduino through Ethernet shield.

anyone can help me?

thanks a lot!
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
I do my own experiments.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

have the ethernet shield regularly poll the server for information.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I hope your projecct is going well,
I'm new to the ethernet sheild, would you be able to point me in the direction of example php and sketches to get the data transfer working please? I'm a bit lost at the minute!

Thanks

Pete
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

I am interested in this also, as i am trying do do two way communication.

For the Moment i can post data via php to a mysql server, but i am not able to control the arduino through php and the ethernet shield.

Do some of you have some code examples i could look into?

Thanks! - Kedde
Logged

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 155
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

could you post your code on having the Arduino be a client to your PHP server?  this is exactly what i'm trying to do with the Arduino Ethernet shield with no success with
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is also posted here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1268504939   - post 7

But i assume that Danish isnt your 2. language so i have translated the comments:

The Arduino Code:
Code:
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 88 };
byte gw[] = {192,168,1,1};
byte server[] = { xxx, xxx, xxx, xxx  }; // Server IP
byte subnet[] = { 255, 255, 255, 0 };
int data = 0;
int tempPin = 2;  // In This case the temperature is taken from pin 2 and sent to a sql server

void setup()
{

pinMode(tempPin, INPUT);
Serial.begin(9600);               // Used for serial debugging

}
void loop()
{

Serial.println("Program running...");

 delay(5000);
 senddata();                                 // Data is sent every 5 seconds

}
void senddata()
{

data = analogRead(tempPin);           //Reading analog data


Ethernet.begin(mac, ip, gw, subnet);
Client client(server, 80);
Serial.println();
Serial.println("Forbinder…");
delay(1000);                                    //Keeps the connection from freezing

if (client.connect()) {
Serial.println("Connected");
client.print("GET http://server.com/script.php?vaerdi=");
client.print(data);
client.println(" HTTP/1.1");
client.println("Host: www.server.com");
client.println();
Serial.println();
                           }

else
{
Serial.println("Connection unsuccesfull");
}
//}
 //stop client
 client.stop();
 while(client.status() != 0)
{
  delay(5);
}
}
 

PHP Code:
Code:
<html>
      <?php

            $DATA 
$_GET['vaerdi'];

                  
//Connect to database
                  
$opendb mysql_connect("xxx.xxx.xxx.xxx""database""password") or mysql_error("Could not connect to database");
                 
mysql_select_db("database");
    
     if (
$opendb)
            &
#123;
                 
mysql_query(" INSERT INTO tabel (Dato, DATA) VALUES ( NOW() , $DATA )");
            
mysql_close($opendb);
              &
#125;
      
?>

</html>

In the MySql server there are 1 table with 2 datasets, one with the name DATA (Integer) and one with the name DATO (DATETIME)

I am not really good with mysql so if someone could tell how to get the MySql server to delete the oldest data i would be very happy smiley

Also, this is not all my work, i modified it to my needs, but unfortunately i dont remember where i got if from..

smiley - Christian
Logged

Orlando, FL
Offline Offline
Full Member
***
Karma: 0
Posts: 155
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Christian I think you solved my problem.  Going to tinker with it tonight.

Logged

Wigan, UK
Offline Offline
God Member
*****
Karma: 19
Posts: 814
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To delete the latest use DELETE FROM table ORDER BY DATO DESC LIMIT 1

That command will delete one row (limit 1), and it might be the oldest one. I say might because I can't remember how dates are treated - if it deletes the lowest one change DESC to ASC (ascending or descending).

I'd recommend adding an auto-incdement ID field as a primary key.
Logged

Wigan, UK
Offline Offline
God Member
*****
Karma: 19
Posts: 814
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Team SCC, Kyle is right - the easiest way is to have your arduino poll a dynamic page which feeds the arduino the latest command.

I've used that technique on my physical hit counter, code available here: http://cowjam.net/
Logged

Silkeborg, Denmark
Offline Offline
Jr. Member
**
Karma: 2
Posts: 72
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Cowjam

I am very interestet in this topic, but I can see your link does'nt point to the code anymore, could you please post the code again.

Thank you.  smiley
Logged

Thansen_DK

Pages: [1]   Go Up
Jump to: