cumminicating with webserver

Hi all,

I was attempting to use a second arduino with ethernet shield to read the example webserver example on the arduino site which displays the analog values.

I believe that I am not using the GET function correctly, since the webclient will connect but not display anything

#include <SPI.h>
#include <Ethernet.h>
String readString, readString1;

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192, 168, 0, 19 };
byte server = { 192, 168, 0, 177 }; //

Client client(server, 80);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println(“starting simple arduino client test”);
Serial.println();
Serial.println(“connecting…”);

if (client.connect()) {
Serial.println(“connected”);
client.println(“GET / HTTP/1.1”);
client.println();
} else {
Serial.println(“connection failed”);
}
}

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

if (!client.connected()) {
client.stop();

//Serial.print(readString);
Serial.println();
Serial.println();

Serial.println(“done”);
for(;;);

}
}

I was attempting to use a second arduino with ethernet shield to read the example webserver example on the arduino site which displays the analog values.

The above is not very clear as to what you are trying to do. The below is a lan ip address and will not connect to the arduino site if that is what you are trying to do. If you are trying to connect to a server on your lan, can you connect to it and get the desired data using a browser like IE?

byte server[] = { 192, 168, 0, 177 };

In addition to the question that zoomkat asks about the IP address really being that of a server, I'm wondering what you are trying to get from the server.

   client.println("GET / HTTP/1.1");

It doesn't really look to me like you are asking for anything.

the example webserver example on the arduino site

This code is not acting as a server.

Thank you for your reply.

when i connect to the webserver example with internet explorer displays:

analog 0: XXXXX analog 1: XXXXX . . . analog 5: XXXXX

What I would like to do is use a second arduino with ethernet shield to grab this data from the first arduino with ethernet shield that has the webserver example on it.

I want the second arduino to grab this data and then this would enable it to preform certain tasks based on the data from the first arduino.

Is is possible to do this?

Is is possible to do this?

Yes. Obviously, the first step is getting the server working, which it appears that you have, since you posted some sample output.

Then, the client needs to request the same data, from the same server, that the browser requested. The server serves up exactly the same response whether the client is IE on a PC, Chrome on a Mac, Firefox on a Sun workstation, or Mozilla on a linux machine.

The client then needs to parse the response to figure out what to do.

What URL are you using in IE to access the Arduino-based server?

Hi Paul,

I access the webserver example by using http://192.168.0.177 with IE.

In my example code I tried using client.println(“GET /192.168.0.177 HTTP/1.1”); however this produced nothing.

This is the webserver example that I am using. Also the two arduinos with individual ethernet shields are connected to basic wireless router.

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

byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip = { 192,168,0, 177 };
Server server(80);

void setup()
{
server.begin();
}

void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();

// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input “);
client.print(analogChannel);
client.print(” is “);
client.print(analogRead(analogChannel));
client.println(”
");
}
break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}

Is your client able to access any web site?

I seem to recall that you need to define and use a subnet mask, on the client, for accessing other than public IP addreses.

Below is some client test code I use with a test file I have on my web site. It will print what it receives from the server to the serial monitor. You will need to change the ip addresses to what you are using. Note that this uses HTTP/1.0 instead of HTTP/1.1, as 1.1 may not be supported.

//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 208, 104, 2, 86 }; // zoomkat

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

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

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt 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.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

Thank you for the reply.

I uploaded your code and change the ip address to match mine and i get this response from the serial monitor:

starting simple arduino client test

connecting…
connected

disconnecting.

Since i am not uploading a text file with the webserver on the arduino do I need alter the GET function?

Since i am not uploading a text file with the webserver on the arduino

You're not? What are all those client.println() statements doing, then?

@zoomkat I've not got an ethernet shield, and I've never used one, but I have a question. Shouldn't this:

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

if statement be a while statement?

I would think that you would want to keep read as long as there is data to be read, just like in serial data access.

If try to connect your server I have success I get this:

starting simple arduino client test

connecting... connected HTTP/1.1 200 OK Date: Wed, 29 Dec 2010 17:36:06 GMT Server: Apache Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT Accept-Ranges: bytes Content-Length: 51 Connection: close Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works! zoomkat

disconnecting.

You may want to try the below server test code on your arduino server (changing the ip addresses, port, and such to match your system), then see if the arduino client and IE get the return message. I haven’t tested this, but it is modified from some working code I have.

//zoomkat 12-29-10
//server test code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
//from client 

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString; 
 
 //////////////////////

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print 
Serial.begin(9600); 
Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string 
readString += c; 
Serial.print(c); //print what is received to the serial momitor
} 

//if HTTP request has ended
if (c == '\n') {

///////////////
Serial.println(readString);

  //now output HTML data header
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();

  client.println("Test string returned from the arduino server");
  client.println("a little more stuff");
  client.println("later, zoomkat");
  
  delay(1);
  //stopping client
client.stop();

/////////////////////
//clearing string for next read
readString="";
  
}}}}}

I would think that you would want to keep read as long as there is data to be read, just like in serial data access.

I think in this client case the "if" is inside a continous loop checking for data as long as a connection with the server is established, which performs the same function as a "while", testing for data/no data. I'm kind of in the "if it works ok, don't modify it" mode.

Thank you for your patients and kindness.

So I am able to connect to the arduino webserver with IE but not my second arduino webclient. Just so you can see this here are both of the codes

webclient:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 117 };
//byte server[] = { 208, 104, 2, 86 }; // zoomkat
byte server[] = { 192, 168, 0, 102 }; // my server

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

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

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt 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.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

And the Webserver:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 102 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(80); //server port

String readString;

 //////////////////////

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print
Serial.begin(9600);
Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString += c;
Serial.print(c); //print what is received to the serial momitor
}

//if HTTP request has ended
if (c == '\n') {

///////////////
Serial.println(readString);

  //now output HTML data header
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();

  client.println("Test string returned from the arduino server");
  client.println("a little more stuff");
  client.println("later, zoomkat");
  
  delay(1);
  //stopping client
client.stop();

/////////////////////
//clearing string for next read
readString="";
  
}}}}}

Any advice? I am not sure why they do not talk

Please note that it is prefered to put code in code boxes using the code tags provided by the # on the post tool bar (you can use "modify" on your previous post to use the code boxes (high light the code and click the # may do it). If the client code works with an external server, and the server code works with IE as a client, then maybe it has something to do with the addressing. Just keep tinkering.

zoomkat and Paul,

Thank you for all the help. After many hours of trail and error I finally determined my problem. I had access to several different type of routers and everyone would not allow connection between the two arduinos. When I did a cross-over connect straight between the two, I had success. Is there any special router that may correct for this timing/buffer problem.

If your client accesses the server via a router, then you probably need the router's ip address like below in the client code (like in the server code). This would send the client communication to the router, which would then send the communication to the server.

byte gateway[] = { 192, 168, 0, 1 }; // internet access via router