Struggling in doing FOTA in RP2040

Hi all, i am trying to do Fota in rp2040 using sim800l i am successfully able to download the .bin file but on restart my old firmware is running

my code is following

#define TINY_GSM_MODEM_SIM800

#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <PicoOTA.h>
#include <LittleFS.h>
#include <SoftwareSerial.h>


#define TINY_GSM_RX_BUFFER          1024

#define FILE_NAME                   "ota.bin"

// set GSM PIN, if any
#define GSM_PIN ""

// Your GPRS credentials, if any
const char apn[]      = "www";
const char gprsUser[] = "";
const char gprsPass[] = "";


// Server details
const char server[]   = "103.127.30.171";
const char resource[] = "/download/Blink.ino.bin";
const int  port       = 8080;

//// Server details
//const char server[] = "vsh.pp.ua";
//const char resource[]    = "/TinyGSM/test_1k.bin";
//const int  port     = 80;


SoftwareSerial SerialAT(16, 17);  // RX, TX
TinyGsm        modem(SerialAT);
TinyGsmClient client(modem);
HttpClient    http(client, server, port);


void setup() {
  pinMode(25, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(5000);
  Serial.println("Rp2040 FOTA LittelFS Wait...");

  if (!LittleFS.begin()) {
    Serial.println("LittleFS Mount Failed");
  }

  if (checkFile(FILE_NAME))
  {
    if (removeFile(FILE_NAME))
    {
      Serial.println("File Deleted successfully");
    } else
    {
      Serial.println("Unable to delete file");
    }
  } else
  {
    Serial.println("File Dose not exist");
  }

  SerialAT.begin(9600);
  delay(6000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.restart();
  // modem.init();
  delay(6000);
  String modemInfo = modem.getModemInfo();
  Serial.print("Modem Info: ");
  Serial.println(modemInfo);

  if (GSM_PIN && modem.getSimStatus() != 3) {
    modem.simUnlock(GSM_PIN);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

  modem.gprsConnect(apn, gprsUser, gprsPass);
  digitalWrite(25 , HIGH);
  digitalWrite(10 , HIGH);
  Serial.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    Serial.println(" fail");
    delay(10000);
    return;
  }
  Serial.println(" success");

  if (modem.isNetworkConnected()) {
    Serial.println("Network connected");
  }

  // GPRS connection parameters are usually set after network registration
  Serial.print(F("Connecting to "));
  Serial.print(apn);
  if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
    Serial.println(" fail");
    delay(10000);
    return;
  }
  Serial.println(" success");
  digitalWrite(11 , HIGH);
  if (modem.isGprsConnected()) {
    Serial.println("GPRS connected");
  }

//
//  Serial.print(F("Connecting to "));
//  Serial.print(server);
//  if (!client.connect(server, port)) {
//    Serial.println(" fail");
//    delay(10000);
//    return;
//  }
//  Serial.println(" OK");
//
//  // Make a HTTP GET request:
//  client.print(String("GET ") + resource + " HTTP/1.1\r\n");
//  client.print(String("Host: ") + server + "\r\n\r\n\r\n\r\n");
//  client.print("Connection: close\r\n\r\n\r\n\r\n");
//
//  long timeout = millis();
//  while (client.available() == 0) {
//    if (millis() - timeout > 5000L) {
//      Serial.println(F(">>> Client Timeout !"));
//      client.stop();
//      delay(10000L);
//      return;
//    }
//  }
//
//  Serial.println(F("Reading response header"));
//  uint32_t contentLength = 67184;
//  
//  File file = LittleFS.open(FILE_NAME, "w");
//
//  while (client.connected()) {
//    String line = client.readStringUntil('\n');
//    line.trim();
//    Serial.println(line);    // Uncomment this to show response header
//    line.toLowerCase();
//    if (line.startsWith("content-length:")) {
//      contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
//    } else if (line.length() == 0) {
//      break;
//    }
//  }
//
//  Serial.println(F("Reading response data"));
//  timeout = millis();
//  uint32_t readLength = 0;
////  CRC32 crc;
//
//  unsigned long timeElapsed = millis();
//  printPercent(readLength, contentLength);
//  while (readLength < contentLength /* && client.connected() /*&& millis() - timeout < 10000L*/) {
//    int i = 0;
//    while (client.available()) {
////      uint8_t c = client.read();
//      if (!file.write(client.read()))
//      {
//          Serial.println("error writing character to LittleFS");
//      }
//      readLength++;
//      if (readLength % (contentLength / 13) == 0) {
//        printPercent(readLength, contentLength);
//      }
//      timeout = millis();
//    }
//  }
//  printPercent(readLength, contentLength);
//  timeElapsed = millis() - timeElapsed;
//  Serial.println();
//
//  // Shutdown
//
//  client.stop();
//  Serial.println(F("Server disconnected"));
//
//  modem.gprsDisconnect();
//  Serial.println(F("GPRS disconnected"));
//
//  float duration = float(timeElapsed) / 1000;
//
//  Serial.println();
//  Serial.print("Content-Length: ");   Serial.println(contentLength);
//  Serial.print("Actually read:  ");   Serial.println(readLength);
////  SerialMon.print("Calc. CRC32:    0x"); SerialMon.println(crc.finalize(), HEX);
////  SerialMon.print("Known CRC32:    0x"); SerialMon.println(knownCRC32, HEX);
//  Serial.print("Duration:       ");   Serial.print(duration); Serial.println("s");
//  
//  Serial.println("starting Update after 3 seconds  ");
//  for (int i = 0; i < 3; i++)
//  {
//      Serial.print(String(i) + "...");
//      delay(1000);
//  }


  Serial.print(F("Performing HTTP GET request... "));
  int err = http.get(resource);
  if (err != 0) {
    Serial.println(F("failed to connect"));
    delay(10000);
    return;
  }
  digitalWrite(12 , HIGH);
  int status = http.responseStatusCode();
  Serial.print(F("Response status code: "));
  Serial.println(status);
  if (!status) {
    delay(10000);
    return;
  }

  Serial.println(F("Response Headers:"));
  while (http.headerAvailable()) {
    String headerName  = http.readHeaderName();
    String headerValue = http.readHeaderValue();
    Serial.println("    " + headerName + " : " + headerValue);
  }

  int length = http.contentLength();
  if (length >= 0) {
    Serial.print(F("Content length is: "));
    Serial.println(length);
  }
  if (http.isResponseChunked()) {
    Serial.println(F("The response is chunked"));
  }

  File file = LittleFS.open(FILE_NAME, "a+"); // "w" opens the file for writing (if it exists)
  if (!file) {
    Serial.println("Failed to open file");
    return;
  }  
  String body = http.responseBody();
  file.write(body.c_str(), 67512);
//  Serial.println(F("Response:"));
//  Serial.println(body);

  Serial.print(F("Body length is: "));
  Serial.println(body.length());

  // Shutdown

  http.stop();
  Serial.println(F("Server disconnected"));


  file.close();

  file = LittleFS.open(FILE_NAME, "r"); // "w" opens the file for writing (if it exists)
  if (!file) {
    Serial.println("Failed to open file");
    return;
  }
 
  if (file) {
    Serial.println("Reading");

    while (file.available()) {
      //  Serial.println("iN FUNCTION");
      uint8_t c = file.read();
      Serial.print(c);   
    }
  }else{
    Serial.println("File Doesnot Exist");
  }
  file.close();

  if (checkFile(FILE_NAME))
  {
    performFOTA(FILE_NAME);

    delay(10000);
    if (removeFile(FILE_NAME))
    {
      Serial.println("File Deleted successfully");
    } else
    {
      return;
    }
  } else
  {
    return;
  }
  digitalWrite(10 , LOW);
  digitalWrite(11 , LOW);
  digitalWrite(12 , LOW);

  LittleFS.end();
  Serial.println("done\n\n");
  Serial.printf("Rebooting in 5 seconds, should begin blinker instead of this app...\n\n");
  delay(5000);
  Serial.println("Restarting Device................\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  rp2040.reboot(); 

}

void performFOTA(const char* fileName)
{
  Serial.printf("Programming OTA commands...");
  picoOTA.begin();
  picoOTA.addFile(fileName);
  picoOTA.commit();
}


bool checkFile(const char* fileName)
{
  // Check if the file exists
  if (LittleFS.exists(fileName)) {
    // File exists
    return true;
  } else {
    // file dose not exist
    return false;
  }
}

bool removeFile(const char* fileName)
{
  // remove exist file
  if (LittleFS.remove(fileName)) {
    // file removed successfully
    Serial.println("File deleted successfully");
    return true;
  } else {
    // failed to remove file
    Serial.println("Error deleting file");
    return false;
  }
}

size_t getFileSize(const char* filename) {
  File file = LittleFS.open(filename, "r");
  if (!file) {
    Serial.println("Failed to open file for reading");
    return 0; // Return 0 if file cannot be opened
  }

  size_t fileSize = file.size();
  file.close();
  return fileSize;
}


void printPercent(uint32_t readLength, uint32_t contentLength) {
  // If we know the total length
  if (contentLength != -1) {
    Serial.print("\r ");
    Serial.print((100.0 * readLength) / contentLength);
    Serial.print('%');
  } else {
    Serial.println(readLength);
  }
}

please suggest what wrong i am doing

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