Arduino to external PHP

I am trying to get the Arduino to talk with an external PHP server. This works great when the PHP server is on the local network. But if i try to get it to connect with an external server, I get connection failed. Have tried to solve this for a while. Is there something that needs to be added when talking to http. I know it's not the port or the router cause i have gotten stuff like the twitter client in the library to work with port 80.

This is the (network part) of the code that works locally. (When I have the server running on a computer on the network).
–––––––––––––––––––––––––––––––––––
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x08, 0x84 };
byte ip[] = { 172, 16, 3, 191 };
byte gateway[] = { 172, 168, 3, 354 };
byte subnet[] = { 255, 255, 252, 0 };
byte server[] = {172,16,3,21};

EthernetClient client;

void setup()
{

Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.begin(9600);

}

void loop()
{

if (client.connect(server, 80)) {

Serial.println("connected...");
Serial.println("ARDUINO: forming HTTP request message");

//Sensor 1
client.print("GET /arduino.php?sensor1=");
client.print("stove&");
client.print("value1=");
client.print(state);
client.print("&");

client.println();

Serial.println("ARDUINO: HTTP message sent");
delay(0);

client.stop();
}
else
{
Serial.println("connection failure");
delay(2000);
}

}

Same code, but to a web server. Doesn't work at the moment.
––––––––––––––––––––––––––––––––––––––––––
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x08, 0x84 };
byte ip[] = { 172, 16, 3, 191 };
byte gateway[] = { 172, 168, 3, 354 };
byte subnet[] = { 255, 255, 252, 0 };
char serverName [] = "http://arpho.mobi";

EthernetClient client;

void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.begin(9600);
}

void loop()
{

if (client.connect(serverName, 80)) {

Serial.println("connected...");
Serial.println("ARDUINO: forming HTTP request message");

//Sensor 1
client.print("GET /app/arduino.php?sensor1=");
client.print("stove&");
client.print("value1=");
client.print(state);
client.print("&");
client.println();

Serial.println("ARDUINO: HTTP message sent");
delay(0);
client.stop();
}
else
{
Serial.println("connection failure");
}
}

You have problems here:

byte ip[] = { 172, 16, 3, 191 };
byte gateway[] = { 172, 168, 3, 354 };
byte subnet[] = { 255, 255, 252, 0 };

The ip and the gateway are not on the same localnet. Find out which of those are correct.

The gateway is an invalid setting. 354 is not valid. The largest allowable number is 255, and the largest available number for an normal ip assignment is 254.

Does the Ethernet library support DNS?

I think you need to use the servers' IP address like you do when it's local, and send a Host header.

like:

byte server[] = {195,74,38,17}; 
....
client.println("GET / HTTP/1.1");
client.println("Host: arpho.mobi");

mikehiow:
Does the Ethernet library support DNS?

Yes, IDE v1.0 supports dns.

Even so, I suspect you still want to ditch the http:// and use the host HTTP header?

mikehiow:
Even so, I suspect you still want to ditch the http:// and use the host HTTP header?

That is correct. Just the server name, not the protocol. I did not catch that. Good eye! :slight_smile: