Skip to content

Commit 40a511f

Browse files
committed
Merge branch 'master' of https://github.com/krzychb/Arduino into krzychb-master
Conflicts: doc/ota_updates/ota_updates.md
2 parents 92069e6 + 421206f commit 40a511f

11 files changed

+223
-156
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Documentation for latest development version:
5050
- [Reference](doc/reference.md)
5151
- [Supported boards](doc/boards.md)
5252
- [Change log](doc/changes.md)
53-
- [OTA Update](doc/ota_updates.md)
53+
- [OTA Update](doc/ota_updates/ota_updates.md)
5454

5555
### Issues and support ###
5656

boards.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ nodemcuv2.menu.CpuFrequency.160.build.f_cpu=160000000L
301301
nodemcuv2.menu.UploadTool.esptool=Serial
302302
nodemcuv2.menu.UploadTool.esptool.upload.tool=esptool
303303
nodemcuv2.menu.UploadTool.esptool.upload.verbose=-vv
304+
nodemcuv2.menu.UploadTool.espota=OTA
305+
nodemcuv2.menu.UploadTool.espota.upload.tool=espota
304306

305307
nodemcuv2.menu.UploadSpeed.115200=115200
306308
nodemcuv2.menu.UploadSpeed.115200.upload.speed=115200
34.3 KB
Loading
105 KB
Loading
63.5 KB
Loading
Loading
75.5 KB
Loading
Loading
69.5 KB
Loading
25.3 KB
Loading
Lines changed: 220 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,220 @@
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-
## HTTP Server
23-
24-
```ESPhttpUpdate``` class can check for updates and download a binary file from HTTP web server.
25-
It is possible to download updates from every IP or domain address on the network or Internet.
26-
27-
#### Requirements
28-
- web server
29-
30-
#### Arduino code
31-
32-
##### Simple updater
33-
34-
Simple updater downloads the file every time the function is called.
35-
36-
```cpp
37-
ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin");
38-
```
39-
40-
##### Advanced updater
41-
42-
Its possible to point update function to a script at the server.
43-
If version string argument is given, it will be sent to the server.
44-
Server side script can use this to check if update should be performed.
45-
46-
Server side script can respond as follows:
47-
- response code 200, and send the firmware image,
48-
- or response code 304 to notify ESP that no update is required.
49-
50-
```cpp
51-
t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here");
52-
switch(ret) {
53-
case HTTP_UPDATE_FAILED:
54-
Serial.println("[update] Update failed.");
55-
break;
56-
case HTTP_UPDATE_NO_UPDATES:
57-
Serial.println("[update] Update no Update.");
58-
break;
59-
case HTTP_UPDATE_OK:
60-
Serial.println("[update] Update ok."); // may not called we reboot the ESP
61-
break;
62-
}
63-
```
64-
65-
#### Server request handling
66-
67-
##### Simple updater
68-
69-
For the simple updater the server only needs to deliver the binary file for update.
70-
71-
##### Advanced updater
72-
73-
For advanced update management a script needs to run at the server side, for example a PHP script.
74-
At every update request the the ESP sends some information in HTTP headers to the server.
75-
76-
Example header data:
77-
```
78-
[HTTP_USER_AGENT] => ESP8266-http-Update
79-
[HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA
80-
[HTTP_X_ESP8266_AP_MAC] => 1A:FE:AA:AA:AA:AA
81-
[HTTP_X_ESP8266_FREE_SPACE] => 671744
82-
[HTTP_X_ESP8266_SKETCH_SIZE] => 373940
83-
[HTTP_X_ESP8266_CHIP_SIZE] => 524288
84-
[HTTP_X_ESP8266_SDK_VERSION] => 1.3.0
85-
[HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19
86-
```
87-
88-
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.
89-
90-
Script example:
91-
```php
92-
<?PHP
93-
94-
header('Content-type: text/plain; charset=utf8', true);
95-
96-
function check_header($name, $value = false) {
97-
if(!isset($_SERVER[$name])) {
98-
return false;
99-
}
100-
if($value && $_SERVER[$name] != $value) {
101-
return false;
102-
}
103-
return true;
104-
}
105-
106-
function sendFile($path) {
107-
header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200);
108-
header('Content-Type: application/octet-stream', true);
109-
header('Content-Disposition: attachment; filename='.basename($path));
110-
header('Content-Length: '.filesize($path), true);
111-
readfile($path);
112-
}
113-
114-
if(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
115-
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
116-
echo "only for ESP8266 updater!\n";
117-
exit();
118-
}
119-
120-
if(
121-
!check_header('HTTP_X_ESP8266_STA_MAC') ||
122-
!check_header('HTTP_X_ESP8266_AP_MAC') ||
123-
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
124-
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
125-
!check_header('HTTP_X_ESP8266_CHIP_SIZE') ||
126-
!check_header('HTTP_X_ESP8266_SDK_VERSION') ||
127-
!check_header('HTTP_X_ESP8266_VERSION')
128-
) {
129-
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
130-
echo "only for ESP8266 updater! (header)\n";
131-
exit();
132-
}
133-
134-
$db = array(
135-
"18:FE:AA:AA:AA:AA" => "DOOR-7-g14f53a19",
136-
"18:FE:AA:AA:AA:BB" => "TEMP-1.0.0"
137-
);
138-
139-
if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {
140-
if($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) ) {
141-
sendFile("./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']]."bin");
142-
} else {
143-
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
144-
}
145-
exit();
146-
}
147-
148-
header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
149-
150-
```
151-
152-
153-
## Updater class
154-
155-
TODO describe Updater class
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+
## Introduction
12+
13+
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.
14+
15+
OTA may be done from:
16+
- [Arduino IDE](#arduino-ide)
17+
- [HTTP server](#http-server)
18+
19+
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.
20+
21+
The following chapters provide more details and both methods of doing OTA.
22+
23+
24+
## Basic Requirements
25+
26+
- Flash chip size is 2x the size of the sketch.
27+
28+
## Arduino IDE
29+
30+
Uploading modules wirelessly from Arduino IDE is intended for the following typical scenarios:
31+
- during firmware development as a quicker alternative to loading over a serial
32+
- for updating small quantity of modules
33+
- only if modules are available on the same network as the computer with Arduino IDE
34+
35+
#### Requirements
36+
- The ESP and the computer must be connected to the same network.
37+
38+
#### Let's Do It
39+
40+
OTA process will be demonstrated using:
41+
- DNS_SD_Arduino_OTA.ino sketch available from Arduino IDE
42+
- NodeMCU 1.0 board with ESP-12E module
43+
44+
1. Before you begin, please make sure that you have the following installed:
45+
- Arduino IDE and ESP8266 board support as described under https://github.com/esp8266/Arduino#installing-with-boards-manager
46+
- Python 2.7.10 (do not install Python 3.5.0 that is not supported):
47+
1. Upload Python from https://www.python.org/
48+
2. Start installer
49+
3. Select “Add python.exe to Path” (see below – that option is not selected by default)
50+
4. Complete remaining steps of installation
51+
52+
![Python installation set up](ota-ide-python-configuration.png)
53+
54+
2. Now prepare the sketch and configuration for the upload over a serial port.
55+
56+
- Start Arduino IDE and load sketch DNS_SD_Arduino_OTA.ino available under File > Examples > ESP8266mDNS
57+
58+
![OTA sketch selection](ota-ide-sketch-selection.png)
59+
60+
- Update ssid and pass in the sketch so the module can join your WiFi network
61+
62+
![ssid and pass entry](ota-ide-ssid-pass-entry.png)
63+
64+
- Configure upload parameters as below (you may need to adjust configuration if you are using a different module):
65+
66+
![configuration of serial upload](ota-ide-serial-upload-configuration.png)
67+
68+
3. Upload the sketch (Ctrl+U). Once done open Serial Monitor (Ctrl+Shift+M) and check if the module has joined your WiFi network.
69+
70+
![check if module joined network](ota-ide-module-joined-wifi.png)
71+
72+
4. Only if module is connected, after a dozen (or two dozens) of seconds the esp8266-ota port will show up in Arduino IDE:
73+
74+
![selection og OTA port](ota-ide-ota-port-selection.png)
75+
76+
5. Now get ready for your first OTA upload by changing configuration settings as follows:
77+
78+
![configuration of OTA upload](ota-ide-ota-upload-configuration.png)
79+
80+
6. If you have successfully completed all the above steps, you can upload (Ctrl+U) the same (or any other) sketch over OTA:
81+
82+
![OTA upload complete](ota-ide-ota-upload-complete.png)
83+
84+
**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.
85+
86+
87+
## HTTP Server
88+
89+
```ESPhttpUpdate``` class can check for updates and download a binary file from HTTP web server.
90+
It is possible to download updates from every IP or domain address on the network or Internet.
91+
92+
#### Requirements
93+
- web server
94+
95+
#### Arduino code
96+
97+
##### Simple updater
98+
99+
Simple updater downloads the file every time the function is called.
100+
101+
```cpp
102+
ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin");
103+
```
104+
105+
##### Advanced updater
106+
107+
Its possible to point update function to a script at the server.
108+
If version string argument is given, it will be sent to the server.
109+
Server side script can use this to check if update should be performed.
110+
111+
Server side script can respond as follows:
112+
- response code 200, and send the firmware image,
113+
- or response code 304 to notify ESP that no update is required.
114+
115+
```cpp
116+
t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here");
117+
switch(ret) {
118+
case HTTP_UPDATE_FAILED:
119+
Serial.println("[update] Update failed.");
120+
break;
121+
case HTTP_UPDATE_NO_UPDATES:
122+
Serial.println("[update] Update no Update.");
123+
break;
124+
case HTTP_UPDATE_OK:
125+
Serial.println("[update] Update ok."); // may not called we reboot the ESP
126+
break;
127+
}
128+
```
129+
130+
#### Server request handling
131+
132+
##### Simple updater
133+
134+
For the simple updater the server only needs to deliver the binary file for update.
135+
136+
##### Advanced updater
137+
138+
For advanced update management a script needs to run at the server side, for example a PHP script.
139+
At every update request the the ESP sends some information in HTTP headers to the server.
140+
141+
Example header data:
142+
```
143+
[HTTP_USER_AGENT] => ESP8266-http-Update
144+
[HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA
145+
[HTTP_X_ESP8266_AP_MAC] => 1A:FE:AA:AA:AA:AA
146+
[HTTP_X_ESP8266_FREE_SPACE] => 671744
147+
[HTTP_X_ESP8266_SKETCH_SIZE] => 373940
148+
[HTTP_X_ESP8266_CHIP_SIZE] => 524288
149+
[HTTP_X_ESP8266_SDK_VERSION] => 1.3.0
150+
[HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19
151+
```
152+
153+
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.
154+
155+
Script example:
156+
```php
157+
<?PHP
158+
159+
header('Content-type: text/plain; charset=utf8', true);
160+
161+
function check_header($name, $value = false) {
162+
if(!isset($_SERVER[$name])) {
163+
return false;
164+
}
165+
if($value && $_SERVER[$name] != $value) {
166+
return false;
167+
}
168+
return true;
169+
}
170+
171+
function sendFile($path) {
172+
header($_SERVER["SERVER_PROTOCOL"].' 200 OK', true, 200);
173+
header('Content-Type: application/octet-stream', true);
174+
header('Content-Disposition: attachment; filename='.basename($path));
175+
header('Content-Length: '.filesize($path), true);
176+
readfile($path);
177+
}
178+
179+
if(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
180+
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
181+
echo "only for ESP8266 updater!\n";
182+
exit();
183+
}
184+
185+
if(
186+
!check_header('HTTP_X_ESP8266_STA_MAC') ||
187+
!check_header('HTTP_X_ESP8266_AP_MAC') ||
188+
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
189+
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
190+
!check_header('HTTP_X_ESP8266_CHIP_SIZE') ||
191+
!check_header('HTTP_X_ESP8266_SDK_VERSION') ||
192+
!check_header('HTTP_X_ESP8266_VERSION')
193+
) {
194+
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
195+
echo "only for ESP8266 updater! (header)\n";
196+
exit();
197+
}
198+
199+
$db = array(
200+
"18:FE:AA:AA:AA:AA" => "DOOR-7-g14f53a19",
201+
"18:FE:AA:AA:AA:BB" => "TEMP-1.0.0"
202+
);
203+
204+
if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {
205+
if($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) ) {
206+
sendFile("./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']]."bin");
207+
} else {
208+
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
209+
}
210+
exit();
211+
}
212+
213+
header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
214+
215+
```
216+
217+
218+
## Updater class
219+
220+
TODO describe Updater class

0 commit comments

Comments
 (0)