WiFly - Server incorrectly displays page upon refresh

Hello

I have a WiFly server with accelerometer data within HTML code being written to a page when i connect to the WiFly shield through the local network (192.168.X.X:2000).
The first time you enter the web address, everything works fine. However, if you refresh the page some of the HTML code is missing, resulting in the page not displaying correctly.

This is my Arduino Code:

#include <SPI.h>
#include <WiFly.h>
// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>
// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

WiFlyServer server(2000);
const int pwmA = 3;
const int pwmB = 5;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 4;
const int dirB = 2;
const int speed = 255;
String readString; //create readString class
void setup() {
  //Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
  //enable serial data print
  Serial.begin(9600);
  Serial.println("Started");
// SET UP ACCELEROMETER
 Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
 
 WiFly.begin();
  if (!WiFly.join("BTHub3-HKP4", "9d7cdbc6ce")) {
    while (1) {
      // Hang on failure.
      Serial.print("failed");
    }
  }
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  
  server.begin();
}
void loop(){
  
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: 2? 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0457;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  //Output(raw, scaled, heading, headingDegrees);
  
  //WIFI SECTION
  // Create a client connection
  delay(500);
  WiFlyClient client = server.available();
  if (client) {
   //Serial.println("Client connected"); 
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 200) {
          //store characters to string
          readString += c;
        }
        //if HTTP request has ended
        if (c == '\n') {

          //now output HTML data header
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
         client.println("<head>");
         client.println("<style media=\"screen\" type=\"text/css\">");
         client.println("#compass {");
         client.println("width: 380px;");
         
         client.println("height: 380px;");
        
         client.println("background-image:url('http://localhost/compass.jpg');");
     
         client.println("position: relative;");
        client.println("}");
        client.println("#arrow {");
         client.println("width: 300px;");
         client.println("height: 150px;");
         client.println("background-image:url('http://localhost/arrow.png');");
         client.println("position: absolute;");
         client.println("top: 115px;");
         client.println("left: 40px;");
         
         client.print("-moz-transform:rotate(");
         client.print(headingDegrees + 90);
         client.println("deg);");
         
  
         client.println("}");
       client.println("</style>");
        
          
          client.print("</head>");
          client.print("<body>");
          // print something, in HTML format:
          client.print("WiFly Webserver Running!");
          
          client.println("
");
          client.print("IP: ");
          client.println(WiFly.ip());
          client.println("");
          //Serial.print("readString = ");
          //Serial.println(readString); //print to serial monitor for debugging
          client.println("
");
          client.print("Raw:\t");
   client.print(raw.XAxis);
   client.print("   ");   
   client.print(raw.YAxis);
   client.print("   ");   
   client.print(raw.ZAxis);
   client.print("   \tScaled:\t");
   
   client.print(scaled.XAxis);
   client.print("   ");   
   client.print(scaled.YAxis);
   client.print("   ");   
   client.print(scaled.ZAxis);

   client.print("   \tHeading:\t");
   client.print(heading);
   client.print(" Radians   \t");
   client.print(headingDegrees);
   client.println(" Degrees   \t");
   
   client.println("<div id=\"compass\">");
   client.println("<div id=\"arrow\"></div>");
   client.println("</div>");
   client.print("</body>");




          delay(1);
          //stopping client
          client.stop();
           //Serial.print("readString = ");
          //Serial.println(readString); //print to serial monitor for debugging
          Serial.println("Stopped client");

          ///////////////////// control arduino pin
MORE CODE BELOW FOR MOTOR CONTROL (shouldnt be relevant)

The code client.print part of the code, is the HTML that will be printed to the webpage upon connection.
It prints the accelerometer data, but also prints a compass in CSS embedded directly.

Upon first connection the HTML code of the website looks as expected, and the website displays correctly:

<head>
<style media="screen" type="text/css">
#compass {
width: 380px;
height: 380px;
background-image:url('http://localhost/compass.jpg');
position: relative;
}
#arrow {
width: 300px;
height: 150px;
background-image:url('http://localhost/arrow.png');
position: absolute;
top: 115px;
left: 40px;
-moz-transform:rotate(117.84deg);
}
</style>
</head><body>WiFly Webserver Running!

IP: 
192.168.1.72



Raw:	344   162   -312   	Scaled:	316.48   149.04   -287.04   	Heading:	0.49 Radians   	27.84 Degrees   	
<div id="compass">
<div id="arrow"></div>
</div>
</body>

Which displays the site correctly. Its worth noting that it takes quite a while to load the page. Im not sure if theres a more efficient way of displaying the webpage.

Once I refresh the page, the webpage displays incorrectly, and by going to 'view page source' i can see that it has missed out some of the HTML code!

<head>
<style media="screen" type="text/css">
#compass {
width: 380px;
height: 3
lerometer
position: relative;
}
#arrow {
width: 300px;
height: 150px;
background-image:url('http://localhost/arrow.png');
position: absolute;
top: 115px;
left: 40px;
-moz-transform:rotate(117.45deg);
}
</style>
</head><body>WiFly Webserver Running!

