Go Down

Topic: WiFly hangs... (Read 3712 times) previous topic - next topic

nadeaup

I am using the WebServer Sketch and modified it to read the GET off of a Submit. I have run into a couple of problems. 1st if I uncomment all the "Option" lines the WiFly shield hangs and the green LED stays solid and does not respond until I close the web browser. The 2nd problem is when I leave the lines commented and then select an item and hit Submit the WiFly Shield hangs 99% of the time until I close the web browser and then it restarts.

I'm thinking it may be a bug in the library... or my newbie coding ;) below I am using substring I have also tried indexOf()...

Code is below!

#include "WiFly.h"
#include "Credentials.h"
#include <WString.h>

String inString = String(100);
#define maxLength 25

#define RUM_PIN 3
#define VODKA_PIN 4
#define PEACHTREE_PIN 5
#define KAHLUA_PIN 6
#define BAILEYS_PIN 7
#define CRANBERRY_PIN 8
#define COKE_PIN 9
#define ORANGEJUICE_PIN 10

Server server(80);
void setup() {
WiFly.begin();
if (!WiFly.join(ssid)) {
while (1) {
}
}
Serial.begin(9600);
Serial.print("IP: ");
Serial.println(WiFly.ip());
server.begin();
}

void loop()
{
Client client = server.available();
if (client) {
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (inString.length() < maxLength) {
inString += c;
}
if (c == '\n' && current_line_is_blank) {
if (inString.substring(0)=="cowboyroy") {
Serial.println("cowboyroy!");
// then open valves and stuff...
} else if (inString.substring(0)=="sexonthebeach11") {
Serial.println("Sex on the Beach!");
// then open valves and stuff...
}

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("<title>OctaBar</title>");
client.println();
client.println("<body><h2>OctaBar!</h2><br><hr>");
client.println("Please select your drink<br><br>");
client.println("<form method=get>");
client.println("<select name=drink>");
client.println("<option selected value='default'>Select a Drink!</option>");
client.println("<option value='agilligansisland'>A Gilligan's Island </option>");
client.println("<option value='jamaicansunrise'>Jamaican Sunrise </option>");
client.println("<option value='sexonthebeach11'>Sex on the Beach 11 </option>");
client.println("<option value='backfire'>Backfire </option>");
client.println("<option value='cowboyroy'>Cowboy Roy </option>");
client.println("<option value='mistolin'>Mistolin </option>");
client.println("<option value='mudslide1'>Mudslide 1 </option>");
client.println("<option value='powerscrewdriver'>Power screwdriver </option>");
client.println("<option value='screamingorgasm'>Screaming Orgasm </option>");
client.println("<option value='woowoo'>Woo Woo </option>");
/*
client.println("<option value='bermudatriangle'>Bermuda Triangle </option>");
client.println("<option value='brassmonkey'>Brass Monkey </option>");
client.println("<option value='cranberrynewt'>Cranberry Newt </option>");
client.println("<option value='cranberrytoad'>Cranberry Toad </option>");
client.println("<option value='fuzzypeachclari'>Fuzzy Peachclari </option>");
client.println("<option value='fuzzyscrew'>Fuzzy Screw </option>");
client.println("<option value='georgiapeach'>Georgia Peach </option>");
client.println("<option value='wuwu'>Wu Wu </option>");
client.println("<option value='airhead'>Airhead </option>");
client.println("<option value='aranov'>Aranov </option>");
client.println("<option value='babyguiness'>Baby Guinness </option>");
client.println("<option value='badhabbit'>Bad Habit </option>");
client.println("<option value='butterynipple3'>Buttery Nipple 3</option>");
client.println("<option value='chisterpetterson'>Christer Petterson</option>");
client.println("<option value='concrete'>Concrete </option>");
client.println("<option value='cranberryfrog'>Cranberry Frog </option>");
client.println("<option value='cucaracha2'>Cucaracha 2</option>");
client.println("<option value='dreamsicle1'>Dreamsicle 1</option>");
client.println("<option value='fuzzynavel'>Fuzzy Navel</option>");
client.println("<option value='fuzzyrussian'>Fuzzy Russian</option>");
client.println("<option value='fuzzyberry'>Fuzzyberry</option>");
client.println("<option value='grandmomsslipper'>Grandmom's slipper </option>");
client.println("<option value='hotafternoon'>Hot Afternoon </option>");
client.println("<option value='irishfloat'>Irish Float</option>");
client.println("<option value='machine'>Machine </option>");
client.println("<option value='orangatang'>Orangatang </option>");
client.println("<option value='orgasmaladenmark'>Orgasm a la Denmark </option>");
client.println("<option value='rumcoke'>Rum and Coke </option>");
client.println("<option value='sailorsdelight'>Sailor's Delight </option>");
client.println("<option value='screwdriver'>Screwdriver </option>");
client.println("<option value='silkpanties'>Silk Panties </option>");
client.println("<option value='skydiver'>Skydiver</option>");
client.println("<option value='thevaitkus'>The Vaitkus </option>");
client.println("<option value='tootsie'>Tootsie </option>");
client.println("<option value='tootsieroll2'>Tootsie Roll 2</option>");
client.println("<option value='twinsorgasm'>Twins Orgasm</option>");
*/
client.println("</select>");
client.println("<INPUT type='submit' value='Send'>");
client.println("</form>");

client.println("</body></html>");
break;
}
if (c == '\n') {
current_line_is_blank = true;
}
else if (c != '\r') {
current_line_is_blank = false;
}
}
}
inString = "";
delay(100);
client.stop();
}
}

