当前位置:   article > 正文

ESP8266 OTA方法二 本地升级_esphttpupdate.update -100

esphttpupdate.update -100

继上篇介绍利用巴法云来实现ota,有朋友表示服务器不稳,故搜索本地实现过程,在网上找到这篇介绍,原本是esp32,现修改为esp8266.

1、本地电脑执行py,将bin放到同级目录

  1. from http.server import BaseHTTPRequestHandler, HTTPServer
  2. import os
  3. class MyHttpRequestHandler(BaseHTTPRequestHandler):
  4. def __init__(self, request, client_address, server, folderPath):
  5. self.folderPath = folderPath
  6. self.UpDateFileName = self.getFileExtensionPath(
  7. self.folderPath, '.bin')
  8. self.UpDateFilePath = self.folderPath + "\\" + \
  9. self.getFileExtensionPath(self.folderPath, '.bin')
  10. super().__init__(request, client_address, server)
  11. @classmethod
  12. def Creator(cls, *args, **kwargs):
  13. def _HandlerCreator(request, client_address, server):
  14. cls(request, client_address, server, *args, **kwargs)
  15. return _HandlerCreator
  16. def do_GET(self):
  17. # 处理逻辑
  18. path = str(self.path)
  19. if path == "/getFile":
  20. self.send_response(200)
  21. self.send_header(
  22. "Content-type", "text/plain")
  23. self.end_headers()
  24. self.wfile.write(bytes(self.UpDateFileName, "utf-8"))
  25. elif path == "/update":
  26. with open(self.UpDateFilePath, 'rb') as resultf:
  27. buf = resultf.read()
  28. length = resultf.tell()
  29. self.send_response(200) # 文件存在返回状态码
  30. # 返回请求格式为"application/octet-stream"
  31. self.send_header("Content-type", "application/octet-stream")
  32. self.send_header("Content-Length", str(length))
  33. self.send_header("Content-Disposition",
  34. "attachment; filename=\"%s\"" % self.UpDateFileName)
  35. self.end_headers()
  36. # 读取文件发送给客户端
  37. self.wfile.write(buf)
  38. def do_POST(self):
  39. pass
  40. def getFileExtensionPath(self, path, name):
  41. for root, dirs, files in os.walk(path): # 遍历该文件夹
  42. for file in files: # 遍历刚获得的文件名files
  43. (filename, extension) = os.path.splitext(file) # 将文件名拆分为文件名与后缀
  44. if (extension == name):
  45. return file
  46. if __name__ == '__main__':
  47. ts = HTTPServer(('0.0.0.0', 8989),
  48. MyHttpRequestHandler.Creator(os.getcwd()))
  49. ts.serve_forever()

2、arduino加入如下代码

  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266httpUpdate.h>
  4. #include <Preferences.h>
  5. // #include <HTTPUpdate.h>
  6. #include "ESPAsyncWebServer.h"
  7. #include <ESP8266HTTPClient.h>
  8. #include <WiFiClient.h>
  9. String getUpDateName; // 获取的升级包名
  10. String upDateName; // 上次的升级包名
  11. bool UpdateVersionIsOk; // 包名判断可以升级的标志位
  12. String getUpdateUrl = "http://192.168.31.69:8989/getFile"; // 获取升级包名的URL
  13. String downLoadUpdateUrl = "http://192.168.31.69:8989/update"; // 下载升级包的URL
  14. // OTA升级开始回调函数
  15. void update_started()
  16. {
  17. Serial.println("CALLBACK: HTTP update process started");
  18. }
  19. // OTA升级完成回调函数
  20. void update_finished()
  21. {
  22. Serial.println("CALLBACK: HTTP update process finished");
  23. Preferences prefs; // NVS读写实例化
  24. prefs.begin("WiFiConfig");
  25. prefs.putString("Version", getUpDateName);
  26. prefs.end();
  27. upDateName = getUpDateName;
  28. UpdateVersionIsOk = false;
  29. Serial.println("升级记录已保存");
  30. }
  31. // 当升级开始时,打印日志
  32. void update_started()
  33. {
  34. Serial.println("CALLBACK: HTTP update process started");
  35. }
  36. // 当升级结束时,打印日志
  37. void update_finished()
  38. {
  39. Serial.println("CALLBACK: HTTP update process finished");
  40. // u8g2.clearBuffer();
  41. // u8g2.drawStr(u8g2.getWidth() / 2, u8g2.getHeight() / 2, "Restart");
  42. // u8g2.sendBuffer();
  43. }
  44. // 当升级中,打印日志
  45. void update_progress(int cur, int total)
  46. {
  47. Serial.printf("CALLBACK: HTTP update process at %d of %d bytes...%d%% \n", cur, total, cur / (total / 100));
  48. //ota_OLED_onProgress(4, 32, 120, 8, cur / (total / 100));
  49. }
  50. // 当升级失败时,打印日志
  51. void update_error(int err)
  52. {
  53. Serial.printf("CALLBACK: HTTP update fatal error code %d\n", err);
  54. }
  55. // OTA升级检查线程
  56. void seachUpdate()
  57. {
  58. while (true)
  59. {
  60. if (WiFi.status() == WL_CONNECTED)
  61. {
  62. WiFiClient client;
  63. HTTPClient http;
  64. http.begin(client, getUpdateUrl);
  65. int httpCode = http.GET();
  66. delay(500);
  67. if (httpCode > 0)
  68. {
  69. if (httpCode == HTTP_CODE_OK)
  70. {
  71. getUpDateName = http.getString();
  72. Serial.println(getUpDateName);
  73. // 判断get的字符串和nvs存储的升级包名不一致
  74. if (getUpDateName != "" && getUpDateName != upDateName)
  75. {
  76. UpdateVersionIsOk = true;
  77. }
  78. else
  79. {
  80. UpdateVersionIsOk = false;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. Serial.println("HTTP GET... failed, error: " + http.errorToString(httpCode));
  87. }
  88. http.end();
  89. WiFiClient UpdateClient;
  90. ESPhttpUpdate.onStart(update_started); // 当升级开始时
  91. ESPhttpUpdate.onEnd(update_finished); // 当升级结束时
  92. ESPhttpUpdate.onProgress(update_progress); // 当升级中
  93. ESPhttpUpdate.onError(update_error); // 当升级失败时
  94. if (UpdateVersionIsOk)
  95. {
  96. ESPhttpUpdate.update(UpDateClient, downLoadUpdateUrl);
  97. }
  98. }
  99. delay(5000);
  100. }
  101. }
  102. void setup()
  103. {
  104. Preferences prefs; // NVS读写实例化
  105. prefs.begin("WiFiConfig");
  106. if (prefs.isKey("Version"))
  107. {
  108. upDateName = prefs.getString("Version");
  109. }
  110. // xTaskCreatePinnedToCore(seachUpdate, "seachUpdate", 10000, NULL, 1, NULL, 0);
  111. }
  112. void loop()
  113. {
  114. delay(10);
  115. }

以上作为记录

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

闽ICP备14008679号