Skip to content

Commit 7594127

Browse files
committed
Merge branch 'feature/add_simple_wifi_example' into 'master'
add feature of simple wifi example See merge request idf/esp-idf!2045
2 parents 9e8d098 + d91456a commit 7594127

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

examples/wifi/simple_wifi/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 := simple_wifi
7+
8+
include $(IDF_PATH)/make/project.mk
9+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
menu "Example Configuration"
2+
3+
choice ESP_WIFI_MODE
4+
prompt "AP or STA"
5+
default ESP_WIFI_IS_STATION
6+
help
7+
Whether the esp32 is softAP or station.
8+
9+
config ESP_WIFI_IS_SOFTAP
10+
bool "SoftAP"
11+
config ESP_WIFI_IS_STATION
12+
bool "Station"
13+
endchoice
14+
15+
config ESP_WIFI_MODE_AP
16+
bool
17+
default y if ESP_WIFI_IS_SOFTAP
18+
default n if ESP_WIFI_IS_STATION
19+
20+
config ESP_WIFI_SSID
21+
string "WiFi SSID"
22+
default "myssid"
23+
help
24+
SSID (network name) for the example to connect to.
25+
26+
config ESP_WIFI_PASSWORD
27+
string "WiFi Password"
28+
default "mypassword"
29+
help
30+
WiFi password (WPA or WPA2) for the example to use.
31+
32+
config MAX_STA_CONN
33+
int "Max STA conn"
34+
default 4
35+
help
36+
Max number of the STA connects to AP.
37+
endmenu
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Main component makefile.
3+
#
4+
# This Makefile can be left empty. By default, it will take the sources in the
5+
# src/ directory, compile them and link them into lib(subdirectory_name).a
6+
# in the build directory. This behaviour is entirely configurable,
7+
# please read the ESP-IDF documents if you need to do this.
8+
#
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/* Simple WiFi Example
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
*/
9+
#include <string.h>
10+
#include "freertos/FreeRTOS.h"
11+
#include "freertos/task.h"
12+
#include "freertos/event_groups.h"
13+
#include "esp_system.h"
14+
#include "esp_wifi.h"
15+
#include "esp_event_loop.h"
16+
#include "esp_log.h"
17+
#include "nvs_flash.h"
18+
19+
#include "lwip/err.h"
20+
#include "lwip/sys.h"
21+
22+
/* The examples use simple WiFi configuration that you can set via
23+
'make menuconfig'.
24+
25+
If you'd rather not, just change the below entries to strings with
26+
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
27+
*/
28+
#define EXAMPLE_ESP_WIFI_MODE_AP CONFIG_ESP_WIFI_MODE_AP //TRUE:AP FALSE:STA
29+
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
30+
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
31+
#define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN
32+
33+
/* FreeRTOS event group to signal when we are connected*/
34+
static EventGroupHandle_t wifi_event_group;
35+
36+
/* The event group allows multiple bits for each event,
37+
but we only care about one event - are we connected
38+
to the AP with an IP? */
39+
const int WIFI_CONNECTED_BIT = BIT0;
40+
41+
static const char *TAG = "simple wifi";
42+
43+
static esp_err_t event_handler(void *ctx, system_event_t *event)
44+
{
45+
switch(event->event_id) {
46+
case SYSTEM_EVENT_STA_START:
47+
esp_wifi_connect();
48+
break;
49+
case SYSTEM_EVENT_STA_GOT_IP:
50+
ESP_LOGI(TAG, "got ip:%s",
51+
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
52+
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
53+
break;
54+
case SYSTEM_EVENT_AP_STACONNECTED:
55+
ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d",
56+
MAC2STR(event->event_info.sta_connected.mac),
57+
event->event_info.sta_connected.aid);
58+
break;
59+
case SYSTEM_EVENT_AP_STADISCONNECTED:
60+
ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d",
61+
MAC2STR(event->event_info.sta_disconnected.mac),
62+
event->event_info.sta_disconnected.aid);
63+
break;
64+
case SYSTEM_EVENT_STA_DISCONNECTED:
65+
esp_wifi_connect();
66+
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
67+
break;
68+
default:
69+
break;
70+
}
71+
return ESP_OK;
72+
}
73+
74+
void wifi_init_softap()
75+
{
76+
wifi_event_group = xEventGroupCreate();
77+
78+
tcpip_adapter_init();
79+
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
80+
81+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
82+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
83+
wifi_config_t wifi_config = {
84+
.ap = {
85+
.ssid = EXAMPLE_ESP_WIFI_SSID,
86+
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
87+
.password = EXAMPLE_ESP_WIFI_PASS,
88+
.max_connection = EXAMPLE_MAX_STA_CONN,
89+
.authmode = WIFI_AUTH_WPA_WPA2_PSK
90+
},
91+
};
92+
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
93+
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
94+
}
95+
96+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
97+
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
98+
ESP_ERROR_CHECK(esp_wifi_start());
99+
100+
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
101+
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
102+
}
103+
104+
void wifi_init_sta()
105+
{
106+
wifi_event_group = xEventGroupCreate();
107+
108+
tcpip_adapter_init();
109+
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
110+
111+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
112+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
113+
wifi_config_t wifi_config = {
114+
.sta = {
115+
.ssid = EXAMPLE_ESP_WIFI_SSID,
116+
.password = EXAMPLE_ESP_WIFI_PASS
117+
},
118+
};
119+
120+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
121+
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
122+
ESP_ERROR_CHECK(esp_wifi_start() );
123+
124+
ESP_LOGI(TAG, "wifi_init_sta finished.");
125+
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
126+
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
127+
}
128+
129+
void app_main()
130+
{
131+
//Initialize NVS
132+
esp_err_t ret = nvs_flash_init();
133+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
134+
ESP_ERROR_CHECK(nvs_flash_erase());
135+
ret = nvs_flash_init();
136+
}
137+
ESP_ERROR_CHECK(ret);
138+
139+
#if EXAMPLE_ESP_WIFI_MODE_AP
140+
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
141+
wifi_init_softap();
142+
#else
143+
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
144+
wifi_init_sta();
145+
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/
146+
147+
}

0 commit comments

Comments
 (0)