Can I do this?

Good day,

I have an UNO, ethernet card and SD card.
I’m having trouble retrieving the file name and writing the contents of the file to a WEB page.

I’m I using the wrong technique? I can’t have both open att he same time?

I tried just the SD portion in a sketch and it works fine.

FYI
I see dir = 1.
Grocery_list.name() is blank.
I just get garbage on the Web page.

Thanks
Randy

#include <SPI.h>
#include <Ethernet.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <SPI.h>
#include <SD.h>
#include <EEPROM.h>

/**********************************************************************************************************************
*                                   MAC address and IP address.
***********************************************************************************************************************/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x98, 0x13 };
byte ip[] = { 192, 168, 1, 200 };

//Strings stored in flash of the HTML we will be transmitting


PROGMEM prog_char content_main_header1[] = "HTTP/1.0 200 OK";
PROGMEM prog_char content_main_header2[] = "Content-Type: text/html";
PROGMEM prog_char content_main_top[] = "<html><head><title>Brazeau Web Server</title></head><body><h1>Brazeau Web Server</h1>";
PROGMEM prog_char content_main_title[] = "<h2>Item list</h2>";
PROGMEM prog_char content_main_form[] = "<form name='input' action='html_form_action.asp' method='get'>";
PROGMEM prog_char content_checkbox_1[] = "<input type='checkbox' name='UPC' ";
PROGMEM prog_char content_checkbox_2[] = "value=";                                              // The item filename.
PROGMEM prog_char content_checkbox_3[] = "/>";                                                  // Item Name or Description starts here.
PROGMEM prog_char content_checkbox_4[] = "
";
PROGMEM prog_char content_footer1[] = "<input type='submit' value='Submit' /></form>";
PROGMEM prog_char content_footer2[] = "</body></html>";

PGM_P main_table[] PROGMEM = {content_main_header1, content_main_header2, content_main_top ,content_main_title ,content_main_form};
PGM_P checkbox_table[] PROGMEM = {content_checkbox_1, content_checkbox_2 ,content_checkbox_3 ,content_checkbox_4};
PGM_P footer_table[] PROGMEM = {content_footer1, content_footer2};

// For sending HTML to the client
#define STRING_BUFFER_SIZE 128
String stringbuffer = String(STRING_BUFFER_SIZE);

// To store data from HTTP request
#define STRING_LOAD_SIZE 128
char load[STRING_LOAD_SIZE];


EthernetServer server(80);

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(4, OUTPUT);
  SD.begin(4);
  Serial.begin(9600); // DEBUG
}

void loop() {

  EthernetClient client = server.available();
  if (client) { // now client is connected to arduino
    // sendRequest( client);
    readRequest( client);
   // sendRequest( client);
    // read HTTP header request... so select what page client are looking for
    

    

    // give the web browser time to receive the data
    delay(200);
    client.stop();
  }  
  
  
}

void readRequest(EthernetClient client) {
  
boolean currentLineIsBlank = true;  
int post = 0;

Serial.println("-----------------------------------------------"); //Debug
 if (client) {
   while (client.connected() && client.available() ) {
     char c = client.read();
     Serial.print(c);
     if (stringbuffer.length() < STRING_BUFFER_SIZE) {
       stringbuffer = stringbuffer + c;



    }
    // if you've gotten to the end of the line (received a newline
    // character) and the line is blank, the http request has ended,
    // so you can send a reply
    if (c == '\n' && currentLineIsBlank) {
      sendRequest( client);
    }
      
    if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
    }      
    else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;  
    }      
  }
 post = stringbuffer.indexOf("UPC=");
 delay(10);          // give the web browser time to receive the data
 client.stop();
 
 }
 Serial.println("Post: ");
 Serial.print(post);
} // End of readRequest.

//-----------------------------------------------------------

