当前位置:   article > 正文

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

arduino-esp8266littlefs

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

  1. bool downloadFile(const String &strPath)
  2. {
  3. WiFiClient client;
  4. HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)
  5. Serial.print(F("HTTP begin\n"));
  6. String url = String(DefineConst::WEB_HOST)+ F("public/static/") + RegDevice::GetSubDir() + strPath
  7. Serial.println(url);
  8. // configure server and url
  9. http.begin(client, url);
  10. //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
  11. Serial.println(F("HTTP GET"));
  12. // start connection and send HTTP header
  13. int httpCode = http.GET();
  14. if ( 0>=httpCode )
  15. {
  16. Serial.println( String(F("HTTP GET failed,error:"))+ http.errorToString(httpCode));
  17. return false;
  18. }
  19. // HTTP header has been send and Server response header has been handled
  20. Serial.print( F("HTTP GET code:"));
  21. Serial.println( httpCode);
  22. // file found at server
  23. if (httpCode != HTTP_CODE_OK)
  24. {
  25. Serial.println( String(F("HTTP GET failed,httpCode:"))+ httpCode);
  26. return false;
  27. }
  28. // get lenght of document (is -1 when Server sends no Content-Length header)
  29. int len = http.getSize();
  30. // create buffer for read
  31. uint8_t buff[128] = { 0 };
  32. // get tcp stream
  33. WiFiClient * stream = &client;
  34. Serial.println(strPath);
  35. fs::File f = FIlE_SYS.open("/"+strPath, "w");
  36. // read all data from server
  37. int nTotalRead=0;
  38. while (http.connected() && (len > 0 || len == -1)) {
  39. // read up to 128 byte
  40. const auto c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
  41. Serial.print( F("readBytes:"));
  42. Serial.println(c);
  43. nTotalRead+=c;
  44. if (!c) {
  45. Serial.println( String(F("read timeout,c=")) + c );
  46. return false;
  47. }
  48. // write it to Serial
  49. // Serial.write(buff, c);
  50. const auto w=f.write(buff, c);
  51. if(c!=w){
  52. Serial.println( String(F("f.write err,"))+c+F("!=")+w+F(",nTotalRead=") + nTotalRead);
  53. return false;
  54. }
  55. if (len > 0) {
  56. len -= c;
  57. }
  58. }
  59. Serial.println( String(F("file be written,nTotalRead=")) + nTotalRead);
  60. f.close();
  61. // Serial.println();
  62. Serial.println(F("HTTP connection closed or file end."));
  63. http.end();
  64. return true;
  65. }

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

用http.getString()替换readBytes

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

  1. bool downloadFile(const String &strPath)
  2. {
  3. WiFiClient client;
  4. HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)
  5. Serial.print(F("HTTP begin\n"));
  6. String url = String(DefineConst::WEB_HOST)+ F("public/static/") + RegDevice::GetSubDir() + strPath;
  7. Serial.println(url);
  8. // configure server and url
  9. http.begin(client, url);
  10. //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
  11. Serial.println(F("HTTP GET"));
  12. // start connection and send HTTP header
  13. int httpCode = http.GET();
  14. if ( 0>=httpCode )
  15. {
  16. Serial.println( String(F("HTTP GET failed,error:"))+ http.errorToString(httpCode));
  17. return false;
  18. }
  19. // HTTP header has been send and Server response header has been handled
  20. Serial.print( F("HTTP GET code:"));
  21. Serial.println( httpCode);
  22. // file found at server
  23. if (httpCode != HTTP_CODE_OK)
  24. {
  25. Serial.println( String(F("HTTP GET failed,httpCode:"))+ httpCode);
  26. return false;
  27. }
  28. // get lenght of document (is -1 when Server sends no Content-Length header)
  29. int len = http.getSize();
  30. // create buffer for read
  31. // uint8_t buff[128] = { 0 };
  32. // get tcp stream
  33. WiFiClient * stream = &client;
  34. Serial.println(strPath);
  35. fs::File f = FIlE_SYS.open("/"+strPath, "w");
  36. // read all data from server
  37. int nTotalRead=0;
  38. // while (http.connected() && (len > 0 || len == -1)) {
  39. // read up to 128 byte
  40. // const auto c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
  41. const auto payload = http.getString();
  42. Serial.print( F("readBytes:"));
  43. Serial.println(payload);
  44. // nTotalRead+=c;
  45. // if (!c) {
  46. // Serial.println( String(F("read timeout,c=")) + c );
  47. // return false;
  48. // }
  49. // write it to Serial
  50. // Serial.write(buff, c);
  51. // const auto w=f.write(buff, c);
  52. const auto w=f.print( payload);//,payload.length());
  53. if( payload.length() != w){
  54. Serial.println( String(F("f.write err,"))+payload.length()+F("!=")+w+F(",nTotalRead=") + nTotalRead);
  55. return false;
  56. }
  57. // if (len > 0) {
  58. // len -= c;
  59. // }
  60. // }
  61. Serial.println( String(F("file be written,nTotalRead=")) + nTotalRead);
  62. f.close();
  63. // Serial.println();
  64. Serial.println(F("HTTP connection closed or file end."));
  65. http.end();
  66. return true;
  67. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/351571?site
推荐阅读
相关标签
  

闽ICP备14008679号