ArduinoOTA
ArduinoOTA
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.
1 http://arduino.esp8266.com/stable/package_esp8266com_index.json
Then go to the map manager and look for esp8266, then click install.
After installation, new examples have been added to the sample menu. Select your
ESP8266 card 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.
1 #include <ESP8266WiFi.h>
2 #include <ESP8266mDNS.h>
3 #include <WiFiUdp.h>
4 #include <ArduinoOTA.h>
5
6 const char* ssid = "..........";
7 const char* password = "..........";
8
9 void setup() {
10 Serial.begin(115200);
11 Serial.println("Booting");
12 WiFi.mode(WIFI_STA);
13 WiFi.begin(ssid, password);
14 while (WiFi.waitForConnectResult() != WL_CONNECTED) {
15 Serial.println("Connection Failed! Rebooting...");
16 delay(5000);
17 ESP.restart();
18 }
19
20 // Port defaults to 8266
21 // ArduinoOTA.setPort(8266);
22
23 // Hostname defaults to esp8266-[ChipID]
24 ArduinoOTA.setHostname("Demo OTA ESP8266");
25
26 // No authentication by default
27 ArduinoOTA.setPassword((const char *)"123");
28
29 ArduinoOTA.onStart([]() {
30 Serial.println("Start");
31 });
32 ArduinoOTA.onEnd([]() {
33 Serial.println("\nEnd");
34 });
35 ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
36 Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
37 });
38 ArduinoOTA.onError([](ota_error_t error) {
39 Serial.printf("Error[%u]: ", error);
40 if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
41 else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
42 else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
43 else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
44 else if (error == OTA_END_ERROR) Serial.println("End Failed");
45 });
46 ArduinoOTA.begin();
47 Serial.println("Ready");
48 Serial.print("IP address: ");
49 Serial.println(WiFi.localIP());
50 }
51
52 void loop() {
53 ArduinoOTA.handle();
54 }
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:
At the end of the setup, once all functions have been defined, start the wireless update
support by calling the begin () method.
It seems that version 1.8.x includes a bug that prevents the devices from updating. If your
ESP does not appear a few seconds after starting the ESP, just restart the Arduino IDE.
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