First User
First User

Reputation: 771

Arduino ESP8266 download a large file in chunks?

I am testing downloading a large file (2.7 KB) from a URL my github repository. I wanted to test downloading the file in chunks of 100 bytes and writing it to an SPIFFS file. I basically want to emulate the following Python code in C++:

import requests
url = "https://link.to.big.file"
response = requests.get(url, stream=True)
for chunk in response.iter_content(chunk_size=100):
    with open(filename, "wb") as f:
        f.write(chunk)

So far I have the following function going:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <FS.h>

int downloadFile(const char* url, const char* fileName, size_t chunkSize)
{
   // assume wifi connected
    std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
    client->setInsecure();
    HTTPClient https;
    https.begin(*client, url); // connect to server over HTTPS
    https.GET(); // make get request
    SPIFFS.begin(); // initialize file system
    File file = SPIFFS.open(fileName, "w") // create a file to write the downloaded data to in chunks of 100
    
    // download in chunks and write to file??
    return 0; // on success
}

I have left out the error checking statements for brevity. I have looked into the HTTPClient documentation here, but could not find what i was looking for. A small code example would really help. I can adapt it to my use case. Thanks!

Upvotes: 0

Views: 351

Answers (0)

Related Questions