#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
// URL of file to download over http
// Change this to the URL of your file. My test file is was created with:
// dd if=/dev/urandom of=./1MB_file.bin bs=1024 count=1024
// The file to get.
//const char* url = "https://192.168.1.1/1MB_file.bin";
const char* url = "https://jwsmythe.com/xfer/esp8266-32-group/1MB_file.bin";
// How many times to get it per loop.
int num_downloads = 10;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
int start_time = micros();
int end_time = micros();
int response_size = 0; // Holds the size of the current chunk.
int total_elapsed = 0; // Holds the elapsed time counter for the loop.
int chunk_elapsed = 0; // Holds the time to download the current chunk. Don't count non-200 status.
float chunk_speed = 0.0; // Holds the speed of the current chunk.
float total_speed = 0.0; // Holds the speed of the total download.
int download_count = 0; // This is the counter. Preset to 0.
int total_bytes = 0; // Holds the total bytes transferred.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print("+++ Serial Initialized \n");
// Connect to WiFi
Serial.print("+++ Connecting to WiFi \n");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\n+++ WiFi Connected \n");
Serial.print("... IP Address: ");
Serial.print(WiFi.localIP());
Serial.print("\n");
}
void loop() {
// Download url to file
HTTPClient http;
http.begin(url);
http.setTimeout(15000);
download_count = 1;
total_bytes = 0;
chunk_elapsed = 0;
total_elapsed = 0;
start_time = 0;
end_time = 0;
Serial.print("****************************************\n");
while(download_count < num_downloads){
start_time = micros();
int http_status = http.GET();
// Check for the returning code
end_time = micros();
chunk_elapsed = end_time - start_time;
if (http_status == 200) {
// Our datafile is random garbage. Don't show it to the user.
//String payload = http.getString();
//Serial.println(payload);
response_size = http.getSize();
total_bytes =+ response_size;
total_elapsed =+ chunk_elapsed;
chunk_speed = ((response_size*8) / 1000) / (chunk_elapsed / 1000000);
Serial.print("+++ Block "); Serial.print(download_count); Serial.print(" "); Serial.print(url); Serial.print("\n");
Serial.print("+++ HTTP status: "); Serial.print(http_status); Serial.print("\n");
Serial.print("... Block size "); Serial.print(response_size); Serial.print(" bytes\n");
Serial.print("... Elapsed time "); Serial.print(chunk_elapsed); Serial.print(" microseconds\n");
Serial.print("... Speed "); Serial.print(chunk_speed); Serial.print(" Kb/s\n");
download_count++;
}else if(http_status == 404){
Serial.print("--- HTTP status: NOT FOUND ("); Serial.print(http_status); Serial.print(")\n");
}else if(http_status == 304){
Serial.print("--- HTTP status: NOT redirct ("); Serial.print(http_status); Serial.print(")\n");
Serial.print("... Give the final URL of the file. It may have been redirected.\n");
}else {
// Unnecessary noise. Throwing HTTP -7 on every other attempt. Not sure what's causing it.
// The web server isn't throwing any errors.
Serial.print("--- HTTP status: OTHER ("); Serial.print(http_status); Serial.print(")\n");
}
http.end(); //Free the resources
};
Serial.print("+++ Total bytes downloaded: "); Serial.print(total_bytes); Serial.print(" +++\n");
Serial.print("+++ Total elapsed time (microseconds) "); Serial.print(total_elapsed); Serial.print(" ...\n");
total_speed = ((total_bytes/1000)*8) / (total_elapsed / 1000000);
Serial.print("+++ Overall Speed: "); Serial.print(total_speed); Serial.print(" Kb/s +++\n");
delay(1000);
}