Pages: [1]   Go Down
Author Topic: ethernet shield doesn't reads the response  (Read 890 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My sketch:
Code:
#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...
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Full Member
***
Karma: 11
Posts: 147
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: February 26, 2011, 02:12:03 pm by iempire » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

I put:

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

And all works! Thanks @DCContrarian smiley-wink
Logged

Rural Arizona
Offline Offline
Edison Member
*
Karma: 7
Posts: 1711
Incorrigible tinkerer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: