2

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(;;)
 ;
 }
}
Chris Stratton
5,41120 silver badges40 bronze badges
asked Mar 6, 2015 at 16:36
1
  • "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. Commented Mar 6, 2015 at 19:42

1 Answer 1

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(;;)
 ;
 }
}
answered Mar 6, 2015 at 16:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.