 SdCard card;
 Fat16 file;
 
-#define FORM_BOUNDARY "--KegScribeCSVFile"
-#define FILE_HEADER "\r\nContent-Disposition: form-data; name=\"f\"; filename=\"filename.csv\"\r\nContent-Type: text/csv\r\n\r\n"
-const char SUCCESS_RESPONSE[] = "HTTP/1.1 200 OK";
+#define FORM_BOUNDARY_PREFIX "--"
+#define FORM_BOUNDARY_BASE "KegScribeCSVFile"
+#define FORM_BOUNDARY FORM_BOUNDARY_PREFIX FORM_BOUNDARY_BASE
+#define FORM_BOUNDARY_END FORM_BOUNDARY FORM_BOUNDARY_PREFIX "\r\n"
+#define FORM_BOUNDARY_START FORM_BOUNDARY "\r\n"
+
+#define CC3000_DELAY 100
+
+#define FILE_HEADER "Content-Disposition: form-data; name=\"file\"; filename=\"filename.csv\"\r\nContent-Type: text/csv\r\n\r\n"
+const char SUCCESS_RESPONSE[] = "HTTP/1.1 200 OK\r\n";
 
 void initSD() {
   pinMode(SD_CHIP_SELECT_PIN, OUTPUT);

  */
  Serial.print(F("tcp connect"));
  Adafruit_CC3000_Client www = cc3000->connectTCP(ip, 80);
  
  uint32_t totalContentLength = ((strlen(FORM_BOUNDARY)+2)*2) + strlen(FILE_HEADER) + 2;
  totalContentLength += file->fileSize();
  
  if (www.connected()) {
    www.fastrprint(F("POST ")); www.fastrprint(CSV_WEBPAGE); www.fastrprint(F(" HTTP/1.1"));
    www.fastrprint(F("\r\nHost: ")); www.fastrprint(WEBSITE);
    www.fastrprint(F("\r\nAuthorization: Basic a2Vnc2NyaWJlOnRlc3Q="));
    www.fastrprint(F("\r\nUser-Agent: KegScribe"));
    www.fastrprint(F("\r\nContent-Length: ")); www.print(totalContentLength);
    www.fastrprint(F("\r\nContent-Type: multipart/form-data; boundary=")); www.fastrprint(FORM_BOUNDARY);
    www.fastrprint("\r\n\r\n"); www.fastrprint(FORM_BOUNDARY);
    www.fastrprint(F(FILE_HEADER));
    
    int16_t n;
    uint8_t buf[7] = {0,0,0,0,0,0,0};// nothing special about 7, just a lucky number.
    // read sizeof(buf)-1 bytes into buf
    while ((n = file->read(buf, sizeof(buf)-1)) > 0) {
      www.fastrprint((char*)buf);
    }
    
    www.fastrprint(FORM_BOUNDARY); www.fastrprint("--");
    
    
    // Read data until either the connection is closed, or the idle timeout is reached.
    byte i = 0;
    unsigned long lastRead = millis();
    while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
      while (www.available() && (i < strlen(SUCCESS_RESPONSE))) {
        char c = www.read();
        if ((i < strlen(SUCCESS_RESPONSE)) && c != SUCCESS_RESPONSE[i++]) {
          Serial.print(FAIL_MSG);
          return 2;
        }
        Serial.print(c);
        lastRead = millis();
      }
    }
    

  Serial.print(OK_MSG);
  
  return 0;
}

int safeprint(Adafruit_CC3000_Client* www, const __FlashStringHelper* string) {
  delay(CC3000_DELAY);
  return www->fastrprint(string);
}

int safeprint(Adafruit_CC3000_Client* www, char* string) {
  delay(CC3000_DELAY);
  return www->fastrprint(string);
}

int reportFile(Fat16* file) {
