在Zephyr ESP32 wifi驅(qū)動簡析一文中簡要分析了esp32 wifi如何集成進Zephyr,本文接著說明要如何使用esp32 wifi。
從前文可知由于esp32 wifi的驅(qū)動和ethernt L2對接,因此在zephyr下只要通過簡單的配置就可以在應(yīng)用層使用socket通過wifi進行網(wǎng)絡(luò)通信,而不需要去關(guān)心esp32 wifi驅(qū)動的細節(jié)。
配置方法
進行下面配置后開機,esp32就會自動取連接配置好的WIFI,并拿到IP地址,之后應(yīng)用通過socket編程即可
1
2
# 配置啟動WIFI,使用ESP32 WIFI,必須
CONFIG_WIFI=y
CONFIG_WIFI_ESP32=y
# 配置使用網(wǎng)絡(luò)和L2 Ethernet, 必須
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
運行l(wèi)og, 最后幾句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/結(jié)果
Net Shell測試ping電腦結(jié)果:
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進行集成封裝,如果應(yīng)用中要做wifi管理,需要應(yīng)用程序直接調(diào)用esp hal API來實現(xiàn)。例如,如果沒有配置CONFIG_ESP32_WIFI_STA_AUTO=y, 就需要在應(yīng)用代碼中加入下面流程,才能完成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”);
}
}
如果應(yīng)用要用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
+關(guān)注
關(guān)注
9文章
2067瀏覽量
154042 -
IP
+關(guān)注
關(guān)注
5文章
1664瀏覽量
149341 -
電腦
+關(guān)注
關(guān)注
15文章
1681瀏覽量
68692 -
代碼
+關(guān)注
關(guān)注
30文章
4753瀏覽量
68368
原文標(biāo)題:Zephyr ESP32 wifi使用簡述
文章出處:【微信號:ZephyrProject,微信公眾號:ZephyrProject】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論