Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iotguider/9bc232053ddd9950563ce4cc4809dbd2 to your computer and use it in GitHub Desktop.
Save iotguider/9bc232053ddd9950563ce4cc4809dbd2 to your computer and use it in GitHub Desktop.
Code for ESP8266 OTA (Over The Air) Update using Arduino IDE
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
const char* ssid = "WiFi_SSID"; //Enter Wi-Fi SSID
const char* password = "WiFi_Password"; //Enter Wi-Fi Password
void setup() {
Serial.begin(115200); //Begin Serial at 115200 Baud
WiFi.begin(ssid, password); //Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED) { //Wait for connection
delay(500);
Serial.println("Waiting to connect...");
}
ArduinoOTA.setHostname("myesp8266"); // Set ESP8266 Hostname (defaults to esp8266-[ChipID])
ArduinoOTA.setPassword("admin"); // No authentication password if you remove it
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be 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()); //Print the local IP
}
void loop() {
ArduinoOTA.handle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment