Cyber Chasse- OTA Updates

OTA Updates (Arduino)

OTA updates (Over the Air) involves loading of firmware to ESP module with Wi-Fi connection instead of a serial port. This functionality turned out to be extremely useful in cases that have no physical access or limited access to the module. 
 
Two separate partitions are created, one for the working firmware and another one for the new firmware. A new firmware gets downloaded in the new partition as soon as the OTA request is created, and the pointer is modified to the new firmware address. If there are issues in downloading or flashing, the previous partition’s address gets pointed. 
 
Let’s now focus on OTA through Arduino IDE. 
 

Note: OTA will not be supported by a chipset with 512KB of space. 

OTA Can Be Done With 

  • Arduino IDE 
  • Web Browser 
  • HTTP Server 

OTA Prerequisites 

  • Arduino ID and ESP8266 on the same network 

Note: If you are a Windows user, choose “Add python.exe to Path” to install python 2.7 

Let’s Begin 
1. Using a USB cable, connect ESP8266 to your device 
 
2. Upload basic OTA.ino using serial communication 
 

OTA Updates

 
3. Choose your ESP board model under Tools. Also, you have to select your ESP’s COM port 

OTA Updates

 
4. Open Arduino IDE. Under Files, choose Examples followed by ArduinoOTA and under that select Basic OTA.  

OTA Updates

 
NOTE:- Sketch example should be changed using your SSID and password. (E.g. SSID- XXXXXX, Password-XXXXXXXXX) 
 
5. In the Arduino IDE click the “Upload” option and wait for the “Done uploading” message 
 
6. At 115200 baud rate, click open the Arduino IDE serial monitor. Your ESP IP address would appear in a few seconds if the network credentials you entered are right. 

Over the Air Updates

 
7. Now you can upload the new sketch Over The Air (OTA). Besides, you can remove the ESP8266 from your device and charge it through any power source (for example a power bank) as the ESP8266 is now ready to get OTA firmware updates. If the ESP8266 has a wireless connection to the router, you can seamlessly upload new firmware. 
 

Over the Air Update

 
8. On the Arduino IDE, click open Tools tab and choose the Port option to see something like esp8266-xxxxxx on your_esp_ip_address. 
 

Over the Air Update

 
9. Copy the below sketch to your Arduino IDE and upload it on the ESP8266. This sketch flashes the ESP12-E NodeMCU kit in-built LED once in every second. 
 
Source code 

 #include <ESP8266WiFi.h>  
   
 #include <ESP8266mDNS.h>  
   
 #include <WiFiUdp.h>  
   
 #include <ArduinoOTA.h>  
   
    
   
 const char* ssid = “XXXXXXX”;  
   
 const char* password = “XXXXXX”;  
   
    
   
 void setup() {  
   
   Serial.begin(115200);  
   
   Serial.println(“Booting”);  
   
   WiFi.mode(WIFI_STA);  
   
   WiFi.begin(ssid, password);  
   
   while (WiFi.waitForConnectResult() != WL_CONNECTED) {  
   
     Serial.println(“Connection Failed! Rebooting…”);  
   
     delay(5000);  
   
     ESP.restart();  
   
   }  
   
    
   
   // Port defaults to 8266  
   
   // ArduinoOTA.setPort(8266);  
   
    
   
   // Hostname defaults to esp8266-[ChipID]  
   
   // ArduinoOTA.setHostname(“myesp8266”);  
   
    
   
   // No authentication by default  
   
   // ArduinoOTA.setPassword(“admin”);  
   
    
   
   // Password can be generated using md5 value as well  
   
   // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3  
   
   // ArduinoOTA.setPasswordHash(“21232f297a57a5a743894a0e4a801fc3”);  
   
    
   
   ArduinoOTA.onStart([]() {  
   
     String type;  
   
     if (ArduinoOTA.getCommand() == U_FLASH)  
   
       type = “sketch”;  
   
     else // U_SPIFFS  
   
       type = “filesystem”;  
   
    
   
     // NOTE: If you want to update the SPIFFS, this is the place to unmount SPIFFS using SPIFFS.end()  
   
     Serial.println(“Start updating ” + type);  
   
   });  
   
   ArduinoOTA.onEnd([]() {  
   
     Serial.println(“\nEnd”);  
   
   });  
   
   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {  
   
     Serial.printf(“Progress: %u%%\r”, (progress / (total / 100)));  
   
   });  
   
   ArduinoOTA.onError([](ota_error_t error) {  
   
     Serial.printf(“Error[%u]: “, error);  
   
     if (error == OTA_AUTH_ERROR) Serial.println(“Auth Failed”);  
   
     else if (error == OTA_BEGIN_ERROR) Serial.println(“Begin Failed”);  
   
     else if (error == OTA_CONNECT_ERROR) Serial.println(“Connect Failed”);  
   
     else if (error == OTA_RECEIVE_ERROR) Serial.println(“Receive Failed”);  
   
     else if (error == OTA_END_ERROR) Serial.println(“End Failed”);  
   
   });  
   
   ArduinoOTA.begin();  
   
   Serial.println(“Ready”);  
   
   Serial.print(“IP address: “);  
   
   Serial.println(WiFi.localIP());  
   
 }  
   
    
   
 void loop() {  
   
   ArduinoOTA.handle();  
   
 }  

