it drove me nuts but this works for me...
this code can bettered as i open the connection in the loop which must be bad (|)
I wanted to use the domain name not the ip address to post data to my drupal site.
The example below is on my local webserver but its the same case if you have a live server with a shared ip address.
I set up a simple test page on my local site http://arduino/arduino.php as a proof of concept which saved the data Arduino posted.
//Libraries
#include <SPI.h>
#include <Ethernet.h>
// Set the unique mac and ip address etc for the ethernet board
// the only bit i havent tested is the gateway on a remote server but it should be fine
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,70 }; // just an ip address not being used on my local pc
byte server[] = { 192,168,0,50 }; // my local machines ip address from ifconfig
byte gateway[] = { 192,168,0,1 }; // my router
byte subnet[] = { 255,255,255,0 };
void setup() {
Serial.begin(9600);
}
// so far this is all standard stuff and should make sense.
// this next step was complete trial and error
// the key difference is that i seemed to have to connect in the loop not in setup
void loop()
{
Client client(server, 80);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
// read the pin values here and decide if to post it (edit to suit), i am only interested when i am being burgled.
//if (A0 >100) {
if (client.connect()) {
Serial.println("client connected OK");
client.print("GET http://arduino/");
// put your own values in here...
client.print("arduino.php?room=kitchen&motion=TRUE&temp=100\n");
client.print("HTTP/1.1\n");
//client.print("Host: arduino\n"); //hmm don't need to seem this anymore
client.print("User-Agent: Arduino (Arduino Test Code)");
client.println("\n");
}
//}
if (!client.connected()) {
Serial.println();
Serial.println("disconnected.");
//client.stop();
}
}
for my test i had a simple php script at http://arduino/arduino.php to store the sent values in a text file like this
// make sure www-data can write to test.txt
file_put_contents('test.txt', print_r( $_GET,1) ."\n", FILE_APPEND) ;
tidy the code as needed, prolly put the client connect inside the if(A0>100) statement for sure but it's late (again).
if stuck pls post