4
$\begingroup$

I recently asked a question about the juniper WiFi shield, and am now working with wifly from spark fun. I've been using an updated version of their experimental library, and have been attempting to set up a webserver. Unfortunately, when I attempt to connect through a web browser, I get an error saying that the page sent no data. Here's my code:

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

WiFlyServer s(80);
boolean current_line_is_blank=true;
void setup() {
  Serial.begin(9600);
  WiFly.begin();
  if(!WiFly.join(placeholderssid, placeholderpass,WPA_MODE)) {
    Serial.println("Connection Failed.");
  } else {
    Serial.println("Connection Succesful!");
    Serial.println(WiFly.ip());
    Serial.println("Receving Client Input...");
    s.begin();

  }
}
void loop() { 
   WiFlyClient c = s.available();
   if(c) {
   Serial.println("Server Ready.");
   current_line_is_blank=true;
   while(c.connected()) {
     Serial.println("Client Connected.");
     if(c.available()) {
       Serial.println("Client Available for data.");
       char tmp = c.read();
       Serial.println(tmp);
       if(tmp == '\n' && current_line_is_blank) {
         Serial.println("Sent OK Response.");
         c.println("HTTP/1.1 200 OK");
         c.println("Content-Type: text/html");
         c.println();
         c.print("WiFly Webserver Running!");
         c.println("<br />");
         break;
       }
      if (tmp == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } else if (tmp != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
       }
   }
   }
    delay(2000);
    c.stop();
}

I am using Arduino Uno, and the serial monitor looks like this:

Connection Succesful!
10.100.1.173
Receving Client Input...

Is there anything obviously wrong with my code?

EDIT: I now have a new shield, but I'm still working with the same problem. Is it a malfunction in the hardware? I just can't figure this out!

$\endgroup$
  • $\begingroup$ I just looked up an example and your method appears correct so I have deleted my answer. I don't see any other issues though it may help to look at the example more closely. $\endgroup$ – DaemonMaker Dec 20 '12 at 4:27
1
$\begingroup$

I've never programmed an Arduino or used this module, but from a quick look at the code and its output, it looks like only setup() is getting run, while loop() is the source of the problem (sort of expected). Try adding a println statement before checking for c to see if the loop gets run at all.

...
void loop() { 
  WiFlyClient c = s.available();
  Serial.println("Looping...");
  if(c) {
    Serial.println("Server Ready.");
    ...

Assuming the above checks out, my hunch is that the problem is in the first line of the loop:

...
WiFlyClient c = s.available();
...

Why are you initializing the client, c, with the server's availability? This seems to imply that the if statement is essentially testing for the server's availability, instead of the client's availability. What's more, you then also run c.connected(), c.available(), and c.println(), which is somewhat at odds with c's initialization.

Try checking what you need to initialize c with and then probably switch the if statement as follows:

...
if(c.available()) {
...

Just my 2c.

$\endgroup$
  • 1
    $\begingroup$ The loop() and setup() functions are special in the Arduino IDE. It always executes setup() and then loop(). Think of it as main(). $\endgroup$ – Joe Baker Dec 22 '12 at 20:56
  • $\begingroup$ Yeah, I assumed so. I was just implying that whereas setup() was executing correctly, loop() wasn't. $\endgroup$ – fgb Dec 26 '12 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.