B&T BW16 Over the Air (OTA)

I am working on Realtek based B&T BW16 board. Currently I am facing the issue in OTA(over the air) firmware update. When I merge Basic OTA example with my code and add this line " ota.beginOTA(OTA_PORT); " in last of setup function in arduino ide, the program then never enters in the loop function.
12:42:57.562 → register mDNS service
12:42:57.562 → [beginOTA] Alloc buffer Init
12:42:57.604 → [beginOTA] Alloc buffer Succeed
12:42:57.651 →
12:42:57.651 →
Listening on port 8082.
12:42:57.698 →
Waiting for client …

It looks like this function is blocking function. what should be the possible solution.

Hi @zakriyazahid ,

You may want to move the beginMDNSService() and ota.beginOTA(OTA_PORT) to the loop function and implement a condition such that they will only be called when needed.

For example, you can use the pin state of a GPIO pin as the condition to perform OTA firmware update.

Thank you.

1 Like

Thank you so much for your suggestion. This would work sound. But If I want to implement it such that It always listen for program Over the Air along with normal functioning of program. Can it be possible with some other approach?

Yes, it’s possible. You can implement Over-the-Air (OTA) updates alongside normal program functionality by using dual-bank flash memory or a bootloader that supports OTA updates. This approach allows the device to listen for OTA updates while running its current program.

Thanks for your response. Can you plz share any example or link to understand and use the dual-bank flash memory or bootloader that supports OTA updates.

You will need to put ota.beginOTA(OTA_PORT); into a thread so it can wait for updates while the rest of your code is running.

#define OTA_PORT 8082

void setup() {
  Serial.begin(115200);

  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  // Send OTA Update to this IP
  Serial.println(WiFi.localIP());

  os_thread_create_arduino(otaThread, NULL, OS_PRIORITY_REALTIME, 4096);
}

void otaThread(const void *){
  while(true){
    if(ota.beginOTA(OTA_PORT) < 0 ) {
      Serial.println("Failed OTA Update");
    }
    delay(10000);
  }
}

void loop() {
  Serial.println(WiFi.localIP());
  delay(5000);
}

@mxv3a , thanks for your response. I was trying this thread mechanism using following code but board/device never appear in arduino IDE OTA port. Can you point out where am I wrong.

#include <OTA.h>
#include <WiFi.h>
#include <AmebaMDNS.h>

#define OTA_PORT 8082

char ssid[] = "****";       // your network SSID (name)
char pass[] = "****";           // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                   // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;        // Indicator of Wifi status

int ret = -1;

OTA ota;
MDNSService service("HelloDear", "_arduino._tcp", "local", 5000);

void setup() {
    // Initialize serial and wait for port to open:
    Serial.begin(115200);
    while (!Serial) {
        ;  // wait for serial port to connect.
    }
    // attempt to connect to Wifi network:
    while (status != WL_CONNECTED) {
        Serial.print("[MAIN] Attempting to connect to SSID: ");
        Serial.println(ssid);
        // Connect to WPA/WPA2 network. Change this line if using open or WEP
        // network:
        status = WiFi.begin(ssid, pass);
        // wait 10 seconds for connection:
        delay(10000);
    }


  os_thread_create_arduino(otaThread, NULL, OS_PRIORITY_REALTIME, 4096);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

    
    // you're connected now, so print out the status:
    printWifiStatus();

}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  }




void otaThread(const void *){

    // setup MDNS service to host OTA Server on the
    // Arduino Network Port
    beginMDNSService();

    // start connecting to OTA server and reboot 
    // with the new image
    ota.beginOTA(OTA_PORT);
}



void printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.println();
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());

    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);

    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}

void beginMDNSService() {
    service.addTxtRecord("board", strlen("ameba_rtl8721d"), "ameba_rtl8721d");
    service.addTxtRecord("auth_upload", strlen("no"), "no");
    service.addTxtRecord("tcp_check", strlen("no"), "no");
    service.addTxtRecord("ssh_upload", strlen("no"), "no");

    printf("Start mDNS service\r\n");
    MDNS.begin();

    printf("register mDNS service\r\n");
    MDNS.registerService(service);
}
type or paste code here

Unfortunately I don’t have a board to try your code right now, but I think you have to set the service port to your ota port.

MDNSService service("HelloDear", "_arduino._tcp", "local", OTA_PORT);

Also BW16 is a rtl8720dn based device.

service.addTxtRecord("board", strlen("ameba_rtl8720dn"), "ameba_rtl8720dn");

I don’t know if that solves the issue, but I can possibly try it out tomorrow.
Edit: checked it with a BW16 and this does fix the issue.

hi @zakriyazahid ,

Thanks for your request. You may want to check out our OTA HTTP example in the latest 3.1.8-build20241002 SDK version.

Here’s the link to the example guide on readthedocs.

Thank you.