Read SD JSON file and send to python server

Hello I'm trying to read a JSON file from my SD card and send it to my python server.
but I'm only able to read the file and can't seem to "post" to my python server !!
Can someone help with the code? I'm a it a bit new at this!

#include <WiFiNINA.h>
#include "SdFat.h"
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h"
#include <ArduinoJson.h>
 
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
 
char serverAddress[] = "xxxxx";  // server address
int port = xxxx;
 
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
 
//SD Card Variables
const int chipSelect = 3;
SdFat SD;
File file;
String filename = "CSVtoJson.json";
char buf[700];
#define error(s) sd.errorHalt(PSTR(s))
 
void setup() {
  Serial.begin(115200);
  while (!Serial);
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.println(ssid);
    delay(5000);
  }
 
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  // SD Initialization
  Serial.print(F("Initializing SD card..."));
  if (!SD.begin(chipSelect, SPI_HALF_SPEED)) SD.initErrorHalt();
  fileExists();
  readFile();
  filestatus();
}
 
void fileExists() {
  if (SD.exists(filename)) {
    Serial.println("found file");
  }
  else {
    Serial.println("file not found");
  }
}
 
void readFile() {
 
  String temp;
  char data;
  Serial.println("making POST request");
  String contentType = "application/json";
  file = SD.open(filename);
  
  if (file) {
   
    int avail_len = file.available();
    int read_len = file.readBytes(buf, avail_len);
    
    while (buf >= 0)
    {
      temp = temp + data;
    }
 
    // close the file:
    file.close();
  }
    Serial.println("CSVtoJson.json:");
    client.post("/", contentType, temp);
    Serial.println(temp);
 
}
void filestatus() {
  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();
 
  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
}
 
 
void loop() {
}

What is the final purpose of this project?
I mean what want you do in the end?

Sending data in JSON-format is not a self-purpose.

You should give an overview about your project.
There is a pretty high chance that there are easier ways to realise the final purpose.

I need to gather a bunch of data (accelerometer, gyroscope,..) and save it on an SD card. So I basically wear the device and gather data on how I walk. I'm using nano rp2040 with WiFiNina.
My pipeline is Arduino IDE --> python server localHost --> drop data in MongoDB for later use.
Files are saved in my SD card in JSON format. All I want is to read the file and send that to my python server!

on what incoming channel does your python-server expect the incoming data?

UDP? TCP?, which portnumber? or HTTP-GET? or HTTP-POST?

You should post information about your python-server.
Or do you have to develop the python-server as well?

Is the part Python-Server sends data to MongoDB working?
Does it have to be MongoDB?
What is "later use"?

Is later use import the data into an Excelsheet? or something else?

So one very simple solution could be:
Your Arduino is sending UDP-messages to a small python-script that is waiting for incoming UDP-messages. This python-script writes the incoming data to a simple CVS-textfile which can be imported very easily into Excel.

This is the reason why I am asking for all the other details.
Me Personal I don't use RP2040. I alway use ESP32-boards (ESP32-nodeMCU) for $7 per piece

I have such systems up and running.

best regards Stefan

My python server is opened on my IP address and port number 8080.
I have my python server up and running and I have managed to send data from Arduino to the server through HTTP-POST and I was able to get it in MongoDB. From what i understand, when you open a connection in HTTP it means initiating a connection in the underlying transport layer(TCP)
Yes, I want to use MongoDB because I can host it and It's easy to use.
"later use" is for training an AI model with my data.
I only need help with my Arduino code.
I can read the JSON file that's on my SD card but can't seem to send it through "POST".

This is the example-code for http-post

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

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

//Your Domain name with URL path or IP address with path
const char* serverName = "http://192.168.1.106:1880/update-sensor";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;
    
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);
      
      // If you need Node-RED/server authentication, insert user and password below
      //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
      
      // Specify content-type header
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      // Data to send with HTTP POST
      String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";           
      // Send HTTP POST request
      int httpResponseCode = http.POST(httpRequestData);
      
      // If you need an HTTP request with a content type: application/json, use the following:
      //http.addHeader("Content-Type", "application/json");
      //int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");

      // If you need an HTTP request with a content type: text/plain
      //http.addHeader("Content-Type", "text/plain");
      //int httpResponseCode = http.POST("Hello, World!");
     
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
        
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

