The ArduinoOTA library is a library that allows to update the Arduino program (and ESP3226, ESP32) wirelessly in Wi-Fi. It is an essential library when developing connected objects. It allows to update the program without having to disassemble the microcontroller (Arduino, ESP8266, ESP32) to connect it to his computer. This library was originally developed to update the Arduino programs, it is perfectly supported by the ESP8266 and ESP32. For this tutorial, we will only address the case of ESP8266.
Install the ArduinoOTA library for ESP8266 on the Arduino IDE
The ArduinoOTA library is integrated into the ESP8266 library. There is nothing more to install. If you’re new to ESP, here’s how to install the ESP library and support. Open the preferences (from the Arduino menu) and add to the list of packages this one
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Then go to the board manager and look for esp8266, then click install.
After installation, new examples have been added to the sample menu. Select your ESP8266 board to display the available samples. Open the BasicOTA example in the ArduinoOTA submenu
This program contains everything you need to manage wireless upgrade in your ESP projects.
#include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <WiFiUdp.h> #include <ArduinoOTA.h> const char* ssid = ".........."; const char* password = ".........."; 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("Demo OTA ESP8266"); // No authentication by default ArduinoOTA.setPassword((const char *)"123"); ArduinoOTA.onStart([]() { Serial.println("Start"); }); 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(); }
Before reversing the program, change the ssid variables (Wi-Fi network to which to connect) and password (password).
Here, the program uses the ESP8266WiFi library to connect to the network. You can also use the WiFiManager library presented earlier.
Once the connection is established, it is possible to define additional (optional) parameters:
- SetPort: by default the communication port is 8266. It is not very useful to change it unless it is already in use. For example, for a project integrating UDP communication.
- SetHostname: This is probably the most important option. It allows you to assign a name to the ESP8266. If you have a lot of objects on your network, it will be much easier to identify it by name than by an identifier built automatically from the serial number.
- SetPassword: used to request a password from the Arduino IDE before running an update. Practice if you work at several or in a school.
There are four other ways to connect treatments in the following cases:
- OnStart: code to run when the update is started
- OnEnd: idem at the end
- OnProgress: during the progress
- OnError: and on error
You will not need to add treatments in most cases. In particular, these functions allow us to remain informed of the smooth running of operations.
At the end of the setup, once all functions have been defined, start the wireless update support by calling the begin () method.
Finally, for everything to work, it is necessary to call the ArduinoOTA.handle () method in the loop () loop.
Wireless Update Test
Upload the program and open the Terminal to verify that the ESP8266 is properly connected to the Wi-Fi network.
The Arduino IDE automatically detects devices that support remote update. They are added to the list of ports in a new section called Network ports.
To update the program, simply select the ESP as the port instead of the usual serial port. Then upload the program as usual. Here, as a password is required, an input window appears in the IDE. It is requested only once
The remote update (or failure) of the remote update can be tracked directly from the Arduino IDE.
It is not possible to open the serial monitor with wireless setup. A small Web interface is required. Read this tutorial to learn how to add a Web server and HTML interface to an ESP8266 project. To go further, here is a series of articles dealing with the subject
- Get Started with HC-SR04, measure distance by ultrasound. Arduino code example 12 February 2021
- ESP32, GPIO pins and associated functions. I/O, PWM, RTC, I2C, SPI, ADC, DAC 9 February 2021
- ESP32-CAM pinout and equipments. ESP-EYE, AI Thinker, TTGO T-Camera, M5Stack Timer Camera … 27 January 2021
- ESP32-CAM. Which model to choose? ESP-EYE, AI Thinker, TTGO T-Camera, M5Stack Timer Camera … 26 January 2021
- M5Stack Atomic GPS. ESP32 TinyGPS++ tracker, GPX export on SD card, visualization on Google Maps or VSCode 25 January 2021
- Home Assistant. Install the snap on Synology NAS on an Ubuntu Virtual Machine 21 January 2021
- Home Assistant. Essential plugins to install. Samba, HACS, File Editor, MQTT Mosquitto 20 January 2021
- Home Assistant Community Store (HACS), easily install themes and components 19 January 2021
- 5 tips for ESP32-CAM. Fixed IP address. AP mode. Image rotation 90°. Automatic recovery WiFi connection. HTML code storage 18 January 2021
- ESP32-CAM. Solder the external antenna to improve the range and stability of the video stream 7 January 2021
- ESP32-CAM. Migrate CameraWebServer project from Arduino IDE to PlatformIO 5 January 2021
- New TTGO T5-4.7 ESP32 with ePaper and LiPo or 18650 holder 3 December 2020
- Waveshare launches two Raspberry Pi 400 + 7″ or 13.3″ touchscreen bundles 27 November 2020
- New M5PAPER ESP32 from M5Stack. eInk IoT or home automation dashboard 27 November 2020
- T-Watch. Simplified code for shutdown and wake-up with BMA423 or AXP202 of the ESP32 27 November 2020
- How to store data on a micro SD card. Arduino code compatible ESP32, ESP8266
- Getting started Arduino. Receive commands from the serial port (ESP32 ESP8266 compatible)
- C++ functions print•println•printf•sprintf for Arduino ESP32 ESP8266. Combine•format → serial port
- C++ String functions. concat•c_srt•indexOf•replace•subString… for Arduino, ESP32, ESP8266
- How to assign a fixed IP to an ESP32 ESP8266 or ESP01 project
- ESP01. Get started with the Arduino or PlatformIO IDE. Which module to choose? Pinout
It is known long-lasting bug that Arduino IDE should be restarted to see the network devices which recently came online (it was present in 1.6.x and I guess in earlier versions as well).
It’s a pity it is still not fixed in 1.8.x 🙁