Go Down

Topic: Ethernet shield not connecting to the Webserver - new to Arduino (Read 6786 times) previous topic - next topic

venardino

Hi all:
I am new to Arduino. Needed some help on connecting to the Webserver and being able to e-mail based on a PHP script. I am using a standard script borrowed from the internet.

Here is my setup:
1. Arduino Uno R3
2. Ethernet Shield R3
3. Mac OS X 10.7.2
4. Arduino IDE 1.0.1

I have posted the code I am using, the corresponding output on the serial monitor and the PHP 'test.php' file content.

Appreciate your help in fixing this or some light on what is that I am missing here fundamentally.

*******************
Code
*******************
Code: [Select]

#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 6 }; //my router IP
byte server[] = {31, 170, 160, 99}; // My webserver IP

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
[size=10pt][size=10pt][/size][/size]  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET /test.php HTTP/1.0");
    client.println();
    Serial.println("Sent the http request");
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}


******************
Begin Output
******************
connected
Sent the http request
connecting...
connected
Sent the http request
HTTP/1.1 408 Request Time-out
Date: Sun, 26 Aug 2012 13:43:17 GMT
Server: Apache
Content-Length: 223
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Time-out</title>
</head><body>
<h1>Request Time-out</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
</body></html>

disconnecting.

******* End Output ***********

********************************
test.php file
********************************
<?php
  echo("<p> A sample string as test message</p>");
?>
********************************

SurferTim

You are missing a CR/LF in your code. You must send a blank line to let the server know you are finished sending the request header. Try this instead.
Code: [Select]
    client.println("GET /test.php HTTP/1.0\r\n");


venardino

Hi SurferTim:
Thank you for a quick response. I did try your suggestion and here is the output. Please validate and let me know if there is anything still missing. Appreciate that.

connecting...
connected
Sent the http request
HTTP/1.1 302 Found
Date: Sun, 26 Aug 2012 14:09:07 GMT
Server: Apache
Location: http://error404.000webhost.com/?
Content-Length: 216
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://error404.000webhost.com/?">here</a>.</p>
</body></html>

disconnecting.

SurferTim

#3
Aug 26, 2012, 04:16 pm Last Edit: Aug 26, 2012, 04:18 pm by SurferTim Reason: 1
I will guess that is a virtual hosting web server. It is redirecting you to a error 404 (file not found) page. If that is the case, then you will need to send the Host parameter also. Like this:
Code: [Select]
client.println("GET /test.php HTTP/1.0");
client.println("Host: www.mydomain.com"\r\n);

Replace www.mydomain.com with your domain name. Note the double CR/LF is after the Host now.

edit: This is a thread that should be in networking. I expect we will be moved there.

venardino

Yes, it is a virtual hosting webserver. Tried your suggestion, still the same error.

Code: [Select]
   
client.print("GET /test.php HTTP/1.0");
client.println("Host: http://venard.hostzi.com\r\n");

dxw00d

SurferTim's example didn't include 'http://' for the host.

Yours:
Code: [Select]
client.println("Host: http://venard.hostzi.com\r\n");

SurferTim's:
Code: [Select]
client.println("Host: www.mydomain.com"\r\n);

venardino

Hi dxw00d:
Thank you for your observation. I did try SurferTim's suggestion and below is the outcome

1. Using this line in the code, I got the following compilation error (in red), hence had to include it within the quotes which works fine. Please let me know if that is correct or am I missing something.

client.println("Host: www.mydomain.com"\r\n);


sketch_aug24c:25: error: stray '\' in program
sketch_aug24c:25: error: stray '\' in program
sketch_aug24c.cpp: In function 'void setup()':
sketch_aug24c:25: error: expected `)' before 'r'


2. I tried both the following and obtained same results

client.println("Host: http://venard.hostzi.com\r\n");
client.println("Host: www.venard.hostzi.com\r\n");

SurferTim

You are getting close! You have tried all but the correct combination.
Code: [Select]
client.println("GET /test.php HTTP/1.0");
client.println("Host: venard.hostzi.com\r\n");


edit: I moved the double quote. I did not notice that until I checked it.

dxw00d

Quote
I moved the double quote. I did not notice that until I checked it.


I didn't spot that either.

venardino

Hi SureferTim:
I did try with the suggested code but the result is the same
Code: [Select]
client.println("GET /test.php HTTP/1.0");
client.println("Host: venard.hostzi.com\r\n");


connecting...
connected
Sent the http request
HTTP/1.1 302 Found
Date: Sun, 26 Aug 2012 15:13:14 GMT
Server: Apache
Location: http://error404.000webhost.com/?
Content-Length: 216
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://error404.000webhost.com/?">here</a>.</p>
</body></html>

disconnecting.

SurferTim

OK. Maybe it won't take HTTP/1.0. It keeps responding with HTTP/1.1, so let's try that.
Code: [Select]
client.println("GET /test.php HTTP/1.1");
client.println("Host: venard.hostzi.com");
client.println("Connection: close\r\n");


SurferTim

My bad. Always check the simple stuff first. Are you using this ip for the server?
Code: [Select]
byte server[] = {31, 170, 160, 99}; // My webserver IP

I show your domain hosted at this ip
Code: [Select]
byte server[] = {31, 170, 161, 36}; // My webserver IP


I used nslookup to find your ip.

venardino

1. What you said is correct, i.e the hostname is resolving to a different IP {31, 170, 161, 36} than what I am currently using which is {31, 170, 160, 99}. However, I did try that earlier and got the same response from both the IPs.

2. With HTTP/1.1 here is the response.
Code: [Select]

client.print("GET /test.php HTTP/1.1");
client.println("Host: venard.hostzi.com");
client.println("Connection: close\r\n");

************************
connecting...
connected
Sent the http request
HTTP/1.1 400 Bad Request
Date: Sun, 26 Aug 2012 15:42:30 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

disconnecting.
**************************


3. With HTTP/1.0 here is the response.
Code: [Select]
client.print("GET /test.php HTTP/1.0");
client.println("Host: venard.hostzi.com");
client.println("Connection: close\r\n");


************************
connecting...
connected
Sent the http request
HTTP/1.1 302 Found
Date: Sun, 26 Aug 2012 15:43:35 GMT
Server: Apache
Location: http://error404.000webhost.com/?
Content-Length: 216
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://error404.000webhost.com/?">here</a>.</p>
</body></html>

disconnecting.
**********************

SurferTim

I used this server:
Code: [Select]
byte server[] = {31, 170, 161, 36}; // My webserver IP

with this request
Code: [Select]
client.println("GET /test.php HTTP/1.0");
client.println("Host: venard.hostzi.com\r\n");

...and it returned, among other things, "A sample string as test message". Is that what it is supposed to say? My web browser says that is correct.


venardino

Hi ServerTim:
Great! That worked for me too.

Not sure what the problem was so far. Did you do some magic?  :). It will be nice if you could share what could've gone wrong and how did it get addressed/fixed finally. I guess one of the factors being the IP address set to what it actually resolved to. The one I've used {31, 170, 160, 99} was given to me by the hosting provider.

Appreciate all the help and persistence all through till it worked.

Thanks and regards.


Here is the output at my end

-----------------------
connecting...
connected
Sent the http request
HTTP/1.1 200 OK
Date: Sun, 26 Aug 2012 16:14:42 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 193
Connection: close
Content-Type: text/html

<p> A sample string as test message</p>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

disconnecting.
-----------------------------

Go Up