Mistake on this page?
Report an issue in GitHub or email us

Wi-Fi

WiFiInterface class hierarchy

The WifiInterface provides a simple C++ API for connecting to the internet over a Wi-Fi device.

Wi-Fi class reference

Public Member Functions
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 WiFiInterfacewifiInterface ()
 Public Member Functions inherited from 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 EthInterfaceethInterface ()
virtual MeshInterfacemeshInterface ()
virtual CellularBasecellularBase ()
virtual EMACInterfaceemacInterface ()
Static Public Member Functions
static WiFiInterfaceget_default_instance ()
 Static Public Member Functions inherited from NetworkInterface
static NetworkInterfaceget_default_instance ()
Static Protected Member Functions
static WiFiInterfaceget_target_default_instance ()
 Static Protected Member Functions inherited from NetworkInterface
static NetworkInterfaceget_target_default_instance ()
Additional Inherited Members
 Public Types inherited from 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
 Protected Member Functions inherited from NetworkInterface
virtual NetworkStackget_stack ()=0

Usage

To bring up the network interface of an external Wi-Fi device (for example, the ESP8266Interface):

  1. Instantiate an implementation of the WiFiInterface class.
    1. Initialize the AT command parser.
  2. Call the connect() function with an SSID and password for the Wi-Fi network.
    1. Command the Wi-Fi device to connect to network.
  3. Once connected, you can use the WiFiInterface as a target for opening network sockets.

To bring up the network interface of an Ethernet-like driver (for example, the OdinWiFiInterface):

  1. Instantiate an implementation of the WiFiInterface class.
    1. Initialize the Wi-Fi driver for the target.
    2. Initialize network stack (LWIP).
  2. Call the connect() function with an SSID and password for the Wi-Fi network.
    1. Connect the Wi-Fi driver to the Wi-Fi network.
    2. Ensure the network stack acquires the IP address and DNS server address.
  3. Once connected, you can use the WiFiInterface as a target for opening network sockets.

Troubleshooting information

Network interface connect() and set_credential() might return following errors:

Error code Possible reason
NSAPI_ERROR_UNSUPPORTED Security mode is unsupported.
NSAPI_ERROR_PARAMETER Wrong parameters supplied for the given security mode, for example, no password.
NSAPI_ERROR_NO_SSID The device did not find the given Wi-Fi network.
NSAPI_ERROR_AUTH_FAILURE Wrong password given.
NSAPI_ERROR_DEVICE_ERROR Unknown failure happened in the device. The device may not be capable of reporting more descriptive error codes.

Security

For specifying security settings, both connect() and set_credential() have optional parameter nsapi_security_t security, which defines the security mode the device uses. WifiInterface supports the following security modes:

nsapi_security_t Security mode
NSAPI_SECURITY_NONE Not secure. Require no password or encryption.
NSAPI_SECURITY_WEP WEP security. Outdated.
NSAPI_SECURITY_WPA WPA security mode. Obsolete by WPA2; do not use.
NSAPI_SECURITY_WPA2 WPA2 security. Mostly used security mode.
NSAPI_SECURITY_WPA_WPA2 Allows either WPA or WPA2 security.

Please note that settings should match the security mode from the access point. Also, not all drivers support every mode. For most compatible settings, use NSAPI_SECURITY_WPA_WPA2, and set the Wi-Fi access point to allow only WPA2 mode.

Wi-Fi example

Here is an example of an HTTP client program. The program brings up an ESP8266 as the underlying network interface, and uses it to perform an HTTP transaction over a TCPSocket. There are multiple Wi-Fi components that implement the WiFiInterface class. The example below uses the ESP8266Interface and OdinWiFiInterface.

The ESP8266Interface uses AT commands over serial interface to connect to an external Wi-Fi device. The OdinWiFiInterface provides an Ethernet-like driver to the Mbed OS network stack. The network stack uses the driver to connect to Wi-Fi:

/* 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");
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.