1. modify simple wifi example

2. add README file
This commit is contained in:
zhangyanjiao 2018-08-30 10:11:21 +08:00
parent 1806a69abf
commit 45d596d821
10 changed files with 291 additions and 74 deletions

View file

@ -3,7 +3,7 @@
# project subdirectory. # project subdirectory.
# #
PROJECT_NAME := simple_wifi PROJECT_NAME := wifi_softAP
include $(IDF_PATH)/make/project.mk include $(IDF_PATH)/make/project.mk

View file

@ -0,0 +1,42 @@
# WiFi softAP example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
## How to use example
### Configure the project
```
make menuconfig
```
* Set serial port under Serial Flasher Options.
* Set WiFi SSID and WiFi Password and Maximal STA connections under Example Configuration Options.
### Build and Flash
Build the project and flash it to the board, then run monitor tool to view serial output:
```
make -j4 flash monitor
```
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
There is the console output for this example:
```
I (917) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0
I (917) wifi: mode : softAP (30:ae:a4:80:45:69)
I (917) wifi softAP: wifi_init_softap finished.SSID:myssid password:mypassword
I (26457) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (26457) wifi: station: 70:ef:00:43:96:67 join, AID=1, bg, 20
I (26467) wifi softAP: station:70:ef:00:43:96:67 join, AID=1
I (27657) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2
```

View file

@ -1,22 +1,5 @@
menu "Example Configuration" menu "Example Configuration"
choice ESP_WIFI_MODE
prompt "AP or STA"
default ESP_WIFI_IS_STATION
help
Whether the esp32 is softAP or station.
config ESP_WIFI_IS_SOFTAP
bool "SoftAP"
config ESP_WIFI_IS_STATION
bool "Station"
endchoice
config ESP_WIFI_MODE_AP
bool
default y if ESP_WIFI_IS_SOFTAP
default n if ESP_WIFI_IS_STATION
config ESP_WIFI_SSID config ESP_WIFI_SSID
string "WiFi SSID" string "WiFi SSID"
default "myssid" default "myssid"
@ -30,7 +13,7 @@ config ESP_WIFI_PASSWORD
WiFi password (WPA or WPA2) for the example to use. WiFi password (WPA or WPA2) for the example to use.
config MAX_STA_CONN config MAX_STA_CONN
int "Max STA conn" int "Maximal STA connections"
default 4 default 4
help help
Max number of the STA connects to AP. Max number of the STA connects to AP.

View file

