diff --git a/README.md b/README.md index 0fbc7ad653..38cfc86b5c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Documentation for latest development version: - [Reference](doc/reference.md) - [Supported boards](doc/boards.md) - [Change log](doc/changes.md) -- [OTA Update](doc/ota_updates.md) +- [OTA Update](doc/ota_updates/ota_updates.md) ### Issues and support ### diff --git a/boards.txt b/boards.txt index d5ca569be7..403194671c 100644 --- a/boards.txt +++ b/boards.txt @@ -301,6 +301,8 @@ nodemcuv2.menu.CpuFrequency.160.build.f_cpu=160000000L nodemcuv2.menu.UploadTool.esptool=Serial nodemcuv2.menu.UploadTool.esptool.upload.tool=esptool nodemcuv2.menu.UploadTool.esptool.upload.verbose=-vv +nodemcuv2.menu.UploadTool.espota=OTA +nodemcuv2.menu.UploadTool.espota.upload.tool=espota nodemcuv2.menu.UploadSpeed.115200=115200 nodemcuv2.menu.UploadSpeed.115200.upload.speed=115200 diff --git a/doc/ota_updates/ota-ide-module-joined-wifi.png b/doc/ota_updates/ota-ide-module-joined-wifi.png new file mode 100644 index 0000000000..34b93e3d20 Binary files /dev/null and b/doc/ota_updates/ota-ide-module-joined-wifi.png differ diff --git a/doc/ota_updates/ota-ide-ota-port-selection.png b/doc/ota_updates/ota-ide-ota-port-selection.png new file mode 100644 index 0000000000..18bbc72102 Binary files /dev/null and b/doc/ota_updates/ota-ide-ota-port-selection.png differ diff --git a/doc/ota_updates/ota-ide-ota-upload-complete.png b/doc/ota_updates/ota-ide-ota-upload-complete.png new file mode 100644 index 0000000000..76e24efb30 Binary files /dev/null and b/doc/ota_updates/ota-ide-ota-upload-complete.png differ diff --git a/doc/ota_updates/ota-ide-ota-upload-configuration.png b/doc/ota_updates/ota-ide-ota-upload-configuration.png new file mode 100644 index 0000000000..0b8cc4584e Binary files /dev/null and b/doc/ota_updates/ota-ide-ota-upload-configuration.png differ diff --git a/doc/ota_updates/ota-ide-python-configuration.png b/doc/ota_updates/ota-ide-python-configuration.png new file mode 100644 index 0000000000..0e78f72a12 Binary files /dev/null and b/doc/ota_updates/ota-ide-python-configuration.png differ diff --git a/doc/ota_updates/ota-ide-serial-upload-configuration.png b/doc/ota_updates/ota-ide-serial-upload-configuration.png new file mode 100644 index 0000000000..1116a0cad2 Binary files /dev/null and b/doc/ota_updates/ota-ide-serial-upload-configuration.png differ diff --git a/doc/ota_updates/ota-ide-sketch-selection.png b/doc/ota_updates/ota-ide-sketch-selection.png new file mode 100644 index 0000000000..1c9053c53b Binary files /dev/null and b/doc/ota_updates/ota-ide-sketch-selection.png differ diff --git a/doc/ota_updates/ota-ide-ssid-pass-entry.png b/doc/ota_updates/ota-ide-ssid-pass-entry.png new file mode 100644 index 0000000000..8aae132aa1 Binary files /dev/null and b/doc/ota_updates/ota-ide-ssid-pass-entry.png differ diff --git a/doc/ota_updates.md b/doc/ota_updates/ota_updates.md similarity index 56% rename from doc/ota_updates.md rename to doc/ota_updates/ota_updates.md index a78ab02eb1..d0b98acea9 100644 --- a/doc/ota_updates.md +++ b/doc/ota_updates/ota_updates.md @@ -1,154 +1,219 @@ ---- -title: OTA Update ---- - -## Table of Contents - * [Basic Requirements](#basic-requirements) - * [Arduino IDE](#arduino-ide) - * [HTTP Server](#http-server) - * [Stream Interface](#stream-interface) - -## Basic Requirements - -- Flash chip size is 2x the size of the sketch. - -## Arduino IDE - -TODO describe Arduino IDE OTA process - -#### Requirements - - The ESP and the computer must be connected to the same network. - -## HTTP Server - -```ESPhttpUpdate``` class can check for updates and download a binary file from HTTP web server. -It is possible to download updates from every IP or domain address on the network or Internet. - -#### Requirements - - web server - -#### Arduino code - -##### Simple updater - -Simple updater downloads the file every time the function is called. - -```cpp -ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin"); -``` - -##### Advanced updater - -Its possible to point update function to a script at the server. -If version string argument is given, it will be sent to the server. -Server side script can use this to check if update should be performed. - -Server side script can respond as follows: -- response code 200, and send the firmware image, -- or response code 304 to notify ESP that no update is required. - -```cpp -t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here"); -switch(ret) { - case HTTP_UPDATE_FAILED: - Serial.println("[update] Update failed."); - break; - case HTTP_UPDATE_NO_UPDATES: - Serial.println("[update] Update no Update."); - break; - case HTTP_UPDATE_OK: - Serial.println("[update] Update ok."); // may not called we reboot the ESP - break; -} -``` - -#### Server request handling - -##### Simple updater - -For the simple updater the server only needs to deliver the binary file for update. - -##### Advanced updater - -For advanced update management a script needs to run at the server side, for example a PHP script. -At every update request the the ESP sends some information in HTTP headers to the server. - -Example header data: -``` - [HTTP_USER_AGENT] => ESP8266-http-Update - [HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA - [HTTP_X_ESP8266_AP_MAC] => 1A:FE:AA:AA:AA:AA - [HTTP_X_ESP8266_FREE_SPACE] => 671744 - [HTTP_X_ESP8266_SKETCH_SIZE] => 373940 - [HTTP_X_ESP8266_CHIP_SIZE] => 524288 - [HTTP_X_ESP8266_SDK_VERSION] => 1.3.0 - [HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19 -``` - -With this information the script now can check if a update is needed. It is also possible to deliver different binaries based on the MAC address for example. - -Script example: -```php - "DOOR-7-g14f53a19", - "18:FE:AA:AA:AA:BB" => "TEMP-1.0.0" -); - -if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) { - if($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) ) { - sendFile("./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']]."bin"); - } else { - header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304); - } -} - -header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500); - -``` - - -## Updater class - -TODO describe Updater class +--- +title: OTA Update +--- + +## Table of Contents + * [Basic Requirements](#basic-requirements) + * [Arduino IDE](#arduino-ide) + * [HTTP Server](#http-server) + * [Stream Interface](#stream-interface) + +## Introduction + +OTA (Over the Air) update is the process of loading the firmware to ESP module using WiFi connection rather that a serial port. Such functionality became extremely useful in case of limited or no physical access to the module. There is no imposed protection on OTA update process. Such protection should be implemented by developer to ensure that updates are allowed only from legitimate / trusted source. + +OTA may be done from: + - [Arduino IDE](#arduino-ide) + - [HTTP server](#http-server) + +In any case first firmware upload have to be done over a serial port. If OTA routines are correctly implemented in sketch, then all subsequent uploads may be done over the air. + +The following chapters provide more details and both methods of doing OTA. + + +## Basic Requirements + +- Flash chip size is 2x the size of the sketch. + +## Arduino IDE + +Uploading modules wirelessly from Arduino IDE is intended for the following typical scenarios: +- during firmware development as a quicker alternative to loading over a serial +- for updating small quantity of modules +- only if modules are available on the same network as the computer with Arduino IDE + +#### Requirements + - The ESP and the computer must be connected to the same network. + +#### Let's Do It + +OTA process will be demonstrated using: +- DNS_SD_Arduino_OTA.ino sketch available from Arduino IDE +- NodeMCU 1.0 board with ESP-12E module + +1. Before you begin, please make sure that you have the following installed: + - Arduino IDE and ESP8266 board support as described under https://github.com/esp8266/Arduino#installing-with-boards-manager + - Python 2.7.10 (do not install Python 3.5.0 that is not supported): + 1. Upload Python from https://www.python.org/ + 2. Start installer + 3. Select “Add python.exe to Path” (see below – that option is not selected by default) + 4. Complete remaining steps of installation + + ![Python installation set up](ota-ide-python-configuration.png) + +2. Now prepare the sketch and configuration for the upload over a serial port. + + - Start Arduino IDE and load sketch DNS_SD_Arduino_OTA.ino available under File > Examples > ESP8266mDNS + + ![OTA sketch selection](ota-ide-sketch-selection.png) + + - Update ssid and pass in the sketch so the module can join your WiFi network + + ![ssid and pass entry](ota-ide-ssid-pass-entry.png) + + - Configure upload parameters as below (you may need to adjust configuration if you are using a different module): + + ![configuration of serial upload](ota-ide-serial-upload-configuration.png) + +3. Upload the sketch (Ctrl+U). Once done open Serial Monitor (Ctrl+Shift+M) and check if the module has joined your WiFi network. + + ![check if module joined network](ota-ide-module-joined-wifi.png) + +4. Only if module is connected, after a dozen (or two dozens) of seconds the esp8266-ota port will show up in Arduino IDE: + + ![selection og OTA port](ota-ide-ota-port-selection.png) + +5. Now get ready for your first OTA upload by changing configuration settings as follows: + + ![configuration of OTA upload](ota-ide-ota-upload-configuration.png) + +6. If you have successfully completed all the above steps, you can upload (Ctrl+U) the same (or any other) sketch over OTA: + + ![OTA upload complete](ota-ide-ota-upload-complete.png) + +**Note** To be able to upload your sketch over and over again using OTA, you need to embed OTA routines inside. Please use DNS_SD_Arduino_OTA.ino as an example. + + +## HTTP Server + +```ESPhttpUpdate``` class can check for updates and download a binary file from HTTP web server. +It is possible to download updates from every IP or domain address on the network or Internet. + +#### Requirements + - web server + +#### Arduino code + +##### Simple updater + +Simple updater downloads the file every time the function is called. + +```cpp +ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin"); +``` + +##### Advanced updater + +Its possible to point update function to a script at the server. +If version string argument is given, it will be sent to the server. +Server side script can use this to check if update should be performed. + +Server side script can respond as follows: +- response code 200, and send the firmware image, +- or response code 304 to notify ESP that no update is required. + +```cpp +t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here"); +switch(ret) { + case HTTP_UPDATE_FAILED: + Serial.println("[update] Update failed."); + break; + case HTTP_UPDATE_NO_UPDATES: + Serial.println("[update] Update no Update."); + break; + case HTTP_UPDATE_OK: + Serial.println("[update] Update ok."); // may not called we reboot the ESP + break; +} +``` + +#### Server request handling + +##### Simple updater + +For the simple updater the server only needs to deliver the binary file for update. + +##### Advanced updater + +For advanced update management a script needs to run at the server side, for example a PHP script. +At every update request the the ESP sends some information in HTTP headers to the server. + +Example header data: +``` + [HTTP_USER_AGENT] => ESP8266-http-Update + [HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA + [HTTP_X_ESP8266_AP_MAC] => 1A:FE:AA:AA:AA:AA + [HTTP_X_ESP8266_FREE_SPACE] => 671744 + [HTTP_X_ESP8266_SKETCH_SIZE] => 373940 + [HTTP_X_ESP8266_CHIP_SIZE] => 524288 + [HTTP_X_ESP8266_SDK_VERSION] => 1.3.0 + [HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19 +``` + +With this information the script now can check if a update is needed. It is also possible to deliver different binaries based on the MAC address for example. + +Script example: +```php + "DOOR-7-g14f53a19", + "18:FE:AA:AA:AA:BB" => "TEMP-1.0.0" +); + +if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) { + if($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) ) { + sendFile("./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']]."bin"); + } else { + header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304); + } +} + +header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500); + +``` + + +## Updater class + +TODO describe Updater class