  */
 void setup() 
 {
-  // initialise card
+    Serial.begin(9600);
     delay(2000);
-    Serial.begin(9600);
     Serial.println("THE BUTTON");
-    
+
+    // we'll keep the LED off until we can press the button
     pinMode(ledPin, OUTPUT);
-    // we'll keep the LED off until we can press the button
     digitalWrite(ledPin, LOW);
 
+    // initialise a session with the wifi card
     WiFly.begin();
-   //WiFly.reboot();
+
     // get MAC address
     Serial << F("MAC: ") << WiFly.getMAC(bufRequest, REQUEST_BUFFER_SIZE) << endl;
 
+    // and join our network
+    Serial << F("No button pressed - attempting association") << endl;
+    wifiJoin();
+}
+
+/**
+ * Main loop - waiting for a button press
+ * Indicates status through the LED:
+ *  - solid LED: network available and button ready
+ *  - slow flashing: network unavailable
+ */
+void loop()
+{
+    // only execute the loop if wifi is available
+    if(wifiReady == 1) {
+
+        // read the state of the button
+        buttonState = digitalRead(buttonPin);
+
+        // HIGH == pressed
+        if (buttonState == HIGH) {
+
+            // if a request is in progress, ignore
+            // additional button presses
+            if(requestMade == 0) {
+
+                // make the request
+                requestMade = 1;
+                request();
+
+            } else {
+                //Serial.println("request already made");
+            }
+
+        } else {
+            requestMade = 0;
+            //Serial.println("button off");
+        }
+
+    } else {
+        // slow flash
+        flashLed(1, 2000);
+        // attempt reconnect
+        // wifiJoin();
+    }
+}
+
+/**
+ * Set the connection parameters for the wifi card
+ */
+void wifiSetParams()
+{
     // set our auth options
     WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK );
     WiFly.setJoinMode( WIFLY_JOIN_AUTO );
+
+    // enable DHCP
     WiFly.setDHCPMode( WIFLY_DHCP_ON );
+
     // use our expensive external antenna
     WiFly.setUseExternalAnt(WIFLY_EXTERNAL_ANT_ON);
-
+}
+
+/**
+ * Join a wifi network
+ */
+boolean wifiJoin()
+{
     Serial << F("Attempting to join network '") << wifiSSID << F("'...") << endl;
+
+    // set out auth options
+    wifiSetParams();
+
     // open link, if we don't already have one
     WiFly.getDeviceStatus();
-    if (! WiFly.isifUp() ) 
-    {
+    if (! WiFly.isifUp()) {
+
+        // leave any joined networks
         WiFly.leave();
-        // join
+
+        // join our network
         WiFly.setSSID(wifiSSID) ;
         WiFly.setPassphrase(wifiPassphrase);
-        if ( WiFly.join() ) 
-        {
+
+        // check that we managed to join
+        if (WiFly.join()) {
+
             Serial << F("We associated with ") << wifiSSID << F(" successfully.") << endl;
-            
+
             // disable default *hello* bullshit
             WiFly.SendCommand("set comm remote 0",">", bufBody, BODY_BUFFER_SIZE);
             memset (bufBody,'\0',BODY_BUFFER_SIZE);
-        
+
             // clear out prior requests.
             WiFly.flush();
-            while (WiFly.available())
-                WiFly.read();    
-        
+            while (WiFly.available()) {
+                WiFly.read();
+            }
+
             // we're ready for the button - switch the LED on
             digitalWrite(ledPin, HIGH);
+            wifiReady = true;
+
+        } else {
+            // could not join network
+            Serial << F("Association with ") << wifiSSID << F(" failed.") << endl;
+            wifiReady = false;
         }
-        else
-        {
-            Serial << F("Association with ") << wifiSSID << F(" failed.") << endl;
-        }
-    }
-    else
-    {
-            Serial << F("Network interface is not available") << endl;
-    }
-}
-
-/**
- * Main loop - waiting for a button press
- * Indicates status through the LED:
- *  - solid LED: network available and button ready
- */
-void loop() 
-{
-    // read the state of the button
-    buttonState = digitalRead(buttonPin);
-    // HIGH == pressed
-    if (buttonState == HIGH) 
-    { 
-        Serial.println("button on");
-        // only make one request per press
-        if(requestMade == 0) 
-        {
-            requestMade = 1;
-            // make the request
-            request();
-        }
-        else
-        {
-            Serial.println("request already made");
-        }
-    } 
-    else 
-    {
-        requestMade = 0;
-        //Serial.println("button off");
-    }
-}
-
+
+    } else {
+        Serial << F("Network interface is not available") << endl;
+        wifiReady = false;
+    }
+
+    return wifiReady;
+}
 
 /**
  * Make an HTTP GET request


    // Open connection, then send GET Request
    Serial << F("Opening connection to ") << serverHost << endl;
    if (WiFly.openConnection( serverHost ) ) 
    {
        
        WiFly <<    strRequest << endl; 

        // buffer server response
        unsigned long TimeOut = millis() + 4000;

        while (    TimeOut > millis() && WiFly.isConnectionOpen() ) 
        {
            if (    WiFly.available() > 0 ) 
            {
                // display
                Serial << (char) WiFly.read();
            }
        }

        // Force close connection
        WiFly.closeConnection();
        
        // force a 2 second delay
        delay(2000);
        // we're done - switch the LED back on
        digitalWrite(ledPin, HIGH);
    } 
    else 
    {
        // Failed to open connection
        Serial << F("Failed to open connection to") << serverHost << endl;
        // force a 2 second delay
        delay(2000);
        digitalWrite(ledPin, HIGH);
    }

    WiFly.setDebugChannel( NULL );
    return 0;
}

int buttonState = 0;
int requestMade = 0;

boolean wifiReady = false;

// initialise the WiFly card
WiFlySerial WiFly(ardRxPin, ardTxPin); 

