{"id":7756,"date":"2023-04-14T09:54:43","date_gmt":"2023-04-14T01:54:43","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=7756"},"modified":"2023-04-14T09:54:43","modified_gmt":"2023-04-14T01:54:43","slug":"esp8266-ota","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=7756","title":{"rendered":"ESP8266 - OTA"},"content":{"rendered":"<p>--<\/p>\n<h2>\u53c3\u8003\u8cc7\u6e90<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.itread01.com\/content\/1560928323.html\" target=\"_blank\" rel=\"noopener\">ESP8266\u958b\u767c\u4e4b\u65c5 \u7db2\u8def\u7bc7\u246f \u7121\u7dda\u66f4\u65b0\u2014\u2014OTA\u97cc\u9ad4\u66f4\u65b0<\/a>\u00a0(\u539f\u7248\u7684\u8981\u9322\u624d\u80fd\u770b\uff0c\u6975\u5c11\u6578\u5217\u8209\u5167\u5bb9\u8fb2\u5834\u7684\u8cc7\u6599)<\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/weixin_34376986\/article\/details\/89659688\" target=\"_blank\" rel=\"noopener\">ESP8266 OTA\u4e4b\u670d\u52a1\u5668\u66f4\u65b0<\/a><\/li>\n<li><a href=\"https:\/\/www.est.idv.tw\/esp8266-ota%E7%B7%9A%E4%B8%8A%E6%9B%B4%E6%96%B0\/\" target=\"_blank\" rel=\"noopener\">ESP8266 OTA\u7dda\u4e0a\u66f4\u65b0<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:arduino decode:true \">#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;ESP8266mDNS.h&gt;\r\n#include &lt;WiFiUdp.h&gt;\r\n#include &lt;ArduinoOTA.h&gt;\r\n\r\n\/\/\u9664\u932f\u5b9a\u7fa9\r\n#define DebugBegin(baud_rate)    Serial.begin(baud_rate)\r\n#define DebugPrintln(message)    Serial.println(message)\r\n#define DebugPrint(message)    Serial.print(message)\r\n#define DebugPrintF(...) Serial.printf( __VA_ARGS__ )\r\n\r\n#define CodeVersion \"CodeVersion V1.1\"\r\n\r\nconst char* ssid = \"ssid\";\r\nconst char* password = \"password\";\r\n\r\nvoid setup() {\r\n  DebugBegin(115200);\r\n  DebugPrintln(\"Booting Sketch....\");\r\n  DebugPrintln(CodeVersion);\r\n  WiFi.mode(WIFI_STA);\r\n  WiFi.begin(ssid, password);\r\n  while (WiFi.waitForConnectResult() != WL_CONNECTED) {\r\n    DebugPrintln(\"Connection Failed! Rebooting...\");\r\n    delay(5000);\r\n  \/\/\u91cd\u555fESP8266\u6a21\u7d44\r\n    ESP.restart();\r\n  }\r\n\r\n  \/\/ Port defaults to 8266\r\n  \/\/ ArduinoOTA.setPort(8266);\r\n\r\n  \/\/ Hostname defaults to esp8266-[ChipID]\r\n  \/\/ ArduinoOTA.setHostname(\"myesp8266\");\r\n\r\n  \/\/ No authentication by default\r\n  \/\/ ArduinoOTA.setPassword(\"admin\");\r\n\r\n  \/\/ Password can be set with it's md5 value as well\r\n  \/\/ MD5(admin) = 21232f297a57a5a743894a0e4a801fc3\r\n  \/\/ ArduinoOTA.setPasswordHash(\"21232f297a57a5a743894a0e4a801fc3\");\r\n\r\n  ArduinoOTA.onStart([]() {\r\n    String type;\r\n  \/\/\u5224\u65b7\u4e00\u4e0bOTA\u5167\u5bb9\r\n    if (ArduinoOTA.getCommand() == U_FLASH) {\r\n      type = \"sketch\";\r\n    } else { \/\/ U_SPIFFS\r\n      type = \"filesystem\";\r\n    }\r\n\r\n    \/\/ NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()\r\n    DebugPrintln(\"Start updating \" + type);\r\n  });\r\n  ArduinoOTA.onEnd([]() {\r\n    DebugPrintln(\"\\nEnd\");\r\n  });\r\n  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {\r\n    DebugPrintF(\"Progress: %u%%\\r\", (progress \/ (total \/ 100)));\r\n  });\r\n  ArduinoOTA.onError([](ota_error_t error) {\r\n    DebugPrintF(\"Error[%u]: \", error);\r\n    if (error == OTA_AUTH_ERROR) {\r\n      DebugPrintln(\"Auth Failed\");\r\n    } else if (error == OTA_BEGIN_ERROR) {\r\n      DebugPrintln(\"Begin Failed\");\r\n    } else if (error == OTA_CONNECT_ERROR) {\r\n      DebugPrintln(\"Connect Failed\");\r\n    } else if (error == OTA_RECEIVE_ERROR) {\r\n      DebugPrintln(\"Receive Failed\");\r\n    } else if (error == OTA_END_ERROR) {\r\n      DebugPrintln(\"End Failed\");\r\n    }\r\n  });\r\n  ArduinoOTA.begin();\r\n  DebugPrintln(\"Ready\");\r\n  DebugPrint(\"IP address: \");\r\n  DebugPrintln(WiFi.localIP());\r\n}\r\n\r\nvoid loop() {\r\n  ArduinoOTA.handle();\r\n}<\/pre>\n<p>\u4e0a\u50b3\u5b8c\u6210\u4e0d\u6703\u6709\u53e6\u5916\u7684\u8a0a\u606f\u901a\u77e5\uff0cOTA \u7121\u6cd5\u958b\u555f\u5e8f\u5217\u57e0\u76e3\u63a7\uff0c\u56e0\u6b64\u53ea\u80fd\u81ea\u5df1\u6191\u7d93\u9a57\u5224\u65b7\u662f\u5426\u5df2\u7d93\u4e0a\u50b3\u5b8c\u6210<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-7763\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148-300x163.png\" alt=\"\" width=\"300\" height=\"163\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148-300x163.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148-768x416.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148-1024x555.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148-500x271.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2020\/12\/20201209_084148.png 1920w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<h2>Server PHP<\/h2>\n<p>&nbsp;<\/p>\n<pre class=\"lang:php decode:true\">&lt;?PHP\r\n$content = file_get_contents('php:\/\/input');\r\nfile_put_contents( '\/opt\/Server\/OTA.log', $content .\"\\n\", FILE_APPEND );\r\n\r\nheader('Content-type: text\/plain; charset=utf8', true);\r\n\r\nfunction check_header($name, $value = false) {\r\n    if(!isset($_SERVER[$name])) {\r\n        return false;\r\n    }\r\n    if($value &amp;&amp; $_SERVER[$name] != $value) {\r\n        return false;\r\n    }\r\n    return true;\r\n}\r\n\r\nfunction sendFile($path) {\r\n    header($_SERVER[\"SERVER_PROTOCOL\"].' 200 OK', true, 200);\r\n    header('Content-Type: application\/octet-stream', true);\r\n    header('Content-Disposition: attachment; filename='.basename($path));\r\n    header('Content-Length: '.filesize($path), true);\r\n    header('x-MD5: '.md5_file($path), true);\r\n    readfile($path);\r\n}\r\n\r\nif(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {\r\n    header($_SERVER[\"SERVER_PROTOCOL\"].' 403 Forbidden', true, 403);\r\n    echo \"only for ESP8266 updater!\\n\";\r\n    exit();\r\n}\r\n\r\nif(!check_header('HTTP_X_ESP8266_STA_MAC') ||\r\n    !check_header('HTTP_X_ESP8266_AP_MAC') ||\r\n    !check_header('HTTP_X_ESP8266_FREE_SPACE') ||\r\n    !check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||\r\n    !check_header('HTTP_X_ESP8266_SKETCH_MD5') ||\r\n    !check_header('HTTP_X_ESP8266_CHIP_SIZE') ||\r\n    !check_header('HTTP_X_ESP8266_SDK_VERSION')) {\r\n    header($_SERVER[\"SERVER_PROTOCOL\"].' 403 Forbidden', true, 403);\r\n    echo \"only for ESP8266 updater! (header)\\n\";\r\n    exit();\r\n}\r\n\r\n$db = array(\r\n    \"2C:F4:32:71:66:6A\" =&gt; \"1.2\"\r\n);\r\n\r\nif(!isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {\r\n    header($_SERVER[\"SERVER_PROTOCOL\"].' 500 ESP MAC not configured for updates', true, 500);\r\n}\r\n\r\n$localBinary = \".\/\".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']].\".bin\";\r\n\r\n\r\nif((!check_header('HTTP_X_ESP8266_SDK_VERSION') &amp;&amp; $db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) || $_SERVER[\"HTTP_X_ESP8266_SKETCH_MD5\"] !=\r\n    md5_file($localBinary)) {\r\n    sendFile($localBinary);\r\n} else {\r\n    header($_SERVER[\"SERVER_PROTOCOL\"].' 304 Not Modified', true, 304);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<h2>\u7de8\u8b6f\u5b8c\u6210\u7684 bin \u6a94\u5728\u54ea\u88e1\uff1f<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.kanda.com\/blog\/microcontrollers\/avr-microcontrollers\/find-arduino-hex-files-output-binaries\/\" target=\"_blank\" rel=\"noopener\">WHERE TO FIND ARDUINO HEX FILES OR OUTPUT BINARIES<\/a><\/li>\n<\/ul>\n<p>\u9810\u8a2d\u8def\u5f91\u61c9\u8a72\u5728\u4ee5\u4e0b\u985e\u4f3c\u9577\u76f8\u7684\u8def\u5f91<\/p>\n<pre class=\"lang:default decode:true\">C:\\Users\\User\\AppData\\Local\\Temp\\arduino_build_xxxxx<\/pre>\n<p>1.8.13 \u4fee\u6539\u8def\u5f91\u7121\u6548<\/p>\n<p>--<\/p>\n<h2>[ERROR]: No response from device<\/h2>\n<ul>\n<li><a href=\"https:\/\/forum.arduino.cc\/index.php?topic=601681.msg4731242#msg4731242\" target=\"_blank\" rel=\"nofollow noopener\">Re: OTA: No response from device<\/a><\/li>\n<\/ul>\n<p>\u95dc\u9589\u96fb\u8166\u7684\u9632\u706b\u7246\u518d\u8a66\u8a66<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7756\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,305&nbsp;total views, &nbsp;3&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u53c3\u8003\u8cc7\u6e90 ESP8266...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"7756\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,305&nbsp;total views, &nbsp;3&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[272],"tags":[275,331],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7756"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7756"}],"version-history":[{"count":3,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7756\/revisions"}],"predecessor-version":[{"id":7767,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/7756\/revisions\/7767"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}