ESP8266-Arduino网络编程实例-OTA升级固件(基于Arduino IDE)

OTA升级固件(基于Arduino IDE)

本文将演示如何在ArduinoIED中通过OTA方式升级固件。

OTA(Over the Air空中下载)更新是使用 WiFi 连接而不是串行通信将新固件加载到 ESP8266 模块的过程。 这种类型的功能在没有物理访问 ESP 模块的情况下非常有用。

从Arduino IDE中通过OTA方式下载固件适用如下典型场景:

  • 在固件开发期间 - 作为通过串行上传新固件更快替代方法
  • 用于更新网络中多个 ESP 的固件

1、硬件准备

  • ESP8266 NodeMCU开发板一块
  • 数据线一条

2、软件准备

  • Arduino IDE或VSCode + PlatformIO

在前面的文章中,对如何搭建ESP8266开发环境做了详细的介绍,请参考:

ESP8266 NodeMCU的引脚介绍在前面的文章中做了详细的介绍,请参考:

3、代码实现

第一步:通过OTA方式升级固件,首先需要将基础OTA固件通过串口下载到ESP8266开发板中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char\* ssid = "\*\*\*\*\*";
const char\* ssid_pwd = "\*\*\*\*\*";

void setup() {
Serial.begin(115200);
Serial.println("Booting");
// 设置WIFI STA工作模式
WiFi.mode(WIFI_STA);
// 连接WIFI
WiFi.begin(ssid, ssid_pwd);
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((const char \*)"123");

// OTA启动事件
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
// OTA启动完成
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
// OTA下载进度
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
// OTA错误回调
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");
});
// 调动OTA
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
// OTA事件处理
ArduinoOTA.handle();
}

固件下载完成后,ESP8266连接到WiFi之后,可以在Arduino IDE中的端口中看到OTA连接信息:

在这里插入图片描述

第二步:通过OTA下载新固件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

// Replace with your network credentials
const char\* ssid = "DESKTOP-4LOBMNU0960";
const char\* ssid_pwd = "iot123456";

const int ESP_BUILTIN_LED = 2;

void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, ssid_pwd);
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((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());
pinMode(ESP_BUILTIN_LED, OUTPUT);
}

void loop() {
ArduinoOTA.handle();
digitalWrite(ESP_BUILTIN_LED, LOW);
delay(1000);
digitalWrite(ESP_BUILTIN_LED, HIGH);
delay(1000);
}

下载结果:

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/127607633