OTA Online Upgrade for Arduino

1. Introduction to OTA

OTA stands for Over-the-Air, which refers to the technology for remote management of SIM card data and applications through mobile communication (GSM or CDMA) over the air interface. The air interface can utilize WAP, GPRS, CDMA1X, and SMS technologies. The application of OTA technology allows mobile communications to not only provide voice and data services but also to offer new business downloads. In the Internet of Things (IoT) field, especially in embedded devices like the ESP8266 Wi-Fi microcontroller, the OTA function allows devices to receive and install new software or firmware updates via wireless connections. This is very useful for remotely managing and maintaining a large number of devices, as it avoids the cumbersome process of manually updating each device. In the Arduino development environment for ESP8266, the OTA function is implemented through specific libraries and code, allowing developers to upload new firmware to the device via Wi-Fi connection. This greatly simplifies the firmware update process and enables devices to continuously receive new features and fixes. Overall, OTA is a powerful and flexible technology that allows embedded devices and IoT devices to perform software updates and maintenance more conveniently.

2. OTA Code

The project uses the Arduino IDE to develop the ESP8266 module and implement the OTA (Over-The-Air) function, allowing remote updates of the firmware on the device via Wi-Fi connection.

#include <ESP8266WiFi.h>  
#include <ESP8266mDNS.h>  
#include <WiFiUdp.h>  
#include <ArduinoOTA.h>  
  
const char* ssid = "your_wifi_ssid";  
const char* password = "your_wifi_password";  
  
void setup() {  
  Serial.begin(115200);  
  delay(10);  
  
  // Connect to Wi-Fi network  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED) {  
    delay(500);  
    Serial.print(".");  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  Serial.print("IP address: ");  
  Serial.println(WiFi.localIP());  
  
  // Initialize mDNS and ArduinoOTA  
  if (!MDNS.begin("esp8266")) {  
    Serial.println("Error setting up mDNS");  
  } else {  
    Serial.println("mDNS responder started");  
  }  
  
  ArduinoOTA.setHostname("myesp8266");  
  ArduinoOTA.setPassword("your_ota_password");  
    
  ArduinoOTA.begin();  
  
  Serial.println("OTA address: " + ArduinoOTA.getLocalIp().toString() + ":8266");  
}  
  
void loop() {  
  // Check for OTA updates  
  ArduinoOTA.handle();  
  
  // Add your main loop code here  
}

First, connect to the Wi-Fi network, then initialize mDNS and ArduinoOTA. ArduinoOTA.setHostname(): Set the device’s hostname. ArduinoOTA.setPassword(): Set the password for OTA updates. ArduinoOTA.begin(): Start the OTA service. Once the code is uploaded successfully and the device is connected to Wi-Fi, you can use the OTA feature in the Arduino IDE to update the firmware. In the Arduino IDE, select “Tools” > “ESP8266 Sketch Data Upload” to upload the new firmware. Note: Ensure the device IP address and OTA password are correct. Follow the above steps to check if the ESP8266 Sketch Data Upload tool is available… You can refer to https://mc.dfrobot.com.cn/thread-314415-1-1.html

3. Third-party Library BGWiFiConfig

The BGWiFiConfig library supports OTA upgrades. Modify the official code for testing, test code:

#include <BGWiFiConfig.h>
BGWiFiConfig wifipw;

void setup() {
  Serial.begin(115200);
  Serial.println();
  //Serial.println("V1.1");// Initial version does not have this print information, upgrade version has this print information
  wifipw.begin();
  wifipw.OTAbegin();
}

void loop() {
  char rxBuf[10];
  wifipw.Loop();
  wifipw.OTALoop();
    if( Serial.available() ) // Check if there is data on the serial port
  {
    String data = Serial.readStringUntil('\n');// Read data
    Serial.println("Rx:");
    Serial.println(data); 
    std::memset(rxBuf,0,sizeof(rxBuf));
    strcpy(rxBuf,data.c_str());
    if(strcmp("wifiClear", rxBuf)==0) 
      wifipw.clearWiFi(); 
  }
}

First, perform network configuration, restart, and obtain the local address after connecting to the network. Enter the address in the web browser, select the bin file to upgrade, and OTA online upgrade can proceed. OTA Online Upgrade for Arduino

Leave a Comment