当前位置:   article > 正文

[ESP32]基于PIO平台的OTA升级教学_esp32 arduino ota

esp32 arduino ota

名词解释:

OTA是Over-the-Air 的简称,称为空中下载技术。利用wifi、ble或某种通讯协议完成非接触的动态下载。

环境搭建需求:

  1. 基于vscode的pio平台
  2. 已安装ArduinoOTA库的ESP32工程
  3. 安装或是更新pio插件

OTA下载:

        在Libraries中搜索ArduinoOTA库并安装

        安装后运行例程(代码放在文末)。

        注意:请更新红框处的wifi账号与密码。

        向ESP32烧录程序后,请将主板放置于能够连接wifi处,打开串口监视器,并按下主板上的en键。在红框处获取ESP32的地址,使用同一网络下的设备访问该地址。

        输入地址后,将访问如下的网址,默认账号密码都为admin

        输入host的内容,也可以完成访问。

        例:http://esp32

        输入账号密码后,将访问如下网址。选择需要烧录的bin文件后,点击update即可完成OTA下载。

生成BIN文件方式:

        打开终端,输入下列指令即可生成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文件,请完成一次编译后在运行上述指令。

代码:

  1. /*
  2. Rui Santos
  3. Complete project details
  4. - Arduino IDE: https://RandomNerdTutorials.com/esp8266-nodemcu-ota-over-the-air-arduino/
  5. - VS Code: https://RandomNerdTutorials.com/esp8266-nodemcu-ota-over-the-air-vs-code/
  6. This sketch shows a Basic example from the AsyncElegantOTA library: ESP8266_Async_Demo
  7. https://github.com/ayushsharma82/AsyncElegantOTA
  8. */
  9. #include <Arduino.h>
  10. #include <WiFi.h>
  11. #include <WiFiClient.h>
  12. #include <WebServer.h>
  13. #include <ESPmDNS.h>
  14. #include <Update.h>
  15. const char* host = "esp32";
  16. const char* ssid = "Avatar-4parts";
  17. const char* password = "12345678";
  18. //variabls to blink without delay:
  19. const int led = 2;
  20. WebServer server(80);
  21. /*
  22. * Login page
  23. */
  24. const char* loginIndex =
  25. "<form name='loginForm'>"
  26. "<table width='20%' bgcolor='A09F9F' align='center'>"
  27. "<tr>"
  28. "<td colspan=2>"
  29. "<center><font size=4><b>ESP32 Login Page</b></font></center>"
  30. "<br>"
  31. "</td>"
  32. "<br>"
  33. "<br>"
  34. "</tr>"
  35. "<td>Username:</td>"
  36. "<td><input type='text' size=25 name='userid'><br></td>"
  37. "</tr>"
  38. "<br>"
  39. "<br>"
  40. "<tr>"
  41. "<td>Password:</td>"
  42. "<td><input type='Password' size=25 name='pwd'><br></td>"
  43. "<br>"
  44. "<br>"
  45. "</tr>"
  46. "<tr>"
  47. "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
  48. "</tr>"
  49. "</table>"
  50. "</form>"
  51. "<script>"
  52. "function check(form)"
  53. "{"
  54. "if(form.userid.value=='admin' && form.pwd.value=='admin')"
  55. "{"
  56. "window.open('/serverIndex')"
  57. "}"
  58. "else"
  59. "{"
  60. " alert('Error Password or Username')/*displays error message*/"
  61. "}"
  62. "}"
  63. "</script>";
  64. /*
  65. * Server Index Page
  66. */
  67. const char* serverIndex =
  68. "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
  69. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
  70. "<input type='file' name='update'>"
  71. "<input type='submit' value='Update'>"
  72. "</form>"
  73. "<div id='prg'>progress: 0%</div>"
  74. "<script>"
  75. "$('form').submit(function(e){"
  76. "e.preventDefault();"
  77. "var form = $('#upload_form')[0];"
  78. "var data = new FormData(form);"
  79. " $.ajax({"
  80. "url: '/update',"
  81. "type: 'POST',"
  82. "data: data,"
  83. "contentType: false,"
  84. "processData:false,"
  85. "xhr: function() {"
  86. "var xhr = new window.XMLHttpRequest();"
  87. "xhr.upload.addEventListener('progress', function(evt) {"
  88. "if (evt.lengthComputable) {"
  89. "var per = evt.loaded / evt.total;"
  90. "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
  91. "}"
  92. "}, false);"
  93. "return xhr;"
  94. "},"
  95. "success:function(d, s) {"
  96. "console.log('success!')"
  97. "},"
  98. "error: function (a, b, c) {"
  99. "}"
  100. "});"
  101. "});"
  102. "</script>";
  103. /*
  104. * setup function
  105. */
  106. void setup(void) {
  107. pinMode(led, OUTPUT);
  108. Serial.begin(115200);
  109. // Connect to WiFi network
  110. WiFi.begin(ssid, password);
  111. Serial.println("");
  112. // Wait for connection
  113. while (WiFi.status() != WL_CONNECTED) {
  114. delay(500);
  115. Serial.print(".");
  116. }
  117. Serial.println("");
  118. Serial.print("Connected to ");
  119. Serial.println(ssid);
  120. Serial.print("IP address: ");
  121. Serial.println(WiFi.localIP());
  122. /*use mdns for host name resolution*/
  123. if (!MDNS.begin(host)) { //http://esp32.local
  124. Serial.println("Error setting up MDNS responder!");
  125. while (1) {
  126. delay(1000);
  127. }
  128. }
  129. Serial.println("mDNS responder started");
  130. /*return index page which is stored in serverIndex */
  131. server.on("/", HTTP_GET, []() {
  132. server.sendHeader("Connection", "close");
  133. server.send(200, "text/html", loginIndex);
  134. });
  135. server.on("/serverIndex", HTTP_GET, []() {
  136. server.sendHeader("Connection", "close");
  137. server.send(200, "text/html", serverIndex);
  138. });
  139. /*handling uploading firmware file */
  140. server.on("/update", HTTP_POST, []() {
  141. server.sendHeader("Connection", "close");
  142. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  143. ESP.restart();
  144. }, []() {
  145. HTTPUpload& upload = server.upload();
  146. if (upload.status == UPLOAD_FILE_START) {
  147. Serial.printf("Update: %s\n", upload.filename.c_str());
  148. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  149. Update.printError(Serial);
  150. }
  151. } else if (upload.status == UPLOAD_FILE_WRITE) {
  152. /* flashing firmware to ESP*/
  153. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  154. Update.printError(Serial);
  155. }
  156. } else if (upload.status == UPLOAD_FILE_END) {
  157. if (Update.end(true)) { //true to set the size to the current progress
  158. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  159. } else {
  160. Update.printError(Serial);
  161. }
  162. }
  163. });
  164. server.begin();
  165. pinMode(14,OUTPUT);
  166. }
  167. void loop(void) {
  168. server.handleClient();
  169. digitalWrite(14,LOW);
  170. delay(600);
  171. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/600725
推荐阅读
相关标签
  

闽ICP备14008679号