2023最新版ESP8266 Arduino Http网页结果存入LittleFS文件

老版本正常2020年下载安装的Aruduino和ESP8266开发板库

  bool downloadFile(const String &strPath)
  {
    WiFiClient client;
    HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)

    Serial.print(F("HTTP begin\n"));
    String url = String(DefineConst::WEB_HOST)+ F("public/static/") + RegDevice::GetSubDir() + strPath
    Serial.println(url);

    // configure server and url
    http.begin(client, url);
    //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");

    Serial.println(F("HTTP GET"));
    // start connection and send HTTP header
    int httpCode = http.GET();
    if ( 0>=httpCode )
     {
      Serial.println( String(F("HTTP GET failed,error:"))+ http.errorToString(httpCode));
      return false;
    }
    
    // HTTP header has been send and Server response header has been handled
    Serial.print( F("HTTP GET code:"));
    Serial.println( httpCode);

    // file found at server
    if (httpCode != HTTP_CODE_OK)
    {
      Serial.println( String(F("HTTP GET failed,httpCode:"))+ httpCode);
      return false;
    }
    // get lenght of document (is -1 when Server sends no Content-Length header)
    int len = http.getSize();

    // create buffer for read
    uint8_t buff[128] = { 0 };

    // get tcp stream
    WiFiClient * stream = &client;
    Serial.println(strPath);
    fs::File f = FIlE_SYS.open("/"+strPath, "w");
    // read all data from server
    int nTotalRead=0;
    while (http.connected() && (len > 0 || len == -1)) {
      // read up to 128 byte
      const auto c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
      Serial.print( F("readBytes:"));
      Serial.println(c);
      nTotalRead+=c;
      if (!c) {
        Serial.println( String(F("read timeout,c=")) + c );
        return false;
      }

      // write it to Serial
//      Serial.write(buff, c);
      const auto w=f.write(buff, c);
      if(c!=w){
        Serial.println( String(F("f.write err,"))+c+F("!=")+w+F(",nTotalRead=") + nTotalRead);
        return false;
      }
      if (len > 0) {
        len -= c;
      }
    }

    Serial.println( String(F("file be written,nTotalRead=")) + nTotalRead);
    f.close();
    // Serial.println();
    Serial.println(F("HTTP connection closed or file end."));

    http.end();
    return true;
  }

新版本2023年10月全新安装在上面的readBytes卡5秒然后返回读取到0字节,

用http.getString()替换readBytes

写文件用f.print( payload)替换f.write(buff, count);

  bool downloadFile(const String &strPath)
  {
    WiFiClient client;
    HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)

    Serial.print(F("HTTP begin\n"));
    String url = String(DefineConst::WEB_HOST)+ F("public/static/") + RegDevice::GetSubDir() + strPath;
    Serial.println(url);

    // configure server and url
    http.begin(client, url);
    //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");

    Serial.println(F("HTTP GET"));
    // start connection and send HTTP header
    int httpCode = http.GET();
    if ( 0>=httpCode )
     {
      Serial.println( String(F("HTTP GET failed,error:"))+ http.errorToString(httpCode));
      return false;
    }
    
    // HTTP header has been send and Server response header has been handled
    Serial.print( F("HTTP GET code:"));
    Serial.println( httpCode);

    // file found at server
    if (httpCode != HTTP_CODE_OK)
    {
      Serial.println( String(F("HTTP GET failed,httpCode:"))+ httpCode);
      return false;
    }
    // get lenght of document (is -1 when Server sends no Content-Length header)
    int len = http.getSize();

    // create buffer for read
    // uint8_t buff[128] = { 0 };

    // get tcp stream
    WiFiClient * stream = &client;
    Serial.println(strPath);
    fs::File f = FIlE_SYS.open("/"+strPath, "w");
    // read all data from server
    int nTotalRead=0;
    // while (http.connected() && (len > 0 || len == -1)) {
      // read up to 128 byte
      // const auto c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
       const auto payload = http.getString();
      Serial.print( F("readBytes:"));
      Serial.println(payload);
      // nTotalRead+=c;
      // if (!c) {
      //   Serial.println( String(F("read timeout,c=")) + c );
      //   return false;
      // }

      // write it to Serial
//      Serial.write(buff, c);
      // const auto w=f.write(buff, c);
      const auto w=f.print( payload);//,payload.length());
      if( payload.length() != w){
        Serial.println( String(F("f.write err,"))+payload.length()+F("!=")+w+F(",nTotalRead=") + nTotalRead);
        return false;
      }
      // if (len > 0) {
      //   len -= c;
      // }
    // }

    Serial.println( String(F("file be written,nTotalRead=")) + nTotalRead);
    f.close();
    // Serial.println();
    Serial.println(F("HTTP connection closed or file end."));

    http.end();
    return true;
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Arduino使用ESP8266LittleFS文件系统可用于存储和管理文件LittleFS是专为嵌入式系统设计的文件系统,可以轻松地在ESP8266上使用。 首先,你需要在Arduino IDE中安装ESP8266核心。然后,在你的Arduino项目中,包含`FS.h`头文件,并在setup函数中初始化文件系统。 ```cpp #include <FS.h> void setup() { // 初始化LittleFS文件系统 if (!LittleFS.begin()) { Serial.println("An error occurred while mounting LittleFS"); return; } } ``` 现在,你可以使用LittleFS文件系统操作文件,比如读取、写入、追加和删除文件等。下面是一些常见的操作示例: 1. 创建一个文件: ```cpp File file = LittleFS.open("/myfile.txt", "w"); if (file) { file.println("Hello, world!"); file.close(); } else { Serial.println("Failed to create file"); } ``` 2. 读取文件内容: ```cpp File file = LittleFS.open("/myfile.txt", "r"); if (file) { while (file.available()) { Serial.write(file.read()); } file.close(); } else { Serial.println("Failed to open file"); } ``` 3. 追加内容到文件末尾: ```cpp File file = LittleFS.open("/myfile.txt", "a"); if (file) { file.println("This will be appended at the end"); file.close(); } else { Serial.println("Failed to open file"); } ``` 4. 删除文件: ```cpp if (LittleFS.remove("/myfile.txt")) { Serial.println("File deleted successfully"); } else { Serial.println("Failed to delete file"); } ``` 这些是基本的文件操作示例,你可以根据需要进一步扩展,并使用LittleFS文件系统进行更复杂的操作。记得在使用完文件后关闭它,以便释放资源。 使用LittleFS文件系统,你可以方便地在ESP8266上读写文件,并且具有更高的灵活性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值