Go Down

Topic: arduino+ethernet shield+php (Read 7 times) previous topic - next topic

TEAM SCC

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!

KyleK

have the ethernet shield regularly poll the server for information.

milneandfletch

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

Kedde

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

stealthtransam

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

Kedde

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: [Select]

#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: [Select]

<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 :)

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

:) - Christian

stealthtransam

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


CowJam

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.

CowJam

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/

Thansen_DK

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.  :)
Thansen_DK

Go Up