IP: 
192.168.1.72



Raw:	348   161   -314   	Scaled:	320.16   148.12   -288.88   	Heading:	0.48 Radians   	27.45 Degrees   	
<div id="compass">
<div id="arrow"></div>
</div>
</body>

At the very top is the bit that is incorrectly printed.
Notice how is says 'lerometer' like its reading from somewhere else, getting interference from something!

I think theres something very strange going on here.
Just on a side note, I had to decrease the amount of HTML code and cut some bits out, because for some reason when viewing the serial output through the USB port, it wouldn't print the I.P address when Serial.println(WiFly.ip()); at the beginning of the code at the top, and just displayed a load of gibberish.

I cant understand why this is happening, has anyone ever had this issue? I cant be the only one printing more than 15 lines of HTML code.

With the new WIFly update by roving networks to 4.0 for the RN-131C, now supports 'page files for servers'. Its fairly new update only released 3 days ago, but im not sure if this means i can just upload an index.html file to my WiFly server, because that would be ideal!
Here was there official statement 'Added support for Multiple Image Format (.mif) files to download multiple images. This new file format contains the firmware image, applications such as WPS, EAP, web server, and supporting page files.'

Sorry for the long post! Hope someone can help me out here, this is for my dissertation and the deadline is getting nearer and nearer!

Thanks alot

I would suspect the String data type.

String readString; //create readString class

It has caused some really strange fails. You should replace it with a character array. I would use something like this.

char readString[128]; //create readString array

  // then later in your code
  WiFlyClient client = server.available();
  if (client) {
    // declare readCount and initialize to zero
    int readCount = 0;
   //Serial.println("Client connected"); 
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readCount < 127) {
          //store characters to array
          readString[readCount] = c;
          readCount++;
          // add terminating zero
          readString[readCount] = 0;
        }

Thanks for the reply

Im not to sure how your code works.

I have implemented it in my code, but it takes about 20 seconds to load the page, and then it doesn't refresh, it gets stuck, keeps loading.

/*
 * Web Server
 *
 * (Based on Ethernet's WebServer Example)
 *
 * A simple web server that shows the value of the analog input pins.
 */

#include <SPI.h>
#include <WiFly.h>
// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>
// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

WiFlyServer server(2000);
const int pwmA = 3;
const int pwmB = 5;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 4;
const int dirB = 2;
const int speed = 255;
String readString; //create readString class
void setup() {
  //Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
  //enable serial data print
  Serial.begin(9600);
  Serial.println("Started");
// SET UP ACCELEROMETER
 Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
 
 WiFly.begin();
  if (!WiFly.join("BTHub3-HKP4", "9d7cdbc6ce")) {
    while (1) {
      // Hang on failure.
      Serial.print("failed");
    }
  }
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  
  server.begin();
}
void loop(){
  
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: 2? 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0457;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  //Output(raw, scaled, heading, headingDegrees);
  
  //WIFI SECTION
  // Create a client connection
  delay(500);
  WiFlyClient client = server.available();
  if (client) {
   //Serial.println("Client connected"); 
    // declare readCount and initialize to zero
    int readCount = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readCount < 127) {
          //store characters to array
          readString[readCount] = c;
          readCount++;
          // add terminating zero
          readString[readCount] = 0;
        }
        //if HTTP request has ended
        if (c == '\n') {

          //now output HTML data header
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
         client.println("<head>");
         client.println("<style media=\"screen\" type=\"text/css\">");
         client.println("#compass {");
         client.println("width: 380px;");
         client.println("height: 380px;");
         client.println("background-image:url('http://localhost/compass.jpg');");
         client.println("position: relative;");
        client.println("}");
        client.println("#arrow {");
         client.println("width: 300px;");
         client.println("height: 150px;");
         client.println("background-image:url('http://localhost/arrow.png');");
         client.println("position: absolute;");
         client.println("top: 115px;");
         client.println("left: 40px;");
         
         client.print("-moz-transform:rotate(");
         client.print(headingDegrees + 90);
         client.println("deg);");
         client.println("}");
       client.println("</style>");
          client.print("</head>");
          client.print("<body>");
          // print something, in HTML format:
          client.print("WiFly Webserver Running!");
          client.println("
");
          client.print("IP: ");
          client.println(WiFly.ip());
          client.println("");
          //Serial.print("readString = ");
          //Serial.println(readString); //print to serial monitor for debugging
          client.println("
");
          client.print("Raw:\t");
   client.print(raw.XAxis);
   client.print("   ");   
   client.print(raw.YAxis);
   client.print("   ");   
   client.print(raw.ZAxis);
   client.print("   \tScaled:\t");
   
   client.print(scaled.XAxis);
   client.print("   ");   
   client.print(scaled.YAxis);
   client.print("   ");   
   client.print(scaled.ZAxis);

   client.print("   \tHeading:\t");
   client.print(heading);
   client.print(" Radians   \t");
   client.print(headingDegrees);
   client.println(" Degrees   \t");
   
   client.println("<div id=\"compass\">");
   client.println("<div id=\"arrow\"></div>");
   client.println("</div>");
   client.print("</body>");

          delay(1);
          //stopping client
          client.stop();
           //Serial.print("readString = ");
          //Serial.println(readString); //print to serial monitor for debugging
          Serial.println("Stopped client");

          ///////////////////// control arduino pin
         
       cutt out, too long


          //clearing string for next read
          readString="";
          //Serial.println("Cleared readString");

        } // end if HTTP request is ended
      } // end if client available
    } // end if client connected
  } // end if client
} // end loop

