在Vscode.platformIO平台通过OTA无线升级ESP8266程序

在Vscode.platformIO平台通过OTA无线升级ESP8266程序

  • 背景
  • 配置步骤
    • 1. 基本要求
    • 2. 程序中import 相应库文件
    • 3. 初始化OTA
    • 4. loop 中监控OTA
    • 5. 首次串口写入程序
    • 6. platformIO.ini 文件设置
    • 7. OTA升级方法1
    • 8. OTA升级方法2
    • 9. 其他事项
    • 10. OTA无线升级成功
  • 参考文献

背景

基于ESP8266的设备安装好后,如果要升级固件,一般需要将设备拆下后通过串口升级程序,如果设备在客户那里,升级将更加麻烦,本文介绍通过ESP8266的OTA功能(over the air)方式升级固件程序的基本设置方法;

配置步骤

1. 基本要求

1
    假设已经具备vscode, platformIO 通过arduino 方式写入ESP8266 的程序的条件

2. 程序中import 相应库文件

1
2
3
4
#include    <ESP8266WiFi.h>
#include    <ESP8266mDNS.h>
#include    <WiFiUdp.h>
#include    <ArduinoOTA.h>

3. 初始化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
void setup() {
// 1. wifimanager 配网
...
// 2. OTA 设置
  // 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());

4. loop 中监控OTA

1
2
3
4
5
void loop() {
    ...
    ArduinoOTA.handle();
    ...
}

5. 首次串口写入程序

1
首先 ESP8266模块必须 先串口写入程序一次,以后才可以OTA升级(串口写程序 的 ini配置如下)
1
2
3
4
5
6
7
8
    [env:esp12e]
    platform            = espressif8266 ; 公司产品,乐鑫
    board               = esp12e        ; 硬件 型号;
    framework           = arduino       ; 软件
    upload_protocol     = esptool       ; 上传方式 1: 串口上传;
    ; upload_protocol   = espota        ; 上传方式 2: OTA 上传;
    upload_speed        = 921600        ; 上传速度;
    monitor_speed       = 115200        ; 监控速度;

在这里插入图片描述

6. platformIO.ini 文件设置

在这里插入图片描述

7. OTA升级方法1

platformio run -t upload --upload-port 10.168.1.220
注: ip地址为模块的IP地址,回车

在这里插入图片描述

8. OTA升级方法2

在这里插入图片描述
注: ip地址为模块的IP地址
在这里插入图片描述

9. 其他事项

程序可仅升级spiffs 文件,也可2者都升级,
设置如下(注销此行,则2者升级)
在这里插入图片描述

10. OTA无线升级成功

在这里插入图片描述

参考文献

  1. OTA in ESP8266 arduino core
    https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html
  2. Espressif 8266 in platformio.org: https://docs.platformio.org/en/latest/platforms/espressif8266.html
  3. ESP8266 OTA之Arduino IDE更新 https://www.jianshu.com/p/90e9f8b23965?spm=a2c4e.11153940.blogcont634500.10.405768719hd7wj