You have to adapt the string in the post-data to that data that will be accepted by your python-server

String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";           
      // Send HTTP POST request
      int httpResponseCode = http.POST(httpRequestData);

the bold printed part

String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";

If you need more support on this you have to provide an example how the post-request looks like

For JSON-format use this

http.addHeader("Content-Type", "application/json");

int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");

still if you have difficulties to adapt this you have to provide an example of your JSON-data

[
 {
   "timestamp": "0",
   "GCX": "-0006382",
   "GCY": "0005893",
   "GCZ": "-0009328",
   "ACX": "0014445",
   "ACY": "-0171631",
   "ACZ": "0001343",
   "GLX": "-0001939",
   "GLY": "-0001954",
   "GLZ": "-0000336",
   "ALX": "-0162476",
   "ALY": "0023356",
   "ALZ": "0003581",
   "GRX": "0002489",
   "GRY": "-0000336",
   "GRZ": "0000534",
   "ARX": "-0156860",
   "ARY": "-0029826",
   "ARZ": "0008993"
 },
 {
   "timestamp": "20",
   "GCX": "-0006916",
   "GCY": "0005527",
   "GCZ": "-0007924",
   "ACX": "0007772",
   "ACY": "-0162516",
   "ACZ": "0007324",
   "GLX": "-0001740",
   "GLY": "-0000229",
   "GLZ": "-0000397",
   "ALX": "-0161865",
   "ALY": "0021281",
   "ALZ": "0003906",
   "GRX": "0000260",
   "GRY": "-0001282",
   "GRZ": "0000122",
   "ARX": "-0156250",
   "ARY": "-0030518",
   "ARZ": "0009318"
 },
 {
   "timestamp": "40",
   "GCX": "-0008565",
   "GCY": "0006733",
   "GCZ": "-0007282",
   "ACX": "0013631",
   "ACY": "-0168864",
   "ACZ": "0005575",
   "GLX": "-0003649",
   "GLY": "0000580",
   "GLZ": "-0000061",
   "ALX": "-0159139",
   "ALY": "0017537",
   "ALZ": "0001668",
   "GRX": "-0001496",
   "GRY": "-0000809",
   "GRZ": "0000244",
   "ARX": "-0156331",
   "ARY": "-0032918",
   "ARZ": "0008993"
 },
 {
   "timestamp": "60",
   "GCX": "-0008550",
   "GCY": "0008382",
   "GCZ": "-0006260",
   "ACX": "0017537",
   "ACY": "-0169596",
   "ACZ": "0001058",
   "GLX": "-0001511",
   "GLY": "0000565",
   "GLZ": "0000290",
   "ALX": "-0161174",
   "ALY": "0016805",
   "ALZ": "0002686",
   "GRX": "-0001954",
   "GRY": "-0000137",
   "GRZ": "-0000229",
   "ARX": "-0156413",
   "ARY": "-0034424",
   "ARZ": "0010661"
 },
 {
   "timestamp": "80",
   "GCX": "-0005160",
   "GCY": "0002672",
   "GCZ": "-0003099",
   "ACX": "0016113",
   "ACY": "-0170003",
   "ACZ": "0006388",
   "GLX": "-0000137",
   "GLY": "-0000412",
   "GLZ": "0000489",
   "ALX": "-0160889",
   "ALY": "0017456",
   "ALZ": "0002197",
   "GRX": "0000534",
   "GRY": "-0000382",
   "GRZ": "-0000244",
   "ARX": "-0155802",
   "ARY": "-0034709",
   "ARZ": "0009888"
 },
 {
   "timestamp": "100",
   "GCX": "-0001649",
   "GCY": "0001084",
   "GCZ": "-0001008",
   "ACX": "0014771",
   "ACY": "-0170654",
   "ACZ": "0004354",
   "GLX": "0000183",
   "GLY": "-0000336",
   "GLZ": "0000519",
   "ALX": "-0160848",
   "ALY": "0018636",
   "ALZ": "0002563",
   "GRX": "0002015",
   "GRY": "0000046",
   "GRZ": "-0000870",
   "ARX": "-0155965",
   "ARY": "-0034953",
   "ARZ": "0008993"
 },
 {
   "timestamp": "120",
   "GCX": "0000168",
   "GCY": "0000931",
   "GCZ": "0000947",
   "ACX": "0014445",
   "ACY": "-0170695",
   "ACZ": "0001709",
   "GLX": "-0000779",
   "GLY": "-0000702",
   "GLZ": "0000061",
   "ALX": "-0161011",
   "ALY": "0020955",
   "ALZ": "0000448",
   "GRX": "0001069",
   "GRY": "-0000611",
   "GRZ": "-0001053",
   "ARX": "-0155640",
   "ARY": "-0033488",
   "ARZ": "0009074"
 },
 {
   "timestamp": "140",
   "GCX": "0001115",
   "GCY": "0000397",
   "GCZ": "0002763",
   "ACX": "0015340",
   "ACY": "-0171224",
   "ACZ": "0002116",
   "GLX": "-0001038",
   "GLY": "0000351",
   "GLZ": "-0000015",
   "ALX": "-0160116",
   "ALY": "0019491",
   "ALZ": "0002279",
   "GRX": "-0000504",
   "GRY": "-0000626",
   "GRZ": "-0001038",
   "ARX": "-0156494",
   "ARY": "-0029989",
   "ARZ": "0010579"
 },
 {
   "timestamp": "160",
   "GCX": "0002198",
   "GCY": "0002366",
   "GCZ": "0004122",
   "ACX": "0016683",
   "ACY": "-0171753",
   "ACZ": "0004028",
   "GLX": "-0000870",
   "GLY": "-0000977",
   "GLZ": "-0000397",
   "ALX": "-0162069",
   "ALY": "0020386",
   "ALZ": "0002563",
   "GRX": "-0000427",
   "GRY": "-0000733",
   "GRZ": "-0000611",
   "ARX": "-0156576",
   "ARY": "-0029215",
   "ARZ": "0011271"
 },
 {
   "timestamp": "180",
   "GCX": "0004595",
   "GCY": "0002809",
   "GCZ": "0005130",
   "ACX": "0017008",
   "ACY": "-0169596",
   "ACZ": "0004272",
   "GLX": "-0003557",
   "GLY": "-0000519",
   "GLZ": "-0000443",
   "ALX": "-0160156",
   "ALY": "0019491",
   "ALZ": "0001628",
   "GRX": "-0001420",
   "GRY": "-0000580",
   "GRZ": "-0000458",
   "ARX": "-0156372",
   "ARY": "-0031169",
   "ARZ": "0009237"
 },
 {
   "timestamp": "200",
   "GCX": "0007542",
   "GCY": "0002489",
   "GCZ": "0005496",
   "ACX": "0015951",
   "ACY": "-0167765",
   "ACZ": "0005046",
   "GLX": "-0004122",
   "GLY": "-0000336",
   "GLZ": "-0000031",
   "ALX": "-0160645",
   "ALY": "0021037",
   "ALZ": "0001994",
   "GRX": "-0002504",
   "GRY": "-0000931",
   "GRZ": "-0000397",
   "ARX": "-0155924",
   "ARY": "-0033366",
   "ARZ": "0009481"
 },
 {
   "timestamp": "220",
   "GCX": "0006977",
   "GCY": "0001511",
   "GCZ": "0004519",
   "ACX": "0015381",
   "ACY": "-0165365",
   "ACZ": "0007121",
   "GLX": "-0003908",
   "GLY": "-0000427",
   "GLZ": "-0000229",
   "ALX": "-0161621",
   "ALY": "0020142",
   "ALZ": "0004150",
   "GRX": "-0001405",
   "GRY": "-0001237",
   "GRZ": "-0001099",
   "ARX": "-0155680",
   "ARY": "-0033895",
   "ARZ": "0007446"
 },
 {
   "timestamp": "240",
   "GCX": "0002855",
   "GCY": "0000046",
   "GCZ": "0001832",
   "ACX": "0016927",
   "ACY": "-0164429",
   "ACZ": "0005819",
   "GLX": "-0002550",
   "GLY": "0000870",
   "GLZ": "0000183",
   "ALX": "-0160563",
   "ALY": "0018066",
   "ALZ": "0004720",
   "GRX": "-0000198",
   "GRY": "-0001649",
   "GRZ": "-0001817",
   "ARX": "-0153239",
   "ARY": "-0034749",
   "ARZ": "0008870"
 },
 {
   "timestamp": "260",
   "GCX": "-0003863",
   "GCY": "-0001847",
   "GCZ": "0000656",
   "ACX": "0013835",
   "ACY": "-0166463",
   "ACZ": "0004354",
   "GLX": "-0001466",
   "GLY": "0001542",
   "GLZ": "0001038",
   "ALX": "-0160685",
   "ALY": "0017619",
   "ALZ": "0003052",
   "GRX": "0000840",
   "GRY": "-0001695",
   "GRZ": "-0003084",
   "ARX": "-0153564",
   "ARY": "-0035441",
   "ARZ": "0006795"
 },
 {
   "timestamp": "280",
   "GCX": "-0005420",
   "GCY": "0000702",
   "GCZ": "0001237",
   "ACX": "0013672",
   "ACY": "-0167928",
   "ACZ": "0006226",
   "GLX": "-0002763",
   "GLY": "0005481",
   "GLZ": "-0000290",
   "ALX": "-0163086",
   "ALY": "0021077",
   "ALZ": "0006388",
   "GRX": "0002397",
   "GRY": "-0001893",
   "GRZ": "-0004107",
   "ARX": "-0154134",
   "ARY": "-0036336",
   "ARZ": "0008016"
 },
 {
   "timestamp": "300",
   "GCX": "-0007237",
   "GCY": "0001313",
   "GCZ": "0002565",
   "ACX": "0011922",
   "ACY": "-0168579",
   "ACZ": "0006144",
   "GLX": "-0008748",
   "GLY": "0004840",
   "GLZ": "-0001481",
   "ALX": "-0164388",
   "ALY": "0019124",
   "ALZ": "0010376",
   "GRX": "0001954",
   "GRY": "-0000397",
   "GRZ": "-0004534",
   "ARX": "-0156291",
   "ARY": "-0033936",
   "ARZ": "0007650"
 },
 {
   "timestamp": "320",
   "GCX": "-0008122",
   "GCY": "0003359",
   "GCZ": "0002916",
   "ACX": "0014323",
   "ACY": "-0170166",
   "ACZ": "0010457",
   "GLX": "-0010901",
   "GLY": "0004779",
   "GLZ": "-0001420",
   "ALX": "-0162679",
   "ALY": "0016154",
   "ALZ": "0002401",
   "GRX": "0001359",
   "GRY": "-0001221",
   "GRZ": "-0005023",
   "ARX": "-0159383",
   "ARY": "-0032796",
   "ARZ": "0006917"
 },
 {
   "timestamp": "340",
   "GCX": "-0005908",
   "GCY": "0004626",
   "GCZ": "0001618",
   "ACX": "0009237",
   "ACY": "-0171346",
   "ACZ": "0007690",
   "GLX": "-0009252",
   "GLY": "0001099",
   "GLZ": "-0002000",
   "ALX": "-0160848",
   "ALY": "0010905",
   "ALZ": "0003255",
   "GRX": "0000962",
   "GRY": "-0000015",
   "GRZ": "-0005511",
   "ARX": "-0157430",
   "ARY": "-0029785",
   "ARZ": "0007365"
 },
 {
   "timestamp": "360",
   "GCX": "-0007695",
   "GCY": "0000595",
   "GCZ": "0000733",
   "ACX": "0009969",
   "ACY": "-0170166",
   "ACZ": "0007161",
   "GLX": "-0003191",
   "GLY": "0007115",
   "GLZ": "-0002076",
   "ALX": "-0158813",
   "ALY": "0008382",
   "ALZ": "0002970",
   "GRX": "0001771",
   "GRY": "-0000794",
   "GRZ": "-0004901",
   "ARX": "-0155884",
   "ARY": "-0028524",
   "ARZ": "0007284"
 },
 {
   "timestamp": "380",
   "GCX": "-0008733",
   "GCY": "0002214",
   "GCZ": "0000519",
   "ACX": "0009440",
   "ACY": "-0168538",
   "ACZ": "0007039",
   "GLX": "0002198",
   "GLY": "0002504",
   "GLZ": "-0000855",
   "ALX": "-0164754",
   "ALY": "0013916",
   "ALZ": "0004517",
   "GRX": "0001328",
   "GRY": "-0000779",
   "GRZ": "-0004794",
   "ARX": "-0156006",
   "ARY": "-0028605",
   "ARZ": "0009603"
 },
 {
   "timestamp": "400",
   "GCX": "-0009908",
   "GCY": "0004198",
   "GCZ": "0000931",
   "ACX": "0008789",
   "ACY": "-0168579",
   "ACZ": "0006266",
   "GLX": "0002779",
   "GLY": "0010107",
   "GLZ": "-0000656",
   "ALX": "-0160970",
   "ALY": "0016520",
   "ALZ": "-0000448",
   "GRX": "0001756",
   "GRY": "-0003084",
   "GRZ": "-0004977",
   "ARX": "-0155436",
   "ARY": "-0028605",
   "ARZ": "0007731"
 },
 {
   "timestamp": "420",
   "GCX": "-0012321",
   "GCY": "0004702",
   "GCZ": "0002489",
   "ACX": "0009644",
   "ACY": "-0170654",
   "ACZ": "0008830",
   "GLX": "0009282",
   "GLY": "0005725",
   "GLZ": "0000198",
   "ALX": "-0163167",
   "ALY": "0015706",
   "ALZ": "0008870",
   "GRX": "0003863",
   "GRY": "-0004092",
   "GRZ": "-0006092",
   "ARX": "-0158569",
   "ARY": "-0028849",
   "ARZ": "0008586"
 },
 {
   "timestamp": "440",
   "GCX": "-0012015",
   "GCY": "0003053",
   "GCZ": "0004672",
   "ACX": "0008667",
   "ACY": "-0168742",
   "ACZ": "0008952",
   "GLX": "0002550",
   "GLY": "0008000",
   "GLZ": "-0001893",
   "ALX": "-0162109",
   "ALY": "0012126",
   "ALZ": "0007609",
   "GRX": "0006000",
   "GRY": "-0004611",
   "GRZ": "-0006901",
   "ARX": "-0155273",
   "ARY": "-0029989",
   "ARZ": "0006592"
 },
 {
   "timestamp": "460",
   "GCX": "-0012489",
   "GCY": "0003878",
   "GCZ": "0004046",
   "ACX": "0007324",
   "ACY": "-0167155",
   "ACZ": "0005615",
   "GLX": "0002885",
   "GLY": "0004779",
   "GLZ": "-0003115",
   "ALX": "-0159953",
   "ALY": "0009237",
   "ALZ": "0007039",
   "GRX": "0007115",
   "GRY": "-0003679",
   "GRZ": "-0006656",
   "ARX": "-0159139",
   "ARY": "-0030314",
   "ARZ": "0003866"
 },
 {
   "timestamp": "480",
   "GCX": "-0006809",
   "GCY": "0003817",
   "GCZ": "0000870",
   "ACX": "-0000081",
   "ACY": "-0168538",
   "ACZ": "0001180",
   "GLX": "0002229",
   "GLY": "0008489",
   "GLZ": "-0006260",
   "ALX": "-0161621",
   "ALY": "0007894",
   "ALZ": "0007365",
   "GRX": "0004870",
   "GRY": "-0003435",
   "GRZ": "-0006840",
   "ARX": "-0155111",
   "ARY": "-0030355",
   "ARZ": "0005249"
 },
 {
   "timestamp": "500",
   "GCX": "-0000763",
   "GCY": "-0000672",
   "GCZ": "0000015",
   "ACX": "0002360",
   "ACY": "-0165609",
   "ACZ": "0005086",
   "GLX": "-0003237",
   "GLY": "0005634",
   "GLZ": "-0008931",
   "ALX": "-0162720",
   "ALY": "0001872",
   "ALZ": "0004639",
   "GRX": "0002489",
   "GRY": "-0002305",
   "GRZ": "-0007145",
   "ARX": "-0155233",
   "ARY": "-0027303",
   "ARZ": "0004395"
 },
 {
   "timestamp": "520",
   "GCX": "-0013893",
   "GCY": "-0001298",
   "GCZ": "0003069",
   "ACX": "-0002238",
   "ACY": "-0167847",
   "ACZ": "0004232",
   "GLX": "0000931",
   "GLY": "0008214",
   "GLZ": "-0008702",
   "ALX": "-0159139",
   "ALY": "0003703",
   "ALZ": "0008748",
   "GRX": "0002321",
   "GRY": "-0004290",
   "GRZ": "-0007511",
   "ARX": "-0159098",
   "ARY": "-0028198",
   "ARZ": "0007894"
 }]

