platformio使用OTA升级esp8266固件

​ 所谓OTA,就是Over-The-Air的缩写。有人将其翻译为“空中下载”,也有翻译为“隔空传输”。无论如何翻译,对于ESP2866来说,通过OTA我们无需将ESP8266与电脑连接,而仅仅通过WiFi就可以用向ESP8266上传程序。当然,可以通过使用Arduino IDE,但是我更喜欢使用platformio,使用如下:

一、使用自带的OTA升级固件
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

const int ESP_BUILTIN_LED = D0;

void connectWifi(){
  //开始连接wifi
  WiFi.begin(ssid, password);
 
  //等待WiFi连接,连接成功打印IP
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected!");  
  Serial.print("IP address:\t");            
  Serial.println(WiFi.localIP());
  pinMode(ESP_BUILTIN_LED, OUTPUT);          
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);       
  Serial.println("");
  connectWifi();

  // OTA设置并启动
  ArduinoOTA.setHostname("ESP8266");
  ArduinoOTA.begin();
  Serial.println("OTA ready");
}

void loop() {
  // put your main code here, to run repeatedly:
  ArduinoOTA.handle();
  
  //Todo

}

​ 将这个程序中的wifi名称和密码替换即可,然后将你要的事情,添加到Todo中即可。按照这个模板esp8266即可满足ota更新固件。

​ platformio配置文件platformio.ini如下:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
upload_protocol = espota
upload_port = 192.168.1.152

​ 其中upload_protocol是固定的,upload_port请替换成esp8266的本地ip地址即可。platformio便可以通过wifi来升级固件了。

二、使用AsyncElegantOTA 来升级固件

​ 上面的方法有个弊端,就是升级太复杂,需要通过platformio或者Arduino IDE来升级固件。这个 库AsyncElegantOTA ,通过在esp8266上启动一个webserver来上传bin文件,从而起到升级固件的目的。

​ 首先,先安装AsyncElegantOTA 库,同时还需要ESPAsyncWebServer和ESPAsyncTCP这两个库。

​ 代码如下:

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

AsyncWebServer server(80);


void setup(void) {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(200, "text/plain", "Hi! I am ESP8266.");
  });

  AsyncElegantOTA.begin(&server);    // Start ElegantOTA
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  AsyncElegantOTA.loop();

  //Todo
}

​ 将这个程序中的wifi名称和密码替换即可,然后将你要的事情,添加到Todo中即可。按照这个模板esp8266即可满足ota更新固件。

​ platformio配置文件platformio.ini如下:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
; upload_protocol = espota
; upload_port = 192.168.1.152
lib_deps = 
	ayushsharma82/AsyncElegantOTA@^2.2.6
	me-no-dev/ESPAsyncTCP@^1.2.2
	me-no-dev/ESP Async WebServer@^1.2.3

在浏览器输入esp8266的本地地址+/update,即可到达更新固件界面:(选择bin文件上传即可)

image-20210724135148481

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ESP8266是一款成本低廉且强大的Wi-Fi模块,可以被广泛应用于物联网和嵌入式系统中。ESP8266固件大全指的是ESP8266模块所支持的各种固件版本和开源软件。这些固件包含了不同的功能和特性,可以根据需要选择合适的固件来进行开发和定制。 其中最常见的固件是官方固件,即由ESP8266制造商提供的固件。官方固件具有较高的稳定性和兼容性,可以直接使用ESP8266的基本功能,如Wi-Fi连接、TCP/IP协议栈等。官方固件一般提供了开发工具链和示例代码,方便开发者进行开发和调试。 除了官方固件外,还有很多第三方固件可供选择。其中较为知名的有NodeMCU、Arduino以及MicroPython。这些固件在官方固件的基础上进行了进一步的封装和增强,提供了更加简洁和易用的开发接口。例如,NodeMCU和Arduino固件提供了Lua和Arduino语言的支持,方便开发者进行快速的原型开发和项目实现。而MicroPython固件则提供了Python语言的支持,适合Python开发者。 ESP8266固件大全还包括了一些专用的固件,如OTA(空中固件升级固件和MQTT(消息队列遥测传输)固件OTA固件支持通过Wi-Fi网络进行远程固件升级,无需物理接触设备。MQTT固件则基于MQTT通信协议,实现了实时的遥测数据传输,适合物联网应用中的数据传输和通信。 综上所述,ESP8266固件大全提供了丰富的选择,可以满足不同开发者的需求。通过选择合适的固件,开发者可以轻松地进行ESP8266的开发和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值