“ESP 32 Examples Study”的版本间的差异
来自个人维基
free6d1823(讨论 | 贡献) |
free6d1823(讨论 | 贡献) (→softwareAp) |
||
(未显示1个用户的1个中间版本) | |||
第10行: | 第10行: | ||
== station == | == station == | ||
+ | Programing flow | ||
+ | <source lang="c"> | ||
+ | static 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, "got ip:%s", | ||
+ | ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); | ||
+ | s_retry_num = 0; | ||
+ | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | ||
+ | break; | ||
+ | case SYSTEM_EVENT_STA_DISCONNECTED: | ||
+ | { | ||
+ | if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { | ||
+ | esp_wifi_connect(); | ||
+ | xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | ||
+ | s_retry_num++; | ||
+ | ESP_LOGI(TAG,"retry to connect to the AP"); | ||
+ | } | ||
+ | ESP_LOGI(TAG,"connect to the AP fail\n"); | ||
+ | break; | ||
+ | } | ||
+ | default: | ||
+ | break; | ||
+ | } | ||
+ | return ESP_OK; | ||
+ | } | ||
+ | |||
+ | wifi_station_init() { | ||
+ | s_wifi_event_group = xEventGroupCreate(); | ||
+ | |||
+ | tcpip_adapter_init(); | ||
+ | esp_event_loop_init(event_handler, NULL); | ||
+ | |||
+ | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
+ | esp_wifi_init(&cfg); | ||
+ | |||
+ | wifi_config_t wifi_config = { | ||
+ | .sta = { | ||
+ | .ssid = EXAMPLE_ESP_WIFI_SSID, | ||
+ | .password = EXAMPLE_ESP_WIFI_PASS | ||
+ | }, | ||
+ | }; | ||
+ | |||
+ | esp_wifi_set_mode(WIFI_MODE_STA); | ||
+ | esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); | ||
+ | esp_wifi_start(); | ||
+ | } | ||
+ | </source> | ||
Log: | Log: | ||
<source lang="c"> | <source lang="c"> | ||
第105行: | 第157行: | ||
== softwareAp == | == softwareAp == | ||
+ | programong flow | ||
+ | <source lang="c"> | ||
+ | static esp_err_t event_handler(void *ctx, system_event_t *event) | ||
+ | { | ||
+ | switch(event->event_id) { | ||
+ | case SYSTEM_EVENT_AP_STACONNECTED: | ||
+ | ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", | ||
+ | MAC2STR(event->event_info.sta_connected.mac), | ||
+ | event->event_info.sta_connected.aid); | ||
+ | break; | ||
+ | case SYSTEM_EVENT_AP_STADISCONNECTED: | ||
+ | ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", | ||
+ | MAC2STR(event->event_info.sta_disconnected.mac), | ||
+ | event->event_info.sta_disconnected.aid); | ||
+ | break; | ||
+ | default: | ||
+ | break; | ||
+ | } | ||
+ | return ESP_OK; | ||
+ | } | ||
+ | |||
+ | void wifi_init_softap() | ||
+ | { | ||
+ | s_wifi_event_group = xEventGroupCreate(); | ||
+ | |||
+ | tcpip_adapter_init(); | ||
+ | esp_event_loop_init(event_handler, NULL); | ||
+ | |||
+ | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
+ | ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | ||
+ | |||
+ | wifi_config_t wifi_config = { | ||
+ | .ap = { | ||
+ | .ssid = EXAMPLE_ESP_WIFI_SSID, | ||
+ | .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), | ||
+ | .password = EXAMPLE_ESP_WIFI_PASS, | ||
+ | .max_connection = EXAMPLE_MAX_STA_CONN, | ||
+ | .authmode = WIFI_AUTH_WPA_WPA2_PSK | ||
+ | }, | ||
+ | }; | ||
+ | if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { | ||
+ | wifi_config.ap.authmode = WIFI_AUTH_OPEN; | ||
+ | } | ||
+ | |||
+ | esp_wifi_set_mode(WIFI_MODE_AP); | ||
+ | esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config); | ||
+ | esp_wifi_start(); | ||
+ | |||
+ | } | ||
+ | </source> | ||
Log | Log | ||
<source lang="c"> | <source lang="c"> |
2020年3月1日 (日) 22:30的最后版本
Blink
GPIO demo
gpio_pad_select_gpio(BLINK_GPIO); gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); gpio_set_level(BLINK_GPIO, 0);
modify main/Kconfig.projbuild
#define BLINK_GPIO CONFIG_BLINK_GPIO range 0 34 default 5 ==> 21 for my EVB
station
Programing flow
static 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, "got ip:%s", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); break; case SYSTEM_EVENT_STA_DISCONNECTED: { if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); s_retry_num++; ESP_LOGI(TAG,"retry to connect to the AP"); } ESP_LOGI(TAG,"connect to the AP fail\n"); break; } default: break; } return ESP_OK; } wifi_station_init() { s_wifi_event_group = xEventGroupCreate(); tcpip_adapter_init(); esp_event_loop_init(event_handler, NULL); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_ESP_WIFI_SSID, .password = EXAMPLE_ESP_WIFI_PASS }, }; esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); esp_wifi_start(); }
Log:
rst:0x1 (POWERON_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:6804 load:0x40078000,len:14076 load:0x40080400,len:4304 entry 0x400806e8 I (71) boot: Chip Revision: 1 I (72) boot_comm: chip revision: 1, min. bootloader chip revision: 0 I (40) boot: ESP-IDF v4.0-dirty 2nd stage bootloader I (40) boot: compile time 00:25:36 I (40) boot: Enabling RNG early entropy source... I (45) boot: SPI Speed : 40MHz I (49) boot: SPI Mode : DIO I (53) boot: SPI Flash Size : 2MB I (57) boot: Partition Table: I (61) boot: ## Label Usage Type ST Offset Length I (68) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (75) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (83) boot: 2 factory factory app 00 00 00010000 00100000 I (90) boot: End of partition table I (94) boot_comm: chip revision: 1, min. application chip revision: 0 I (101) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1494c ( 84300) map I (141) esp_image: segment 1: paddr=0x00024974 vaddr=0x3ffb0000 size=0x0326c ( 12908) load I (146) esp_image: segment 2: paddr=0x00027be8 vaddr=0x40080000 size=0x00400 ( 1024) load 0x40080000: _WindowOverflow4 at C:/esp-idf/components/freertos/xtensa_vectors.S:1778 I (148) esp_image: segment 3: paddr=0x00027ff0 vaddr=0x40080400 size=0x08020 ( 32800) load I (170) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x65424 (414756) map 0x400d0018: _stext at ??:? I (319) esp_image: segment 5: paddr=0x00095444 vaddr=0x40088420 size=0x0ccac ( 52396) load 0x40088420: ppRxPkt at ??:? I (354) boot: Loaded app from partition at offset 0x10000 I (354) boot: Disabling RNG early entropy source... I (354) cpu_start: Pro cpu up. I (358) cpu_start: Application information: I (363) cpu_start: Project name: wifi_station I (368) cpu_start: App version: v4.0-dirty I (373) cpu_start: Compile time: Feb 24 2020 00:25:18 I (380) cpu_start: ELF file SHA256: 2518224bf889f5ed... I (386) cpu_start: ESP-IDF: v4.0-dirty I (391) cpu_start: Starting app cpu, entry point is 0x40081260 0x40081260: call_start_cpu1 at C:/esp-idf/components/esp32/cpu_start.c:272 I (0) cpu_start: App cpu up. I (401) heap_init: Initializing. RAM available for dynamic allocation: I (408) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (414) heap_init: At 3FFB9198 len 00026E68 (155 KiB): DRAM I (420) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM I (427) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (433) heap_init: At 400950CC len 0000AF34 (43 KiB): IRAM I (439) cpu_start: Pro cpu start user code I (458) spi_flash: detected chip: generic I (458) spi_flash: flash io: dio W (458) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header. I (469) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (543) wifi station: ESP_WIFI_MODE_STA I (563) wifi: wifi driver task: 3ffc0b14, prio:23, stack:3584, core=0 I (563) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE I (563) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE I (593) wifi: wifi firmware version: 581f422 I (593) wifi: config NVS flash: enabled I (593) wifi: config nano formating: disabled I (593) wifi: Init dynamic tx buffer num: 32 I (593) wifi: Init data frame dynamic rx buffer num: 32 I (603) wifi: Init management frame dynamic rx buffer num: 32 I (603) wifi: Init management short buffer num: 32 I (613) wifi: Init static rx buffer size: 1600 I (613) wifi: Init static rx buffer num: 10 I (623) wifi: Init dynamic rx buffer num: 32 I (713) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0 I (713) wifi: mode : sta (bc:dd:c2:d0:20:90) I (723) wifi station: wifi_init_sta finished. I (843) wifi: new:<3,1>, old:<1,0>, ap:<255,255>, sta:<3,1>, prof:1 I (843) wifi: state: init -> auth (b0) I (843) wifi: state: auth -> assoc (0) I (853) wifi: state: assoc -> run (10) I (1303) wifi: connected with ASUS81951, aid = 5, channel 3, 40U, bssid = 30:5a:3a:a1:e0:48 I (1303) wifi: security type: 3, phy: bgn, rssi: -42 I (1303) wifi: pm start, type: 1 I (1333) wifi: AP's beacon interval = 102400 us, DTIM period = 1 I (2043) tcpip_adapter: sta ip: 172.17.0.123, mask: 255.255.255.0, gw: 172.17.0.1 I (2043) wifi station: got ip:172.17.0.123 I (2043) wifi station: connected to ap SSID:ASUS81951 password:0282214863
softwareAp
programong flow
static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { case SYSTEM_EVENT_AP_STACONNECTED: ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid); break; case SYSTEM_EVENT_AP_STADISCONNECTED: ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", MAC2STR(event->event_info.sta_disconnected.mac), event->event_info.sta_disconnected.aid); break; default: break; } return ESP_OK; } void wifi_init_softap() { s_wifi_event_group = xEventGroupCreate(); tcpip_adapter_init(); esp_event_loop_init(event_handler, NULL); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { .ap = { .ssid = EXAMPLE_ESP_WIFI_SSID, .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID), .password = EXAMPLE_ESP_WIFI_PASS, .max_connection = EXAMPLE_MAX_STA_CONN, .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } esp_wifi_set_mode(WIFI_MODE_AP); esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config); esp_wifi_start(); }
Log
rst:0x1 (POWERON_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:6804 load:0x40078000,len:14076 load:0x40080400,len:4304 entry 0x400806e8 I (71) boot: Chip Revision: 1 I (71) boot_comm: chip revision: 1, min. bootloader chip revision: 0 I (39) boot: ESP-IDF v4.0-dirty 2nd stage bootloader I (39) boot: compile time 00:44:22 I (40) boot: Enabling RNG early entropy source... I (44) boot: SPI Speed : 40MHz I (49) boot: SPI Mode : DIO I (53) boot: SPI Flash Size : 2MB I (57) boot: Partition Table: I (60) boot: ## Label Usage Type ST Offset Length I (67) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (75) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (82) boot: 2 factory factory app 00 00 00010000 00100000 I (90) boot: End of partition table I (94) boot_comm: chip revision: 1, min. application chip revision: 0 I (101) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x147cc ( 83916) map I (140) esp_image: segment 1: paddr=0x000247f4 vaddr=0x3ffb0000 size=0x0326c ( 12908) load I (146) esp_image: segment 2: paddr=0x00027a68 vaddr=0x40080000 size=0x00400 ( 1024) load 0x40080000: _WindowOverflow4 at C:/esp-idf/components/freertos/xtensa_vectors.S:1778 I (147) esp_image: segment 3: paddr=0x00027e70 vaddr=0x40080400 size=0x081a0 ( 33184) load I (170) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x651e8 (414184) map 0x400d0018: _stext at ??:? I (318) esp_image: segment 5: paddr=0x00095208 vaddr=0x400885a0 size=0x0cb2c ( 52012) load 0x400885a0: ppCalTxAMPDULength at ??:? I (353) boot: Loaded app from partition at offset 0x10000 I (353) boot: Disabling RNG early entropy source... I (354) cpu_start: Pro cpu up. I (357) cpu_start: Application information: I (362) cpu_start: Project name: wifi_softAP I (367) cpu_start: App version: v4.0-dirty I (373) cpu_start: Compile time: Feb 24 2020 00:43:56 I (379) cpu_start: ELF file SHA256: f91187939d235192... I (385) cpu_start: ESP-IDF: v4.0-dirty I (390) cpu_start: Starting app cpu, entry point is 0x40081260 0x40081260: call_start_cpu1 at C:/esp-idf/components/esp32/cpu_start.c:272 I (0) cpu_start: App cpu up. I (400) heap_init: Initializing. RAM available for dynamic allocation: I (407) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (413) heap_init: At 3FFB9180 len 00026E80 (155 KiB): DRAM I (420) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM I (426) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (432) heap_init: At 400950CC len 0000AF34 (43 KiB): IRAM I (438) cpu_start: Pro cpu start user code I (457) spi_flash: detected chip: generic I (457) spi_flash: flash io: dio W (457) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header. I (468) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (540) wifi softAP: ESP_WIFI_MODE_AP I (560) wifi: wifi driver task: 3ffc0b5c, prio:23, stack:3584, core=0 I (560) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE I (560) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE I (590) wifi: wifi firmware version: 581f422 I (590) wifi: config NVS flash: enabled I (590) wifi: config nano formating: disabled I (590) wifi: Init dynamic tx buffer num: 32 I (590) wifi: Init data frame dynamic rx buffer num: 32 I (600) wifi: Init management frame dynamic rx buffer num: 32 I (600) wifi: Init management short buffer num: 32 I (610) wifi: Init static rx buffer size: 1600 I (610) wifi: Init static rx buffer num: 10 I (610) wifi: Init dynamic rx buffer num: 32 I (720) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0 I (720) wifi: mode : softAP (bc:dd:c2:d0:20:91) I (720) wifi: Total power save buffer number: 16 I (720) wifi: Init max length of beacon: 752/752 I (730) wifi: Init max length of beacon: 752/752 I (730) wifi softAP: wifi_init_softap finished. SSID:myssid password:mypassword -- when connect device -- I (75750) wifi: new:<1,1>, old:<1,0>, ap:<1,1>, sta:<255,255>, prof:1 I (75750) wifi: station: 14:a5:1a:23:8c:a3 join, AID=1, bgn, 40U I (75810) wifi softAP: station 14:a5:1a:23:8c:a3 join, AID=1 I (76290) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2