Go Down

Topic: ethernet shield doesn't reads the response (Read 974 times) previous topic - next topic

iempire

My sketch:
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
  192, 168, 0, 21 };
byte server[] = {
  188, 127, 238, 102 }; // ???????? ?? ????

Client client(server, 80);

void setup() {
  Serial.begin(9600);
}

void loop()
{
  Ethernet.begin(mac, ip);
 
  delay(1000);
 
  if (client.connect()) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /arduino/1.php HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
  }
 
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  client.stop();
}


It prints "connected" in loop only, without printing server response. Probably, client.available() may return false, but why?

All IPs are real, and you can ensure that the url http://188.127.238.102/arduino/1.php is correct.
Please, help me...

zoomkat

You may want to try the below code to see if it works for you (modify the arduino ip address for your LAN). Your code runs in a continous loop which may cause issues.

Code: [Select]

//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 }; // Arduino IP address
byte server[] = { 208, 104, 2, 86 }; // zoomkat's web site

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

DCContrarian

Ethernet.available() won't immediately return true after you send the get, so the loop will exit with the connection closed. You need to put in a delay or wait for available bytes somehow.

iempire

#3
Feb 26, 2011, 08:07 pm Last Edit: Feb 26, 2011, 08:12 pm by iempire Reason: 1
zoomkat,
It works!
Quote
starting simple arduino client test
connecting...
connected
HTTP/1.1 200 OK
Date: Sat, 26 Feb 2011 19:06:15 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.
==================================


But how to make a lot of requests in loop?

zoomkat

Quote
But how to make a lot of requests in loop?


I suggest you put client code inside a conditional statement so that it will run only when you want it to run. Servers have to run in continous loops to lisen for client request, but running a client in a tight loop can create a lot network churn that might not be desired. Just depends on what you are actually trying to accomplish.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

iempire


Quote
But how to make a lot of requests in loop?


I suggest you put client code inside a conditional statement so that it will run only when you want it to run. Servers have to run in continous loops to lisen for client request, but running a client in a tight loop can create a lot network churn that might not be desired. Just depends on what you are actually trying to accomplish.

Instead of
Code: [Select]
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }


I put:

Code: [Select]
  delay(2000);
 
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }


And all works! Thanks @DCContrarian ;)

Ran Talbott

Quote
But how to make a lot of requests in loop?

There are a couple of ways to do this.

One is to start the connection at the beginning of loop(), and do all the processing before you exit it.  Most (all??) of the Client examples do this.

If you have other things to do that are time-sensitive, though, you need to take a different approach to avoid delays while talking with the server.  It's a little more complicated, but "no pain, no gain".

What you need to do is declare a global buffer, fill it up by checking available() every pass through loop(), and acting when it fills up.  You also need to track the state of the connection: whether it's been initiated, is sending or receiving, etc.  I don't know whether there's an example available online, but you should try using the phrase "state machine" along with Ethernet-related terms to hunt for it.

Go Up