void sendRequest(EthernetClient client) {
  char buffer[STRING_BUFFER_SIZE];
 // String file_name = "12";
//  String description = "Cream of Mushroom Soup";
  int counter = 0; //Debugging
  File dir;
  File upcFile;
  File Grocery_list;
  
  
  dir = SD.open("/LIST");
  

  if (!upcFile) {
  #ifdef DEBUGSD
    Serial.println("Error opening file.");                                       // If the file didn't open, print an error and return.
  #endif
   
  }    
  
  
  // Header  
  strcpy_P(buffer, (char*)pgm_read_word(&(main_table[0])));
  client.println(buffer);
  Serial.println(buffer);
  strcpy_P(buffer, (char*)pgm_read_word(&(main_table[1])));
  client.println(buffer);
  client.println();
   Serial.println(buffer);
  strcpy_P(buffer, (char*)pgm_read_word(&(main_table[2])));
  client.print(buffer);
   Serial.println(buffer);
  strcpy_P(buffer, (char*)pgm_read_word(&(main_table[3])));
  client.println(buffer);
   Serial.println(buffer);
  strcpy_P(buffer, (char*)pgm_read_word(&(main_table[4])));
  client.println(buffer);
   Serial.println(buffer);
  
   Grocery_list = dir.openNextFile();
  Serial.println("--------------");              //Debug
  Serial.println(dir.openNextFile());      //Debug
  Serial.println(Grocery_list.name());   //Debug
  Serial.println("--------------");   //Debug
 
  
  while (!Grocery_list) {
  // Grocery List
  strcpy_P(buffer, (char*)pgm_read_word(&(checkbox_table[0])));
  client.print(buffer);
  Serial.print(buffer);
  strcpy_P(buffer, (char*)pgm_read_word(&(checkbox_table[1])));
  client.print(buffer);
  Serial.print(buffer);
  client.print(Grocery_list.name());
  Serial.print(Grocery_list.name());
  //client.print(file_name);
  strcpy_P(buffer, (char*)pgm_read_word(&(checkbox_table[2])));
  client.print(buffer);
  Serial.print(buffer);
  
  upcFile = SD.open(Grocery_list.name());
  
  while (upcFile.available()) {
        char c = upcFile.read();
    	client.print(c);
        
//For debugging
        Serial.print(c);
        counter++;
        if (counter == 3) break;    
    }
  
  
  
    strcpy_P(buffer, (char*)pgm_read_word(&(checkbox_table[3])));
    client.print(buffer);
    Serial.println(buffer);
  
    Grocery_list = dir.openNextFile();
 
  }
  
 
  // Footer
  strcpy_P(buffer, (char*)pgm_read_word(&(footer_table[0])));
  client.print(buffer);
   Serial.print(buffer);
  strcpy_P(buffer, (char*)pgm_read_word(&(footer_table[1])));
  client.print(buffer);
  Serial.print(buffer);
  
  upcFile.close();
  dir.close();                           // Close the file: 
  client.stop();
  
}
// For sending HTML to the client
#define STRING_BUFFER_SIZE 128
String stringbuffer = String(STRING_BUFFER_SIZE);

I don't think this is doing what you think it is. If I'm not mistaken that just creates a string with "128" inside it, not a 128-byte string.

If I’m not mistaken that just creates a string with “128” inside it, not a 128-byte string.

You are not.

  Serial.println(Grocery_list.name());   //Debug

Where did Grocery_list get assigned a value? You can’t call methods of unassigned instances.

Thanks for the help gentlemen.

FYI
The WEB part works.
The HTTP request returns the HTML portion correctly but I'm trying to loop through the files on the SD card and return the contents of each file with a checkbox to the browser.
So for example, I have 4 files on the SD card and each files contains text "Checkbox 1", "Checkbox 2" ..."Checkbox 4".
Serial.print(Grocery_list.name()); has a blank value yet when I test the code without the WEB Server code it returns the files name and text in the file.

I have been trying diiferent things without success, so I'm missing something here.

Randy

Serial.print(Grocery_list.name()); has a blank value

As it should, because you have not assigned Grocery_list a value.

yet when I test the code without the WEB Server code

Which isn’t the code you posted. You haven’t posted “the code without the WEB Server”.

I have been trying diiferent things without success, so I’m missing something here.

No argument here.