乐鑫技术交流
直播中

小峰

13年用户 972经验值
私信 关注
[问答]

下载大文件使用HTTPClient和WiFiClient崩溃了怎么解决?

我想从受 SSL/TLS 保护的站点将文件下载到我的 ESP8266。
我从 HTTP over TLS (HTTPS) 示例草图开始,下载一个 3 字节的小文件后效果很好。当我将文件步进到大约 100,000 字节的更大文件时,我遇到了崩溃。崩溃似乎发生在 GET 调用之后的一小段时间。如果我在 GET 之后注释掉所有访问 HTTPClient 对象的调用,我就不会崩溃。如果我只注释掉 http.getStreamPtr(); 我仍然在 .End() 上遇到崩溃。似乎没有任何帮助。我可以调用 http.getSize(); 连续 5 次,没有崩溃,但之后的任何其他调用都崩溃了。
文件的内容是否会导致 HTTPClient 崩溃?
我会不会内存不足并且 http 对象被杀死,然后任何调用任何 http.xxx() 的尝试都会导致取消引用失败?我可以尝试缩小范围吗?
代码:全选/*
    HTTP over TLS (HTTPS) example sketch
    This example demonstrates how to use
    WiFiClientSecure class to access HTTPS API.
    We fetch and display the status of
    esp8266/Arduino project continuous integration
    build.
    Limitations:
      only RSA certificates
      no support of Perfect Forward Secrecy (PFS)
      TLSv1.2 is supported since version 2.4.0-rc1
    Created by Ivan Grokhotkov, 2015.
    This example is in public domain.
*/

#include
#include
#include

const char* ssid = "xxxx";
const char* password = "xxxx";
const char* host = "test.test.com";
const int httpsPort = 8080;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19";
uint8_t buff[128] = { 0 };
int len;
HTTPClient http;
WiFiClientSecure client;
WiFiClient* stream;

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  len=0;  
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  if (client.verify(fingerprint, host)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }

  http.begin("https://test.test.com/app/upload/common/platform/recovery/2_1/test.bin" , fingerprint);

  String postMessage = "";
  http.setReuse(true);
  int httpCode = http.POST(postMessage);
  if(httpCode == 200) {
    Serial.print("http result:");
    Serial.println(httpCode);
    Serial.println("File Size: ");
    len = http.getSize();
    Serial.println(len);
    if(http.connected()) {
      Serial.println("Getting Stream");
      stream = http.getStreamPtr();      
      Serial.println("Got Stream");
    }
    Serial.println("After Stream");   
    http.end();
  }
  else {
    Serial.println("Call Failed");
  }
}

void loop() {
}


更多回帖

发帖
×
20
完善资料,
赚取积分