I was looking for hours and hours in this forum, I saw a lot of discusses about this problem but with no solution, no example. For an open source, and for a simple loading image through webserver with arduino, I think it should be an example with the arduino package.
Please give me a simple example index.htm file to be accessed by a client, file located onto microSD of the shield W5100 which has an image tag <src=image.jpg> also for the image having the location onto microSD. How can be the page loaded in browser having the image displayed. Please give a short example, explain very shortly the example for everybody to understand a small needed thing for next development steps.
Here is a sample of a small webserver loading a index.htm file from the SD card. The index.htm file contains a reference to a jpeg picture on the SD card.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xE7, 0xAC };
IPAddress ip(192, 168, 178, 90); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile;
char header[80];
char filename[80];
int txtlen;
uint8_t buf[1000];
int aantal;
void setup()
{
pinMode(53, OUTPUT);
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
}
void loop()
{
EthernetClient client = server.available(); // try to get client
txtlen = 0;
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
if (txtlen < 79) {
header[txtlen++] = c;
header[txtlen] = 0;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
ExtractFileName();
Serial.print("Extracted filename is: ");
Serial.println(filename);
webFile = SD.open(filename); // open web file
if (!webFile) {
Serial.println("File not found!!");
client.println("HTTP/1.1 404 NOT FOUND");
}
else {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
}
client.println("Connection: close");
client.println();
// send web page
if (webFile) {
while(aantal = webFile.available()) {
if (aantal > 1000) {
webFile.read(buf, 1000);
client.write(buf, 1000);
}
else {
webFile.read(buf, aantal);
client.write(buf, aantal); // send web page to client
}
}
webFile.close();
}
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(100); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
void ExtractFileName()
{
int i, j;
i=0;
while (header[i++] != ' ') {
;
}
j=0;
while (header[i] != ' ') {
filename[j++] = header[i++];
}
filename[j] = 0;
if (filename[0] == '/' && filename[1] == 0) {
strcpy(filename, "index.htm");
}
}
Are you sure that is not a Mega? Here are the hints:
// you are going to start all this
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// then later
uint8_t buf[1000];
// and in setup(), this is the Mega's slave select
pinMode(53, OUTPUT);
I don't think an Uno would survive that with 2K SRAM. It does if you reduce the size of buf[] to about 64 to have any useable SRAM left.
Yes, I am using this on a Mega2560. Reducing the size of the buf[] to say 50 or 100 will do just fine on an Uno.
Adjust the tests for maximum read and write accordingly.
When I try to write the arduino I have the following errors:
arduino-1.0.3\libraries\SD\SdFatUtil.cpp: In function 'void SdFatUtil::SerialPrint_P(const prog_char*)':
arduino-1.0.3\libraries\SD\SdFatUtil.cpp:65: error: 'SdFat' has not been declared
arduino-1.0.3\libraries\SD\SdFatUtil.cpp: In function 'void SdFatUtil::SerialPrintln_P(const prog_char*)':
arduino-1.0.3\libraries\SD\SdFatUtil.cpp:73: error: 'SdFat' has not been declared