I want to access the following page on my local using my Arduino with ethernet shield" 192.168.1.2/zombie/arduino.php. My arduino is conencted to a PC through a router. The system keeps timing out
I am using the following code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,3 };
char server[] = "192.168.1.2/zombie/arduino.php";
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
int res = client.connect(server, 80);
Serial.println(res);
if (res) {
Serial.println("connected");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.println("data:");
Serial.println(c);
}else{
Serial.println("NA");
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
-
"localhost" means the loopback interface (ie, 127.0.0.1), and so by definition cannot be accessed from any other system. You have a local server (presumably on the same subnet as your Arduino), not a "localhost" one.Chris Stratton– Chris Stratton2015年03月06日 19:42:56 +00:00Commented Mar 6, 2015 at 19:42
1 Answer 1
I discovered the answer after some testing.
The server var:
char server[] = "192.168.1.2/zombie/arduino.php";
should only contain the ip address. So it should look like this:
byte server[] = { 192,168,1,2 };
The additional URL elements should be added using:
client.println("GET /zombie/arduino.php");
after a connection has been established
The final code should look like this:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,3 };
byte server[] = { 192,168,1,2 };
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
int res = client.connect(server, 80);
Serial.println(res);
if (res) {
Serial.println("connected");
client.println("GET /zombie/arduino.php");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.println("data:");
Serial.println(c);
}else{
Serial.println("NA");
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}