Thanks for the help!
-Patrick

PaulS

Quote
I'm thinking it may be a bug in the library... or my newbie coding

I'm casting a vote for the problem being in your code. You are using an awful lot of constant strings. Each string takes up space in SRAM, along with arrays, the stack, and the heap. I'd be willing to bet that you are out of SRAM.

Look at PROGMEM in the playground.

The option value does not have to bear any relationship to the option name. The value can be a number, like '0', '1', etc.

This will reduce the amount of space needed. It will also make selecting the correct action on return a lot simpler. Convert the returned value to an integer, and use a switch statement.

nadeaup

Thanks for the PROGMEM tip -- I forget I am not writing code on a PC with 4gb!

As a quick test I shrunk the code down to it's very basics... Still hangs on submit and it only seems to partially print the inString varible I never see the full header... Any other ideas?

#include "WiFly.h"
#include "Credentials.h"
#include <WString.h>

String inString = String(100);
#define maxLength 25

Server server(80);
void setup() {
  WiFly.begin();
  if (!WiFly.join(ssid)) {
    while (1) {
    }
  }
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  server.begin();
}

void loop()
{
  Client client = server.available();
  if (client) {
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (inString.length() < maxLength) {
          inString += c;
        }       
        if (c == '\n' && current_line_is_blank) {
          Serial.println(inString);
        if (inString.substring(0)=="6") {
          Serial.println("6 - cowboyroy");
        }
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("<title>OctaBar</title>");
          client.println();
          client.println("<body><h2>OctaBar!</h2><br><hr>");
          client.println("Please select your drink<br><br>");
          client.println("<form method=get>");
          client.println("<select name=drink>");
            client.println("<option selected value='0'>Select a Drink!</option>");
            client.println("<option value='1'>A Gilligan's Island </option>");
            client.println("<option value='2'>Jamaican Sunrise </option>");
            client.println("<option value='3'>Sex on the Beach 11 </option>");
            client.println("<option value='5'>Backfire </option>");
            client.println("<option value='6'>Cowboy Roy </option>");
            client.println("<option value='7'>Mistolin </option>");
          client.println("</select>");
          client.println("<INPUT type='submit' value='Send'>");
          client.println("</form>");

          client.println("</body></html>");
          break;
       }
        if (c == '\n') {
          current_line_is_blank = true;
        }
        else if (c != '\r') {
          current_line_is_blank = false;
        }
      }
    }
    inString = "";
    delay(100);
    client.stop();
  }
}

