Go Down

Topic: WiFly hangs when using as server (Read 906 times) previous topic - next topic

soend

Jun 27, 2012, 10:19 pm Last Edit: Jun 27, 2012, 11:17 pm by soend Reason: 1
Code: [Select]
/*
* 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>

#include "Credentials.h"

WiFlyServer server(80);

void setup() {
 WiFly.begin();

 if (!WiFly.join(ssid, passphrase)) {
   while (1) {
     // Hang on failure.
   }
 }

 Serial.begin(9600);
 Serial.print("IP: ");
 Serial.println(WiFly.ip());
 
 server.begin();
}

void loop() {
 WiFlyClient client = server.available();
 if (client) {
   // an http request ends with a blank line
   boolean current_line_is_blank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       // if we've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so we can send a reply
       if (c == '\n' && current_line_is_blank) {
         // send a standard http response header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();

         client.print("Hello World!");

         break;
       }
       if (c == '\n') {
         // we're starting a new line
         current_line_is_blank = true;
       } else if (c != '\r') {
         // we've gotten a character on the current line
         current_line_is_blank = false;
       }
     }
   }
   // give the web browser time to receive the data
   delay(100);
   client.stop();
 }
}


This is my simple hello world code im tryng to use with my WiFly. Wifly is associating with netowork and everyhting looks fine but when i make a request to WiFly ip then the green led on it stops flashing but the page on browser wont load. Sometimes when i press refresh the red led beside the green one flashes. Very rare occasions i manage to get the response from WiFly and its showed on browser.

Any ideas what should i check/do?

* Using Arduino UNO
* No other shields

EDIT:
When using SpiUartTerminal scetch then afther pointing my broser to Wifly i get this feedback:
Code: [Select]
*OPEN*GET / HTTP/1.1
Host: 192.168.69.3
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.11 Safari/537.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

And thats it, no response to the browser.. and if i try to send command through terminal it wont go through also.

soend

Seems like this was just a timing issue. So if anybody having similar problems then adding
Code: [Select]
delay(200); before
Code: [Select]
WiFlyClient client = server.available(); then everything should work just fine.
Heres the full working code:
Code: [Select]

/*
* 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>

#include "Credentials.h"

WiFlyServer server(80);

void setup() {
 WiFly.begin();

 if (!WiFly.join(ssid, passphrase)) {
   while (1) {
     // Hang on failure.
   }
 }

 Serial.begin(9600);
 Serial.print("IP: ");
 Serial.println(WiFly.ip());
 
 server.begin();
}

void loop() {
 delay(200);
 WiFlyClient client = server.available();

 if (client) {
   Serial.println("We have a client!");
   // an http request ends with a blank line
   boolean current_line_is_blank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       Serial.print(c);
       // if we've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so we can send a reply
       if (c == '\n' && current_line_is_blank) {
         Serial.println("Request ended, send data back");
         // send a standard http response header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
         
         client.println("Hello World");
         
         client.stop();
         break;
       }
       if (c == '\n') {
         // we're starting a new line
         current_line_is_blank = true;
       } else if (c != '\r') {
         // we've gotten a character on the current line
         current_line_is_blank = false;
       }
     }
   }
   // give the web browser time to receive the data
   delay(200);
   client.stop();
   Serial.println("Client stopped");
 }
}

Thanks - got the example server working when I inserted the delay.

I seem to be fairly limited as to how quickly I can refresh though.  Too fast and the browser hangs.  I can stop it and reload, and its okay.  If I don't stop it and reload it'll just hang.  Any thoughts on this?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy