0

I am experiencing a delay in receiving the HTTP POST request body when sending data over a TCP connection using the TinyGSM library with a SIM7000G and an ESP32 (LilyGo T-SIM7000G).

Below is my function. I believe it stalls at the while loop for reading the response, and then then TCP connection automatically closes - you can see some AT responses being printed amidst the garbage body of the request response.

Why might this be? I have been trying to troubleshoot for a few days now. No success. Below is my function and a sample print out over Serial

#define TINY_GSM_MODEM_SIM7000
#define TINY_GSM_RX_BUFFER 1024 
#define TINY_GSM_DEBUG Serial
#include <TinyGsmClient.h>
HardwareSerial SerialAT(1);
TinyGsm modem(SerialAT);
TinyGsmClient gsmClient(modem);
bool fileUpload_GSM(String filename) {
 delay(10);
 Serial.println("[GSM UPLOAD] --- Starting GSM Upload ---");
 client = &gsmClient;
 if (!SDCARD) {
 Serial.println("[GSM UPLOAD] SDCARD not available");
 return false;
 }
 File dataFile = SD.open(filename.c_str(), FILE_READ);
 if (!dataFile) {
 Serial.println("[GSM UPLOAD] Failed to open file");
 return false;
 }
 size_t fileSize = dataFile.size();
 String remoteName = filename;
 remoteName.replace("/", "-");
 const char* boundary = "jg2qVIUS8teOAbN3";
 String fileType = "text/plain";
 String startContent = "--" + String(boundary) + "\r\n";
 startContent += "Content-Disposition: form-data; name=\"fileToUpload\"; filename=" + remoteName + "\r\n";
 startContent += "Content-Type: " + fileType + "\r\n\r\n";
 String closingContent = "\r\n--" + String(boundary) + "--\r\n";
 size_t totalLength = startContent.length() + fileSize + closingContent.length();
 Serial.printf("[GSM UPLOAD] File: %s | Size: %u | Total body: %u\n", filename.c_str(), fileSize, totalLength);
 delay(100);
 if (!client->connect(appCache.host.c_str(), appCache.port)) {
 Serial.println("[GSM UPLOAD] Failed to connect to host");
 dataFile.close();
 return false;
 }
 // Send headers
 Serial.println("[GSM UPLOAD] Sending headers...");
 client->println(String("POST ") + appCache.uploadFilePath + " HTTP/1.1");
 client->println(String("Host: ") + appCache.host);
 client->println("Keep-Alive: 300");
 client->println("Connection: keep-alive");
 //client->println("Connection: close");
 client->println("Content-Length: " + String(totalLength));
 client->println("Content-Type: multipart/form-data; boundary=" + String(boundary));
 client->println();
 // Send body header
 Serial.println("[GSM UPLOAD] Sending body header...");
 client->print(startContent);
 Serial.println("[GSM UPLOAD] Sending file content...");
 const size_t bufSize = 512;
 char buffer[bufSize];
 size_t totalSent = 0;
 while (dataFile.available()) {
 size_t bytesRead = dataFile.readBytes(buffer, bufSize);
 if (bytesRead > 0) {
 client->write((const uint8_t*)buffer, bytesRead);
 totalSent += bytesRead;
 Serial.printf("[GSM UPLOAD] Sent %u bytes\n", bytesRead);
 }
 delay(10);
 }
 dataFile.close();
 Serial.printf("[GSM UPLOAD] Finished sending file data (%u bytes)\n", totalSent);
 Serial.println("[GSM UPLOAD] Sending closing boundary...");
 client->print(closingContent);
 client->flush();
 Serial.print("[GSM UPLOAD] client->connected(): ");
 Serial.println(client->connected() ? "YES" : "NO");
 // Wait up to 15s for server response
 unsigned long timeoutStart = millis();
 unsigned long lastByteTime = millis();
 String rawResponse = "";
 bool gotSomething = false;
 Serial.println("[GSM UPLOAD] Waiting for server response...");
 while ((millis() - timeoutStart < 15000)) {
 while (client->available()) {
 char c = client->read();
 rawResponse += c;
 Serial.print(c);
 gotSomething = true;
 lastByteTime = millis();
 }
 if (!client->available() && gotSomething && millis() - lastByteTime > 1000) {
 break;
 }
 delay(50);
 Serial.print(".");
 }
 if (!gotSomething) {
 Serial.println("[GSM UPLOAD] No data received from server.");
 }
 Serial.print("[GSM] Read COMPLETE");
 client->stop(); //close client as we dont need anymore
 int httpCode = 0;
 String statusLine, body;
 if (!parseHttpResponseRaw(rawResponse, statusLine, body, httpCode)) {
 return false;
 }
 return handleHTTPJsonResponse(httpCode, body);
}

