 int buttonState = 0;
 int requestMade = 0;
 
+boolean wifiReady = false;
+
 // initialise the WiFly card
 WiFlySerial WiFly(ardRxPin, ardTxPin); 
 


    // 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;
}

 */
void setup() 
{
    Serial.begin(9600);
    delay(2000);
    Serial.println("THE BUTTON");

    // we'll keep the LED off until we can press the button
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);

    // initialise a session with the wifi card
    WiFly.begin();

    // 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()) {

        // leave any joined networks
        WiFly.leave();

        // join our network
        WiFly.setSSID(wifiSSID) ;
        WiFly.setPassphrase(wifiPassphrase);

        // 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();
            }

            // 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("Network interface is not available") << endl;
        wifiReady = false;
    }

    return wifiReady;
}

/**
 * Make an HTTP GET request
