I have a PHP script on an external web server. If I view www.example.com/test/myphp.php
in my browser, the server runs the script just fine. Now I want the server to run the script when my Arduino says so, instead of when I view it in my browser. I want the Arduino to remotely trigger the script to run.
I am a complete newbie to servers / php / the web, so any tips would be appreciated, especially with the GET synatx (if that's even the right command?). The Arduino has an ethernet shield and can connect to the server fine. The below code correctly connects, but unlike when I open www.example.com/test/myphp.php
in my browser, the PHP file doesn't actually run when it's accessed from the Arduino :(
Do you have any suggestions for how I can modify this to get the PHP file to run when my Arduino tells it to?
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.example.com";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("SERIAL: Failed to configure Ethernet using DHCP");
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("SERIAL: connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
client.print("GET /test/myphp.php");
client.println(" HTTP/1.1");
client.println("Host: www.example.com");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
client.println("Connection: close");
client.println();
}
else {
// if you didn't get a connection to the server:
Serial.println("SERIAL: connection failed");
}
}
void loop()
{
// if there are incoming bytes available, print them
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
}