Reputation: 1
I'm trying to make a module that both works with WiFi and SIM7600 module. The module receives a long (~300 KB) JSON file from an HTTPS server and saves it to flash memory. The function that works with WiFi works without any problem but SIM7600 function is not working well.
p.s: I can fetch short contents like 1 KB with SIM7600 without any problem.
These are my functions for both WiFi and SIM7600 module:
WiFi
bool FetchAndSaveJsonWiFi() {
HTTPClient http; //from HTTPClient.h library
http.begin(getAddress);
http.addHeader("Authorization", "Bearer " + String(bearerToken));
http.setTimeout(30000);
int httpCode = http.GET();
if (httpCode != 200) {
Serial.printf("FetchAndSaveJson GET failed, error: %s\n", http.errorToString(httpCode).c_str());
http.end();
return false;
}
Serial.print("FetchAndSaveJsonWiFi http response: "); Serial.print(httpCode);
int contentLength = http.getSize();
Serial.print(", content size: "); Serial.println(contentLength);
if (contentLength <= 0) {
Serial.println("FetchAndSaveJsonWiFi invalid content length.");
http.end();
return false;
}
File file = SPIFFS.open(filePath, FILE_WRITE);
if (!file) {
Serial.println("FetchAndSaveJsonWiFi SPIFFS file couldn't open.");
http.end();
return false;
}
WiFiClient* stream = http.getStreamPtr();
uint8_t buff[256] = { 0 };
int totalBytes = 0;
while (http.connected() && (stream->available() || totalBytes < contentLength)) {
int len = stream->readBytes(buff, sizeof(buff));
if (len > 0) {
file.write(buff, len);
Serial.write(buff, len);
Serial.println();
totalBytes += len;
}
else {
Serial.println("FetchAndSaveJsonWiFi failed to read data during connection.");
file.close();
http.end();
return false;
}
}
file.close();
http.end();
if (totalBytes == 0 || totalBytes != contentLength) {
Serial.println("Could not get new JSON completely. Operation failed.");
return false;
}
Serial.printf("New JSON successfully written to memory. Total written bytes: %d\n", totalBytes);
return true;
}
SIM7600
bool FetchAndSaveJsonSIM() {
HttpClient httpGetCards(secure_layer, mainServer, 443); //http from ArduinoHttpClient.h and secure_layer from SSLClient.h (because TinyGsm doesn't have secure layer for SIM7600)
httpGetCards.connectionKeepAlive();
httpGetCards.beginRequest();
httpGetCards.get(getAddressPath);
httpGetCards.sendHeader("Authorization", String("Bearer ") + bearerToken);
httpGetCards.endRequest();
int httpCode = httpGetCards.responseStatusCode();
int contentLength = httpGetCards.contentLength();
if (httpCode != 200) {
Serial.printf("FetchAndSaveJsonSIM GET failed, error: %d\n", httpCode);
httpGetCards.stop();
return false;
}
Serial.print("FetchAndSaveJsonSIM http response: "); Serial.print(httpCode);
Serial.print(", content size: "); Serial.println(contentLength);
if (contentLength <= 0) {
Serial.printf("FetchAndSaveJsonSIM invalid content length: %d.\n", contentLength);
httpGetCards.stop();
return false;
}
File file = SPIFFS.open(filePath, FILE_WRITE);
if (!file) {
Serial.println("FetchAndSaveJsonSIM SPIFFS file couldn't open.");
httpGetCards.stop();
return false;
}
const int bufferSize = 256;
uint8_t buffer[bufferSize];
int bytesRead = 0;
while (httpGetCards.connected() && bytesRead < contentLength) {
int readSize = httpGetCards.read(buffer, sizeof(buffer));
//int readSize = httpGetCards.readBytes(buffer, sizeof(buffer)); //this also gives the same result
if (readSize > 0) {
file.write(buffer, readSize);
Serial.write(buff, len);
Serial.println();
bytesRead += readSize;
}
else {
break; //Whole content is written
Serial.println("break.");
}
delay(10);
}
file.close();
httpGetCards.stop();
Serial.print("bytesRead: "); Serial.println(bytesRead);
if (bytesRead == 0 || bytesRead != contentLength) {
Serial.println("Could not get new JSON completely. Operation failed.");
return false;
}
Serial.printf("New JSON successfully written to memory. Total written bytes: %d\n", bytesRead);
return true;
}
The SIM7600 function is closing the HTTPS connection after 2/3 of the file is fetched (approximately 240 KB). I need to be able to fetch the whole content with SIM7600 module. I have been trying to fix this for a week day and night but still couldn't find any solutions. Any improvements you can make to my code are welcome.
I tried to change HTTP Client library that I used but there were not any different libraries that uses "Client" object. Others are using NetworkClient vut TinyGsmClient's Client is not a NetworkClient.
Upvotes: 0
Views: 119