Here is my Serial output around 2 minutes ago:

[GSM UPLOAD] Sending headers...
[GSM UPLOAD] Sending body header...
[GSM UPLOAD] Sending file content...
[GSM UPLOAD] Sent 512 bytes
[GSM UPLOAD] Sent 32 bytes
[GSM UPLOAD] Finished sending file data (544 bytes)
[GSM UPLOAD] Sending closing boundary...
[GSM UPLOAD] client->connected(): YES
... Minutes later ...
22:45:48:437 -> HTTP/1.1 201 Created //<--- MEANS file was actually stored!
22:45:48:437 -> Date: 2025年6月01日 14:43:54 GMT
22:45:48:437 -> Content-Type: application
/json 
22:45:48:437 -> Content-Length: 
158 
22:45:48:437 -> Connection: close
22:45:48:437 -> Set-Cookie: PHPSESSID=4bk6f5dk5qm0lbha0bsovmaa0k; path=/; secure; HttpOnly
22:45:48:437 -> 
22:45:48:437 -> Expires: 1981年11月19日 08:52:00
22:45:48:437 -> GMT
22:45:48:437 -> Cache-Control: no-store, no-cache, must-revalidate
22:45:48:556 -> Pragma: no-cache
22:45:48:597 -> cf-cache-stat
22:45:48:628 -> us: AcJOzmDeRYMvaQRl1X3Fw2kp%2FXhd1E7Ukidd2vO79B9RMtgU6M8fd0DQ1BRFDpxymqvP0m4nPrHDmBgzJA0nN5SuNmEm"}],"group":"cf-nel","max_age":604800} 
22:45:50:150 -> NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
22:45:50:151 -> Server: cloudflare
22:45:50:151 -> CF-RAY: 948f7c62df12ec0c-NRT
22:45:50:151 -> alt-svc: h3=":443"; ma=86400
22:45:50:151 -> server-timing: cfL4;desc="?proto=TCP&rtt=160082&min_rtt=160082&rtt_var=80041&sent=21&recv=23&lost=0&retrans=0&sent_bytes=0&recv_bytes=1017&delivery_rate=0&cwnd=58&unsent_bytes=0&cid=0000000000000000&ts=0&x=0"
22:45:50:151 -> 
22:45:50:151 -> {"message":"The file successf
22:45:50:151 -> OK
22:45:50:151 -> ������
22:45:50:151 -> 0, CLOSED
22:45:50:151 -> �����������������������������������
22:45:50:151 -> ���������������������������������������������������������������������.[GSM] Read COMPLET
asked Jun 3 at 3:05
2
  • maybe the HTTP header is malformed ... the web server may be waiting for more data Commented Jun 3 at 20:59
  • The log Expires: 1981年11月19日 08:52:00 suggesting that either the ESP32 or the SIM7000G probably doesn't have a proper setup of a real time clock, so if you using SSL, your cert validation will not be correct due to the wrong date. BTW, you shows us the fileUpload_GSM() but the log shows when the JSON payload get cut off {"message":"The file successf has nothing to do with the function that you show us. In summary, we don't really know what's going on because you didn't show us the related code. Commented Jun 4 at 0:18

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.