赞
踩
OTA是Over-the-Air 的简称,称为空中下载技术。利用wifi、ble或某种通讯协议完成非接触的动态下载。
在Libraries中搜索ArduinoOTA库并安装。
安装后运行例程(代码放在文末)。
注意:请更新红框处的wifi账号与密码。
向ESP32烧录程序后,请将主板放置于能够连接wifi处,打开串口监视器,并按下主板上的en键。在红框处获取ESP32的地址,使用同一网络下的设备访问该地址。
输入地址后,将访问如下的网址,默认账号密码都为admin
输入host的内容,也可以完成访问。
例:http://esp32
输入账号密码后,将访问如下网址。选择需要烧录的bin文件后,点击update即可完成OTA下载。
打开终端,输入下列指令即可生成bin文件
platformio run -e nodemcu-32s
bin文件的默认存放路径为:.pio/build/nodemcu-32s(根据新建工程时,选择的board而改变,上方指令的最后一个参数也是如此)。
Q:如何查看board?
A:打开platformio.ini文件,可以直接查看board。
Q:如果运行上述指令后,提示“ 无法将“platformio”项识别为 cmdlet、函数、脚本文件或可运行程序的名称”,怎么办?
A:可能是没安装或没更新pio插件导致的,运行pip install -U platformio命令安装pio插件即可解决。
Q:为什么生成的bin文件与预计不同?
A:使用platformio run -e nodemcu-32s命令后,生成的bin文件内容为最近一次编译的程序内容。若需要生成当前程序的bin文件,请完成一次编译后在运行上述指令。
- /*
- Rui Santos
- Complete project details
- - Arduino IDE: https://RandomNerdTutorials.com/esp8266-nodemcu-ota-over-the-air-arduino/
- - VS Code: https://RandomNerdTutorials.com/esp8266-nodemcu-ota-over-the-air-vs-code/
-
- This sketch shows a Basic example from the AsyncElegantOTA library: ESP8266_Async_Demo
- https://github.com/ayushsharma82/AsyncElegantOTA
- */
-
- #include <Arduino.h>
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
- #include <Update.h>
- const char* host = "esp32";
- const char* ssid = "Avatar-4parts";
- const char* password = "12345678";
- //variabls to blink without delay:
- const int led = 2;
- WebServer server(80);
-
- /*
- * Login page
- */
- const char* loginIndex =
- "<form name='loginForm'>"
- "<table width='20%' bgcolor='A09F9F' align='center'>"
- "<tr>"
- "<td colspan=2>"
- "<center><font size=4><b>ESP32 Login Page</b></font></center>"
- "<br>"
- "</td>"
- "<br>"
- "<br>"
- "</tr>"
- "<td>Username:</td>"
- "<td><input type='text' size=25 name='userid'><br></td>"
- "</tr>"
- "<br>"
- "<br>"
- "<tr>"
- "<td>Password:</td>"
- "<td><input type='Password' size=25 name='pwd'><br></td>"
- "<br>"
- "<br>"
- "</tr>"
- "<tr>"
- "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
- "</tr>"
- "</table>"
- "</form>"
- "<script>"
- "function check(form)"
- "{"
- "if(form.userid.value=='admin' && form.pwd.value=='admin')"
- "{"
- "window.open('/serverIndex')"
- "}"
- "else"
- "{"
- " alert('Error Password or Username')/*displays error message*/"
- "}"
- "}"
- "</script>";
- /*
- * Server Index Page
- */
- const char* serverIndex =
- "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
- "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
- "<input type='file' name='update'>"
- "<input type='submit' value='Update'>"
- "</form>"
- "<div id='prg'>progress: 0%</div>"
- "<script>"
- "$('form').submit(function(e){"
- "e.preventDefault();"
- "var form = $('#upload_form')[0];"
- "var data = new FormData(form);"
- " $.ajax({"
- "url: '/update',"
- "type: 'POST',"
- "data: data,"
- "contentType: false,"
- "processData:false,"
- "xhr: function() {"
- "var xhr = new window.XMLHttpRequest();"
- "xhr.upload.addEventListener('progress', function(evt) {"
- "if (evt.lengthComputable) {"
- "var per = evt.loaded / evt.total;"
- "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
- "}"
- "}, false);"
- "return xhr;"
- "},"
- "success:function(d, s) {"
- "console.log('success!')"
- "},"
- "error: function (a, b, c) {"
- "}"
- "});"
- "});"
- "</script>";
- /*
- * setup function
- */
- void setup(void) {
- pinMode(led, OUTPUT);
- Serial.begin(115200);
- // Connect to WiFi network
- 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());
- /*use mdns for host name resolution*/
- if (!MDNS.begin(host)) { //http://esp32.local
- Serial.println("Error setting up MDNS responder!");
- while (1) {
- delay(1000);
- }
- }
- Serial.println("mDNS responder started");
- /*return index page which is stored in serverIndex */
- server.on("/", HTTP_GET, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/html", loginIndex);
- });
- server.on("/serverIndex", HTTP_GET, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/html", serverIndex);
- });
- /*handling uploading firmware file */
- server.on("/update", HTTP_POST, []() {
- server.sendHeader("Connection", "close");
- server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
- ESP.restart();
- }, []() {
- HTTPUpload& upload = server.upload();
- if (upload.status == UPLOAD_FILE_START) {
- Serial.printf("Update: %s\n", upload.filename.c_str());
- if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_WRITE) {
- /* flashing firmware to ESP*/
- if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
- Update.printError(Serial);
- }
- } else if (upload.status == UPLOAD_FILE_END) {
- if (Update.end(true)) { //true to set the size to the current progress
- Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
- } else {
- Update.printError(Serial);
- }
- }
- });
- server.begin();
- pinMode(14,OUTPUT);
- }
- void loop(void) {
- server.handleClient();
-
- digitalWrite(14,LOW);
- delay(600);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。