My Project
 All Classes Functions Variables Pages
Public Member Functions | Static Public Member Functions | List of all members
WiFi Class Reference

WiFi driver. More...

#include <WiFi.h>

Public Member Functions

 WiFi ()
 Creates and uses a default event handler.
 
 ~WiFi ()
 Deletes the event handler that was used by the class.
 
void addDNSServer (const std::string &ip)
 Add a reference to a DNS server. More...
 
void addDNSServer (const char *ip)
 
void addDNSServer (ip_addr_t ip)
 
void setDNSServer (int numdns, const std::string &ip)
 Set a reference to a DNS server. More...
 
void setDNSServer (int numdns, const char *ip)
 
void setDNSServer (int numdns, ip_addr_t ip)
 
struct in_addr getHostByName (const std::string &hostName)
 Lookup an IP address by host name. More...
 
struct in_addr getHostByName (const char *hostName)
 
uint8_t connectAP (const std::string &ssid, const std::string &password, bool waitForConnection=true, wifi_mode_t mode=WIFI_MODE_STA)
 Connect to an external access point. More...
 
void dump ()
 Dump diagnostics to the log.
 
bool isConnectedToAP ()
 Returns whether wifi is connected to an access point.
 
std::vector< WiFiAPRecordscan ()
 Perform a WiFi scan looking for access points. More...
 
void startAP (const std::string &ssid, const std::string &passwd, wifi_auth_mode_t auth=WIFI_AUTH_OPEN)
 Start being an access point. More...
 
void startAP (const std::string &ssid, const std::string &passwd, wifi_auth_mode_t auth, uint8_t channel, bool ssid_hidden, uint8_t max_connection)
 Start being an access point. More...
 
void setIPInfo (const std::string &ip, const std::string &gw, const std::string &netmask)
 Set the IP info and enable DHCP if ip != 0. If called with ip == 0 then DHCP is enabled. If called with bad values it will do nothing. More...
 
void setIPInfo (const char *ip, const char *gw, const char *netmask)
 
void setIPInfo (uint32_t ip, uint32_t gw, uint32_t netmask)
 Set the IP Info based on the IP address, gateway and netmask. More...
 
void setWifiEventHandler (WiFiEventHandler *wifiEventHandler)
 Set the event handler to use to process detected events. More...
 

Static Public Member Functions

static std::string getApMac ()
 Get the MAC address of the AP interface. More...
 
static tcpip_adapter_ip_info_t getApIpInfo ()
 Get the AP IP Info. More...
 
static std::string getApSSID ()
 Get the AP SSID. More...
 
static std::string getApIp ()
 Get the current ESP32 IP form AP. More...
 
static std::string getApNetmask ()
 Get the current AP netmask. More...
 
static std::string getApGateway ()
 Get the current AP Gateway IP. More...
 
static std::string getMode ()
 Get the WiFi Mode. More...
 
static tcpip_adapter_ip_info_t getStaIpInfo ()
 Get the STA IP Info. More...
 
static std::string getStaMac ()
 Get the MAC address of the STA interface. More...
 
static std::string getStaSSID ()
 Get the STA SSID. More...
 
static std::string getStaIp ()
 Get the current ESP32 IP form STA. More...
 
static std::string getStaNetmask ()
 Get the current STA netmask. More...
 
static std::string getStaGateway ()
 Get the current STA Gateway IP. More...
 

Detailed Description

WiFi driver.

Encapsulate control of WiFi functions.

Here is an example fragment that illustrates connecting to an access point.

#include <WiFi.h>
#include <WiFiEventHandler.h>
class TargetWiFiEventHandler: public WiFiEventHandler {
esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip) {
ESP_LOGD(tag, "MyWiFiEventHandler(Class): staGotIp");
// Do something ...
return ESP_OK;
}
};
WiFi wifi;
TargetWiFiEventHandler *eventHandler = new TargetWiFiEventHandler();
wifi.setWifiEventHandler(eventHandler);
wifi.connectAP("myssid", "mypassword");

Member Function Documentation

void WiFi::addDNSServer ( const std::string &  ip)

Add a reference to a DNS server.

Here we define a server that will act as a DNS server. We can add two DNS servers in total. The first will be the primary, the second will be the backup. The public Google DNS servers are "8.8.8.8" and "8.8.4.4".

For example:

wifi.addDNSServer("8.8.8.8");
wifi.addDNSServer("8.8.4.4");
Parameters
[in]ipThe IP address of the DNS Server.
Returns
N/A.
uint8_t WiFi::connectAP ( const std::string &  ssid,
const std::string &  password,
bool  waitForConnection = true,
wifi_mode_t  mode = WIFI_MODE_STA 
)

Connect to an external access point.

The event handler will be called back with the outcome of the connection.

Parameters
[in]ssidThe network SSID of the access point to which we wish to connect.
[in]passwordThe password of the access point to which we wish to connect.
[in]waitForConnectionBlock until the connection has an outcome.
[in]modeWIFI_MODE_AP for normal or WIFI_MODE_APSTA if you want to keep an Access Point running while you connect
Returns
ESP_OK if we are now connected and wifi_err_reason_t if not.
std::string WiFi::getApGateway ( )
static

Get the current AP Gateway IP.

Returns
The Gateway IP.
std::string WiFi::getApIp ( )
static

Get the current ESP32 IP form AP.

Returns
The ESP32 IP.
tcpip_adapter_ip_info_t WiFi::getApIpInfo ( )
static

Get the AP IP Info.

Returns
The AP IP Info.
std::string WiFi::getApMac ( )
static

Get the MAC address of the AP interface.

Returns
The MAC address of the AP interface.
std::string WiFi::getApNetmask ( )
static

Get the current AP netmask.

Returns
The Netmask IP.
std::string WiFi::getApSSID ( )
static

Get the AP SSID.

Returns
The AP SSID.
struct in_addr WiFi::getHostByName ( const std::string &  hostName)

Lookup an IP address by host name.

Parameters
[in]hostNameThe hostname to resolve.
Returns
The IP address of the host or 0.0.0.0 if not found.
std::string WiFi::getMode ( )
static

Get the WiFi Mode.

Returns
The WiFi Mode.
std::string WiFi::getStaGateway ( )
static

Get the current STA Gateway IP.

Returns
The Gateway IP.
std::string WiFi::getStaIp ( )
static

Get the current ESP32 IP form STA.

Returns
The ESP32 IP.
tcpip_adapter_ip_info_t WiFi::getStaIpInfo ( )
static

Get the STA IP Info.

Returns
The STA IP Info.
std::string WiFi::getStaMac ( )
static

Get the MAC address of the STA interface.

Returns
The MAC address of the STA interface.
std::string WiFi::getStaNetmask ( )
static

Get the current STA netmask.

Returns
The Netmask IP.
std::string WiFi::getStaSSID ( )
static

Get the STA SSID.

Returns
The STA SSID.
std::vector< WiFiAPRecord > WiFi::scan ( )

Perform a WiFi scan looking for access points.

An access point scan is performed and a vector of WiFi access point records is built and returned with one record per found scan instance. The scan is performed in a blocking fashion and will not return until the set of scanned access points has been built.

Returns
A vector of WiFiAPRecord instances.
void WiFi::setDNSServer ( int  numdns,
const std::string &  ip 
)

Set a reference to a DNS server.

Here we define a server that will act as a DNS server. We use numdns to specify which DNS server to set

For example:

wifi.setDNSServer(0, "8.8.8.8");
wifi.setDNSServer(1, "8.8.4.4");
Parameters
[in]numdnsThe DNS number we wish to set
[in]ipThe IP address of the DNS Server.
Returns
N/A.
void WiFi::setIPInfo ( const std::string &  ip,
const std::string &  gw,
const std::string &  netmask 
)

Set the IP info and enable DHCP if ip != 0. If called with ip == 0 then DHCP is enabled. If called with bad values it will do nothing.

Do not call this method if we are being an access point ourselves.

For example, prior to calling connectAP() we could invoke:

myWifi.setIPInfo("192.168.1.99", "192.168.1.1", "255.255.255.0");
Parameters
[in]ipIP address value.
[in]gwGateway value.
[in]netmaskNetmask value.
Returns
N/A.
void WiFi::setIPInfo ( uint32_t  ip,
uint32_t  gw,
uint32_t  netmask 
)

Set the IP Info based on the IP address, gateway and netmask.

Parameters
[in]ipThe IP address of our ESP32.
[in]gwThe gateway we should use.
[in]netmaskOur TCP/IP netmask value.
void WiFi::setWifiEventHandler ( WiFiEventHandler wifiEventHandler)

Set the event handler to use to process detected events.

Parameters
[in]wifiEventHandlerThe class that will be used to process events.
void WiFi::startAP ( const std::string &  ssid,
const std::string &  password,
wifi_auth_mode_t  auth = WIFI_AUTH_OPEN 
)

Start being an access point.

Parameters
[in]ssidThe SSID to use to advertize for stations.
[in]passwordThe password to use for station connections.
[in]authThe authorization mode for access to this access point. Options are:
  • WIFI_AUTH_OPEN
  • WIFI_AUTH_WPA_PSK
  • WIFI_AUTH_WPA2_PSK
  • WIFI_AUTH_WPA_WPA2_PSK
  • WIFI_AUTH_WPA2_ENTERPRISE
  • WIFI_AUTH_WEP
Returns
N/A.
void WiFi::startAP ( const std::string &  ssid,
const std::string &  password,
wifi_auth_mode_t  auth,
uint8_t  channel,
bool  ssid_hidden,
uint8_t  max_connection 
)

Start being an access point.

Parameters
[in]ssidThe SSID to use to advertize for stations.
[in]passwordThe password to use for station connections.
[in]authThe authorization mode for access to this access point. Options are:
  • WIFI_AUTH_OPEN
  • WIFI_AUTH_WPA_PSK
  • WIFI_AUTH_WPA2_PSK
  • WIFI_AUTH_WPA_WPA2_PSK
  • WIFI_AUTH_WPA2_ENTERPRISE
  • WIFI_AUTH_WEP
[in]channelfrom the access point.
[in]isthe ssid hidden, ore not.
[in]limitingnumber of clients.
Returns
N/A.

The documentation for this class was generated from the following files: