Go Down

Topic: Using the webclient example sketch (Read 1 time) previous topic - next topic

psfletcher

Hi,
I'm trying to get the Ethernet Sheild talking to the outside world.
I've tried running the webclient sketch with no success, it doesn't make a connection, i've tried editing it to add the subnet mask and gateway still no success!
Any ideas?
Also is there a way of using an URL rather than a IP address?
It would make calling a page much nicer!

Thanks everyone!

Pete

Code: [Select]

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 16, 1, 59 };
byte gateway[] = { 172, 16, 1, 205 };
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

void setup()
{
 Ethernet.begin(mac, ip, gateway, subnet);
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
 if (client.connect()) {
   Serial.println("connected");
   client.println("GET /search?q=arduino HTTP/1.0");
   client.println();
 } 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(;;)
     ;
 }
}


psfletcher

Hi Again,

I've tired using an internal Apache server i have access to, i now get a responce!!! But i'm told its a bad one :-( I've put the code and the serial responce below, if anyone has any ideas what i am doing wrong i would love to here them,

Thanks

Pete

Code: [Select]

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 16, 1, 59 };
byte gateway[] = { 172, 16, 1, 205 };
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 172, 16, 1, 57 }; // Google

Client client(server, 80);

void setup()
{
 Ethernet.begin(mac, ip, gateway, subnet);
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
 if (client.connect()) {
//   Serial.println("connected");
//   client.println("GET leviathan/bigd/psf/links/links.php HTTP/1.0");
//   client.println();
 
 Serial.println("connected");
 client.print("GET leviathan/bigd/psf/links/jquery.html HTTP/1.1\n");
 client.print("Host: www.milneandfletch.co.uk\n\n");
 client.println();

} 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(;;)
     ;
 }
}


Code: [Select]

connecting...
connected
HTTP/1.1 400 Bad Request
Date: Tue, 30 Mar 2010 15:10:12 GMT
Server: Apache/2.2.8 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8g PHP/5.2.6
Content-Length: 353
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>
<hr>
<address>Apache/2.2.8 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8g PHP/5.2.6 Server at www.milneandfletch.co.uk Port 80</address>
</body></html>

disconnecting.


MLeBlanc

I've noticed that the IP address given for Google no longer works. Try 74, 125, 39, 103.

psfletcher

Hi,
Thanks for the advice!
I've got it working, a couple things i found out,
  • You need to define the gateway and subnet mask in the ethernet begin command
  • You need to add the Host Website (the place where your trying to connect to
  • You need to added the User Agent
  • The syntax needs to be correct! Even down to the space between .php and the HTTP 1.1 info

Code is below, your now able to change the client.print to add ?item=134&data=345 as you want.

Pete

Code: [Select]


#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 172, 16, 1, 59 };
byte gateway[] = { 172, 16, 1, 205 };
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 172, 16, 1, 57 }; // Leviathan

Client client(server, 80);

void setup()
{
 Ethernet.begin(mac, ip, gateway, subnet);
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
 if (client.connect()) {
//   Serial.println("connected");
//   client.println("GET leviathan/bigd/psf/links/links.php HTTP/1.0");
//   client.println();
 
 Serial.println("connected");
 //client.print("GET bigd/psf/test.html HTTP/1.1\n");
 //client.print("Host: www.milneandfletch.co.uk\n\n");
 //client.println();
     Serial.println("GET request to retrieve");

     client.print("GET /bigd/psf/");
     client.print("test.php ");
     client.print("HTTP/1.1\n"); //Ht: URL or IP of webserver
     client.print("Host: 172.16.1.57\n"); //Ht: URL or IP of webserver
     client.print("User-Agent: Arduino (Arduino Test Code)");
     client.println("\n");
 
 

} 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(;;)
     ;
 }
}

Go Up