Hello! This is my first time in this forum. I am developing a device that takes measurements from a microphone and stores these measurements into a txt file, logging each second. The end goal is then sending the contents of this .txt file in a .JSON object over HTTP to a server to make datalogging and analysing easy. A minute of calculation data creates a file about 20kb.<br/>
The json object will look like this:
Code: Untitled.js Select all
{
"DeviceId": "123456",
"Data": "THE MULTI-LINE DATA READ FROM THE SD CARD AS A VARIABLE"
}
- Read the file into a character array with the size of the file
- Format said array into a string like json
- Send it over http
(Libraries used: WiFi.h, HTTPClient.h, ArduinoJson.h, FS.h, SD.h)
Code: Untitled.cpp Select all
void readPost(fs::FS &fs, const char * path, const char * httpUrl) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file) {
Serial.println("Failed to open file for reading");
return;
}
const size_t fLen = file.size();
char * fileBuffer = new char[fLen + 1];
while(file.available()){
fileBuffer[file.position() - 1] = file.read();
}
file.close();
char * fileData = new char[fLen + 33];
sprintf(fileData, "{\"DeviceId\":\"%s\",\"Data\":\"%s\"}", deviceId, fileBuffer);
delete [] fileBuffer;
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
http.begin(String(httpUrl)); //Specify the URL and certificate
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST((uint8_t *)fileData, sizeof(fileData));
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end(); //Free the resources
}
delete [] fileData;
}