WiFly reading file on server x number of times instead of one time

Hi Everyone,

I'm using the following WiFly sketch to communicate with my server as a client. However, WiFly only contacts the server once, retrieves data, and then does not reconnect. I would like WiFly to indefinitely contact the server, say every 2 seconds, to determine if a value has changed. My goal is to light an LED on my Arduino from my iPhone. What do I need to change in the sketch?

Thank you for your help in advance

Andrew

Below is the code I'm using:

// (Based on Ethernet's WebClient Example)

#include "WiFly.h"

#include "Credentials.h"

byte server[] = { 66, 96, xx, xx }; // Google

//Client client(server, 80);

Client client("xxxxxxxxxxxx.com", 80);

void setup() {

Serial.begin(9600);

WiFly.begin();

if (!WiFly.join(ssid, passphrase)) {
Serial.println("Association failed.");
while (1) {
// Hang on failure.
}

}

Serial.println("Can u reach me ?");
Serial.print("IP: ");
Serial.println(WiFly.ip());

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

if (client.connect()) {
Serial.println("connected");
client.println("GET xxxxxxxxxxx.com 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(;:wink:
; }

}

This is the response I get in the Serial Monitor, but I only get one and then WiFly disconnects...

Can u reach me ?
IP: 192.168.1.104
connecting...
connected
HTTP/1.1 200 OK
Date: Sat, 09 Jul 2011 22:51:34 GMT
Content-Type: text/plain
Connection: close
Server: Apache/Nginx/Varnish
Last-Modified: Thu, 17 Mar 2011 18:03:59 GMT
ETag: "b4ad73e1-1e-49eb17c581e59"
Cache-Control: max-age=14400, public
Expires: Sun, 10 Jul 2011 01:47:06 GMT
Content-Length: 30
Accept-Ranges: bytes
Age: 3868

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

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;smiley-wink
      ;  }

}

So, when loop gets called, you test for the presence of data from the client. If there is data, you read and print one character. When all the data has been extracted from the client, the client closes the connection, at which point you go into an infinite do-nothing loop, rather than looking for another client to connect. Why?

When all the data has been extracted from the client, the client closes the connection, at which point you go into an infinite do-nothing loop, rather than looking for another client to connect. Why?

"Maybe in order to understand mankind, we have to look at the word itself: "Mankind". Basically, it's made up of two separate words - "mank" and "ind". What do these words mean ? It's a mystery, and that's why so is mankind." - Deep Thoughts by Jack Handy

I'm not sure why the client disconnects, is the loop supposed to keep pinging the server?

I'd like the client to remain connected to the server or at least connect every few seconds to poll the server.

I'd like the client to remain connected to the server

That is not the nature of a client/server architecture when applied to internet access.

or at least connect every few seconds to poll the server.

So, loop() should make the call to client.connect(), not setup(). And get rid of the infinite loop when the client goes away.

Thanks for your help Paul. I'll experiment with this tonight and let you know how it goes.

I was able to poll the server continously. I'll post the code I used later.