@ -1,4 +1,4 @@
/* Simple WiFi Example /* WiFi softAP Example
This example code is in the Public Domain (or CC0 licensed, at your option.) This example code is in the Public Domain (or CC0 licensed, at your option.)
@ -19,38 +19,23 @@
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sys.h" #include "lwip/sys.h"
/* The examples use simple WiFi configuration that you can set via /* The examples use WiFi configuration that you can set via 'make menuconfig'.
'make menuconfig'.
If you'd rather not, just change the below entries to strings with If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/ */
#define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_ESP_WIFI_MODE_AP //TRUE:AP FALSE:STA
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN #define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN
/* FreeRTOS event group to signal when we are connected*/ /* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, static const char *TAG = "wifi softAP";
but we only care about one event - are we connected
to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "simple wifi";
static esp_err_t event_handler(void *ctx, system_event_t *event) static esp_err_t event_handler(void *ctx, system_event_t *event)
{ {
switch(event->event_id) { 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));
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_AP_STACONNECTED: case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac), MAC2STR(event->event_info.sta_connected.mac),
@ -61,10 +46,6 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
MAC2STR(event->event_info.sta_disconnected.mac), MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid); event->event_info.sta_disconnected.aid);
break; break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
break;
default: default:
break; break;
} }
@ -73,7 +54,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
void wifi_init_softap() void wifi_init_softap()
{ {
wifi_event_group = xEventGroupCreate(); s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
@ -101,31 +82,6 @@ void wifi_init_softap()
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} }
void wifi_init_sta()
{
wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(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 = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
void app_main() void app_main()
{ {
//Initialize NVS //Initialize NVS
@ -136,12 +92,6 @@ void app_main()
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
#if EXAMPLE_ESP_WIFI_MODE_AP
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP"); ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap(); wifi_init_softap();
#else
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/
} }

View file

@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := wifi_station
include $(IDF_PATH)/make/project.mk

View file

@ -0,0 +1,97 @@
# WiFi station example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
## How to use example
### Configure the project
```
make menuconfig
```
* Set serial port under Serial Flasher Options.
* Set WiFi SSID and WiFi Password and Maximum retry under Example Configuration Options.
### Build and Flash
Build the project and flash it to the board, then run monitor tool to view serial output:
```
make -j4 flash monitor
```
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
There is the console output for station connects to ap successfully:
```
I (727) wifi station: ESP_WIFI_MODE_STA
I (727) wifi: wifi driver task: 3ffc0c68, prio:23, stack:3584, core=0
I (727) wifi: wifi firmware version: 19b3110
I (727) wifi: config NVS flash: enabled
I (727) wifi: config nano formating: disabled
I (737) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (747) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (777) wifi: Init dynamic tx buffer num: 32
I (777) wifi: Init data frame dynamic rx buffer num: 32
I (777) wifi: Init management frame dynamic rx buffer num: 32
I (777) wifi: Init static rx buffer size: 1600
I (787) wifi: Init static rx buffer num: 10
I (787) wifi: Init dynamic rx buffer num: 32
I (907) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0
I (907) wifi: mode : sta (30:ae:a4:80:45:68)
I (907) wifi station: wifi_init_sta finished.
I (907) wifi station: connect to ap SSID:myssid password:mypassword
I (1027) wifi: n:6 0, o:1 0, ap:255 255, sta:6 0, prof:1
I (2017) wifi: state: init -> auth (b0)
I (2017) wifi: state: auth -> assoc (0)
I (2027) wifi: state: assoc -> run (10)
I (2067) wifi: connected with myssid, channel 6
I (2067) wifi: pm start, type: 1
I (3227) event: sta ip: 172.20.10.7, mask: 255.255.255.240, gw: 172.20.10.1
I (3227) wifi station: got ip:172.20.10.7
```
There is the console output for station connects to ap failed:
```
I (728) wifi station: ESP_WIFI_MODE_STA
I (728) wifi: wifi driver task: 3ffc0c68, prio:23, stack:3584, core=0
I (728) wifi: wifi firmware version: 19b3110
I (728) wifi: config NVS flash: enabled
I (738) wifi: config nano formating: disabled
I (738) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (748) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (778) wifi: Init dynamic tx buffer num: 32
I (778) wifi: Init data frame dynamic rx buffer num: 32
I (778) wifi: Init management frame dynamic rx buffer num: 32
I (788) wifi: Init static rx buffer size: 1600
I (788) wifi: Init static rx buffer num: 10
I (788) wifi: Init dynamic rx buffer num: 32
I (908) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0
I (908) wifi: mode : sta (30:ae:a4:80:45:68)
I (908) wifi station: wifi_init_sta finished.
I (918) wifi station: connect to ap SSID:myssid password:mypassword
I (3328) wifi station: retry to connect to the AP
I (3328) wifi station: connect to the AP fail
I (5738) wifi station: retry to connect to the AP
I (5738) wifi station: connect to the AP fail
I (8148) wifi station: retry to connect to the AP
I (8148) wifi station: connect to the AP fail
I (10558) wifi station: retry to connect to the AP
I (10558) wifi station: connect to the AP fail
I (12968) wifi station: retry to connect to the AP
I (12968) wifi station: connect to the AP fail
I (15378) wifi station: connect to the AP fail
```

View file

@ -0,0 +1,20 @@
menu "Example Configuration"
config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
config ESP_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
endmenu

View file

@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

View file

@ -0,0 +1,108 @@
/* WiFi station Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
/* The examples use WiFi configuration that you can set via 'make menuconfig'.
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about one event
* - are we connected to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "wifi station";
static int s_retry_num = 0;
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;
}
void wifi_init_sta()
{
s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(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 = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
void app_main()
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
}