I have the server up and running on my home network and it will list any files in the root directory of the SD card as links and when you click on the links the browser will spit out whatever is in the file.
It's working GREAT with smaller text files (csv). I've been testing with larger file sizes and it's exponentially getting slower as the file size goes up. Right now a 30kb text file takes about 30-40 sec to open or download.
Is there any way to up my RAM (its using a lot) or modify how much data is being sent per "send" on the WiShield?
ALSO, any pointers on optimizing the code would be nice. This is my first Arduino project. Thanks for any help!
- Code: Select all
/*
* This sketch will list all files in the root directory and
* then do a recursive list of all directories on the SD card.
*
*/
#include <SdFat.h>
#include <SdFatUtil.h>
#include <WiServer.h>
#include <string.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
192,168,1,2}; // IP address of WiShield
unsigned char gateway_ip[] = {
192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {
255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {
"linksys"}; // max 32 bytes
unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {
"12345678"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters --------------------------------------------------------------------------------------
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
// This is our page serving function that generates web pages ---------------------------------------------------------------------
boolean sendMyPage(char* URL) {
// Check if the requested URL matches "/"
if (strcmp(URL, "/") == 0) {
// Use WiServer's print and println functions to write out the page content
WiServer.print("<html>");
WiServer.print("Hello World!");
WiServer.print("<p>");
WiServer.print("Volume is FAT");
WiServer.print(volume.fatType(), DEC);
WiServer.print("</p>");
WiServer.print("Files found in root:");
WiServer.print("<p>");
root.ls(LS_DATE | LS_SIZE);
WiServer.print("</html>");
ListFiles(0);
// URL was recognized
}
URL++;
if (file.open(&root, URL, O_READ)) { //this opens the file at the url that was requested if it exists
}else{
}
int16_t n;
uint8_t buf[3];// nothing special about 7, just a lucky number.
while ((n = file.read(buf, sizeof(buf))) > 0) {
for (uint8_t i = 0; i < n; i++) WiServer.print(buf[i]);
}
file.close();
return true;
// URL not found
return false;
}
//--------------------------------------------------------------------------------------------------------------------------------
void ListFiles(uint8_t flags) {
dir_t p;
root.rewind();
WiServer.println("<ul>");
while (root.readDir(p) > 0) {
// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) break;
// skip deleted entry and entries for . and ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
// only list subdirectories and files
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
// print any indent spaces
WiServer.print("<li><a href=\"");
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) {
WiServer.print('.');
}
WiServer.print(p.name[i]);
}
WiServer.print("\">");
// print file name with possible blank fill
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) {
WiServer.print('.');
}
WiServer.print(p.name[i]);
}
WiServer.print("</a>");
if (DIR_IS_SUBDIR(&p)) {
WiServer.print('/');
}
// print modify date/time if requested
if (flags & LS_DATE) {
root.printFatDate(p.lastWriteDate);
WiServer.print(' ');
root.printFatTime(p.lastWriteTime);
}
// print size if requested
if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
WiServer.print(' ');
WiServer.print(p.fileSize);
}
WiServer.println("</li>");
}
WiServer.println("</ul>");
}
void setup() { //----------------------------------------------------------------------------------------------------------------
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
WiServer.setIndicatorPins(5, 3);
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH);
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!card.init(SPI_HALF_SPEED, 7)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
WiServer.init(sendMyPage);
}
void loop() { //-----------------------------------------------------------------------------------------------------------------
WiServer.server_task();
}