Hello everyone,
So I've recently come into possession of an ESP32 and am in the process of learning how to use it. I've been able to hook up a bunch of sensors just fine and have them writing to a log file on an micro SD card. What Id like to do now is to send them to the cloud (AWS lambda via Gateway) maybe once an hour or so.
My code is attached below and appears that I can send files from SD card to cloud fine however that appears to only work for small files say < 100 bytes in testing, but actual log files which are typically 1M just halt.
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
const char* ssid = "myssid"; // your network SSID (name of wifi network)
const char* password = "mypassword"; // your network password
const char* server = "fmslkga113.execute-api.ap-southeast-1.amazonaws.com"; // Server https URL
WiFiClientSecure client;
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
}
Serial.print("Connected to ");
Serial.println(WiFi.localIP());
if(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
listDir(SD, "/", 0);
Serial.println("\nStarting connection to server...");
if (!client.connect(server, 443))
Serial.println("Connection failed!");
else {
Serial.println("Connected to server!");
File file = SD.open("/config.json");
// Make a HTTP request:
client.println(F("POST /test/upload?filepath=config.json HTTP/1.1"));
client.print(F("Host: "));
client.println(server);
client.println(F("Connection: close"));
client.println(F("Content-Type: text/plain"));
client.print(F("Content-Length: "));
client.println(file.size());
client.println();
while (file.available()) {
client.write(file.read());
}
file.close();
client.stop();
}
}
void loop() {
// do nothing
}
And the output gives me this:
18:37:20.554 -> Attempting to connect to SSID: ssid
18:37:20.687 -> .Connected to 192.168.0.186
18:37:21.686 -> Listing directory: /
18:37:21.686 -> FILE: /test.txt SIZE: 1048576
18:37:21.719 -> FILE: /foo.txt SIZE: 13
18:37:21.719 -> FILE: /config.json SIZE: 58
18:37:21.719 -> FILE: /hello.txt SIZE: 6
18:37:21.719 -> FILE: /aabbccdd.log SIZE: 461514
18:37:21.719 -> FILE: /a100.log SIZE: 101378
18:37:21.719 -> FILE: /bb.log SIZE: 204697
18:37:21.719 -> FILE: /a10.log SIZE: 10215
18:37:21.719 -> FILE: /a50.log SIZE: 47393
18:37:21.719 -> FILE: /a21.log SIZE: 21628
18:37:21.719 -> FILE: /a1.log SIZE: 1571
18:37:21.752 -> FILE: /a200.log SIZE: 214281
18:37:21.752 -> FILE: /a75.log SIZE: 76898
18:37:21.752 ->
18:37:21.752 -> Starting connection to server...
18:37:22.618 -> Connected to server!
So I've attempted to see if slightly larger files work and at a reasonable speed - what I've discovered is that I can send 100k files in approximately 3 mins and would really like to speed that up. Anything beyond that takes too long for the log files to upload,
I've looked through the Wifi client code and found a write method which appears to take a pointer and length and figured that would be faster, but when I attempt to compile and use that function I get this error
arduino15/packages/esp32/hardware/esp32/1.0.2/cores/esp32/Print.h:69:12: error: 'size_t Print::write(const char*, size_t)' is inaccessible
size_t write(const char *buffer, size_t size)
where my code is defined as :
char testBuf[6] = "abcde";
client.write(testBuf, sizeof(testBuf) );
So Im not sure how to proceed with this to be honest and would really appreciate if someone who has this working could post what they did or if there is some other library or the like that I should be using.
Thank you
Have a great day
Cheers