|
| 1 | +--- |
| 2 | +title: OTA Update |
| 3 | +--- |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + * [Basic Requirements](#basic-requirements) |
| 7 | + * [Arduino IDE](#arduino-ide) |
| 8 | + * [HTTP Server](#http-server) |
| 9 | + * [Stream Interface](#stream-interface) |
| 10 | + |
| 11 | +## Basic Requirements |
| 12 | + |
| 13 | +- Flash chip size is 2x the size of the sketch |
| 14 | + |
| 15 | +## Arduino IDE |
| 16 | + |
| 17 | +TODO describe Arduino IDE OTA process |
| 18 | + |
| 19 | +#### Requirements |
| 20 | + - The ESP and the Computer must be connected to the Same network. |
| 21 | + |
| 22 | + |
| 23 | +## HTTP Server |
| 24 | + |
| 25 | +the ```ESPhttpUpdate``` class can check for updates and download a binary file form a HTTP web server. |
| 26 | +It is possible to download updates from every IP or domain address on the Network or Internet. |
| 27 | + |
| 28 | + |
| 29 | +#### Requirements |
| 30 | + - web server |
| 31 | + |
| 32 | + |
| 33 | +#### Arduino code |
| 34 | + |
| 35 | +##### simple updater |
| 36 | + |
| 37 | +the Simple Updater downloads the File every time the function is called. |
| 38 | + |
| 39 | +```cpp |
| 40 | +ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin"); |
| 41 | +``` |
| 42 | + |
| 43 | +##### advanced updater |
| 44 | + |
| 45 | +Its possible to point to a script at the server. |
| 46 | +If a version String is delivered to the Function this String will be send to the server. |
| 47 | +A Server side Update check is now possible. |
| 48 | + |
| 49 | +the Server can return a binary file for update (Header 200) |
| 50 | +or it return header 304 to notify the ESP that no Update is needed. |
| 51 | + |
| 52 | +```cpp |
| 53 | +t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here"); |
| 54 | +switch(ret) { |
| 55 | + case HTTP_UPDATE_FAILED: |
| 56 | + Serial.println("[update] Update failed."); |
| 57 | + break; |
| 58 | + case HTTP_UPDATE_NO_UPDATES: |
| 59 | + Serial.println("[update] Update no Update."); |
| 60 | + break; |
| 61 | + case HTTP_UPDATE_OK: |
| 62 | + Serial.println("[update] Update ok."); // may not called we reboot the ESP |
| 63 | + break; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +#### Server request handling |
| 68 | + |
| 69 | +##### simple updater |
| 70 | + |
| 71 | +for the simple Updater the Server only needs to deliver the binary file for update. |
| 72 | + |
| 73 | +##### advanced updater |
| 74 | + |
| 75 | +for advanced update management a Script needs to run at the Server side, for example a PHP script. |
| 76 | +at every Update request the the ESP sends some informations in the Header to the Server |
| 77 | + |
| 78 | +example Header data: |
| 79 | +``` |
| 80 | + [HTTP_USER_AGENT] => ESP8266-http-Update |
| 81 | + [HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA |
| 82 | + [HTTP_X_ESP8266_AP_MAC] => 1A:FE:AA:AA:AA:AA |
| 83 | + [HTTP_X_ESP8266_FREE_SPACE] => 671744 |
| 84 | + [HTTP_X_ESP8266_SKETCH_SIZE] => 373940 |
| 85 | + [HTTP_X_ESP8266_CHIP_SIZE] => 524288 |
| 86 | + [HTTP_X_ESP8266_SDK_VERSION] => 1.3.0 |
| 87 | + [HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19 |
| 88 | +``` |
| 89 | + |
| 90 | +with this information the script now can check if a update is needed. |
| 91 | +It is also possible to deliver different binary´s based on the MAC address for example. |
| 92 | + |
| 93 | +script example: |
| 94 | +```php |
| 95 | +<?PHP |
| 96 | + |
| 97 | +header('Content-type: text/plain; charset=utf8', true); |
| 98 | + |
| 99 | +function check_header($name, $value = false) { |
| 100 | + if(!isset($_SERVER[$name])) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + if($value && $_SERVER[$name] != $value) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +function sendFile($path) { |
| 110 | + header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200); |
| 111 | + header('Content-Type: application/octet-stream', true); |
| 112 | + header('Content-Disposition: attachment; filename='.basename($path)); |
| 113 | + header('Content-Length: '.filesize($path), true); |
| 114 | + readfile($path); |
| 115 | +} |
| 116 | + |
| 117 | +if(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) { |
| 118 | + header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403); |
| 119 | + echo "only for ESP8266 updater!\n"; |
| 120 | + exit(); |
| 121 | +} |
| 122 | + |
| 123 | +if( |
| 124 | + !check_header('HTTP_X_ESP8266_STA_MAC') || |
| 125 | + !check_header('HTTP_X_ESP8266_AP_MAC') || |
| 126 | + !check_header('HTTP_X_ESP8266_FREE_SPACE') || |
| 127 | + !check_header('HTTP_X_ESP8266_SKETCH_SIZE') || |
| 128 | + !check_header('HTTP_X_ESP8266_CHIP_SIZE') || |
| 129 | + !check_header('HTTP_X_ESP8266_SDK_VERSION') || |
| 130 | + !check_header('HTTP_X_ESP8266_VERSION') |
| 131 | +) { |
| 132 | + header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403); |
| 133 | + echo "only for ESP8266 updater! (header)\n"; |
| 134 | + exit(); |
| 135 | +} |
| 136 | + |
| 137 | +$db = array( |
| 138 | + "18:FE:AA:AA:AA:AA" => "DOOR-7-g14f53a19", |
| 139 | + "18:FE:AA:AA:AA:BB" => "TEMP-1.0.0" |
| 140 | +); |
| 141 | + |
| 142 | +if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) { |
| 143 | + if($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) ) { |
| 144 | + sendFile("./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']]."bin"); |
| 145 | + } else { |
| 146 | + header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500); |
| 151 | + |
| 152 | +``` |
| 153 | + |
| 154 | + |
| 155 | +## Stream Interface |
| 156 | + |
| 157 | +TODO describe Stream Interface update proccess |
| 158 | + |
| 159 | +```cpp |
| 160 | + ESP.updateSketch(client, length); |
| 161 | +``` |
| 162 | + |
0 commit comments