Go Down

Topic: Arduino uno + W5100 + SD to load the image jpg (Read 2237 times) previous topic - next topic

antonio1

Hi,

   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.

   Thank you a lot for any help,
    Best regards,

adwortel

#1
Apr 09, 2013, 05:47 pm Last Edit: Apr 09, 2013, 05:53 pm by adwortel Reason: 1
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.

Code: [Select]
#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");
  }
}


This is the index.htm file on the SD card:


  • <html>

    <head>
    <meta http-equiv="Content-Language" content="nl">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>This picture is from the Arduino</title>
    </head>

    <body>

    <p><font size="5">This picture is from the Arduino webserver:</font></p>
    <p>
    <img border="0" src="http://192.168.178.90/picture.jpg" width="349" height="262"></p>
    <p>&nbsp;</p>

    </body>

    </html>



SurferTim

#2
Apr 09, 2013, 06:02 pm Last Edit: Apr 09, 2013, 06:13 pm by SurferTim Reason: 1
Are you sure that is not a Mega? Here are the hints:
Code: [Select]
// 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.

edit: Read this thread from here down:
http://arduino.cc/forum/index.php/topic,134868.msg1123725.html#msg1123725

adwortel

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.

antonio1

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

   How could I solve these errors?

thank you,

softm


bianchi

I got this message, even "index.htm" is already on SD card, why is it ??

Initializing SD card...
SUCCESS - SD card initialized.
GET / HTTP/1.1
Host: 192.168.1.90
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

Extracted filename is: index.htm
File not found!!

Go Up