WiFiInterface 类层次结构
WifiInterface 提供了一个简单的 C++ API,用于通过 Wi-Fi 设备连接到互联网。
WiFiInterface 类参考
公共成员函数 | |
virtual nsapi_error_t | set_credentials (const char *ssid, const char *pass, nsapi_security_t security=NSAPI_SECURITY_NONE)=0 |
virtual nsapi_error_t | set_channel (uint8_t channel)=0 |
virtual int8_t | get_rssi ()=0 |
virtual nsapi_error_t | connect (const char *ssid, const char *pass, nsapi_security_t security=NSAPI_SECURITY_NONE, uint8_t channel=0)=0 |
virtual nsapi_error_t | connect ()=0 |
virtual nsapi_error_t | disconnect ()=0 |
virtual nsapi_size_or_error_t | scan (WiFiAccessPoint *res, nsapi_size_t count)=0 |
virtual WiFiInterface * | wifiInterface () |
公共成员函数继承自 NetworkInterface | |
virtual const char * | get_mac_address () |
virtual const char * | get_ip_address () |
virtual const char * | get_netmask () |
virtual const char * | get_gateway () |
virtual nsapi_error_t | set_network (const char *ip_address, const char *netmask, const char *gateway) |
virtual nsapi_error_t | set_dhcp (bool dhcp) |
virtual nsapi_error_t | gethostbyname (const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC) |
virtual nsapi_value_or_error_t | gethostbyname_async (const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC) |
virtual nsapi_error_t | gethostbyname_async_cancel (int id) |
virtual nsapi_error_t | add_dns_server (const SocketAddress &address) |
virtual void | attach (mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) |
virtual nsapi_connection_status_t | get_connection_status () const |
virtual nsapi_error_t | set_blocking (bool blocking) |
virtual EthInterface * | ethInterface () |
virtual MeshInterface * | meshInterface () |
virtual CellularBase * | cellularBase () |
virtual EMACInterface * | emacInterface () |
静态公共成员函数 | |
static WiFiInterface * | get_default_instance () |
静态公共成员函数继承自 NetworkInterface | |
static NetworkInterface * | get_default_instance () |
静态保护的成员函数 | |
static WiFiInterface * | get_target_default_instance () |
静态保护的成员函数继承自 NetworkInterface | |
static NetworkInterface * | get_target_default_instance () |
其他继承成员 | |
继承自的公共类型 NetworkInterface | |
typedef mbed::Callback< void(nsapi_error_t result, SocketAddress *address)> | hostbyname_cb_t |
Public Types inherited from DNS | |
typedef mbed::Callback< void(nsapi_error_t result, SocketAddress *address)> | hostbyname_cb_t |
受保护的成员函数继承自 NetworkInterface | |
virtual NetworkStack * | get_stack ()=0 |
调出外部 Wi-Fi 设备的网络接口(例如,ESP8266 接口):
调出类似以太网的驱动程序的网络接口(例如,OdinWiFiInterface):
网络接口 connect() 和 set_credential() 可能会返回以下错误:
错误代码 | 可能的原因 |
---|---|
NSAPI_ERROR_UNSUPPORTED |
不支持安全模式。 |
NSAPI_ERROR_PARAMETER |
为给定安全模式提供的参数错误,例如,没有密码。 |
NSAPI_ERROR_NO_SSID |
该设备未找到给定的 Wi-Fi 网络。 |
NSAPI_ERROR_AUTH_FAILURE |
密码错误。 |
NSAPI_ERROR_DEVICE_ERROR |
设备中发生了未知故障。设备可能无法报告更多描述性错误代码。 |
为了指定安全性设置,connect() 和 set_credential() 都具有可选参数 nsapi_security_t security,它定义了设备使用的安全模式。 WifiInterface 支持以下安全模式:
nsapi_security_t |
安全模式 |
---|---|
NSAPI_SECURITY_NONE |
不安全。不需要密码或加密。 |
NSAPI_SECURITY_WEP |
WEP 安全性。过时了。 |
NSAPI_SECURITY_WPA |
WPA 安全模式。WPA 已淘汰;不使用。 |
NSAPI_SECURITY_WPA2 |
WPA2 安全性。主要用于安全模式。 |
NSAPI_SECURITY_WPA_WPA2 |
允许 WPA 或 WPA2 安全性。 |
请注意,设置应与接入点的安全模式匹配。此外,并非所有驱动程序都支持每种模式 对于大多数兼容设置,请使用 NSAPI_SECURITY_WPA_WPA2,并将 Wi-Fi 接入点设置为仅允许 WPA2 模式。
以下是 HTTP 客户端程序的示例。该程序将 ESP8266 作为底层网络接口,并使用它通过 TCPSocket 执行 HTTP 事务。有多个 Wi-Fi 组件实现了 WiFiInterface 类。以下示例使用 ESP8266Interface 和 OdinWiFiInterface。
ESP8266 接口通过串行接口使用 AT 命令连接到外部 Wi-Fi 设备。OdinWiFiInterface 为 Mbed OS 网络栈提供类似以太网的驱动程序。网络栈使用驱动程序连接到 Wi-Fi:
main.cpp 导入到 Mbed IDE
/* WiFi Example
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "TCPSocket.h"
#if TARGET_UBLOX_EVK_ODIN_W2
#include "OdinWiFiInterface.h"
OdinWiFiInterface wifi;
#else
#if !TARGET_FF_ARDUINO
#error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
#endif
#include "ESP8266Interface.h"
ESP8266Interface wifi(D1, D0);
#endif
const char *sec2str(nsapi_security_t sec)
{
switch (sec) {
case NSAPI_SECURITY_NONE:
return "None";
case NSAPI_SECURITY_WEP:
return "WEP";
case NSAPI_SECURITY_WPA:
return "WPA";
case NSAPI_SECURITY_WPA2:
return "WPA2";
case NSAPI_SECURITY_WPA_WPA2:
return "WPA/WPA2";
case NSAPI_SECURITY_UNKNOWN:
default:
return "Unknown";
}
}
void scan_demo(WiFiInterface *wifi)
{
WiFiAccessPoint *ap;
printf("Scan:\r\n");
int count = wifi->scan(NULL,0);
/* Limit number of network arbitrary to 15 */
count = count < 15 ? count : 15;
ap = new WiFiAccessPoint[count];
count = wifi->scan(ap, count);
for (int i = 0; i < count; i++)
{
printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
}
printf("%d networks available.\r\n", count);
delete[] ap;
}
void http_demo(NetworkInterface *net)
{
TCPSocket socket;
printf("Sending HTTP request to www.arm.com...\r\n");
// Open a socket on the network interface, and create a TCP connection to www.arm.com
socket.open(net);
socket.connect("www.arm.com", 80);
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
// Close the socket to return its memory and bring down the network interface
socket.close();
}
int main()
{
printf("WiFi example\r\n\r\n");
scan_demo(&wifi);
printf("\r\nConnecting...\r\n");
int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
if (ret != 0) {
printf("\r\nConnection error\r\n");
return -1;
}
printf("Success\r\n\r\n");
printf("MAC: %s\r\n", wifi.get_mac_address());
printf("IP: %s\r\n", wifi.get_ip_address());
printf("Netmask: %s\r\n", wifi.get_netmask());
printf("Gateway: %s\r\n", wifi.get_gateway());
printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
http_demo(&wifi);
wifi.disconnect();
printf("\r\nDone\r\n");
}