--- disqus: ahb0222 GA : G-VF9ZT413CG --- # ESP32FOTA_尤老師分享 > [color=#40f1ef][name=LHB阿好伯, 2021/07/16][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `Arduino` `Node-RED` [TOC] [分享簡報](https://docs.google.com/presentation/d/1yhlrVkVlItxobvH1SnPAMXuxkkppRkMrQB47kqDa2tI/edit#slide=id.p) ![](https://hackmd.io/_uploads/S12J8w3EY.png) [使用套件ESP32FOTA](https://github.com/chrisjoyce911/esp32FOTA) 使用範例 ![](https://hackmd.io/_uploads/Skvkqv3VK.png) :::success 需要有開放的網路伺服器放置bin檔 ::: ```c= /** esp32 firmware OTA Purpose: Perform an OTA update from a bin located on a webserver (HTTP Only) Setup: Step 1 : Set your WiFi (ssid & password) Step 2 : set esp32fota() Upload: Step 1 : Menu > Sketch > Export Compiled Library. The bin file will be saved in the sketch folder (Menu > Sketch > Show Sketch folder) Step 2 : Upload it to your webserver Step 3 : Update your firmware JSON file ( see firwmareupdate ) */ #include <esp32fota.h> #include <WiFi.h> // Change to your WiFi credentials const char *ssid = ""; const char *password = ""; // esp32fota esp32fota("<設備名稱>", <版本編號>); esp32FOTA esp32FOTA("esp32-fota-http", 1); void setup() { esp32FOTA.checkURL = "http://server/fota/fota.json"; Serial.begin(115200); setup_wifi(); } void setup_wifi() { delay(10); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println(WiFi.localIP()); } void loop() { bool updatedNeeded = esp32FOTA.execHTTPcheck(); if (updatedNeeded) { esp32FOTA.execOTA(); } delay(2000); } ``` 網路伺服器託管資料,包含有關最新固件的資訊 讓ESP32可以用於比對版本資訊 ```json= { "type": "esp32-fota-http", "version": 2, "host": "192.168.0.100", "port": 80, "bin": "/fota/esp32-fota-http-2.bin" } ``` :::danger 更新程式碼要包含OTA更新 ::: 快速匯出二進位檔 在上傳到伺服器讓ESP32去抓 ![](https://hackmd.io/_uploads/r1I2hvhEY.png) 時間戳記 ![](https://hackmd.io/_uploads/HJmKQ_nVt.png) 🌟全文可以至下方連結觀看或是補充 :::spoiler 節點紀錄 [ { "id": "e5358f0f7d428be5", "type": "tab", "label": "流程1", "disabled": false, "info": "" }, { "id": "14a5721863bcdc6f", "type": "mqtt in", "z": "e5358f0f7d428be5", "name": "", "topic": "Youjunjer/ESP32OTA/Base1/hb", "qos": "2", "datatype": "auto", "broker": "e97f8dc3b0a13a5f", "nl": false, "rap": true, "rh": 0, "x": 130, "y": 120, "wires": [ [ "cc2dd7911960f79a" ] ] }, { "id": "625246ddf8dc142c", "type": "mqtt in", "z": "e5358f0f7d428be5", "name": "", "topic": "youjunjer/esp32/Base2", "qos": "2", "datatype": "auto", "broker": "e97f8dc3b0a13a5f", "nl": false, "rap": true, "rh": 0, "x": 100, "y": 200, "wires": [ [ "cc2dd7911960f79a" ] ] }, { "id": "635277ea9e125f35", "type": "mqtt in", "z": "e5358f0f7d428be5", "name": "", "topic": "youjunjer/esp32/Base3", "qos": "2", "datatype": "auto", "broker": "e97f8dc3b0a13a5f", "nl": false, "rap": true, "rh": 0, "x": 100, "y": 300, "wires": [ [ "cc2dd7911960f79a" ] ] }, { "id": "cc2dd7911960f79a", "type": "function", "z": "e5358f0f7d428be5", "name": "補上時間", "func": "date = new Date();\nvar year = date.getFullYear();\nvar month = date.getMonth() +1;\nvar day = date.getDate();\nvar hour = date.getHours();\nvar mins = date.getMinutes();\nmsg.payload=year+'/' + month+'/' + day+' '+ hour+\n ':' + mins + ',' + msg.topic + ','+ msg.payload\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 340, "y": 200, "wires": [ [ "c86730a50d3826c3", "75af8d5672fc06bf", "dfee5e194eb50da2" ] ] }, { "id": "c86730a50d3826c3", "type": "switch", "z": "e5358f0f7d428be5", "name": "", "property": "payload", "propertyType": "msg", "rules": [ { "t": "cont", "v": "Base1", "vt": "str" } ], "checkall": "false", "repair": false, "outputs": 1, "x": 510, "y": 140, "wires": [ [ "d9c858f36229ad2a" ] ] }, { "id": "2eb575cdd7735e83", "type": "ui_text", "z": "e5358f0f7d428be5", "group": "369dc1aac59c819e", "order": 1, "width": 0, "height": 0, "name": "Base1", "label": "Update", "format": "{{msg.payload}}", "layout": "row-left", "x": 790, "y": 140, "wires": [] }, { "id": "75af8d5672fc06bf", "type": "switch", "z": "e5358f0f7d428be5", "name": "", "property": "payload", "propertyType": "msg", "rules": [ { "t": "cont", "v": "Base2", "vt": "str" } ], "checkall": "false", "repair": false, "outputs": 1, "x": 510, "y": 200, "wires": [ [ "b6d608698d688e02" ] ] }, { "id": "3d7731db69835fa0", "type": "ui_text", "z": "e5358f0f7d428be5", "group": "8f7de35d1d79ea29", "order": 1, "width": 0, "height": 0, "name": "Base2", "label": "Update", "format": "{{msg.payload}}", "layout": "row-left", "x": 790, "y": 200, "wires": [] }, { "id": "dfee5e194eb50da2", "type": "switch", "z": "e5358f0f7d428be5", "name": "", "property": "payload", "propertyType": "msg", "rules": [ { "t": "cont", "v": "Base3", "vt": "str" } ], "checkall": "false", "repair": false, "outputs": 1, "x": 510, "y": 260, "wires": [ [ "d861d8ab1ceae5cf" ] ] }, { "id": "75062d5daea34e0f", "type": "ui_text", "z": "e5358f0f7d428be5", "group": "b5a62f3255dc2a66", "order": 1, "width": 0, "height": 0, "name": "Base3", "label": "Update", "format": "{{msg.payload}}", "layout": "row-left", "x": 790, "y": 260, "wires": [] }, { "id": "d9c858f36229ad2a", "type": "change", "z": "e5358f0f7d428be5", "name": "刪除", "rules": [ { "t": "change", "p": "payload", "pt": "msg", "from": "Youjunjer/ESP32OTA/Base1/hb,", "fromt": "str", "to": "<br> Ver.", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 650, "y": 140, "wires": [ [ "2eb575cdd7735e83" ] ] }, { "id": "b6d608698d688e02", "type": "change", "z": "e5358f0f7d428be5", "name": "刪除", "rules": [ { "t": "change", "p": "payload", "pt": "msg", "from": ",youjunjer/esp32/Base2,", "fromt": "str", "to": "<br> Ver.", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 650, "y": 200, "wires": [ [ "3d7731db69835fa0" ] ] }, { "id": "d861d8ab1ceae5cf", "type": "change", "z": "e5358f0f7d428be5", "name": "刪除", "rules": [ { "t": "change", "p": "payload", "pt": "msg", "from": ",youjunjer/esp32/Base3,", "fromt": "str", "to": "<br> Ver.", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 650, "y": 260, "wires": [ [ "75062d5daea34e0f" ] ] }, { "id": "41db115a2e12c0f4", "type": "mqtt out", "z": "e5358f0f7d428be5", "name": "", "topic": "Youjunjer/ESP32OTA/Base1", "qos": "", "retain": "", "respTopic": "", "contentType": "", "userProps": "", "correl": "", "expiry": "", "broker": "e97f8dc3b0a13a5f", "x": 620, "y": 400, "wires": [] }, { "id": "c142373e5e1a646f", "type": "ui_form", "z": "e5358f0f7d428be5", "name": "", "label": "", "group": "369dc1aac59c819e", "order": 2, "width": 0, "height": 0, "options": [ { "label": "Host", "value": "Host", "type": "text", "required": true, "rows": null }, { "label": "BinUrl", "value": "BinUrl", "type": "text", "required": true, "rows": null } ], "formValue": { "Host": "", "BinUrl": "" }, "payload": "", "submit": "submit", "cancel": "cancel", "topic": "topic", "topicType": "flow", "splitLayout": false, "x": 150, "y": 440, "wires": [ [ "c7ea3afba1ee92fd" ] ] }, { "id": "c7ea3afba1ee92fd", "type": "function", "z": "e5358f0f7d428be5", "name": "", "func": "var host=msg.payload[\"Host\"];\nvar BinUrl=msg.payload[\"BinUrl\"];\nmsg.payload=host + \",\" + BinUrl;\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 370, "y": 420, "wires": [ [ "41db115a2e12c0f4" ] ] }, { "id": "b63b485e9cc9b67e", "type": "mqtt out", "z": "e5358f0f7d428be5", "name": "", "topic": "ChangeToYourTopic/ESP32OTA/Base2", "qos": "", "retain": "", "respTopic": "", "contentType": "", "userProps": "", "correl": "", "expiry": "", "broker": "e97f8dc3b0a13a5f", "x": 680, "y": 600, "wires": [] }, { "id": "50b0896b15464228", "type": "ui_form", "z": "e5358f0f7d428be5", "name": "", "label": "", "group": "8f7de35d1d79ea29", "order": 2, "width": 0, "height": 0, "options": [ { "label": "Host", "value": "Host", "type": "text", "required": true, "rows": null }, { "label": "BinUrl", "value": "BinUrl", "type": "text", "required": true, "rows": null } ], "formValue": { "Host": "", "BinUrl": "" }, "payload": "", "submit": "submit", "cancel": "cancel", "topic": "topic", "topicType": "flow", "splitLayout": false, "x": 170, "y": 640, "wires": [ [ "39d45f14988b082d" ] ] }, { "id": "98457a2e1de569bf", "type": "mqtt out", "z": "e5358f0f7d428be5", "name": "", "topic": "ChangeToYourTopic/ESP32OTA/Base3", "qos": "", "retain": "", "respTopic": "", "contentType": "", "userProps": "", "correl": "", "expiry": "", "broker": "e97f8dc3b0a13a5f", "x": 640, "y": 820, "wires": [] }, { "id": "b00f0cf66afc5ad6", "type": "ui_form", "z": "e5358f0f7d428be5", "name": "", "label": "", "group": "b5a62f3255dc2a66", "order": 2, "width": 0, "height": 0, "options": [ { "label": "Host", "value": "Host", "type": "text", "required": true, "rows": null }, { "label": "BinUrl", "value": "BinUrl", "type": "text", "required": true, "rows": null } ], "formValue": { "Host": "", "BinUrl": "" }, "payload": "", "submit": "submit", "cancel": "cancel", "topic": "topic", "topicType": "flow", "splitLayout": false, "x": 130, "y": 860, "wires": [ [ "33074cdafa917149" ] ] }, { "id": "39d45f14988b082d", "type": "function", "z": "e5358f0f7d428be5", "name": "", "func": "var host=msg.payload[\"Host\"];\nvar BinUrl=msg.payload[\"BinUrl\"];\nmsg.payload=host + \",\" + BinUrl;\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 390, "y": 620, "wires": [ [ "b63b485e9cc9b67e" ] ] }, { "id": "33074cdafa917149", "type": "function", "z": "e5358f0f7d428be5", "name": "", "func": "var host=msg.payload[\"Host\"];\nvar BinUrl=msg.payload[\"BinUrl\"];\nmsg.payload=host + \",\" + BinUrl;\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 350, "y": 840, "wires": [ [ "98457a2e1de569bf" ] ] }, { "id": "e97f8dc3b0a13a5f", "type": "mqtt-broker", "name": "mqtt.eclipseprojects.io", "broker": "mqtt.eclipseprojects.io", "port": "1883", "clientid": "", "usetls": false, "protocolVersion": "4", "keepalive": "60", "cleansession": true, "birthTopic": "", "birthQos": "0", "birthPayload": "", "birthMsg": {}, "closeTopic": "", "closeQos": "0", "closePayload": "", "closeMsg": {}, "willTopic": "", "willQos": "0", "willPayload": "", "willMsg": {}, "sessionExpiry": "" }, { "id": "369dc1aac59c819e", "type": "ui_group", "name": "Base1", "tab": "ca3529efc31442c1", "order": 1, "disp": true, "width": "6", "collapse": false }, { "id": "8f7de35d1d79ea29", "type": "ui_group", "name": "Base2", "tab": "ca3529efc31442c1", "order": 2, "disp": true, "width": "6", "collapse": false }, { "id": "b5a62f3255dc2a66", "type": "ui_group", "name": "Base3", "tab": "ca3529efc31442c1", "order": 3, "disp": true, "width": "6", "collapse": false }, { "id": "ca3529efc31442c1", "type": "ui_tab", "name": "ESP32", "icon": "dashboard", "order": 2, "disabled": false, "hidden": false } ] ::: 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/nHEcVmm.jpg)