title: Arduino WiFly - ad hoc network setup
body: <p>I'm currently working with an Arduino trying to build an ad hoc network to which a device can connect to and send web requests to. The problem I am currently having is that I can only set up one connection and then when that connection is terminated (with <code>client.stop()</code>), all subsequent connections are not picked up by the server, even a <a href="http://en.wikipedia.org/wiki/CURL" rel="nofollow">cURL</a> command just sits there spinning. The first connection I start when I reset the server works fine, and I am able to talk to the server; but after that, the Arduino can no longer find new clients (even though it's trying with the library given).</p>
  
  <p>I`m using the SparkFun library for the WiFly shield <a href="https://github.com/sparkfun/WiFly-Shield.git" rel="nofollow">cloned from GitHub</a>, along with an <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino Uno</a>.</p>
  
  <p>My current code is based off their default example 'WiFly_AdHoc_Example', but I had to remove a few things to get the network to start up which might be the cause of this problem. </p>
  
  <p>Here is the <a href="http://www.fileinfo.com/extension/ino" rel="nofollow">.ino</a> file that I am running.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;SPI.h&gt;
  #include &lt;WiFly.h&gt;
  //#include &lt;SoftwareSerial.h&gt;
  
  //SoftwareSerial mySerial( 5, 4); //Part from example not used (see below)
  
  WiFlyServer server(80); //Use telnet port instead, if debugging with telnet
  
  void setup() 
  {
      Serial.begin(9600);
  
      //The code below is from the example, but when I run it the WiFly will hang
      // on Wifly.begin(). Without it, the WiFly starts up fine.
      //mySerial.begin(9600);
      //WiFly.setUart(&amp;mySerial); // Tell the WiFly library that we are not 
                                  // using the SPIUart
  
      Serial.println("**************Starting WiFly**************");
      // Enable Adhoc mod
      WiFly.begin(true);
      Serial.println("WiFly started, creating network.");
  
      if (!WiFly.createAdHocNetwork("wifly")) 
      {
          Serial.print("Failed to create ad hoc network.");
          while (1) 
          {
              // Hang on failure.
          }
      }
      Serial.println("Network created");
      Serial.print("IP: ");
      Serial.println(WiFly.ip());
  
      Serial.println("Starting Server...");
      server.begin();
      Serial.print("Server started, waiting for client.");
  }
  
  void loop() 
  {
      delay(200);
      WiFlyClient client = server.available();
      if (client) 
      {
          Serial.println("Client Found.");
  
          // A string to store received commands
          String current_command = "";
  
          while (client.connected()) 
          {
              if (client.available()) 
              {
                  //Gets a character from the sent request.
                  char c = client.read();
                  if (c=='#' || c=='\n') //End of extraneous output
                  {
                      current_command = "";
                  }
                  else if(c!= '\n')
                  {
                      current_command+=c;
                  }
  
                  if (current_command== "get") 
                  {
                      // output the value of each analog input pin
                      for (int i = 0; i &lt; 6; i++) 
                      {
                          client.print("analog input ");
                          client.print(i);
                          client.print(" is ");
                          client.print(analogRead(i));
                          client.println("&lt;br /&gt;");
                      }     
                  }
                  else if(current_command== "hello")
                  {
                      client.println("Hello there, I'm still here.");
                  }
                  else if (current_command== "quit")
                  {
                      client.println("Goodbye...");
                      client.stop();
                      current_command == "";
                      break;
                  }
                  else if (current_command == "*OPEN*")
                  {
                      current_command == "";
                  }
              }
          }
          // Give the web browser time to receive the data
          delay(200);
          // close the connection
          client.stop();
      }
  }
  </code></pre>
  
  <p>This script is just a mini protocol I set up to test. Once connected with the wifly module you can send text such as "get" "hello" or "quit" and the wifly module should respond back.</p>
  
  <p>Using <a href="http://en.wikipedia.org/wiki/Telnet" rel="nofollow">Telnet</a> I can successfully connect (the first time) and send commands to the <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino</a> including "quit" to terminate the connection (calls the <code>client.stop()</code> method). But when I try to reconnect though <a href="http://en.wikipedia.org/wiki/Telnet" rel="nofollow">Telnet</a>, it says the connection was successful, but on the <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino</a> it's still looping thinking the client is still false. What??</p>
  
  <p>I know right, I'm getting mixed messages from <a href="http://en.wikipedia.org/wiki/Telnet" rel="nofollow">Telnet</a> vs <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino</a>. None of the commands work obviously since the <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Ardunio</a> is still looping waiting for a client that evaluates to true. I'm going to take a look at <a href="https://github.com/sparkfun/WiFly-Shield/blob/master/WiFlyServer.cpp" rel="nofollow">WiFlyServer from the library I imported</a> and see if I can dig up the problem, because somehow that <strong>server.available() method isn't finding new clients</strong>.</p>
  
  <p>I am noticing a lot of TODO's in the library code....</p>
  
  <hr>
  
  <p>So I found the reason for the problem. It was in the <a href="https://github.com/sparkfun/WiFly-Shield/blob/master/WiFlyServer.cpp" rel="nofollow"><code>WiFlyServer.cpp</code></a> file from the <a href="https://github.com/sparkfun/WiFly-Shield.git" rel="nofollow">SparkFun library</a>. The code that was causing the reconnect issue was in fact the <code>server.availible()</code> method. Right at the top of the method, there is a check:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// TODO: Ensure no active non-server client connection.
  
  if (!WiFly.serverConnectionActive) {
      activeClient._port = 0;
  }
  </code></pre>
  
  <p>For some reason <strong>when I comment this out, I can connect and reconnect perfectly fine and everything works as it should</strong>. I will now dive into the library and see if I can fix this, I'm not exactly sure what this is doing, but it gets called when the server connection is not active and is somehow blocking subsequent connections. The problem with this solution is that the <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino</a> always thinks it has found a client since <code>client</code> and <code>client.connected()</code> evaluate to true even if one doesn't exist. Even <code>client.available()</code> evaluates to true right when the connection is terminated and the ghost "client" is found, but after that first run through the if-statement the ghost "client" is no longer <code>available()</code>. Even with this flaw it still picks up a new client when it comes along which is why it works.</p>
  
  <ul>
  <li><p>How might I get to the root of this problem without using this commenting hack?</p></li>
  <li><p>Are their any risks or future problems I might run into doing it this way?</p></li>
  <li><p>What is the purpose of the block that I commented out in the first place?</p></li>
  </ul>