2016-2019 2019 08 08
Contents 1 3 1.1 ESP32.............................. 3 1.2.................................................. 5 2 7 2.1............................................. 7 2.2 ESP-IDF............................................ 7 2.3....................................... 8 2.4.................................................. 11 2.5............................................... 12 3 13 3.1............................................... 13 3.2................................................ 14 3.3............................................... 15 4 Wi-Fi 17 4.1.................................................. 17 4.2............................................... 19 5 SoftAP BLE 21 5.1.................................................. 21 5.2.................................................. 23 5.3............................................... 23 5.4 NVS....................................... 26 5.5............................................. 27 5.6............................................... 27 6 29 i
6.1.............................................. 30 6.2.......................................... 30 6.3 AWS IoT........................................... 30 6.4............................................... 33 7 35 7.1 Flash.............................................. 35 7.2 (OTA)........................................... 35 7.3.................................................. 38 7.4 URL......................................... 39 7.5............................................... 39 8 41 8.1 NVS............................................ 41 8.2.................................................. 42 8.3............................................. 42 8.4............................................... 43 9 45 9.1............................................. 45 9.2............................................. 46 ii
[English] ESP32 Jumpstart Now Contents 1
2 Contents
CHAPTER 1 [English] 1.1 ESP32 app ESP32 ESP32 ESP32 / / ESP32 ESP-IDF GPIO Wi-Fi app / app / GPIO / GPIO / GPIO OTA 3
1: 2: 4 Chapter 1.
ESP32 ESP32-DevKitC ESP32 PC Windows Linux Mac OS 1.1.1 ESP8266 ESP8266 ESP8266-DevKitC ESP8266 ESP8266_RTOS_SDK ESP8266 ESP8266 IDF ESP8266_RTOS_SDK ESP8266 ESP32 ESP8266 1.2 / 1. 2. AWS IoT AWS IOT 3. 4. 5. app / app 6. AWS IoT 7. 1.2. 5
6 Chapter 1.
CHAPTER 2 [English] ESP32 ESP32 2.1 ESP32 Linux Windows MacOS ESP32 USB ESP-IDF ( SDK) 2.2 ESP-IDF ESP-IDF ESP32 ESP-IDF ESP32 ESP-IDF 2.2.1 ESP-IDF ESP-IDF ESP-IDF 7
1: ESP32 ESP-IDF 2.2.2 ESP-IDF ESP-IDF ESP-IDF Wi-Fi HTTP ESP-IDF SDK main Makefile sdkconfig.defaults SDK 2.3 ESP-IDF 8 Chapter 2.
2: 2.3. 9
3: 10 Chapter 2.
$ git clone --recursive https://github.com/espressif/esp-jumpstart ESP-IDF ESP-IDF v3.2 $ cd esp-idf $ git checkout -b release/v3.2 remotes/origin/release/v3.2 $ git submodule update --recursive Hello World $ cd esp-jumpstart/1_hello_world $ make -j8 menuconfig $ export ESPPORT=/dev/cu.SLAB_USBTOUART # Or the correct device name for your setup $ export ESPBAUD=921600 $ make -j8 flash monitor SDK 2.3.1 ESP8266 IDF_PATH ESP8266_RTOS_SDK esp-jumpstart platform/esp8266 $ cd esp-jumpstart $ git checkout -b platform/esp8266 origin/platform/esp8266 2.4 Hello World #include <stdio.h> #include "freertos/freertos.h" #include "freertos/task.h" void app_main() { ( ) 2.4. 11
} int i = 0; while (1) { printf("[%d] Hello world!\n", i); i++; vtaskdelay(5000 / porttick_period_ms); } ( ) app_main() FreeRTOS ESP32 FreeRTOS ESP32 app_main() SDK printf() strlen() time() C IDF newlib C newlib C stdio stdlib / / C signal locale wchr printf() FreeRTOS ESP32 FreeRTOS vtaskdelay 5 FreeRTOS API FreeRTOS 2.5 ESP32 12 Chapter 2.
CHAPTER 3 [English] ESP32 API / GPIO esp-jumpstart 2_drivers/ app_driver.c 3.1 DevkitC Boot GPIO 0 / 3.1.1 13
#include <iot_button.h> button_handle_t btn_handle=iot_button_create(jumpstart_board_button_gpio, JUMPSTART_BOARD_BUTTON_ACTIVE_LEVEL); iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, push_btn_cb, "RELEASE"); iot_button iot_button GPIO DevKitC BUTTON_GPIO GPIO 0 esp-timer push_btn_cb esp-timer push_btn_cb static void push_btn_cb(void* arg) { static uint64_t previous; uint64_t current = xtaskgettickcount(); if ((current - previous) > DEBOUNCE_TIME) { previous = current; app_driver_set_state(!g_output_state); } } xtaskgettickcount() FreeRTOS 300 app_driver_toggle_state() 3.2 GPIO GPIO 3.2.1 GPIO gpio_config_t io_conf; io_conf.mode = GPIO_MODE_OUTPUT; io_conf.pull_up_en = 1; ( ) 14 Chapter 3.
io_conf.pin_bit_mask = ((uint64_t)1 << JUMPSTART_BOARD_OUTPUT_GPIO); ( ) /* Configure the GPIO */ gpio_config(&io_conf); GPIO 27 gpio_config_t GPIO /* Assert GPIO */ gpio_set_level(jumpstart_board_output_gpio, target); gpio_set_level() GPIO 3.3 ESP32 Wi-Fi 3.3. 15
16 Chapter 3.
CHAPTER 4 Wi-Fi [English] Wi-Fi Wi-Fi 3_wifi_connection/ 4.1 #include <esp_wifi.h> #include <esp_event_loop.h> tcpip_adapter_init(); esp_event_loop_init(event_handler, NULL); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg); esp_wifi_set_mode(wifi_mode_sta); wifi_config_t wifi_config = {.sta = {.ssid = EXAMPLE_ESP_WIFI_SSID,.password = EXAMPLE_ESP_WIFI_PASS, }, ( ) 17
}; esp_wifi_set_config(esp_if_wifi_sta, &wifi_config); esp_wifi_start(); ( ) tcpip_adapter_init() TCP/IP esp_wifi_init() esp_wifi_set_mode() Wi-Fi station SSID Wi-Fi esp_wifi_start() station Wi-Fi IP (event loop) TCP/IP Wi-Fi esp_event_loop_init() event loop event loop Event Loop esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { case SYSTEM_EVENT_STA_START: esp_wifi_connect(); break; case SYSTEM_EVENT_STA_GOT_IP: ESP_LOGI(TAG, "Connected with IP Address:%s", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); break; case SYSTEM_EVENT_STA_DISCONNECTED: esp_wifi_connect(); break; return ESP_OK; } 3 SYSTEM_EVENT_STA_START station esp_wifi_connect() Wi-Fi station esp_wifi_connect() ESP32 IP SYSTEM_EVENT_STA_GOT_IP IP 18 Chapter 4. Wi-Fi
4.2 Wi-Fi SSID ESP32 Wi-Fi IP GPIO Wi-Fi 4.2. 19
20 Chapter 4. Wi-Fi
CHAPTER 5 SoftAP BLE [English] Wi-Fi SSID PASSWORD Wi-Fi esp-jumpstart 4_network_config/ 5.1 Wi-Fi Wi-Fi Wi-Fi Wi-Fi SoftAP (BLE) 5.1.1 SoftAP SoftAP Wi-Fi Wi-Fi Wi-Fi 21
1: Wi-Fi Wi-Fi app Wi-Fi SoftAP Wi-Fi app ios Android app SoftAP Wi-Fi 5.1.2 BLE BLE BLE BLE BLE Wi-Fi ios Android app BLE BLE flash BLE 22 Chapter 5. SoftAP BLE
5.2 APP esp-jumpstart 4_network_config/ 4_network_config BLE app ble-sec1 app app app Wi-Fi Wi-Fi 5.2.1 ESP8266 ESP8266 SoftAP ESP8266 wifi-sec1 app 5.3 / softap/ble Wi-Fi SoftAP + HTTP BLE - WPA2 SoftAP just-works BLE Protocol Buffers Wi-Fi Protocol Buffers Wi-Fi SoftAP BLE 5.2. 23
IDF ESP-IDF ios Android Android ios 5.3.1 if (conn_mgr_prov_is_provisioned(&provisioned)!= ESP_OK) { return; } if (provisioned!= true) { /* Starting unified provisioning */ conn_mgr_prov_start_provisioning(prov_type, security, pop, service_name, service_key); } else { /* Start the station */ wifi_init_sta(); } conn_mgr_prov conn_mgr_prov_is_provisionined() API Wi-Fi NVS flash NVS Non-volatile storage Wi-Fi conn_mgr_prov_start_provisioning() API API 1. SoftAP BLE 2. Wi-Fi BLE 3. 4. NVS 5. SoftAP BLE HTTP NVS Wi-Fi wifi_init_sta() API Wi-Fi station Wi-Fi station 24 Chapter 5. SoftAP BLE
Wi-Fi (event handler) esp_err_t event_handler(void *ctx, system_event_t *event) { conn_mgr_prov_event_handler(ctx, event);......... switch(event->event_id) { case SYSTEM_EVENT_STA_START: API /* Starting unified provisioning */ conn_mgr_prov_start_provisioning(prov_type, security, pop, service_name, service_key); API API 1. (Security) security0 security1 Security0 Security1 curve25519 AES-CTR 2. (Transport) SoftAP BLE 3. (Proof of Possession) BLE SoftAP 4. (Service Name) app SoftAP Wi-Fi SSID BLE BLE 5. (Service Key) SoftAP Wi-Fi BLE 5.3. 25
BLE just-works SoftAP BLE app app 5.3.2 5.4 NVS Wi-Fi NVS NVS NVS NVS flash NVS NVS flash NVS NVS NULL BLOB NVS API /* Store the value of key 'my_key' to NVS */ nvs_set_u32(nvs_handle, "my_key", chosen_value); /* Read the value of key 'my_key' from NVS */ nvs_get_u32(nvs_handle, "my_key", &chosen_value); 5.4.1 NVS. 26 Chapter 5. SoftAP BLE
5.5 NVS NVS iot_button_() 5.5.1 4_network_config/ /* Register 3 second press callback */ iot_button_add_on_press_cb(btn_handle, 3, button_press_3sec_cb, NULL); btn_handle 3 button_press_3sec_cb() btn_handle static void button_press_3sec_cb(void *arg) { nvs_flash_erase(); esp_restart(); } NVS NVS 4_network_config/ 3 5.6 app Wi-Fi Wi-Fi / 5.5. 27
28 Chapter 5. SoftAP BLE
CHAPTER 6 [English] 1: Web API API 29
Alexa 6.1 (Transport Layer Security, TLS) TLS TLS HTTP MQTT TLS 6.1.1 CA TLS CA TLS CA CA TLS CA 6.2 CA ESP-IDF component.mk component.mk COMPONENT_EMBED_TXTFILES := cloud_cfg/server.cert cloud_cfg/server.cert extern const uint8_t certificate_pem_crt_start[] asm("_binary_server_cert_start"); extern const uint8_t certificate_pem_crt_end[] asm("_binary_server_cert_end"); 6.3 AWS IoT AWS IoT 30 Chapter 6.
6.3.1 AWS IoT 14 OTA AWS IoT 1. 2. 3. 6.3.2 AWS IoT 1. 2. 3. ID 4. AWS IoT CA 5. URL esp-jumpstart 5_cloud/ AWS IoT 1. 5_cloud/ 2..txt AWS CA 5_cloud/main/cloud_cfg/server.cert 5_cloud/main/cloud_cfg/device.key 5_cloud/main/cloud_cfg/device.cert ID 5_cloud/main/cloud_cfg/deviceid.txt 5_cloud/main/cloud_cfg/endpoint.txt 3. AWS IoT 6.3. AWS IoT 31
6.3.3 AWS IoT Web API Web API curl curl Linux/Windows/Mac curl --tlsv1.2 --cert cloud_cfg/device.cert \ --key cloud_cfg/device.key \ https://a3orti3lw2padm-ats.iot.us-east-1.amazonaws.com:8443/things/<contents-of- deviceid.txt-file>/shadow \ python -mjson.tool deviceid.txt things shadow <contents-ofdeviceid.txt-file> AWS device.cert device.key curl / curl -d '{"state":{"desired":{"output":false}}}' \ --tlsv1.2 --cert cloud_cfg/device.cert \ --key cloud_cfg/device.key \ https://a3orti3lw2padm-ats.iot.us-east-1.amazonaws.com:8443/things/<contents-of- deviceid.txt-file>/shadow \ python -mjson.tool curl HTTP POST POST JSON JSON AWS IoT false curl true false 6.3.4 cloud_aws.c AWS IoT SDK API app_driver_get_state() app_driver_toggle_state() AWS IoT 3 AWS CA 5_cloud/main/cloud_cfg/server.cert 32 Chapter 6.
5_cloud/main/cloud_cfg/device.key 5_cloud/main/cloud_cfg/device.cert 6.4 / OTA 6.4. 33
34 Chapter 6.
CHAPTER 7 [English] flash 7.1 Flash ESP-IDF flash flash 0x9000 Bootloader flash 1 NVS 1 7.2 (OTA) - flash OTA Data OTA 0 OTA 0 OTA Data 1 OTA 1 2 3 OTA Data OTA 1 35
1: Flash 7.2.1 Flash IDF OTA Data 2 CSV Comma Separated Values IDF # Name, Type, SubType, Offset, Size, Flags # Note: if you change the phy_init or app partition offset # make sure to change the offset in Kconfig.projbuild nvs, data, nvs,, 0x6000, otadata, data, ota,, 0x2000, phy_init, data, phy,, 0x1000, ota_0, app, ota_0,, 1600K, ota_1, app, ota_1,, 1600K, IDF NVS OTA Data OTA 0 OTA 1 IDF SDK 6_ota/sdkconfig.defaults make menuconfig menuconfig -> Partition Table 36 Chapter 7.
2: OTA Flash 7.2. (OTA) 37
3: 7.2.2 ESP8266 ESP8266 2 MB flash # Name, Type, SubType, Offset, Size, Flags # Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild nvs, data, nvs, 0x9000, 0x4000, otadata, data, ota, 0xd000, 0x2000, phy_init, data, phy, 0xf000, 0x1000, ota_0, app, ota_0, 0x10000, 0xC5000, ota_1, app, ota_1, 0x110000, 0xC5000, 7.3 esp_http_client_config_t config = {.url = url,.cert_pem = (char *)upgrade_server_cert_pem_start, }; esp_err_t ret = esp_https_ota(&config); 38 Chapter 7.
esp_http_client_config_t OTA URL CA CA esp_https_ota() API API URL GitHub CA GitHub CA 7.4 URL URL URL URL GitHub 1_hello_world curl -d '{"state":{"desired":{"ota_url":"https://raw.githubusercontent.com/wiki/ espressif/esp-jumpstart/images/hello-world.bin"}}}' \ --tlsv1.2 --cert cloud_cfg/device.cert \ --key cloud_cfg/device.key \ https://a3orti3lw2padm-ats.iot.us-east-1.amazonaws.com:8443/things/<contents-of- deviceid.txt-file>/shadow python -mjson.tool Hello World 7.5 7.4. URL 39
40 Chapter 7.
CHAPTER 8 [English] NVS NVS flash NVS NVS NVS NVS 8.1 NVS Flash flash Flash flash fctry NVS 7_mfg/partitions.csv 41
8.2 fctry NVS NVS API NVS NVS NVS #define MFG_PARTITION_NAME "fctry" /* Error checks removed for brevity */ nvs_handle fctry_handle; nvs_flash_init_partition(mfg_partition_name); nvs_open_from_partition(mfg_partition_name, "mfg_ns", NVS_READWRITE, &fctry_handle); fctry_handle NVS NVS fctry NVS : nvs_get_str(fctry_handle, "serial_no", buf, &buflen); fctry NVS 8.3 fctry 1: components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py NVS flash fctry CSV CSV NVS CSV 42 Chapter 8.
NVS NVS mfg_config.csv CSV CSV NVS bin $ python $IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py -- input mfg_config.csv --output my_mfg.bin --size 0x6000 my_mfg.bin NVS NVS bin flash $ $IDF_PATH/components/esptool_py/esptool/esptool.py --port $ESPPORT write_flash 0x340000 my_mfg.bin 8.3.1 ESP8266 ESP8266 2 MB flash my_mfg.bin $ $IDF_PATH/components/esptool_py/esptool/esptool.py --port $ESPPORT write_flash 0x1D5000 my_mfg.bin 8.4 8.4. 43
44 Chapter 8.
CHAPTER 9 [English] 9.1 TLS ESP-IDF mbedtls mbedtls TLS 9.1.1 CA TLS CA / esp_tls API CA esp_tls_cfg_t cfg = {.cacert_pem_buf = server_root_cert_pem_start,.cacert_pem_bytes = server_root_cert_pem_end - server_root_cert_pem_start, }; ( ) 45
( ) struct esp_tls *tls = esp_tls_conn_http_new("https://www.example.com", &cfg); TLS CA 9.1.2 CA CA CA $ openssl s_client -showcerts -connect www.example.com:443 < /dev/null 9.2 ESP32 9.2.1 ESP8266 ESP8266 9.2.2 Secure Boot Secure Boot ESP32 flash Bootloader Bootloader ESP32 efuse Bootloader Bootloader ESP32 flash ESP32 BootROM efuse Bootloader 46 Chapter 9.
1: Secure Boot Bootloader BootROM Bootloader Bootloader Bootloader Secure Boot Secure Boot 9.2.3 Flash flash ESP32 flash flash flash flash efuse AES AES efuse Secure Boot flash flash flash 9.2.4 NVS NVS NVS ESP-IDF NVS AES-XTS flash NVS flash 9.2. 47
2: flash nvs_partition_gen.py nvs_flash_read_security_cfg() API nvs_sec_cfg_t nvs_flash_secure_init() API nvs_flash_secure_init_partition() API NVS flash NVS NVS 48 Chapter 9.