--
參考資源
- ESP8266開發之旅 網路篇⑯ 無線更新——OTA韌體更新 (原版的要錢才能看,極少數列舉內容農場的資料)
- ESP8266 OTA之服务器更新
- ESP8266 OTA線上更新
--
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <WiFiUdp.h> #include <ArduinoOTA.h> //除錯定義 #define DebugBegin(baud_rate) Serial.begin(baud_rate) #define DebugPrintln(message) Serial.println(message) #define DebugPrint(message) Serial.print(message) #define DebugPrintF(...) Serial.printf( __VA_ARGS__ ) #define CodeVersion "CodeVersion V1.1" const char* ssid = "ssid"; const char* password = "password"; void setup() { DebugBegin(115200); DebugPrintln("Booting Sketch...."); DebugPrintln(CodeVersion); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { DebugPrintln("Connection Failed! Rebooting..."); delay(5000); //重啟ESP8266模組 ESP.restart(); } // Port defaults to 8266 // ArduinoOTA.setPort(8266); // Hostname defaults to esp8266-[ChipID] // ArduinoOTA.setHostname("myesp8266"); // No authentication by default // ArduinoOTA.setPassword("admin"); // Password can be set with it's md5 value as well // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); ArduinoOTA.onStart([]() { String type; //判斷一下OTA內容 if (ArduinoOTA.getCommand() == U_FLASH) { type = "sketch"; } else { // U_SPIFFS type = "filesystem"; } // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() DebugPrintln("Start updating " + type); }); ArduinoOTA.onEnd([]() { DebugPrintln("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { DebugPrintF("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { DebugPrintF("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { DebugPrintln("Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { DebugPrintln("Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { DebugPrintln("Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { DebugPrintln("Receive Failed"); } else if (error == OTA_END_ERROR) { DebugPrintln("End Failed"); } }); ArduinoOTA.begin(); DebugPrintln("Ready"); DebugPrint("IP address: "); DebugPrintln(WiFi.localIP()); } void loop() { ArduinoOTA.handle(); } |
上傳完成不會有另外的訊息通知,OTA 無法開啟序列埠監控,因此只能自己憑經驗判斷是否已經上傳完成
--
Server PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?PHP $content = file_get_contents('php://input'); file_put_contents( '/opt/Server/OTA.log', $content ."\n", FILE_APPEND ); header('Content-type: text/plain; charset=utf8', true); function check_header($name, $value = false) { if(!isset($_SERVER[$name])) { return false; } if($value && $_SERVER[$name] != $value) { return false; } return true; } function sendFile($path) { header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200); header('Content-Type: application/octet-stream', true); header('Content-Disposition: attachment; filename='.basename($path)); header('Content-Length: '.filesize($path), true); header('x-MD5: '.md5_file($path), true); readfile($path); } if(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) { header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403); echo "only for ESP8266 updater!\n"; exit(); } if(!check_header('HTTP_X_ESP8266_STA_MAC') || !check_header('HTTP_X_ESP8266_AP_MAC') || !check_header('HTTP_X_ESP8266_FREE_SPACE') || !check_header('HTTP_X_ESP8266_SKETCH_SIZE') || !check_header('HTTP_X_ESP8266_SKETCH_MD5') || !check_header('HTTP_X_ESP8266_CHIP_SIZE') || !check_header('HTTP_X_ESP8266_SDK_VERSION')) { header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403); echo "only for ESP8266 updater! (header)\n"; exit(); } $db = array( "2C:F4:32:71:66:6A" => "1.2" ); if(!isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) { header($_SERVER["SERVER_PROTOCOL"].' 500 ESP MAC not configured for updates', true, 500); } $localBinary = "./".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']].".bin"; if((!check_header('HTTP_X_ESP8266_SDK_VERSION') && $db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) || $_SERVER["HTTP_X_ESP8266_SKETCH_MD5"] != md5_file($localBinary)) { sendFile($localBinary); } else { header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304); } |
--
編譯完成的 bin 檔在哪裡?
預設路徑應該在以下類似長相的路徑
1 |
C:\Users\User\AppData\Local\Temp\arduino_build_xxxxx |
1.8.13 修改路徑無效
--
[ERROR]: No response from device
關閉電腦的防火牆再試試
--
1,670 total views, 1 views today