I have included all my code, at the bottom is the readString =""
which i cut out in the last code i pasted.

Is there any further coding i need to do to make this work?
Thanks, much appreciated

This is not right:

        //if HTTP request has ended
        if (c == '\n') {

You are probably leaving the rest of the client request in the rx buffer. If that is the case, the connection/socket will not close properly. You must read the request until you get a blank line (double cr/lf), not just a new line.

Thanks for the help, im unfamiliar with this notation '(double cr/lf)
\n is all i can find on google looking for blank line notations.

thanks

That is correct. Two of them together. But there may be a carriage return between them.
carriage return(\r) - line feed(\n) - carriage return(\r) - line feed(\n)

Thanks for the help.

Sorry, im a noob. i don't know how that works.

i take it its not simply if (c == '\r\n'\r\n) {

So far ive been using coding examples to make everything work, and ive never had the chance to properly learn C++.

Thanks again

Take a look here. It has the correct way to get the request. It is for the ethernet shield, but the transport is transparent at this point.
http://playground.arduino.cc/Code/WebServerST
This is the important line:

        if (c == '\n' && currentLineIsBlank) {

If you follow how the value of currentLineIsBlank is determined, that may help.

Does a small web page refresh properly?

Cheers, the link was pretty helpfull, however its still not working. I can load it fine, but refreshing it doesn't work at all.

Heres how i have changed my code;

#include <SPI.h>
#include <WiFly.h>
// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>
// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;

WiFlyServer server(2000);
const int pwmA = 3;
const int pwmB = 5;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 4;
const int dirB = 2;
const int speed = 255;
String readString; //create readString class
void setup() {
  //Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
  //enable serial data print
  Serial.begin(9600);
  Serial.println("Started");
// SET UP ACCELEROMETER
 Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
  compass = HMC5883L(); // Construct a new HMC5883 compass.
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
 
 WiFly.begin();
  if (!WiFly.join("BTHub3-HKP4", "9d7cdbc6ce")) {
    while (1) {
      // Hang on failure.
      Serial.print("failed");
    }
  }
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  
  server.begin();
}
void loop(){
  
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: 2? 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0457;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  //Output(raw, scaled, heading, headingDegrees);
  
  //WIFI SECTION
  // Create a client connection
  delay(500);
  WiFlyClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
   //Serial.println("Client connected"); 
    // declare readCount and initialize to zero
    int readCount = 0;
    while (client.connected()) {
      while (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (currentLineIsGet && readCount < 127) {
          //store characters to array
          readString[readCount] = c;
          readCount++;
          // add terminating zero
          readString[readCount] = 0;
        }
        //if HTTP request has ended
        if (c == '\n' && currentLineIsBlank) {

          //now output HTML data header
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
         client.println("<head>");
         client.println("<style media=\"screen\" type=\"text/css\">");
         client.println("#compass {");
         client.println("width: 380px;");
         client.println("height: 380px;");
         client.println("background-image:url('http://localhost/compass.jpg');");
         client.println("position: relative;");
        client.println("}");
        client.println("#arrow {");
         client.println("width: 300px;");
         client.println("height: 150px;");
         client.println("background-image:url('http://localhost/arrow.png');");
         client.println("position: absolute;");
         client.println("top: 115px;");
         client.println("left: 40px;");
         
         client.print("-moz-transform:rotate(");
         client.print(headingDegrees + 90);
         client.println("deg);");
         client.println("}");
       client.println("</style>");
          client.print("</head>");
          client.print("<body>");
          // print something, in HTML format:
          client.print("WiFly Webserver Running!");
          client.println("
");
          client.print("IP: ");
          client.println(WiFly.ip());
          client.println("");
          //Serial.print("readString = ");
          //Serial.println(readString); //print to serial monitor for debugging
          client.println("
");
          client.print("Raw:\t");
   client.print(raw.XAxis);
   client.print("   ");   
   client.print(raw.YAxis);
   client.print("   ");   
   client.print(raw.ZAxis);
   client.print("   \tScaled:\t");
   
   client.print(scaled.XAxis);
   client.print("   ");   
   client.print(scaled.YAxis);
   client.print("   ");   
   client.print(scaled.ZAxis);

   client.print("   \tHeading:\t");
   client.print(heading);
   client.print(" Radians   \t");
   client.print(headingDegrees);
   client.println(" Degrees   \t");
   
   client.println("<div id=\"compass\">");
   client.println("<div id=\"arrow\"></div>");
   client.println("</div>");
   client.print("</body>");

          delay(1);
          //stopping client
          client.stop();
           //Serial.print("readString = ");
          //Serial.println(readString); //print to serial monitor for debugging
          Serial.println("Stopped client");

          ///////////////////// control arduino pin
         
          if(readString.indexOf("forward") >0)//checks for f
          {
         
          digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
          digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
          digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
          digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
          analogWrite(pwmA, speed); //Spins the motor on channel A at full speed 
          analogWrite(pwmB, speed); //Spins the motor on channel A at full speed 
          Serial.println("Forwards");
          }
else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        } // end if HTTP request is ended
         readString = "";
        }
      } // end if client available
    } // end if client connected
  } // end if client
} // end loop

Also, the end part of my code if(readString.indexOf("forward") >0)//checks for f
No longer works, i type 192.168.1.72:2000/?forwards, and this part no longer initiates.

Sorry to keep on with the same problem.

cheers

zoomkat:
Does a small web page refresh properly?

yes a small webpage does with my own orriginal code, but its still very slow (about 15 seconds to refresh and load)
Which is very bad if im controlling a robot via http

Hello

Ive used the code on the link provided and simply changed to use Wifi used to to display the simple html part.
Yet i STILL cannot refresh the page!!
I cannot understand why its behaving this way, its consuming so much of my time.

Here is my code, which is only slightly different from the example SurferTim gave me by link.

/*
   Web server sketch for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
*/

#include <SPI.h>
#include <WiFly.h>
#include <SD.h>

// this must be unique


WiFlyServer server(2000);

void setup()
{
  Serial.begin(9600);
 WiFly.begin();
  if (!WiFly.join("BTHub3-HKP4", "9d7cdbc6ce")) {
    while (1) {
      // Hang on failure.
      Serial.print("failed");
    }
  }
  
  delay(2000);
  server.begin();
  Serial.println(F("Ready"));
}

void loop()
{
 WiFlyClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[127];
    int r,t;
    char *pch;

    Serial.print(F("Client request: "));

    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 126)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print(F("POST data: "));
          while(client.available()) Serial.write(client.read());
          Serial.println();

          Serial.println(F("Sending response"));
          client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));

          client.print(F("<head><script type=\"text/javascript\">"));
          client.print(F("function show_alert() {alert(\"This is an alert\");}"));
          client.print(F("</script></head"));


          client.print(F("<body><H1>TEST</H1>"));

          client.print(F("<form method=GET onSubmit=\"show_alert()\">T: <input type=text name=t>
"));
          client.print(F("R: <input type=text name=r>
<input type=submit></form>"));


          client.print(F("</body></html>\r\n\r\n"));
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println(F("done"));
  }
}

note, it takes about 6 seconds for the initial load

please help me :frowning:

The ethernet shield has no problem with either the original load (<1 second) or a refresh (<1 second). Tested on a localnet.

It must be the WiFly library. The transport should be transparent once you have the WiFly connected to an AP. It appears in your case the transport is having trouble. :frowning:

Thats ok, thanks for the help

I have tried connecting using the WiFly HQ library to a webserver i cant initiate the device.
it could be related to the issue im having here, but the webserver in WiFlyHQ uses a SoftSerial to put a new serial through pins 8,9 which i have connected to the RX/TX terminals as instructed, and disconnected pins 0,1 from the arduino like instructed. Its worked for everyone else but me.
Im not sure if i have a defective board or what. really starting to make my head hurt.

Heres the link; WiFlyHQ cant connect with any examples - SparkFun Electronics Forum

If anyone has experience with this library please let me know.

thanks