Go Down

Topic: sdcard load pictures with effective way (Read 737 times) previous topic - next topic

mostafa_fawzy

i could to upload a picture stored in sdcard using zoomkat codes  like this 

     if(readString.indexOf("image") >=0) {
          client.println("Content-Type: image/jpeg");
          client.println(); }
         
         
         
                 
         
          //select file to send to browser
          if(readString.indexOf("image") >= 0) {
            File myFile = SD.open("image.jpg");
            if (myFile) {
              while (myFile.available()) {
                client.write(myFile.read());
              }
              myFile.close();
            }
          }
         

my  problem  now  is  i have  many pictures on the sd card and need to upload all of them  if i repeat the code  i will waste  a lot of  memory  so

my question is can i do a for loop  to  identify  and upload many pictures ??? i  tried  but  not working  any  suggestion  will be  appreciated 

the picture  i  want  to  upload  has  this  name (IMAGE00 -> IMAGE09)

UKHeliBob

Please post the code you tried even if it didn't work.

It sounds to me as though you need a function to read a file and for the name of the file to be passed to the function as a parameter.  That way the file reading code only has to be be written and maintained in the function, not multiple times.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mostafa_fawzy

this  is  all  of  my  code  for  one  picture  only   

//zoomkat 1/26/13
//SD server slider test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84/servosld.htm when submited
//for use with W5100 based ethernet shields
//put the servosld.htm, slider.js, bluev_sl.gif,
//and bluev_bg.gif on the SD card
//files at http://web.comporium.net/~shb/servoslider.htm page


#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
String readString  ;
byte mac[] = {0x90, 0xA2, 0xDA, 0x80, 0x0A, 0xC6 }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

//////////////////////

void setup(){

  Serial.begin(9600);
 
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  //delay(2000);
  server.begin();


}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging
         
         //select proper header for file to be sent to browser
         client.println("HTTP/1.1 200 ok"); //send new page
                   
                           
           if(readString.indexOf("bk") >=0) {
          client.println("Content-Type: image/jpeg");
          client.println(); }
         
         
         
                 
         
          //select file to send to browser
          if(readString.indexOf("bk") >= 0) {
            File myFile = SD.open("bk.jpg");
            if (myFile) {
              while (myFile.available()) {
                client.write(myFile.read());
              }
              myFile.close();
            }
          }
         
          delay(1);
          //stopping client
          client.stop();
         
       
          readString="";
         
         
        }
      }
    }
  }
}

XOIIO

Maybe make the image name a variable, and add a counter so that it increases it every time it loops to upload an image?

mostafa_fawzy

The question is this             File myFile = SD.open("bk.jpg");  accept variable name ???

UKHeliBob

Code: [Select]
File myFile = SD.open("bk.jpg");
try
Code: [Select]
File myFile = SD.open(variable);

PREDICTION : Your next question will be "how do I create a series of filenames and put them into a variable to use with the function"
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mostafa_fawzy

it`s  true  :smiley-roll:   my  skills  in  coding  is  very  poor  so   it`s  a  problem  to  me 

UKHeliBob

Start by explicitly setting a variable to a filename in the program then using the variable with SD.open()
If it works then you are moving in the right direction.  You could then consider setting the variable explicitly to the next filename and calling the function and so on, but maybe you can do better ....

Will the filenames always be the same, will there always be the same number of them, is there a pattern to the filenames ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mostafa_fawzy

yes  all  the  pictures  has  the  name (IMAGE00 -> IMAGE09)   10  pictures  with  these  names

UKHeliBob

In which case you can use something like this to create the filenames
Code: [Select]
char fileName[] = {"IMAGE0 "};

void setup()
{
  Serial.begin(115200);

  for (byte fileNumber = 0; fileNumber < 10; fileNumber++)
  {
    fileName[6] = (char)fileNumber + 48;
    Serial.println(fileName);
  }
}

void loop()
{
}

Instead of printing the filename you would, of course, pass it to your function that loads the file from the SD card.

NOTE - this will only work for filenames from 00 to 09
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mostafa_fawzy

thanx  a lot for  your  effort  and  reply   i  try  your  code  and  passing  a variable name  to   
            File myFile = SD.open(fileName.jpg);

but  this  error  face  me  "request for member 'jpg in 'fileName' which is of non-class type char[8]"

UKHeliBob

You can't just stick an extension (or anything else) on the end of a variable and expect it to work, hence the error.
The code was meant to give you an idea of what you could do, not a complete solution.

