Skip to content

Commit 9c9b96d

Browse files
ESP-YJMespressif-bot
authored andcommitted
examples: Add a separate static_ip example
1 parent 936523b commit 9c9b96d

File tree

7 files changed

+319
-0
lines changed

7 files changed

+319
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(static_ip)

examples/protocols/static_ip/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 := static_ip
7+
8+
include $(IDF_PATH)/make/project.mk
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# WiFi station example
2+
3+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
4+
5+
6+
## How to use example
7+
8+
### Configure the project
9+
10+
```
11+
idf.py menuconfig
12+
```
13+
14+
Set following parameters under Example Configuration Options:
15+
16+
* Set `WiFi SSID` and `WiFi Password` and `Maximal retry` under Example Configuration Options.
17+
18+
* Set `Static IP address` of your device static IP.
19+
20+
* Set `Static netmask address` of your device static netmask address.
21+
22+
* Set `Static gateway address` of your device static gateway address.
23+
24+
### Build and Flash
25+
26+
Build the project and flash it to the board, then run monitor tool to view serial output:
27+
28+
```
29+
idf.py -p PORT flash monitor
30+
```
31+
32+
(To exit the serial monitor, type ``Ctrl-]``.)
33+
34+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
35+
36+
## Example Output
37+
Note that the output, in particular the order of the output, may vary depending on the environment.
38+
39+
Console output if station connects to AP successfully:
40+
```
41+
I (563) static_ip: ESP_WIFI_MODE_STA
42+
I (573) wifi:wifi driver task: 3ffc1bfc, prio:23, stack:6656, core=0
43+
I (573) system_api: Base MAC address is not set
44+
I (573) system_api: read default base MAC address from EFUSE
45+
I (603) wifi:wifi firmware version: 6b2834e
46+
I (603) wifi:wifi certification version: v7.0
47+
I (603) wifi:config NVS flash: enabled
48+
I (603) wifi:config nano formating: disabled
49+
I (613) wifi:Init data frame dynamic rx buffer num: 32
50+
I (613) wifi:Init management frame dynamic rx buffer num: 32
51+
I (623) wifi:Init management short buffer num: 32
52+
I (623) wifi:Init dynamic tx buffer num: 32
53+
I (633) wifi:Init static rx buffer size: 1600
54+
I (633) wifi:Init static rx buffer num: 10
55+
I (643) wifi:Init dynamic rx buffer num: 32
56+
I (643) wifi_init: rx ba win: 6
57+
I (643) wifi_init: tcpip mbox: 32
58+
I (653) wifi_init: udp mbox: 6
59+
I (653) wifi_init: tcp mbox: 6
60+
I (653) wifi_init: tcp tx win: 5744
61+
I (663) wifi_init: tcp rx win: 5744
62+
I (663) wifi_init: tcp mss: 1440
63+
I (673) wifi_init: WiFi IRAM OP enabled
64+
I (673) wifi_init: WiFi RX IRAM OP enabled
65+
I (723) phy_init: phy_version 4660,0162888,Dec 23 2020
66+
W (723) phy_init: failed to load RF calibration data (0xffffffff), falling back to full calibration
67+
I (1093) wifi:mode : sta (4c:11:ae:ea:69:fc)
68+
I (1093) wifi:enable tsf
69+
I (1093) static_ip: wifi_init_sta finished.
70+
I (2313) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1
71+
I (3053) wifi:state: init -> auth (b0)
72+
I (3083) wifi:state: auth -> assoc (0)
73+
I (3213) wifi:new:<11,2>, old:<11,0>, ap:<255,255>, sta:<11,2>, prof:1
74+
I (3213) wifi:state: assoc -> run (10)
75+
I (3233) wifi:connected with myssid, aid = 2, channel 11, 40D, bssid = 34:29:12:43:c5:40
76+
I (3233) wifi:security: WPA2-PSK, phy: bgn, rssi: -7
77+
I (3243) wifi:pm start, type: 1
78+
79+
I (3253) esp_netif_handlers: sta ip: 192.168.4.2, mask: 255.255.255.0, gw: 192.168.4.1
80+
I (3253) static_ip: static ip:192.168.4.2
81+
I (3253) static_ip: connected to ap SSID:myssid password:mypassword
82+
W (3273) wifi:<ba-add>idx:0 (ifx:0, 34:29:12:43:c5:40), tid:5, ssn:2, winSize:64
83+
I (3303) wifi:AP's beacon interval = 102400 us, DTIM period = 1
84+
I (3303) wifi:new:<11,0>, old:<11,2>, ap:<255,255>, sta:<11,0>, prof:1
85+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "static_ip_example_main.c"
2+
INCLUDE_DIRS ".")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
menu "Example Configuration"
2+
3+
config EXAMPLE_WIFI_SSID
4+
string "WiFi SSID"
5+
default "myssid"
6+
help
7+
SSID (network name) for the example to connect to.
8+
9+
config EXAMPLE_WIFI_PASSWORD
10+
string "WiFi Password"
11+
default "mypassword"
12+
help
13+
WiFi password (WPA or WPA2) for the example to use.
14+
15+
config EXAMPLE_MAXIMUM_RETRY
16+
int "Maximum retry"
17+
default 5
18+
help
19+
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
20+
21+
config EXAMPLE_STATIC_IP_ADDR
22+
string "Static IP address"
23+
default "192.168.4.2"
24+
help
25+
Set static IP address.
26+
27+
config EXAMPLE_STATIC_NETMASK_ADDR
28+
string "Static netmask address"
29+
default "255.255.255.0"
30+
help
31+
Set static netmask address.
32+
33+
config EXAMPLE_STATIC_GW_ADDR
34+
string "Static gateway address"
35+
default "192.168.4.1"
36+
help
37+
Set static gateway address.
38+
endmenu
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+
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/* Static IP 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.h"
16+
#include "esp_log.h"
17+
#include "nvs_flash.h"
18+
19+
/* The examples use configuration that you can set via project configuration menu
20+
21+
If you'd rather not, just change the below entries to strings with
22+
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
23+
*/
24+
#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
25+
#define EXAMPLE_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD
26+
#define EXAMPLE_MAXIMUM_RETRY CONFIG_EXAMPLE_MAXIMUM_RETRY
27+
#define EXAMPLE_STATIC_IP_ADDR CONFIG_EXAMPLE_STATIC_IP_ADDR
28+
#define EXAMPLE_STATIC_NETMASK_ADDR CONFIG_EXAMPLE_STATIC_NETMASK_ADDR
29+
#define EXAMPLE_STATIC_GW_ADDR CONFIG_EXAMPLE_STATIC_GW_ADDR
30+
31+
/* FreeRTOS event group to signal when we are connected*/
32+
static EventGroupHandle_t s_wifi_event_group;
33+
34+
/* The event group allows multiple bits for each event, but we only care about two events:
35+
* - we are connected to the AP with an IP
36+
* - we failed to connect after the maximum amount of retries */
37+
#define WIFI_CONNECTED_BIT BIT0
38+
#define WIFI_FAIL_BIT BIT1
39+
40+
static const char *TAG = "static_ip";
41+
42+
static int s_retry_num = 0;
43+
44+
static void example_set_static_ip(esp_netif_t *netif)
45+
{
46+
if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
47+
ESP_LOGE(TAG, "Failed to stop dhcp client");
48+
return;
49+
}
50+
esp_netif_ip_info_t ip;
51+
memset(&ip, 0 , sizeof(esp_netif_ip_info_t));
52+
ip.ip.addr = ipaddr_addr(EXAMPLE_STATIC_IP_ADDR);
53+
ip.netmask.addr = ipaddr_addr(EXAMPLE_STATIC_NETMASK_ADDR);
54+
ip.gw.addr = ipaddr_addr(EXAMPLE_STATIC_GW_ADDR);
55+
if (esp_netif_set_ip_info(netif, &ip) != ESP_OK) {
56+
ESP_LOGE(TAG, "Failed to set ip info");
57+
return;
58+
}
59+
ESP_LOGD(TAG, "Success to set static ip: %s, netmask: %s, gw: %s", EXAMPLE_STATIC_IP_ADDR, EXAMPLE_STATIC_NETMASK_ADDR, EXAMPLE_STATIC_GW_ADDR);
60+
}
61+
62+
static void event_handler(void* arg, esp_event_base_t event_base,
63+
int32_t event_id, void* event_data)
64+
{
65+
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
66+
esp_wifi_connect();
67+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
68+
example_set_static_ip(arg);
69+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
70+
if (s_retry_num < EXAMPLE_MAXIMUM_RETRY) {
71+
esp_wifi_connect();
72+
s_retry_num++;
73+
ESP_LOGI(TAG, "retry to connect to the AP");
74+
} else {
75+
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
76+
}
77+
ESP_LOGI(TAG,"connect to the AP fail");
78+
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
79+
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
80+
ESP_LOGI(TAG, "static ip:" IPSTR, IP2STR(&event->ip_info.ip));
81+
s_retry_num = 0;
82+
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
83+
}
84+
}
85+
86+
void wifi_init_sta(void)
87+
{
88+
s_wifi_event_group = xEventGroupCreate();
89+
90+
ESP_ERROR_CHECK(esp_netif_init());
91+
92+
ESP_ERROR_CHECK(esp_event_loop_create_default());
93+
94+
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
95+
assert(sta_netif);
96+
97+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
98+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
99+
100+
esp_event_handler_instance_t instance_any_id;
101+
esp_event_handler_instance_t instance_got_ip;
102+
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
103+
ESP_EVENT_ANY_ID,
104+
&event_handler,
105+
sta_netif,
106+
&instance_any_id));
107+
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
108+
IP_EVENT_STA_GOT_IP,
109+
&event_handler,
110+
sta_netif,
111+
&instance_got_ip));
112+
113+
wifi_config_t wifi_config = {
114+
.sta = {
115+
.ssid = EXAMPLE_WIFI_SSID,
116+
.password = EXAMPLE_WIFI_PASS,
117+
/* Setting a password implies station will connect to all security modes including WEP/WPA.
118+
* However these modes are deprecated and not advisable to be used. Incase your Access point
119+
* doesn't support WPA2, these mode can be enabled by commenting below line */
120+
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
121+
122+
.pmf_cfg = {
123+
.capable = true,
124+
.required = false
125+
},
126+
},
127+
};
128+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
129+
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
130+
ESP_ERROR_CHECK(esp_wifi_start() );
131+
132+
ESP_LOGI(TAG, "wifi_init_sta finished.");
133+
134+
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
135+
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
136+
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
137+
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
138+
pdFALSE,
139+
pdFALSE,
140+
portMAX_DELAY);
141+
142+
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
143+
* happened. */
144+
if (bits & WIFI_CONNECTED_BIT) {
145+
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
146+
EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
147+
} else if (bits & WIFI_FAIL_BIT) {
148+
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
149+
EXAMPLE_WIFI_SSID, EXAMPLE_WIFI_PASS);
150+
} else {
151+
ESP_LOGE(TAG, "UNEXPECTED EVENT");
152+
}
153+
154+
/* The event will not be processed after unregister */
155+
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
156+
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
157+
vEventGroupDelete(s_wifi_event_group);
158+
}
159+
160+
void app_main(void)
161+
{
162+
//Initialize NVS
163+
esp_err_t ret = nvs_flash_init();
164+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
165+
ESP_ERROR_CHECK(nvs_flash_erase());
166+
ret = nvs_flash_init();
167+
}
168+
ESP_ERROR_CHECK(ret);
169+
170+
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
171+
wifi_init_sta();
172+
}

0 commit comments

Comments
 (0)