My data is already stored in the SD file.
It's a long file so I took a bit from it.

#include <WiFiNINA.h>
#include "SdFat.h"
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h"
#include <ArduinoJson.h>

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

char serverAddress[] = "xxxx";  // server address
int port = xxxx;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

//SD Card Variables
const int chipSelect = 3;
SdFat SD;
File file;
String filename = "CSVtoJson.json";
char buf[700];
#define error(s) sd.errorHalt(PSTR(s))
const size_t LINE_DIM = 50000;
char line[LINE_DIM];

void setup() {
  Serial.begin(115200);
  while (!Serial);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.println(ssid);
    delay(5000);
  }

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

  // SD Initialization
  Serial.print(F("Initializing SD card..."));
  if (!SD.begin(chipSelect, SPI_HALF_SPEED)) SD.initErrorHalt();
  fileExists();
  readFile();

}

void fileExists() {
  if (SD.exists(filename)) {
    Serial.println("found file");
  }
  else {
    Serial.println("file not found");
  }
}

void readFile() {
  size_t n;
  Serial.println("making POST request");
  String contentType = "application/json";
  file = SD.open(filename, FILE_READ);
  // make data to String;
  if (file) {
    while (file.available()) {
      while ((n = file.read(line, sizeof(line))) > 0) {
        // Print line number.
  
        client.post(line);
      }
    }
    file.close();

  }
void loop() {

}

I tried this code but on the server side, it gave me an error saying **400 code, message Bad HTTP/0.9 request type ('POST') **
I'm still not sure on how to read the file and send it correctly through post!
It worked when I tried sending a one line Json doc without using the SD. I received it on my server and it got sent to MongoDB so I'm guessing it's an issue while reading an SD file and the way i'm sending it!

You are working on an informatic project. And what is needed most in an informatic project is information.

I'm not familiar with JSON nor with http-POST.
My way of analyising it is moving forward in very small steps

post this code-version.
And for analysing what the differences are you should add serial debug-output to your code
to see what is read in from the SD-card and then sent to the python-server.

take a new look at this line of code

http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");

there is a very particular pattern with identifiers and values
you have to compare the pattern of your single-line successful send
with the multiline SD-read-send character for character
and check if the content fullfills all rules that the pattern must have.

best regards Stefan

when I run this code I get this error because of "client.post(filedata)"
error message :
testSDarray:90:27: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
client.post(filedata);
^
In file included from C:\Users\Christiane\Documents\Arduino\libraries\ArduinoHttpClient\src/ArduinoHttpClient.h:8:0,
from C:\Users\Christiane\Desktop\micropython\PYServerHttp\testSDarray\testSDarray.ino:2:
C:\Users\christiane\Documents\Arduino\libraries\ArduinoHttpClient\src/HttpClient.h:85:9: note: initializing argument 1 of 'int HttpClient::post(const char*)'
int post(const char* aURLPath);
^~~~
exit status 1
invalid conversion from 'char' to 'const char*' [-fpermissive]

//libs
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include "SdFat.h"
//SD vars
File myFile;
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
int status = WL_IDLE_STATUS;

//parse data vals
int dataarr[60] = {};     //array of parsed data
int arrpointer = 0;       //array pointer
int column = 4;           //column to parse
int columncount = 1;      //start at column 1 not 0
int rowincrement = 2;     //increment the number of parse rows - set to 1 for all rows
int rowcount = 1;         //start at row 1 not 0
String datatemp;          //temp to store datapoint

const int chipSelect = 3;
SdFat SD;
#define error(s) sd.errorHalt(PSTR(s))
String filename = "csv3.csv";


char serverAddress[] = "xxxx";  // server address
int port = xx;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

void setup() {

  Serial.begin(115200);
  while (!Serial);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.println(ssid);
    delay(5000);
  }

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

  // SD Initialization
  Serial.print(F("Initializing SD card..."));
  if (!SD.begin(chipSelect, SPI_HALF_SPEED)) SD.initErrorHalt();

  Serial.println("making POST request");
  String contentType = "application/json";

  //Read file
  myFile = SD.open("csv3.csv");

  if (myFile) {

    Serial.println();

    while (myFile.available()) {

      char filedata = myFile.read();

      //////////////
      // parse
      //////////////

      //read data vals into string
      if ((filedata != ',') && (filedata != ' ')) {

        //test for row increment
        if (rowcount % rowincrement == 0) {

          //look for column
          if (columncount == column) {

            //push datapoint to array
            Serial.print("*");
            datatemp = datatemp += (filedata - '0');
            dataarr[arrpointer] = datatemp.toInt();

          }

        }

      }

      Serial.write(filedata);

      //end of datapoint
      if (filedata == ',') {
        columncount++;
      }

      //end of row
      if (filedata == '\n') {

        //print data temp at end of line
//                Serial.write(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//                Serial.print(datatemp.toInt());
//                Serial.println();

        //reset vars to read next row
        rowcount++;
        columncount = 1;
        datatemp = "";
        //arrpointer++;

      }

      //////////////
      // end parse
      //////////////
      client.post(filedata);
    }
  }

  Serial.println();
  //Serial.println();
  //Serial.println("--------------------------");


  //cycle through data array
  for (int i = 0; i < arrpointer; i++) {
    Serial.print("Selected Data >>>>>>> ");
    Serial.println(dataarr[i]);
    Serial.print(", ");
  }

  myFile.close();

}


void loop() {
}

Hi Christane,

If I copy your source- code from post number #9 into my IDE and then look up

line 90

I see these lines

Line 90 is a comment

      //end of datapoint

image

The line mentioned in the compiler-message is line 114

      client.post(filedata);

image

rhetorical question:
If your phone-number would be
0123/987654123
and I try to call
0123/987654124

will your phone ring??
surely not.

How should I try to analyse what the error might be when the sourcecode-file does not

exactly

match to the error-message?

You should be very precise compiling a sourcecode and the copy the sourcecode without the slightest modification so the sourcecode will really match with the error-message

anyway
You are trying to use a variable of type char inside the function-call

client.post(filedata);

but the function client.post() expects a pointer to a char which is something very different than a char itself.

Again: please post

two complete skteches

a complete sketch with code-version that works with single line

a complete sketch with code-version that throws the error-message

instead of repeating sending single characters it should be possible to put mutliple characters into an array of char and then use this array of char inside function client.post()

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.