在Zephyr ESP32 wifi驱动简析一文中简要分析了esp32 wifi如何集成进Zephyr,本文接着说明要如何使用esp32 wifi。
从前文可知由于esp32 wifi的驱动和ethernt L2对接,因此在zephyr下只要通过简单的配置就可以在应用层使用socket通过wifi进行网络通信,而不需要去关心esp32 wifi驱动的细节。
配置方法
进行下面配置后开机,esp32就会自动取连接配置好的WIFI,并拿到IP地址,之后应用通过socket编程即可
1
2
# 配置启动WIFI,使用ESP32 WIFI,必须
CONFIG_WIFI=y
CONFIG_WIFI_ESP32=y
CONFIG_NETWORKING=y
CONFIG_NET_L2_ETHERNET=y
# IPV4/IPV6任选, 必须
CONFIG_NET_IPV6=n
CONFIG_NET_IPV4=y
# DHCP开启,可选
CONFIG_NET_DHCPV4=y
# 开启DHCP后要使用NET_MGMT通知DHCP完成
CONFIG_NET_MGMT=y
CONFIG_NET_MGMT_EVENT=y
# hal esp的wpa_supplicant依赖mbedtls,必须
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_ENTROPY_ENABLED=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED=y
CONFIG_MBEDTLS_ECP_ALL_ENABLED=y
# 配置连接WIFI的SSID和密码,必须
CONFIG_ESP32_WIFI_SSID=“myssid”
CONFIG_ESP32_WIFI_PASSWORD=“mypassword”
# 配置自动启动WIFI连接,非必须,如果不配置需要另外添加代码使用esp hal API进行连接
CONFIG_ESP32_WIFI_STA_AUTO=y
示例测试
使用samples etsocketsdumb_http_server 示例来演示esp32 作为server,电脑可以通过wifi访问该server.
电脑IP 192.168.101.108
Zephyr server IP 192.168.101.105
修改配置
1
2
3
4
5
6
# Network driver config
CONFIG_TEST_RANDOM_GENERATOR=y
# Network address config
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_WIFI=y
CONFIG_WIFI_ESP32=y
CONFIG_HEAP_MEM_POOL_SIZE=98304
CONFIG_NETWORKING=y
CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_IPV6=n
CONFIG_NET_IPV4=y
CONFIG_NET_DHCPV4=y
CONFIG_NET_TCP=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_MGMT=y
CONFIG_NET_MGMT_EVENT=y
CONFIG_NET_LOG=y
CONFIG_NET_SHELL=y
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_ENTROPY_ENABLED=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED=y
CONFIG_MBEDTLS_ECP_ALL_ENABLED=y
CONFIG_ESP32_WIFI_SSID=“AP8”
CONFIG_ESP32_WIFI_PASSWORD=“2885887452”
CONFIG_ESP32_WIFI_STA_AUTO=y
# Networking tweaks
# Required to handle large number of consecutive connections,
# e.g. when testing with ApacheBench.
CONFIG_NET_TCP_TIME_WAIT_DELAY=0
# Network debug config
CONFIG_NET_LOG=y
#CONFIG_NET_SHELL=y
编译代码并下载
1
2
west build -p -b esp32 zephyrproject/zephyr/samples/net/sockets/dumb_http_server
west flash --esp-device /dev/ttyS11
运行log, 最后几句Connection,就是PC浏览器在访问server
1
2
3
4
5
uart:~$ I (828) wifi:wifi driver task: 3ffb2280, prio:2, stack:3584, core=0
I (829) wifi:wifi firmware version: 1865b55
I (829) wifi:wifi certification version: v7.0
I (831) wifi:config NVS flash: disabled
I (835) wifi:config nano formating: disabled
I (839) wifi:Init data frame dynamic rx buffer num: 32
I (843) wifi:Init management frame dynamic rx buffer num: 32
I (849) wifi:Init management short buffer num: 32
I (853) wifi:Init dynamic tx buffer num: 32
I (857) wifi:Init static rx buffer size: 1600
I (861) wifi:Init static rx buffer num: 10
I (865) wifi:Init dynamic rx buffer num: 32
phy_version: 4500, 0cd6843, Sep 17 2020, 1507, 0, 2
I (1001) wifi:mode : softAP (c457f8:dd)
I (1002) wifi:Total power save buffer number: 16
I (1002) wifi:Init max length of beacon: 752/752
I (1007) wifi:Init max length of beacon: 752/752
I (1012) wifi:mode : sta (c457f8:dc)
*** Booting Zephyr OS build v2.6.0-rc1-300-g6ce0f2ee6606 ***
Single-threaded dumb HTTP server waits for a connection on port 8080.。。
I (1137) wifi《1,0》, old:《1,1》, ap:《255,255》, sta:《1,0》, prof:1
I (1785) wifi init -》 auth (b0)
I (1831) wifi auth -》 assoc (0)
I (1835) wifi assoc -》 run (10)
[0001.020,000] 《inf》 net_config: Initializing network
[0001.020,000] 《inf》 net_config: Waiting interface 1 (0x3ffb01f8) to be up.。。
I (2044) wifi:connected with AP8, aid = 1, channel 1, BW20, bssid = c0dd89:7f
I (2044) wifi WPA2-PSK, phy: bgn, rssi: -53
I (2047) wifi:pm start, type: 1
[0001.023,000] 《inf》 esp_event: WIFI_EVENT_STA_START
[0001.023,000] 《inf》 net_config: Interface 1 (0x3ffb01f8) coming up
[0001.023,000] 《inf》 net_config: Running dhcpv4 client.。。
uart:~$ I (2120) wifi:AP‘s beacon interval = 102400 us, DTIM period = 1
[0002.051,000] 《inf》 esp_event: WIFI_EVENT_STA_CONNECTED
[0006.220,000] 《inf》 net_dhcpv4: Received: 192.168.101.105
[0006.220,000] 《inf》 net_config: IPv4 address: 192.168.101.105
[0006.220,000] 《inf》 net_config: Lease time: 7200 seconds
[0006.220,000] 《inf》 net_config: Subnet: 255.255.255.0
[0006.220,000] 《inf》 net_config: Router: 192.168.101.1
uart:~$ Connection #0 from 192.168.101.108
Connection from 192.168.101.108 closed
Connection #1 from 192.168.101.108
Connection from 192.168.101.108 closed
电脑浏览器访问http://192.168.101.105:8080/结果
Net Shell测试ping电脑结果:
1
2
3
4
5
uart:~$ net ping 192.168.101.108
PING 192.168.101.108
28 bytes from 192.168.101.108 to 192.168.101.105: icmp_seq=0 ttl=128 time=153 ms
28 bytes from 192.168.101.108 to 192.168.101.105: icmp_seq=1 ttl=128 time=30 ms
28 bytes from 192.168.101.108 to 192.168.101.105: icmp_seq=2 ttl=128 time=7 ms
其它
再次说明由于Zephyr没有对esp32的scan/connect/disconnect进行集成封装,如果应用中要做wifi管理,需要应用程序直接调用esp hal API来实现。例如,如果没有配置CONFIG_ESP32_WIFI_STA_AUTO=y, 就需要在应用代码中加入下面流程,才能完成wifi连接
1
if (!IS_ENABLED(CONFIG_ESP32_WIFI_STA_AUTO)) {
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_ESP32_WIFI_SSID,
.password = CONFIG_ESP32_WIFI_PASSWORD,
},
};
esp_err_t ret = esp_wifi_set_mode(WIFI_MODE_STA);
ret |= esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
ret |= esp_wifi_connect();
if (ret != ESP_OK) {
LOG_ERR(“connection failed”);
}
}
如果应用要用socket编程,需要添加下面配置项
1
2
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
参考
https://docs.zephyrproject.org/latest/samples/boards/esp32/wifi_station/README.html
编辑:jq
-
PC
+关注
关注
9文章
2065浏览量
153999 -
IP
+关注
关注
5文章
1642浏览量
149323 -
电脑
+关注
关注
15文章
1680浏览量
68662 -
代码
+关注
关注
30文章
4741浏览量
68325
原文标题:Zephyr ESP32 wifi使用简述
文章出处:【微信号:ZephyrProject,微信公众号:ZephyrProject】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论