Trying to create an ESP32 access to a public Spple calendar, but after connecting to the server and saving to SPIFFS it fails at the same point each time. I end up with a file of around 220 kB, when the total file should be 800 kB.
If it's helpful:
Headers from URL show:
Tranfer-Encoding: Chunked
Connection: keep-alive
Arduino IDE:
Sketch uses 891474 bytes (68%) of program storage space. Maximum is 1310720 bytes.
Global variables use 39640 bytes (12%) of dynamic memory, leaving 288040 bytes for local variables. Maximum is 327680 bytes.
Any help would be greatly appreciated, I've spent way more hours than I'd like to admit even getting here.
#include <WiFi.h>
#include <HTTPClient.h>
#include "FS.h"
#include "SPIFFS.h"
/* You only need to format SPIFFS the first time you run a
test or else use the SPIFFS plugin to create a partition
https://github.com/me-no-dev/arduino-esp32fs-plugin */
#define FORMAT_SPIFFS_IF_FAILED true
const char* ssid = "House N"; // your network SSID (name of wifi network)
const char* password = "Sebastian1963George"; // your network password
int LED_inbuilt = 2;
void setup() {
Serial.end();
delay(100);
Serial.begin(115200);
pinMode(LED_inbuilt, OUTPUT);
delay(1000);
Serial.print("WIFI status = ");
Serial.println(WiFi.getMode());
WiFi.disconnect(true);
delay(1000);
WiFi.mode(WIFI_STA);
delay(1000);
Serial.print("WIFI status = ");
Serial.println(WiFi.getMode());
digitalWrite(LED_inbuilt, HIGH);
delay(500);
digitalWrite(LED_inbuilt, LOW);
delay(500);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
Serial.println("SPIFFS Mount Failed");
return;
}
}
const char* root_ca= \
"-----BEGIN CERTIFICATE-----\n" \
"MIICiTCCAg+gAwIBAgIQH0evqmIAcFBUTAGem2OZKjAKBggqhkjOPQQDAzCBhTEL\n" \
"MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" \
"BxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMT\n" \
"IkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDgwMzA2MDAw\n" \
"MDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdy\n" \
"ZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09N\n" \
"T0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBFQ0MgQ2VydGlmaWNhdGlv\n" \
"biBBdXRob3JpdHkwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQDR3svdcmCFYX7deSR\n" \
"FtSrYpn1PlILBs5BAH+X4QokPB0BBO490o0JlwzgdeT6+3eKKvUDYEs2ixYjFq0J\n" \
"cfRK9ChQtP6IHG4/bC8vCVlbpVsLM5niwz2J+Wos77LTBumjQjBAMB0GA1UdDgQW\n" \
"BBR1cacZSBm8nZ3qQUfflMRId5nTeTAOBgNVHQ8BAf8EBAMCAQYwDwYDVR0TAQH/\n" \
"BAUwAwEB/zAKBggqhkjOPQQDAwNoADBlAjEA7wNbeqy3eApyt4jf/7VGFAkK+qDm\n" \
"fQjGGoe9GKhzvSbKYAydzpmfz1wPMOG+FDHqAjAU9JM8SaczepBGR7NjfRObTrdv\n" \
"GDeAU/7dIOA1mjbRxwG55tzd8/8dLDoWV9mSOdY=\n" \
"-----END CERTIFICATE-----\n";
void loop() {
getCalendar();
delay(1000);
readFile(SPIFFS, "/calendar.txt");
}
void getCalendar() {
File file = SPIFFS.open("/calendar.txt", "a");
if (!file) {
Serial.println("- failed to open file for writing");
return;
}
if ((WiFi.status() == WL_CONNECTED)) { // Check the current connection status
HTTPClient http;
http.begin("https://p64-caldav.icloud.com/published/2/MTExOTIzNTUyNDExMTkyM_t4rBfkJzy02anGOy6aHJIsgECeJGkvldH8kJEjylMtRScq7BUY4vOst7NZU_0bLtKJ0JICjDL0ECuF9L_p8lQ", root_ca); //Specify the URL and certificate
int httpCode = http.GET(); // Make the request
if (httpCode == HTTP_CODE_OK) { // Check for the returning code
http.writeToStream(&file);
}
file.close();
Serial.println("you have finished downloading");
http.end(); // Free the resources
}
delay(1000);
}
void readFile(fs::FS &fs, const char * path) {
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return;
}
Serial.println("- read from file:");
while (file.available()) {
Serial.write(file.read());
}
}
void deleteFile(fs::FS &fs, const char * path) {
Serial.printf("Deleting file: %s\r\n", path);
if (fs.remove(path)) {
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}