使用Arduino Web服务器下载文件?

时间:2017-12-04 09:03:25

标签: file download arduino webserver ethernet

我想从本地网络下载存储在SD卡上的文本文件。

我使用WIZnet W5500的Arduino Uno和以太网屏蔽。我的文本文件名为TEST.txt

在互联网上找到不同的信息,我写了下面的代码,但是当我把IP地址放在我的网络浏览器中时,我自动下载了一个名为" TEST.txt"但它完全是空的!我不明白为什么我的文件中没有数据。

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

#define REQ_BUF_SZ   20
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xA5, 0xC0};
IPAddress ip(169, 254, 155, 166);
byte gateway [] = {169, 254, 155, 1};
byte subnet [] = {255, 255, 0, 0};
EthernetServer server(80);
File myFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;
String readString;

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

void setup() {
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  Serial.begin(9600);
  // initialize SD card
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;
  }
  Serial.println("SUCCESS - SD card initialized.");
  if (!SD.exists("TEST.txt")) {
    Serial.println("ERROR - Can't find index.htm file!");
    return;  // can't find index file
  }
  Serial.println("SUCCESS - Found TEST.txt file.");
  Ethernet.begin(mac, ip, gateway, subnet);  // initialize Ethernet device
  server.begin(); // start to listen for clients
  Serial.println("Ready");
}

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    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' && currentLineIsBlank) {
          // open requested web page file
          client.println();
          if (HTTP_req, "GET /TEST.txt") {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Disposition: attachment; filename=TEST.txt");
            client.println("Connection: close");
            client.println();
            Serial.println(readString); //print to serial monitor for debuging
            if (myFile) {
              byte clientBuf[64];
              int clientCount = 0;
              while (myFile.available()) {
                clientBuf[clientCount] = myFile.read();
                clientCount++;
                if (clientCount > 63) {
                  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 = "";
          }
        }
      }
    }
  }
}

0 个答案:

没有答案