Pages: [1]   Go Down
Author Topic: WiFly hangs when using as server  (Read 726 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
 * 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:
*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.
« Last Edit: June 27, 2012, 04:17:58 pm by soend » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seems like this was just a timing issue. So if anybody having similar problems then adding
Code:
delay(200);
before
Code:
WiFlyClient client = server.available();
then everything should work just fine.
Heres the full working code:
Code:
/*
 * 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");
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: