Skip to content

Commit 52588f1

Browse files
committed
Merge branch 'feature/add_ota_demo' into 'master'
feat: add ota demo See merge request sdk/ESP8266_RTOS_SDK!371
2 parents 4e15ca4 + f997e4d commit 52588f1

File tree

7 files changed

+468
-0
lines changed

7 files changed

+468
-0
lines changed

examples/system/ota/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := ota
7+
8+
include $(IDF_PATH)/make/project.mk
9+

examples/system/ota/OTA_workflow.png

56.1 KB
Loading

examples/system/ota/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
# Simple OTA Demo
3+
4+
This example demonstrates a working OTA (over the air) firmware update workflow.
5+
6+
This example is a *simplified demonstration*, for production firmware updates you should use a secure protocol such as HTTPS.
7+
8+
---
9+
10+
# Aim
11+
12+
An app running on ESP8266 can upgrade itself by downloading a new app "image" binary file, and storing it in flash.
13+
14+
In this example, the ESP8266 has 3 images in flash: factory, OTA_0, OTA_1. Each of these is a self-contained partition. The number of OTA image partition is determined by the partition table layout.
15+
16+
Flashing the example over serial with "make flash" updates the factory app image. On first boot, the bootloader loads this factory app image which then performs an OTA update (triggered in the example code). The update downloads a new image from an http server and saves it into the OTA_0 partition. At this point the example code updates the ota_data partition to indicate the new app partition, and resets. The bootloader reads ota_data, determines the new OTA image has been selected, and runs it.
17+
18+
19+
# Workflow
20+
21+
The OTA_workflow.png diagram demonstrates the overall workflow:
22+
23+
![OTA Workflow diagram](OTA_workflow.png)
24+
25+
## Step 1: Connect to AP
26+
27+
Connect your host PC to the same AP that you will use for the ESP8266.
28+
29+
## Step 2: Run HTTP Server
30+
31+
Python has a built-in HTTP server that can be used for example purposes.
32+
33+
For our upgrade example OTA file, we're going to use the `get-started/hello_world` example.
34+
35+
Open a new terminal to run the HTTP server, then run these commands to build the example and start the server:
36+
37+
```
38+
cd $IDF_PATH/examples/get-started/hello_world
39+
make
40+
cd build
41+
python -m SimpleHTTPServer 8070
42+
```
43+
44+
While the server is running, the contents of the build directory can be browsed at http://localhost:8070/
45+
46+
NB: On some systems, the command may be `python2 -m SimpleHTTPServer`.
47+
48+
NB: You've probably noticed there is nothing special about the "hello world" example when used for OTA updates. This is because any .bin app file which is built by esp-idf can be used as an app image for OTA. The only difference is whether it is written to a factory partition or an OTA partition.
49+
50+
If you have any firewall software running that will block incoming access to port 8070, configure it to allow access while running the example.
51+
52+
## Step 3: Build OTA Example
53+
54+
Change back to the OTA example directory, and type `make menuconfig` to configure the OTA example. Under the "Example Configuration" submenu, fill in the following details:
55+
56+
* WiFi SSID & Password
57+
* IP address of your host PC as "HTTP Server"
58+
* HTTP Port number (if using the Python HTTP server above, the default is correct)
59+
60+
If serving the "hello world" example, you can leave the default filename as-is.
61+
62+
Save your changes, and type `make` to build the example.
63+
64+
## Step 4: Flash OTA Example
65+
66+
When flashing, use the `erase_flash` target first to erase the entire flash (this deletes any leftover data in the ota_data partition). Then flash the factory image over serial:
67+
68+
```
69+
make erase_flash flash
70+
```
71+
72+
(The `make erase_flash flash` means "erase everything, then flash". `make flash` only erases the parts of flash which are being rewritten.)
73+
74+
## Step 5: Run the OTA Example
75+
76+
When the example starts up, it will print "ota: Starting OTA example..." then:
77+
78+
1. Connect to the AP with configured SSID and password.
79+
2. Connect to the HTTP server and download the new image.
80+
3. Write the image to flash, and configure the next boot from this image.
81+
4. Reboot
82+
83+
# Troubleshooting
84+
85+
* Check your PC can ping the ESP8266 at its IP, and that the IP, AP and other configuration settings are correct in menuconfig.
86+
* Check if any firewall software is preventing incoming connections on the PC.
87+
* Check you can see the configured file (default hello-world.bin) if you browse the file listing at http://127.0.0.1/
88+
* If you have another PC or a phone, try viewing the file listing from the separate host.
89+
90+
## Error "ota_begin error err=0x104"
91+
92+
If you see this error then check that the configured (and actual) flash size is large enough for the partitions in the partition table. The default "two OTA slots" partition table only works with 4MB flash size. To use OTA with smaller flash sizes, create a custom partition table CSV (look in components/partition_table) and configure it in menuconfig.
93+
94+
If changing partition layout, it is usually wise to run "make erase_flash" between steps.
95+
96+
## Production Implementation
97+
98+
If scaling this example for production use, please consider:
99+
100+
* Using an encrypted communications channel such as HTTPS.
101+
* Dealing with timeouts or WiFi disconnections while flashing.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
menu "Example Configuration"
2+
3+
config WIFI_SSID
4+
string "WiFi SSID"
5+
default "myssid"
6+
help
7+
SSID (network name) for the example to connect to.
8+
9+
config WIFI_PASSWORD
10+
string "WiFi Password"
11+
default "myssid"
12+
help
13+
WiFi password (WPA or WPA2) for the example to use.
14+
15+
Can be left blank if the network has no security set.
16+
17+
config SERVER_IP
18+
string "HTTP Server IP"
19+
default "192.168.0.3"
20+
help
21+
HTTP Server IP to download the image file from.
22+
23+
See example README.md for details.
24+
25+
config SERVER_PORT
26+
string "HTTP Server Port"
27+
default "8070"
28+
help
29+
HTTP Server port to connect to.
30+
Should be chosen not to conflict with any other port used
31+
on the system.
32+
33+
config EXAMPLE_FILENAME
34+
string "HTTP GET Filename"
35+
default "/hello-world.bin"
36+
help
37+
Filename of the app image file to download for
38+
the OTA update.
39+
40+
endmenu

examples/system/ota/main/component.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#
2+
# "main" pseudo-component makefile.
3+
#
4+
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

0 commit comments

Comments
 (0)