nadeaup

Even this does not work...

#include "WiFly.h"
#include "Credentials.h"
#include <WString.h>

String inString = String(150);
#define maxLength 50

Server server(80);
void setup() {
  WiFly.begin();
  if (!WiFly.join(ssid)) {
    while (1) {
    }
  }
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  server.begin();
}

void loop()
{
  Client client = server.available();
  if (client) {
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (inString.length() < maxLength) {
          inString += c;
        }       
        if (c == '\n' && current_line_is_blank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("<title>OctaBar</title>");
          client.println();
          client.println("<body><h2>OctaBar!</h2><br><hr>");
          client.println("Please select your drink<br><br>");
          client.println("<form method=get>");
          client.println("<select name=drink>");
            client.println("<option selected value='0'>Select a Drink!</option>");
            client.println("<option value='6'>Cowboy Roy </option>");
          client.println("</select>");
          client.println("<INPUT type='submit' value='Send'>");
          client.println("</form>");

          client.println("</body></html>");
          break;
       }
        if (c == '\n') {
          current_line_is_blank = true;
        }
        else if (c != '\r') {
          current_line_is_blank = false;
        }
      }
    }
    inString = "";
    delay(500);
    client.stop();
  }
}

I have to be doing something wrong - I'm sure it will be silly too!

nadeaup

UPDATE:  Ok now i'm thinking it's not my code... but a hardware or settings issue... I can connect to my network and get an IP --- But if I use the Auto-Connect Sketch it fails... After messing around with it and doing the commands manually once I get to "show c" it returns 8330 so it does not seems to be connecting properly. Any ideas?

nadeaup

I am glad to say my First Arduino Project is a success!

My WiFly, Speaking, WebBased Bartender is now up and running! 47 drinks from 8 bottles!
Order your drink from any web browser and Octabar is happy to make it for you!
I'll update this post with my website link to show how to build it...

Thanks for the PROGMEM TIP!!!

I can't post all the code here, so look for an update to this post for a link to my site with all the info...

nadeaup

Ok my site is up that reviews my current project OctaBar - the automated bartender that makes 47 drinks, speaks and lets you order from it's web server via wifi wit any web browser (tuned for an iphone)! The site also covers my other upcoming projects!

Overview, the build, the problems I hit, future enhancements as well as the code can be found there...

Razorcontrol.com

Hope there is something there that others can use...

beerygaz

I know it's bad form to dredge up old threads, but I'm having exactly the same issue.  The basic server code waits for for a blank line before returning data to the client.  In many instances, the ardunio seems to hang and no longer receives data from the browser.  It's like the browser headers are too much for it to cope with and it just gives up.  If I telnet to port 80 and simply send a CR/LF or even a few characters followed by a blank line, all is well, but if I access the WiFly server via a standard browser, it fails 9 times out of 10.

I've updated the firmware (4.41) on the device via ftp - that makes it associate a lot faster (but the update to 4.41 caused the WiFly.IP() call to return "0.0.0.0" for some reason) but the behaviour does not change.

Is it possible that some sort of receive buffer on the WiFly shield is getting full because the browser headers are too long ofr it to cope with?

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() {
    Serial.begin(9600);
    Serial.println("Trying to connect to wifi");
  WiFly.begin();
  if (!WiFly.join(ssid, passphrase)) {
    while (1) {
      // Hang on failure.
    }
  }


  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();
         
          // output the value of each analog input pin
          for (int i = 0; i < 6; i++) {
            client.print("analog input ");
            client.print(i);
            client.print(" is ");
            client.print(analogRead(i));
            client.println("<br />");
          }
          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();
  }
  delay(500);
  Serial.println(millis());
}

Go Up