Change
Code: [Select]
char fileName[] = {"IMAGE0 "};to
Code: [Select]
char fileName[] = {"IMAGE0 .jpg"};in my code and run it to make sure that you get the results that you expect before trying it in your code.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

zoomkat

I've tried to figure out a single function to support uploading several files from the SD card for the below code. So far I haven't figured out how to set the file name to a variable that is acceptable to the SD file open actions.   

Code: [Select]

//zoomkat 1/26/13
//SD server slider test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84/servosld.htm when submited
//for use with W5100 based ethernet shields
//put the servosld.htm, slider.js, bluev_sl.gif,
//and bluev_bg.gif on the SD card
//files at http://web.comporium.net/~shb/servoslider.htm page


#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo
Servo myservoe, myservof, myservog;
String readString, pos;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 102 }; // ip in lan
byte gateway[] = {
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

//////////////////////

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  //delay(2000);
  server.begin();
  Serial.println("Ready");
 
  myservoa.attach(2);  //the pin for the servoa control
  myservob.attach(3);  //the pin for the servob control
  myservoc.attach(5);  //the pin for the servoc control
  myservod.attach(6);  //the pin for the servod control
  myservoe.attach(7);  //the pin for the servoa control
  myservof.attach(8);  //the pin for the servob control
  myservog.attach(9);  //the pin for the servoc control
  //myservoh.attach(10);  //the pin for the servod control

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging
         
         //select proper header for file to be sent to browser
           if(readString.indexOf("Submit") >=0) { //don't send new page
           client.println("HTTP/1.1 204 Zoomkat");
           client.println();
           client.println(); }
           
          //client.println("HTTP/1.1 200 OK"); //send new page
          if(readString.indexOf("servosld") >=0) {
          client.println("HTTP/1.1 200 OK"); //send new page         
          client.println("Content-Type: text/html");
          client.println(); }

          if(readString.indexOf("slider") >=0) {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: application/x-javascript");
          client.println(); }
         
          if(readString.indexOf("bluev") >=0) {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: image/gif");
          client.println(); }
         
          //select file to send to browser
          if(readString.indexOf("servosld") >=0) {
            File myFile = SD.open("SERVOSLD.HTM");
            if (myFile) {
              while (myFile.available()) {
                client.write(myFile.read());
              }
              myFile.close();
            }
          }

          if(readString.indexOf("slider") >=0) {
            File myFile = SD.open("slider.js");
            if (myFile) {
              while (myFile.available()) {
                client.write(myFile.read());
              }
              myFile.close();
            }
          }

          if(readString.indexOf("bluev_sl") >=0) {
            File myFile = SD.open("bluev_sl.gif");
            if (myFile) {
              while (myFile.available()) {
                client.write(myFile.read());
              }
              myFile.close();
            }
          }

          if(readString.indexOf("bluev_bg") >=0) {
            File myFile = SD.open("bluev_bg.gif");
            if (myFile) {
              while (myFile.available()) {
                client.write(myFile.read());
              }
              myFile.close();
            }
          }

          delay(1);
          //stopping client
          client.stop();

          //process GET string request from client and and position servo
         
          pos = readString.substring(8, 12); //get the first four characters         
          //Serial.println(pos);
          int n = pos.toInt();  //convert readString into a number   
          Serial.println(n);
          Serial.println();
         
          if(readString.indexOf("?0") >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf("?1") >0) myservob.writeMicroseconds(n);
          if(readString.indexOf("?2") >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf("?3") >0) myservod.writeMicroseconds(n);
          if(readString.indexOf("?4") >0) myservoe.writeMicroseconds(n);
          if(readString.indexOf("?5") >0) myservof.writeMicroseconds(n);
          if(readString.indexOf("?6") >0) myservog.writeMicroseconds(n);
          //only seven servo pins, so back to myservoa for testing
          if(readString.indexOf("?7") >0) myservoa.writeMicroseconds(n);

          //clearing string for next read
          readString="";
          pos="";
        }
      }
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PaulS

Quote
So far I haven't figured out how to set the file name to a variable that is acceptable to the SD file open actions.   

That's because you think that the only character-handling tool on the Arduino is the String class. There are also strings -  NULL terminated arrays of chars. sprintf() would let you construct a name based on a variable:
Code: [Select]

int servoNum = 12;
char buff[16];
sprintf(buff, "Servo%d.dat", servoNum);
file servoData = SD.open(buff);

SurferTim

And that ethernet transfer code is slow. Read this and the posts that follow in this thread.
http://forum.arduino.cc/index.php?topic=134868.msg1123725#msg1123725

Go Up