- Using SPI_CLOCK_DIV2 in Adafruit_CC3000 constructor,
#define SEND_NON_BLOCKING 1 in Socket.cpp,
Modifying the write function as vheun mentioned,
and adding some delays before closing and after available() calls.
Code: Select all
// Check if request matches a file name
// "start" is the buffer position where the file name starts.
SdFile file;
if ( file.open( _root, start, O_READ ) )
{
Serial.print( start );
Serial.println( F( " opened!" ) );
sendFileHeaders( client, start, file.fileSize() );
Serial.println( F( "Sent file headers." ) );
delay( 5 );
int16_t numRead;
while ( (numRead = file.read( _sdBuffer, SD_BUFFER_SIZE ) ) > 0 )
{
client.write( _sdBuffer, numRead );
delay( 50 );
}
file.close();
Serial.print( start );
Serial.println( " closed!" );
}
Code: Select all
void sendFileHeaders( Adafruit_CC3000_ClientRef client, char* fileName, uint32_t fileSize )
{
client.fastrprintln( F( "HTTP/1.1 200 OK"));
client.fastrprint( F( "Content-Type: " ) );
if ( endsWith( ".jpg", fileName ) )
client.fastrprintln( F( "image/jpg" ) );
else if ( endsWith( ".png", fileName ) )
client.fastrprintln( F( "image/png" ) );
else if ( endsWith( ".css", fileName ) )
client.fastrprintln( F( "text/css" ) );
else if ( endsWith( ".js", fileName ) )
client.fastrprintln( F( "text/javascript " ) );
else if ( endsWith( ".txt", fileName ) )
client.fastrprintln( F( "text/plain" ) );
else
client.fastrprintln( F( "text/html" ) );
client.fastrprint( F( "Content-Length: " ) );
client.write( (const void*)&fileSize, 4, 0 );
client.fastrprintln("");
client.fastrprintln( F( "Cache-Control: max-age=31449600" ) );
client.fastrprintln("");
}
Now, fileSize shows up as squares in firebug/chrome dev console. I now have to convert the file size to a string before sending, like:
Code: Select all
char buffer[16];
sprintf(buffer,"%lu", fileSize);
client.fastrprint( F( "Content-Length: " ) );
client.fastrprintln( buffer );