view rawota_updates.ino hosted with ❤ by GitHub 

 
 
10. On the Arduino IDE, click the “Upload” option and wait till the “Done uploading” message pops up 
 
11. The built-in LED should blink every second in your ESP 
 
Code Explanation 
1. Add all OTA-dependent files 
 
 

  • #include<ESP8266WiFi.h> 
  • #include<esp8266mdns.h> 
  • #include<WiFiUdp.h> 
  • #include<ArduinoOTA.h> 

2. Generate network credentials 
 
 

  • const char* ssid = “XXXXXXX”; 
  • const char* password = “XXXXXX”; 

3. Setup Wi-Fi connection 
 
 

  • void setup() { 
  •   Serial.begin(115200); 
  •   Serial.println(“Booting”); 
  •   WiFi.mode(WIFI_STA); 
  •   WiFi.begin(ssid, password); 
  •   while (WiFi.waitForConnectResult() != WL_CONNECTED) { 
  •     Serial.println(“Connection Failed! Rebooting…”); 
  •     delay(5000); 
  •     ESP.restart(); 
  •   } 

4. ArduinoOTA.onStart function checks the file type, downloads the sketch, alters the pointerand resets. 
 
 

  • ArduinoOTA.onStart([]() { 
  •     String type; 
  •     if (ArduinoOTA.getCommand() == U_FLASH) 
  •       type = “sketch”; 
  •     else // U_SPIFFS 
  •       type = “filesystem”; 
  •   
  •     // NOTE: If you want  to update the SPIFFS, then this is the place to unmount SPIFFS using SPIFFS.end() 
  •     Serial.println(“Start updating ” + type); 
  •   }); 
  •   ArduinoOTA.onEnd([]() { 
  •     Serial.println(“\nEnd”); 
  •   }); 

5. OTA.onError function highlights the types of errors that occur while the code flashes 
 
 

  •     ArduinoOTA.onError([](ota_error_t error) { 
  •     Serial.printf(“Error[%u]: “, error); 
  •     if (error == OTA_AUTH_ERROR) Serial.println(“Auth Failed”); 
  •     else if (error == OTA_BEGIN_ERROR) Serial.println(“Begin Failed”); 
  •     else if (error == OTA_CONNECT_ERROR) Serial.println(“Connect Failed”); 
  •     else if (error == OTA_RECEIVE_ERROR) Serial.println(“Receive Failed”); 
  •     else if (error == OTA_END_ERROR) Serial.println(“End Failed”); 
  •   }); 

6. OTA begin function executes OTA functionality 
 
 

  • ArduinoOTA.begin(); 

7. OTA Handle handles the functions received from esp_ota_begin in the loop function. This keeps checking for the OTA request.  
 
 

  • ArduinoOTA.handle(); 

In this blog, we learned about OTA Updates. If you still face issues related to OTA Updates, feel free to post your questions in the Comment Box below and don’t forget to follow us on 👍 Social Networks😉