Repeated request from web[Solved]

hi there,

Im trying to read data from a text file from a website. The Arduino uses a HTTP GET request to get info from a text file on the web and depending on whats in the file led1 or led2 is lit. The program works ok but I want to have the HTTP request to occur say every 15 seconds how can I achieve this. any pointers would be appreciated thank you.
cheers

#include <SPI.h>
#include <WiFi.h>


char ssid[] = "ThomsonEEB616";      //  your network SSID (name)  
char pass[] = "FF07A32DE7";   // your network password

int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

char server[] = "www.****.com"; // name address for Google (using DNS)

String location= "/datatest.txt HTTP/1.0";

char material;

int led1 = 8;

int led2 = 9;

WiFiClient client;


void setup() 
{
  
  //Initialize serial and wait for port to open:

Serial.begin(9600);
  
pinMode(led1,OUTPUT);

pinMode(led2,OUTPUT);

digitalWrite(led1, LOW);

digitalWrite(led2, LOW);

// check for the presence of the shield:  
  
if (WiFi.status() == WL_NO_SHIELD) 
{

Serial.println("WiFi shield not present");  
// don’t continue:

while(true);  
}

// attempt to connect to Wifi network:

while ( status != WL_CONNECTED) {

Serial.print("Attempting to connect to SSID: ");

Serial.println(ssid);

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:

status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:

delay(10000);
}
  
Serial.println("Connected to wifi");

//printWifiStatus();

Serial.println("\nStarting connection to server...");

// if you get a connection, report back via serial:

if (client.connect(server, 80)) 
{

Serial.println("connected to server");

// Make a HTTP request:

client.print("GET ");

client.println(location);

client.println("Host: *****.com");

// client.println(“Connection: close”);

client.println();  
}
else
{

Serial.println("connection failed");

}

}
  

void loop()
{
  while (client.available()) 
  {

 material = client.read();

  Serial.write(material);
  
  if (material == '50')
  {
  digitalWrite(led1, HIGH);
  }
  else
  {
    digitalWrite(led2, HIGH);
  }
  }
}

As I see it, you should move most of your code in setup() to an external function, call it something like webConnect(). Then reduce your setup() to the bare necessity.

Now you can call webConnect() function from setup() on a power up or reset. And you can also setup a 15S counter within your loop(), then call the webConnect() each time a 15S interval elapses.

The whole setup is very close to the concept of the BWoD demo sketch, except you do a call to the web, instead of changing state on a LED.

char server[] = "www.****.com"; // name address for Google (using DNS)

Did you really feel it necessary to redact google? Or were you just too lazy to fix the useless comment?

String location= "/datatest.txt HTTP/1.0";

Useless waste of resources. A char * would do the same thing, without the overhead of wrapping the string in a class instance.

  if (material == '50')

Please post a picture of your keyboard with the ONE key you pressed to get the ONE character in the single quotes circled.

Simple client code that night be of interest.

//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString 

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // myIP server test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  Serial.println();
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.1"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    readString += c; //places captured byte in readString
  }

  //Serial.println();
  client.stop(); //stop client
  Serial.println("client disconnected.");
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor 
  Serial.println();  
  Serial.println();
  Serial.println("End of readString");
  Serial.println("==================");
  Serial.println();
  readString=""; //clear readString variable

}