Skip to content

Commit 7fe8b71

Browse files
sylvioalvesnashif
authored andcommitted
wifi: esp32: add board wifi station sample
add esp32 wifi station board example Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
1 parent e16e606 commit 7fe8b71

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
5+
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
6+
project(esp32_wifi_station)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
.. _esp32_wifi_station:
2+
3+
Espressif ESP32 WiFi Station
4+
############################
5+
6+
Overview
7+
********
8+
9+
This sample demonstrates how to use ESP32 to connect to a WiFi network as a station device.
10+
To configure WiFi credentials, edit ``prj.conf``.
11+
Enabling the ``net_shell`` module provides a set of commands to test the connection.
12+
See :ref:`network shell <net_shell>` for more details.
13+
14+
Building and Running
15+
********************
16+
17+
Make sure you have the ESP32 connected over USB port.
18+
19+
The sample can be built and flashed as follows:
20+
21+
.. code-block:: console
22+
23+
west build -b esp32 samples/boards/esp32/wifi_station
24+
west flash --esp-device /dev/ttyUSB0
25+
26+
Sample Output
27+
=============
28+
29+
To check output of this sample, any serial console program can be used (i.e. on Linux minicom, putty, screen, etc)
30+
This example uses ``picocom`` on the serial port ``/dev/ttyUSB0``:
31+
32+
.. code-block:: console
33+
34+
$ picocom /dev/ttyUSB0 - 115200
35+
36+
.. code-block:: console
37+
38+
I (288) boot: Loaded app from partition at offset 0x10000
39+
I (288) boot: Disabling RNG early entropy source...
40+
I (611) wifi:wifi driver task: 3ffb2be8, prio:2, stack:3584, core=0
41+
I (613) wifi:wifi firmware version: 9c89486
42+
I (613) wifi:wifi certification version: v7.0
43+
I (614) wifi:config NVS flash: disabled
44+
I (618) wifi:config nano formating: disabled
45+
I (622) wifi:Init data frame dynamic rx buffer num: 32
46+
I (627) wifi:Init management frame dynamic rx buffer num: 32
47+
I (632) wifi:Init management short buffer num: 32
48+
I (636) wifi:Init dynamic tx buffer num: 32
49+
I (640) wifi:Init static rx buffer size: 1600
50+
I (645) wifi:Init static rx buffer num: 10
51+
I (648) wifi:Init dynamic rx buffer num: 32
52+
phy_version: 4350, 18c5e94, Jul 27 2020, 21:04:07, 0, 2
53+
I (783) wifi:mode : softAP (24:6f:28:80:32:e9)
54+
I (784) wifi:Total power save buffer number: 16
55+
I (784) wifi:Init max length of beacon: 752/752
56+
I (788) wifi:Init max length of beacon: 752/752
57+
*** Booting Zephyr OS build zephyr-v2.4.0-49-g4da59e1678f7 ***
58+
I (798) wifi:mode : sta (24:6f:28:80:32:e8)
59+
I (1046) wifi:new:<4,1>, old:<1,1>, ap:<255,255>, sta:<4,1>, prof:1
60+
I (1694) wifi:state: init -> auth (b0)
61+
I (1711) wifi:state: auth -> assoc (0)
62+
I (1717) wifi:state: assoc -> run (10)
63+
I (1745) wifi:connected with myssid, aid = 4, channel 4, 40U, bssid = d8:07:b6:dd:47:7a
64+
I (1745) wifi:security: WPA2-PSK, phy: bgn, rssi: -57
65+
I (1747) wifi:pm start, type: 1
66+
67+
esp_event: WIFI STA start
68+
esp_event: WIFI STA connected
69+
I (1813) wifi:AP's beacon interval = 102400 us, DTIM period = 1
70+
net_dhcpv4: Received: 192.168.68.102
71+
esp32_wifi_sta: Your address: 192.168.68.102
72+
esp32_wifi_sta: Lease time: 7200 seconds
73+
esp32_wifi_sta: Subnet: 255.255.255.0
74+
esp32_wifi_sta: Router: 192.168.68.1
75+
76+
Sample console interaction
77+
==========================
78+
79+
If the :option:`CONFIG_NET_SHELL` option is set, network shell functions
80+
can be used to check internet connection.
81+
82+
.. code-block:: console
83+
84+
shell> net ping 8.8.8.8
85+
PING 8.8.8.8
86+
28 bytes from 8.8.8.8 to 192.168.68.102: icmp_seq=0 ttl=118 time=19 ms
87+
28 bytes from 8.8.8.8 to 192.168.68.102: icmp_seq=1 ttl=118 time=16 ms
88+
28 bytes from 8.8.8.8 to 192.168.68.102: icmp_seq=2 ttl=118 time=21 ms
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CONFIG_WIFI=y
2+
CONFIG_WIFI_ESP32=y
3+
CONFIG_HEAP_MEM_POOL_SIZE=98304
4+
5+
CONFIG_NETWORKING=y
6+
CONFIG_NET_L2_ETHERNET=y
7+
8+
CONFIG_NET_IPV6=n
9+
CONFIG_NET_IPV4=y
10+
CONFIG_NET_DHCPV4=y
11+
12+
CONFIG_NET_MGMT=y
13+
CONFIG_NET_MGMT_EVENT=y
14+
15+
CONFIG_NET_LOG=y
16+
CONFIG_NET_SHELL=n
17+
18+
CONFIG_MBEDTLS=y
19+
CONFIG_MBEDTLS_ENTROPY_ENABLED=y
20+
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED=y
21+
CONFIG_MBEDTLS_ECP_ALL_ENABLED=y
22+
23+
CONFIG_ESP32_WIFI_SSID="myssid"
24+
CONFIG_ESP32_WIFI_PASSWORD="mypassword"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sample:
2+
name: Espressif ESP32 WiFi Station
3+
tests:
4+
sample.board.esp32:
5+
platform_allow: esp32
6+
tags: esp32
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2020 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <sys/printk.h>
9+
#include <esp_wifi.h>
10+
#include <esp_timer.h>
11+
#include <esp_event.h>
12+
13+
#include <net/net_if.h>
14+
#include <net/net_core.h>
15+
#include <net/net_context.h>
16+
#include <net/net_mgmt.h>
17+
18+
#include <logging/log.h>
19+
LOG_MODULE_REGISTER(esp32_wifi_sta, LOG_LEVEL_DBG);
20+
21+
static struct net_mgmt_event_callback dhcp_cb;
22+
23+
static void handler_cb(struct net_mgmt_event_callback *cb,
24+
uint32_t mgmt_event, struct net_if *iface)
25+
{
26+
if (mgmt_event != NET_EVENT_IPV4_DHCP_BOUND) {
27+
return;
28+
}
29+
30+
char buf[NET_IPV4_ADDR_LEN];
31+
32+
LOG_INF("Your address: %s",
33+
log_strdup(net_addr_ntop(AF_INET,
34+
&iface->config.dhcpv4.requested_ip,
35+
buf, sizeof(buf))));
36+
LOG_INF("Lease time: %u seconds",
37+
iface->config.dhcpv4.lease_time);
38+
LOG_INF("Subnet: %s",
39+
log_strdup(net_addr_ntop(AF_INET,
40+
&iface->config.ip.ipv4->netmask,
41+
buf, sizeof(buf))));
42+
LOG_INF("Router: %s",
43+
log_strdup(net_addr_ntop(AF_INET,
44+
&iface->config.ip.ipv4->gw,
45+
buf, sizeof(buf))));
46+
}
47+
48+
void main(void)
49+
{
50+
struct net_if *iface;
51+
52+
net_mgmt_init_event_callback(&dhcp_cb, handler_cb,
53+
NET_EVENT_IPV4_DHCP_BOUND);
54+
55+
net_mgmt_add_event_callback(&dhcp_cb);
56+
57+
iface = net_if_get_default();
58+
if (!iface) {
59+
LOG_ERR("wifi interface not available");
60+
return;
61+
}
62+
63+
net_dhcpv4_start(iface);
64+
65+
if (!IS_ENABLED(CONFIG_ESP32_WIFI_STA_AUTO)) {
66+
wifi_config_t wifi_config = {
67+
.sta = {
68+
.ssid = CONFIG_ESP32_WIFI_SSID,
69+
.password = CONFIG_ESP32_WIFI_PASSWORD,
70+
},
71+
};
72+
73+
esp_err_t ret = esp_wifi_set_mode(WIFI_MODE_STA);
74+
75+
ret |= esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
76+
ret |= esp_wifi_connect();
77+
if (ret != ESP_OK) {
78+
LOG_ERR("connection failed");
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)