chipKIT® Development Platform

Inspired by Arduino™

ChipKit Uno - Webserver- send web pages from SD

Created Mon, 01 Apr 2013 23:58:35 +0000 by del109


del109

Mon, 01 Apr 2013 23:58:35 +0000

Hi

I really need some help. i want to send webpages from my SD card to the browse once the chipkit uno webserver is accessed from a browse.

i saw this code on a site for a arduino uno :

webFile = SD.open("index.htm");        // open web page file
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(webFile.read()); // send web page to client
                            }
                            webFile.close();
                        }

in terms of the chipkit libary how would i send the webpage to the browse/client as they did above "client.write(webFile.read()); "

i tried tcpClient.println that didnt work.

please can someone point me in the right direction, i really need some help

i must also remind u i am still new to the world of arduino.

regards

Del


Jacob Christ

Sat, 27 Apr 2013 19:50:03 +0000

Here is a snip-it of code that is too be released at some later point. This was tested with a highly modified version of the the sever example that ships with chipKIT Ethernet library (not the DKnet library).

Jacob

/*
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This sketch is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this sketch; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
  Web Server Library Routines
 
 These routines are meant to work with the (original) chipKIT Webserver to serve
 files off of SD and/or render some simple pages or 404 page missing files.
 
 created 2012
 by pontech.com (Jacob Christ, Brian Dobrovodsky and Michael Skoczen)
 
 */

void render_web_page_not_found(Client *client) {
  Serial.println("render_web_page_not_found");
  client->println("HTTP/1.1 404 NOT FOUND");
  client->println("Content-Type: text/plain");
  client->println();
  client->println("PONTECH.COM quicK Web Server");
  client->println();
  client->println("ERROR 404: No matching content found");
  client->println();
}

void render_web_page_simple(Client *client, String *message) {
  Serial.println("render_web_page_simple");
  client->println("HTTP/1.1 200 OK");
  client->println("Content-Type: text/plain");
  client->println();
  client->println(*message);
}

void render_web_page_from_file(Client *client, char *filename) {
  Serial.println("render_web_page_from_file");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
#ifdef USE_SD
  char c[100];
  us8 index = 0;
  File dataFile = SD.open(filename);


  // if the file is available, write to it:
  if (dataFile) {
    // send a standard http response header
    client->println("HTTP/1.1 200 OK");
    client->println("Content-Type: text/html");
    //client->println("Content-Type: text/plain");
    client->println();
    while (dataFile.available()) {
      c[index++] = dataFile.read();
      c[index] = 0;
      if( index >= 99 ) {
        client->print(c);
        index = 0;
        c[index] = 0;
      }
      //Serial.print(c[0]);
    }
    client->print(c);
    dataFile.close();
    client->println("");
  }  
  // if the file isn't open, pop up an error:
  else {
#endif
    render_web_page_not_found(client);
#ifdef USE_SD
  } 
#endif
}

Jacob