1

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");
  }
}

ocrdu
  • 1,673
  • 2
  • 9
  • 22
A. Coote
  • 21
  • 1
  • 3
  • this sounds like it's begging to be pre-processed on a different computer/VPS, summarized, then provided to the ESP32 in no-mess form. – dandavis Aug 03 '20 at 08:12
  • Haha, potentially, but I was hoping to get a standalone device that did everything, and it seems like ESP32 should be able to do it, plus I don't have much experience in either, and learning a whole new area of computer programming to get it working seems overwhelming! – A. Coote Aug 03 '20 at 08:15
  • 1
    You are basically running out of memory, use [streamHttpClient](https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino). – hcheung Aug 03 '20 at 08:28
  • Just wanted to say thanks to dandavis and hcheung! Got that bit working! The project as a whole has defeated me, but you both were really helpful, thanks! – A. Coote Aug 03 '20 at 10:19

0 Answers0