OTA update ESP32 via Bluetooth classic

i try to update esp32 firmware via bluetooth classic using "serial bluetooth terminal app" for sending bin file

when i send file it wait for 100% to complete in the app
image
, and then it show me this message Timeout: No data received.


is my code ok ?



#include <BluetoothSerial.h>
BluetoothSerial ble_serial;  // Create Bluetooth Serial object
#include <Update.h>


void setup(){
  ble_serial.begin("ar_123");  // Initialize Bluetooth with a specific name
}

void loop(){
 if (SerialBT.available()) {
    String command = SerialBT.readStringUntil('\n');

    if (command == "OTA?") {
      Serial.println("OTA Command Received! Ready for firmware...");
      SerialBT.println("Send Firmware Now...");
      OTA();
    }
  }
}



void OTA() {
  ble_serial.println("Waiting for firmware file via Bluetooth...");

  // Wait for the first data packet
  while (!ble_serial.available()) {
    delay(100);
  }

  // Begin OTA update with unknown file size
  if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
    ble_serial.println("Failed to start OTA update.");
    return;
  }

  size_t written = 0;
  unsigned long lastDataTime = millis();
 
  while (true) {
    if (ble_serial.available()) {        //without buffer
      uint8_t data = ble_serial.read();  // Read one byte at a time
      Update.write(&data, 1);
      written++;
      lastDataTime = millis();  // Reset timeout timer
      esp_task_wdt_reset();  //RESET  WDT watchdog
    }
 
    // Check for timeout (no data received for 5 seconds)
    if (millis() - lastDataTime > 5000) {
      ble_serial.println("Timeout: No data received.");
      Update.abort();
      return;
    }

    // Check if update is complete
    if (Update.isFinished()) {
      break;
    }
  }

  ble_serial.printf("Firmware received: %d bytes\n", written);

  if (Update.end()) {
    ble_serial.println("Firmware Installed! Restarting...");
    ESP.restart();
  } else {
    ble_serial.println("Firmware Update Failed!");
  }
}

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