Ethernet shield not connecting to the Webserver - new to Arduino

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


#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

408 Request Time-out

Request Time-out

Server timeout waiting for the HTTP request from the client.

disconnecting.

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


test.php file


<?php echo("

A sample string as test message

"); ?>

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.

    client.println("GET /test.php HTTP/1.0\r\n");

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

302 Found

Found

The document has moved here.

disconnecting.

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:

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.

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

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

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

Yours:

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

SurferTim's:

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

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'

  1. 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");

You are getting close! You have tried all but the correct combination.

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.

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

I didn't spot that either.

Hi SureferTim:
I did try with the suggested code but the result is the same

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

302 Found

Found

The document has moved here.

disconnecting.

OK. Maybe it won't take HTTP/1.0. It keeps responding with HTTP/1.1, so let's try that.

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

My bad. Always check the simple stuff first. Are you using this ip for the server?

byte server[] = {31, 170, 160, 99}; // My webserver IP

I show your domain hosted at this ip

byte server[] = {31, 170, 161, 36}; // My webserver IP

I used nslookup to find your ip.

  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.

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

400 Bad Request

Bad Request

Your browser sent a request that this server could not understand.

disconnecting.


  1. With HTTP/1.0 here is the response.
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

302 Found

Found

The document has moved here.

disconnecting.


I used this server:

byte server[] = {31, 170, 161, 36}; // My webserver IP

with this request

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.

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

A sample string as test message

disconnecting.

That is what I got! 8)

No magic here. I am not the Wizard of Oz. I just used what I know about the internet and tcp/ip protocols.

You may want to change that to a dns type server. You can use a domain name for a server now, instead of an ip address. I have not tried it yet, but that would eliminate the possibility of your web host changing the ip of the server. They do that occasionally to do maintenance on the servers.

SurferTim:
I used this server:

byte server[] = {31, 170, 161, 36}; // My webserver IP

with this request

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.

Thanks for sharing this trick, you've saved me a headache :wink:

I want control device over internet.
I have server host at 000webhost.com . server name: http://tiendt343.comli.com
Ardui connect sheid ethenet is client.
i want ask? i problemed as:
i connected to server.
But when i get request , Server reply as is Form html.

 connecting...
connected
HTTP/1.1 200 OK
Date: Fri, 03 May 2013 00:51:01 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 279
Connection: close
Content-Type: text/html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>

@01$</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

disconnecting.

Data then i receive is " Form web html" as top.
But i want send 1 request to server and receive 1 value when i request from server. How do ?
this is code arduino

// Chuong trinh Client ket noi webserver
// Ngay bat dau: 23/4/2013
// Arthor : Bui Duy Tien
#include <SPI.h>
#include <Ethernet.h>
// Khai bao dia chi MAC cua Ethernet Shield.
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Khai bao server name k?t n?i
char serverName[] = "http://tiendt343.comli.com";
// 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);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(100);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /vlir/index.php?device=hardware&temp=30&as=200 HTTP/1.0");
    client.println("Host: tiendt343.comli.com");
    client.println("Connection: close\r\n");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and 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);
  }
}

and this code php .

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>

<?php

/**
 * @author tiendientu343
 * @copyright 2013
 */

mysql_connect('localhost', 'root', '');//K?t n?i CSDL
mysql_select_db('tien');//Ch?n CSDL

/**
 * ?i?u khi?n thi?t b?, t? web l?u vào CSDL
 */ 
if (isset($_GET['sub'])){//?ã submit d? li?u trong form
    mysql_query("UPDATE thietbi SET dieukhien='".$_GET['rd1']."' WHERE id='1'");
    mysql_query("UPDATE thietbi SET dieukhien='".$_GET['rd2']."' WHERE id='2'");
    //header("location:?device=webservice");
}
/**
 * L?y d? li?u t? CSDL
 */
$tttb1 = "";
$tttb2 = "";
$txttemp = "";
$txtas = "";
$sql = mysql_query("SELECT * FROM thietbi"); //??c t? CSDL
if ($row = mysql_fetch_object($sql)){
    $tttb1 = $row->dieukhien;
    $txttemp = $row->giatri;
}
if ($row = mysql_fetch_object($sql)){
    $tttb2 = $row->dieukhien;
    $txtas = $row->giatri;
}

/**
 * 
 */  
if (isset($_GET['device'])){//Ki?m tra thi?t b? truy c?p
    if ($_GET['device']=="hardware"){
        /**
         * Ph?n c?ng truy c?p
         * http://localhost/tien/?device=hardware&temp=30&as=400
         */
        if (isset($_GET['temp'])){
            mysql_query("UPDATE thietbi SET giatri='".$_GET['temp']."' WHERE id='1'");    
        }
        if (isset($_GET['as'])){
            mysql_query("UPDATE thietbi SET giatri='".$_GET['as']."' WHERE id='2'");    
        }
        echo '@'.$tttb1.'&tttb2='.$tttb2.'

;//response
       
        /**
        * Xem trên web
        * http://localhost/tien/?device=webservice
        */ 
    }elseif ($_GET['device']=="webservice"){
        ?>
       
       


           
               
               
               
               
               
               
           
           
               
               
               
               
               
           
       
DKTB1:
                    B?t<input type="radio"  name="rd1" value="1" <?php if ($tttb1==1) echo 'checked="checked"'; ?> />
                    T?t<input type="radio" name="rd1" value="0" <?php if ($tttb1==0) echo 'checked="checked"'; ?>/>
               
Nhi?t ??:
                   
               

                   
               
DKTB2:
                    B?t<input type="radio"  name="rd2" value="1" <?php if ($tttb2==1) echo 'checked="checked"'; ?>/>
                    T?t<input type="radio" name="rd2" value="0" <?php if ($tttb2==0) echo 'checked="checked"'; ?>/>
               
Ánh sáng:
                   
               

       
        <?php     } } ?>

```

Your shield is connecting and getting this as a return from the php server:

        echo '@'.$tttb1.'&tttb2='.$tttb2.'

It apparently is printing the "@" and the two values ("01") with the "$", but I don't know why it is not printing the "&ttb2=" part in the middle though.

You should start another topic in networking.
;//response


It apparently is printing the "@" and the two values ("01") with the "$", but I don't know why it is not printing the "&ttb2=" part in the middle though.

You should start another topic in networking.

response is " @01$" because i want split form html from server same is uart form. but i want get value? how?