Upload files from SD card to backend server(NodeJS)

Hi,

I am new to Arduino framework.

I am trying to capture the images using ESP32-CAM and save it to SD card. After certain interval I am trying to upload those save JPEG files to backend server.

I tried using WifiClient but it didn't worked for me, in case of WifiClient I am not receiving any file.

Looking for suggestion for file sharing over the WiFi or Bluetooth.

Thanks in advance.

Script****************

#include <WiFi.h>
#include "SD_MMC.h"

const char* ssid = "**************";
const char* password = "*************";

const char* host = "*************";

char filename[14] = "f1.jpg";
void setup()
{
Serial.begin(9600);

if(!SD_MMC.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();

if(cardType == CARD_NONE){
Serial.println("No SD_MMC card attached");
return;
}

uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);

delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

int value = 0;

void loop()
{
delay(5000);
++value;

Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 3000;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

// Prepare HTTP request
String start_request = "";
String end_request = "";
start_request = start_request + "\n" + "--AaB03x" + "\n" + "Content-Disposition: form-data; name="image"; filename="+filename+"\n" + "Content-Type: file" + "\n" + "Content-Transfer-Encoding: binary" + "\n" + "\n";
end_request = end_request + "\n" + "--AaB03x--" + "\n";
String userId ="5c9e7ab9e9ee5102a406dd1f";
String deviceId ="1";
// We now create a URI for the request
String url = "";
url += "/camera/saveCameraData/"+deviceId+"/"+userId;
File myFile = SD_MMC.open("/f1.jpg");
uint16_t jpglen = myFile.size();
uint16_t extra_length;
extra_length = start_request.length() + end_request.length();
uint16_t len = jpglen + extra_length;

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");

client.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
client.print(F("Content-Length: "));
client.println(len);
client.println(start_request);
Serial.println(F("Host: 192.168.0.103"));
Serial.println(F("Content-Type: multipart/form-data; boundary=AaB03x"));
Serial.print(F("Content-Length: "));
Serial.println(len);
Serial.println(start_request);
if (myFile) {
byte clientBuf[32];
int clientCount = 0;

while(myFile.available()){
clientBuf[clientCount] = myFile.read();
clientCount++;

if(clientCount > 31)
{
client.write(clientBuf,32);
clientCount = 0;
}
}
if(clientCount > 0) client.write(clientBuf,clientCount);

client.print(end_request);
client.println();
}
else{
Serial.println("File not found");
}
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}

// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("closing connection");
}

WiFiClient is the only option
use client.write(file) instead your bufferimg
why do you do it as a web form upload?

Thanks for responding Juraj,

I am uploading all the images to s3 so I should recieve the file in backend as a part of file object in backend.

I tried client.write(file) as well but file is missing in backend.

Please share if you have any working example. I will have a look at it.

abhi1005:
Thanks for responding Juraj,

I am uploading all the images to s3 so I should recieve the file in backend as a part of file object in backend.

I tried client.write(file) as well but file is missing in backend.

Please share if you have any working example. I will have a look at it.

I coded http file upload many times in different languages, but not yet on Arduino. I have in my arduino project a a web server with file download on Arduino, but no upload.

You try to simulate a html form style upload (multipart content type) used by browsers to mix form data with uploaded file. But if the server can accept a POST request with the file as body then you can simply send the file as request's body.

Solution: c++ - Try to send image file to PHP with HTTPClient - Stack Overflow