Why is it that I cannot seem to read the contents of a web page outside of the main loop. My code looks like this:
void function(){
while(client.available()){
char c = client.read();
Serial.print(c);
}
}
I call my function from void setup(). All of the examples I have seen show the client.read() from void loop(), but I certainly hope that there is a way to do it outside of there...
Yes, I should have been more specific. It really looks more like this:
Ethernet.begin(mac);
Serial.println(Ethernet.localIP());
Serial.println("Attempting to connect");
client.connect(time, 80);
client.println("GET /timezone.cgi?Central/d/-6 HTTP/1.0");
client.println();
while(client.available()){
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
Like I said, I have no trouble retrieving the webpage when I am calling client.read from void loop.
Also, I was looking through EthernetClient.h and noticed
virtual int read(uint8_t *buf, size_t size);
but I'm not sure what those parameters mean. I also looked at the function definition in EthernetClient.cpp and that didn't clear it up for me.
I have seen that the read buffer is only 2k, but that would be enough for me if I could just read 2k once and be done with it...
Posting snippets will not allow us to answer your question(s). Typically, Ethernet.begin() happens in setup().
Then, in loop(), you determine that some client connected to your server. If you want a function to handle that connection, you need to pass the client object to that function. You do not typically test for a client connection in another function, unless that function is called often enough. In which case, having it as a function doesn't make a lot of sense.
If your Arduino IS a client, it can make a connection to a server in a function. What, exactly, you are trying to do isn't clear from your snippets.
I suspect the problem is that the server has not responded before you reach this line:
while(client.available()){
Since the data has not arrived your read loop immediately exits. Perhaps you should put in a delay before assuming that all of the characters have arrived.
You could wait a couple seconds, or do what the TCP protocol recommends. The server will close the connection when it is finished sending packets.
// wait for another packet
while(client.connected()) {
// read this packet
while(client.available()){
char c = client.read();
Serial.print(c);
}
}
// close the connection after the server closes its end
client.stop();
This does not have a timeout for a connection failure. Here is that part:
edit: This is most important if using Arduino server code like this.
If you notice in the server response to the client, every client.write() call is a separate packet. You must read the current packet (empty the w5100 rx buffer) before the server will send another packet. When you read the last packet from the server, the server will close the connection.
I apologize for not posting all of my code. John and Tim were both correct in their assessment however, and Tim's solution worked perfect for me.
Thanks everyone.