OTA in esp32 with GSM A7670C using GITHUB API for bin file but above 400kb file didn't get and connection closed #127787
Unanswered
Nazneenashrafi
asked this question in
Programming Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Body
I am using esp32 and gsm a7670c module .... to do OTA using GitHub as a server to Download bin files from their using GitHub API's ... above 400kb bin file ... OTA didn't work ... I tried to change the buffer Rx buffer size but above I got nothing after increasing the buffer size above 1500 ...
this is my code
`#include <ArduinoHttpClient.h>
#define TINY_GSM_MODEM_SIM7600
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#ifndef AVR_ATmega328P
#define SerialAT Serial1
// or Software Serial on Uno, Nano
#else
#include <HardwareSerial.h>
HardwareSerial SerialAT(2); // RX, TX
#endif
#if !defined(TINY_GSM_RX_BUFFER)
#define TINY_GSM_RX_BUFFER 1500
#endif
#define TINY_GSM_DEBUG SerialMon
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "jionet";
const char gprsUser[] = "";
const char gprsPass[] = "";
bool readLengthExceeded = false;
bool clientDisconnected = false;
bool timeoutOccurred = false;
bool bodyEndReached = false;
// Server details
const int port= 443;
#include <TinyGsmClient.h>
#include <CRC32.h>
#include <ArduinoHttpClient.h>
#include "SSLClient.h"
#include <SPIFFS.h>
#include <FS.h>
#include "FS.h"
#include "SPIFFS.h"
#include<Update.h>
// Just in case someone defined the wrong thing..
#if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS false
#define TINY_GSM_USE_WIFI true
#endif
#if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#endif
const char server[] = "api.github.com";
const char resource[] = "/repos/Nazneenashrafi/ota/contents/test.txt";
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient base_client(modem, 0);
SSLClient secure_layer(&base_client);
HttpClient client = HttpClient( secure_layer, server, port);
const char * root_ca =
"-----BEGIN CERTIFICATE-----\n"
"MIIEojCCBEigAwIBAgIRAIvcD/9Udy+q0XMnPyM2Kq8wCgYIKoZIzj0EAwIwgY8x\n"
"CzAJBgNVBAYTAkdCMRswGQYDVQQIExJHcmVhdGVyIE1hbmNoZXN0ZXIxEDAOBgNV\n"
"BAcTB1NhbGZvcmQxGDAWBgNVBAoTD1NlY3RpZ28gTGltaXRlZDE3MDUGA1UEAxMu\n"
"U2VjdGlnbyBFQ0MgRG9tYWluIFZhbGlkYXRpb24gU2VjdXJlIFNlcnZlciBDQTAe\n"
"Fw0yNDAzMDcwMDAwMDBaFw0yNTAzMDcyMzU5NTlaMBcxFTATBgNVBAMMDCouZ2l0\n"
"aHViLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHADGElEmwEKQTOjCTeZ\n"
"EQ+YFacbykIKQ+I0OI2NQqjTnlj+3zpJ/j8XYiau+kL+Wz5r97U8Q+qZYaDQ2A6I\n"
"bzKjggL6MIIC9jAfBgNVHSMEGDAWgBT2hQo7EYbhBH0Oqgss0u7MZHt7rjAdBgNV\n"
"HQ4EFgQULNWfMkiYavm5W71lUenpddcgsZYwDgYDVR0PAQH/BAQDAgeAMAwGA1Ud\n"
"EwEB/wQCMAAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMEkGA1UdIARC\n"
"MEAwNAYLKwBQcBAQR4MHYwTwYIKwYBBQUHMAKG\n"
"Q2h0dHA6Ly9jcnQuc2VjdGlnby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FjK9Dpx1WywV4ilAbe7/ctvbq5AfjJXy\n"
"ij0IckKJUAfiORVsAYfZFhr1wHUrxeZWEQff2Ji8fJ8ZOd+LygBkc7xGEJuTI42+\n"
"FsMuCIKchjN0djsoTI0DQoWz4rIjQtUfenVqGtF8qmchxDM6OW1TyaLtYiKou+JV\n"
"bJlsQ2uRl9EMC5MCHdK8aXdJ5htN978UeAOwproLtOGFfy/cQjutdAFI3tZs4RmY\n"
"CV4Ks2dH/hzg1cEo70qLRDEmBDeNiXQ2Lu+lIg+DdEmSx/cQwgwp+7e9un/jX9Wf\n\n"
"8qn0dNW44bOwgeThpWOjzOoEeJBuv/c=\n"
"-----END CERTIFICATE-----\n";
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10)
SerialMon.println("Wait...");
// Set GSM module baud rate
SerialAT.begin(115200,SERIAL_8N1,16,17);
delay(6000);
if (!SPIFFS.begin(true))
{
SerialMon.println("SPIFFS Mount Failed");
return;
}
SPIFFS.format();
// listDir(SPIFFS, "/", 0);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
#endif
secure_layer.setCACert(root_ca);
client.setHttpResponseTimeout(120000);
client.setHttpWaitForDataDelay(500);
}
void printPercent(uint32_t readLength, uint32_t contentLength) {
// If we know the total length
if (contentLength != (uint32_t)-1) {
SerialMon.print("\r ");
SerialMon.print((100.0 * readLength) / contentLength);
SerialMon.print('%');
} else {
SerialMon.println(readLength);
}
}
void loop() {
#if TINY_GSM_USE_WIFI
// Wifi connection parameters must be set before waiting for the network
SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
#endif
#if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
// The XBee must run the gprsConnect function BEFORE waiting for network!
modem.gprsConnect(apn, gprsUser, gprsPass);
#endif
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); }
#if TINY_GSM_USE_GPRS
// GPRS connection parameters are usually set after network registration
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isGprsConnected()) { SerialMon.println("GPRS connected"); }
#endif
SerialMon.print(F("Connecting to "));
SerialMon.println(" success");
client.beginRequest();
// client.sendHeader("Url"," HTTP/1.0");
client.get(resource);
client.sendHeader("User-Agent","tiny-gsm-module");
client.sendHeader("Accept","application/vnd.github.v3.raw");
client.sendHeader("Authorization","Bearer ghp_77CN7XOGCw81UZ08x");
client.sendHeader("X-GitHub-Api-Version","2022-11-28");
client.endRequest();
// File file = SPIFFS.open("/update.bin", FILE_APPEND);
SerialMon.println(F("Waiting for response header"));
// Let's see what the entire elapsed time is, from after we send the request.
uint32_t timeElapsed = millis();
const uint32_t clientReadTimeout = 10000;
uint32_t clientReadStartTime = millis();
String headerBuffer;
bool finishedHeader = false;
uint32_t contentLength = 0;
while (!finishedHeader) {
int nlPos;
// Let's exit and process if we find a new line
if (headerBuffer.indexOf(F("\r\n")) >= 0) break;
}
uint32_t readLength = 0;
CRC32 crc;
if (finishedHeader) {
SerialMon.println(F("Reading response data"));
clientReadStartTime = millis();
printPercent(readLength, contentLength);
while (true) {
if (readLength >= contentLength) {
Serial.println("readLength reached");
readLengthExceeded = true;
break;
}
}
printPercent(readLength, contentLength);
}
// file.close();
timeElapsed = millis() - timeElapsed;
SerialMon.println();
if (readLengthExceeded) {
Serial.println("Exited loop because readLength >= contentLength");
}
if (clientDisconnected) {
Serial.println("Exited loop because client disconnected");
}
if (timeoutOccurred) {
Serial.println("Exited loop because of timeout");
}
if (bodyEndReached) {
Serial.println("Exited loop because end of body reached");
}
// Shutdown
client.stop();
SerialMon.println(F("Server disconnected"));
#if TINY_GSM_USE_WIFI
modem.networkDisconnect();
SerialMon.println(F("WiFi disconnected"));
#endif
#if TINY_GSM_USE_GPRS
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
#endif
// updateFromFS();
float duration = float(timeElapsed) / 1000;
SerialMon.println();
SerialMon.print("Content-Length: ");
SerialMon.println(contentLength);
SerialMon.print("Actually read: ");
SerialMon.println(readLength);
SerialMon.print("Calc. CRC32: 0x");
SerialMon.println(crc.finalize(), HEX);
// SerialMon.print("Known CRC32: 0x");
// SerialMon.println(knownCRC32, HEX);
SerialMon.println(headerBuffer);
SerialMon.print("Duration: ");
SerialMon.print(duration);
SerialMon.println("s");
// Do nothing forevermore
while (true) { delay(1000); }
}
void appendFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Appending to file: %s\n", path);
}
void readFile(fs::FS &fs, const char *path)
{
Serial.printf("Reading file: %s\n", path);
}
void writeFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Writing file: %s\n", path);
}
void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
Serial.printf("Listing directory: %s\n", dirname);
}
void deleteFile(fs::FS &fs, const char *path)
{
Serial.printf("Deleting file: %s\n", path);
if (fs.remove(path))
{
Serial.println("File deleted");
}
else
{
Serial.println("Delete failed");
}
}
void updateFromFS()
{
File updateBin = SPIFFS.open("/update.bin");
if (updateBin)
{
if (updateBin.isDirectory())
{
Serial.println("Directory error");
updateBin.close();
return;
}
}
void performUpdate(Stream &updateSource, size_t updateSize)
{
if (Update.begin(updateSize))
{
size_t written = Update.writeStream(updateSource);
if (written == updateSize)
{
Serial.println("Writes : " + String(written) + " successfully");
}
else
{
Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
}
if (Update.end())
{
Serial.println("OTA finished!");
if (Update.isFinished())
{
Serial.println("Restart ESP device!");
ESP.restart();
}
else
{
Serial.println("OTA not fiished");
}
}
else
{
Serial.println("Error occured #: " + String(Update.getError()));
}
}
else
{
Serial.println("Cannot beggin update");
}
}
`
output when buffer size is above 1500
+CPIN: READY
SMS DONE
Modem Info: ATI Manufacturer: INCORPORATED Model: A7670C-LASL Revision: A7670M7_V1.11.1 IMEI: 866651062719516 +GCAP: +CGSM,+FCLASS,+DS
Waiting for network... success
Network connected
Connecting to jionet success
GPRS connected
Connecting to success
[25813] ### Unhandled: PB DONE
[31110] ### Closed: 0
Status code: -3
Waiting for response header
Client Timeout !
Server disconnected
GPRS disconnected
Content-Length: 0
Actually read: 0
Calc. CRC32: 0x0
Duration: 10.00s
output when 1500 buffer size
+CPIN: READY
SMS DONE
Modem Info: ATI Manufacturer: INCORPORATED Model: A7670C-LASL Revision: A7670M7_V1.11.1 IMEI: 866651062719516 +GCAP: +CGSM,+FCLASS,+DS
Waiting for network... success
Network connected
Connecting to jionet success
GPRS connected
Connecting to success
Status code: 200
Waiting for response header
Got Content Length: 1339192
Reading response data
0.00% "content is here" timeoutOccurred
28.48%
Exited loop because of timeout
Server disconnected
GPRS disconnected
Content-Length: 1339192
Actually read: 381338
Calc. CRC32: 0x0
Duration: 79.71s
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions