Im working with esp32s3 feather right now. I need to log some data when there is no WiFi connection. Write works fine for me but when I want to read line with readStringUntil(), i always get "null" at the end of read string. Here is code:
if ((millis() - sdLast) > sdTime)
{
for (int i = 0; i < maxSensors; i++)
{
if (activeSensors[i] != "")
{
String requestData = "{\"data\":[{\"name\":\"" + sensorNames[i] + "\" ,\"temp\": \"" + actTemp[i] + "\",\"hum\": \"" + actHum[i] + "\",\"time\": \"" + actTime[i] + "\",\"scanCount\": \"" + scanCount[i] + "\"}]}\n";
appendFile(SD, "/all.txt", requestData.c_str());
sdReady = true;
}
}
sdLast = millis();
}
Function to read from file:
void readLinesSD(fs::FS &fs, const char *path)
{
File file = fs.open(path);
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
if (!file)
{
Serial.println("Failed to open file for reading");
return;
}
while (file.available())
{
buffer = file.readStringUntil('\n');
serializeJson(doc, buffer);
Serial.println(buffer);
int httpResponseCode = http.POST(buffer);
Serial.println(httpResponseCode);
doc.clear();
delay(200);
}
http.end();
file.close();
}
Append function:
void appendFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file)
{
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message))
{
Serial.println("Message appended");
}
else
{
Serial.println("Append failed");
}
file.close();
}
SO basically I want to save data to file and then, when the WiFi connection is back I want to send data to database for further presentation. When I read file i got this results:
{"data":[{"name":"P RHT 902631" ,"temp": "19.53","hum": "48","time": "1674746950","scanCount": "4"}]}null
{"data":[{"name":"P RHT 90262A" ,"temp": "19.38","hum": "50","time": "1674746957","scanCount": "4"}]}null
{"data":[{"name":"P RHT 902629" ,"temp": "19.36","hum": "49","time": "1674746958","scanCount": "5"}]}null
I tried using some special characters like "%" at the end of lines and then read line untill this special character but got same problem. When I used the same function on my other esp32 board everything was read fine. Anyone know what might cause this problem? Thanks for any help