Pages: [1]   Go Down
Author Topic: File download with Arduino web server?  (Read 1503 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6899
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a thought. If I have Arduino server loaded with web pages, why not having a few links on the web pages that point to several data log files for download?

My question is, how to? I have some rough ideas so I appreciate it if experts can help me polish the ideas before I go ahead and code it (into non-working result of course).

Arduino web server has a web page and a link on the page say:

<a herf="datalog.txt" target="_blank">Download data log</a>.

The upon getting a GET method like the following, Arduino will confirm the file name exists on the SD card:

GET /datalog.txt HTTP/1.1

Arduino server then writes a response like:

something something
Content-Type: text/csv
Connection: close

1234, 567, 890 (log file content)
2345, 678, 901

(end of transmission)

Will this be sufficient? Thank you!
Logged


Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What code base do you use for your web server? Most web server code serving data from the SD card already includes your needed features with the exception that they don't select the text/csv MIME type if a *.txt file is chosen.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6899
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What code base do you use for your web server? Most web server code serving data from the SD card already includes your needed features with the exception that they don't select the text/csv MIME type if a *.txt file is chosen.

pylon,

I am writing code to make my own web server on arduino MEGA so I don't know what code base. Do you mean some sort of encoding binary into alphanumerical code? This needs to be integrated into an existing project so I prefer writing my own code and make it only as powerful as needed not to be capable of processing other things that I don't need. My goal is to put a link on my arduino-generated web page. If it gets clicked, arduino will reply with the file, like if you click on a .csv file link on this webpage (look below) and your browser asks you where to store that file or maybe just displays it on the browser screen. I wanted to know whether my naive thinking is correct.

* fcm-eddm-retail.csv (1 KB - downloaded 9 times.)
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6899
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My initial tests are positive. I can make Arduino serve files to a web client (firefox browser). But the speed is very slow around 70-80 bytes per second. I was reading file from SD card and sending via wifi shield. I'll have to find the reason of this slow speed or it's not practical.
Logged


0
Offline Offline
Tesla Member
***
Karma: 115
Posts: 8933
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code that increases the sent packet size from an SD card.

Code:
//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

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
String readString;

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

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");
}

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

            client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          //client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");

          client.println();

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");

          if (myFile) {

            byte clientBuf[64];
            int clientCount = 0;

            while(myFile.available())
            {
              clientBuf[clientCount] = myFile.read();
              clientCount++;

              if(clientCount > 63)
              {
                // Serial.println("Packet");
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
            //final <64 byte cleanup packet
            if(clientCount > 0) client.write(clientBuf,clientCount);           
            // close the file:
            myFile.close();
          }
          delay(1);
          //stopping client
          client.stop();
          readString="";
        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But the speed is very slow around 70-80 bytes per second. I was reading file from SD card and sending via wifi shield. I'll have to find the reason of this slow speed or it's not practical.

My guess is (as was the guess of zoomkat) that you're sending one complete packet for every character you want to transmit. You get a much higher throughput if you take care (by not using the print/println methods but the write(ptr, size) method) that packets contain more than one byte of data.

If you show us your code we may be able to help you.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6899
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But the speed is very slow around 70-80 bytes per second. I was reading file from SD card and sending via wifi shield. I'll have to find the reason of this slow speed or it's not practical.

My guess is (as was the guess of zoomkat) that you're sending one complete packet for every character you want to transmit. You get a much higher throughput if you take care (by not using the print/println methods but the write(ptr, size) method) that packets contain more than one byte of data.

If you show us your code we may be able to help you.

I bet I was!

My essential code:
Code:
while(myFile.available()
{
  client.write(myFile.read());
}

After doing something similar to zoomkat's code, I was able to get about 20KB/s (hard to measure unless I send a much larger file)

I noticed the client.print in wifi class has a size limit of around 90 bytes. Beyond the limit if you print a string of say 100 bytes, nothing is received.

Zoomkat,

Is the 64 just the buffer size (small to save SRAM) or is it tied to limitation of Ethernet hardware? Thanks.
Logged


0
Offline Offline
Tesla Member
***
Karma: 115
Posts: 8933
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is the 64 just the buffer size (small to save SRAM) or is it tied to limitation of Ethernet hardware? Thanks.

I didn't write it, but I think the size was chosen to not challenge the SRAM limitations.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: