ESP8266 - Over The Air (OTA) Uploads

Espressif ESP8266 boards support Over The Air (OTA) software uploads, allowing you to update the code on-chip without having to reconnect it to your PC. 

This can be especially useful if you have embedded the board into a project in a hard to reach or inconvenient place, such as your loft or garden.

 

OTA with auto-discovery

"Apple Bonjor for Windows" must be installed for IP address auto discovery to work. This will automatically look for IP addresses on the local network.

If an upload password has been set in the espcode, a password dialog will appear when OTA begins

OTA configuration is automatic. The image shows two esp boards have been discovered using the Apple Bonjour Service.

 

OTA without requiring Apple Bonjour auto-discovery (optional)

Ip addresses can be manually entered into the Visual Micro ports (serial) list when the combo box is in the collapsed state. After entering the Ip address, press the ENTER key to confirm.

mDNS properties such as {network.port} will not be available when an ip address is entered manually (and the address has not been discovered by the Bonjour Service)

Instead of using mDNS a local project "board.txt" file can be added to the project.

The image shows the project's board.txt in the lower editor. Apple Bonjour or IP Auto-Discovery is not required. You can manually enter an ip address and press the ENTER key. 

 

Visual Micro Pro users can use board.txt in the project folder to provide overrides (instead of using Bonjour)

Board.txt password required example:-

 # local project board.txt overrides
  network.password=123
  network.auth_upload=yes
  network.port=8266

Board.txt password not required example:-

 # local project board.txt overrides
  network.port=8266

 

Full working ESP8266 OTA Code Example:-

Tested with Esp OTA code (esp core v2.3.0) :-


#include <ArduinoOTA.h>
bool ota_started;

const char* ssid = "YOUR_ROUTER_SSID";
const char* password = "YOUR_ROUTER_WIFI_PASSWORD";
void setup() 
{
Serial.begin(115200);
ConnectToWiFi();
StartOTAIfRequired();
PrintWifiStatus();
Serial.println("Connected to wifi");
}
void loop() 
{
//Serial.println("Hello world");
HandleOTA();
}

void ConnectToWiFi()
{

Serial.println("Booting");
WiFi.mode(WIFI_STA);
Serial.println("Mode set");
WiFi.begin(ssid, password);
Serial.println("Begin complete");
}
void HandleOTA()
{
StartOTAIfRequired();
ArduinoOTA.handle();
}
void StartOTAIfRequired()
{
if (ota_started)
return;
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
//if (ArduinoOTA.getHostname() && ArduinoOTA.getHostname().length())

// No authentication by default
ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
Serial.println("OTA Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nOTA End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r\n", (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();
ota_started = true;
delay(500);

}
void PrintWifiStatus() 
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
//using dhcp? wait for ip or ip not set!
if (WiFi.localIP()[0] == 0)
{
Serial.println("DHCP: Waiting for IP Address ...");
while (WiFi.localIP()[0] == 0)
{
yield();
}
}
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
//Serial.println(WiFi.status());
}