My Project
 All Classes Functions Variables Pages
WiFi.h
1 /*
2  * WiFi.h
3  *
4  * Created on: Feb 25, 2017
5  * Author: kolban
6  */
7 
8 #ifndef MAIN_WIFI_H_
9 #define MAIN_WIFI_H_
10 #include "sdkconfig.h"
11 
12 #include <string>
13 #include <vector>
14 #include <mdns.h>
15 #include <esp_err.h>
16 #include "FreeRTOS.h"
17 #include "WiFiEventHandler.h"
18 
22 /*
23 class MDNS {
24 public:
25  MDNS();
26  ~MDNS();
27  void serviceAdd(const std::string& service, const std::string& proto, uint16_t port);
28  void serviceInstanceSet(const std::string& service, const std::string& proto, const std::string& instance);
29  void servicePortSet(const std::string& service, const std::string& proto, uint16_t port);
30  void serviceRemove(const std::string& service, const std::string& proto);
31  void setHostname(const std::string& hostname);
32  void setInstance(const std::string& instance);
33  // If we the above functions with a basic char*, a copy would be created into an std::string,
34  // making the whole thing require twice as much processing power and speed
35  void serviceAdd(const char* service, const char* proto, uint16_t port);
36  void serviceInstanceSet(const char* service, const char* proto, const char* instance);
37  void servicePortSet(const char* service, const char* proto, uint16_t port);
38  void serviceRemove(const char* service, const char* proto);
39  void setHostname(const char* hostname);
40  void setInstance(const char* instance);
41 
42 private:
43  mdns_server_t* m_mdns_server = nullptr;
44 
45 };
46 */
47 
48 class WiFiAPRecord {
49 public:
50  friend class WiFi;
51 
56  wifi_auth_mode_t getAuthMode() {
57  return m_authMode;
58  }
59 
64  int8_t getRSSI() {
65  return m_rssi;
66  }
67 
72  std::string getSSID() {
73  return m_ssid;
74  }
75 
76  std::string toString();
77 
78 private:
79  uint8_t m_bssid[6];
80  int8_t m_rssi;
81  std::string m_ssid;
82  wifi_auth_mode_t m_authMode;
83 
84 };
85 
111 class WiFi {
112 private:
113  static esp_err_t eventHandler(void* ctx, system_event_t* event);
114  void init();
115  uint32_t ip;
116  uint32_t gw;
117  uint32_t netmask;
118  WiFiEventHandler* m_pWifiEventHandler;
119  uint8_t m_dnsCount = 0;
120  bool m_eventLoopStarted;
121  bool m_initCalled;
122  uint8_t m_apConnectionStatus; // ESP_OK = we are connected to an access point. Otherwise receives wifi_err_reason_t.
123  FreeRTOS::Semaphore m_connectFinished = FreeRTOS::Semaphore("ConnectFinished");
124 
125 public:
126  WiFi();
127  ~WiFi();
128  void addDNSServer(const std::string& ip);
129  void addDNSServer(const char* ip);
130  void addDNSServer(ip_addr_t ip);
131  void setDNSServer(int numdns, const std::string& ip);
132  void setDNSServer(int numdns, const char* ip);
133  void setDNSServer(int numdns, ip_addr_t ip);
134  struct in_addr getHostByName(const std::string& hostName);
135  struct in_addr getHostByName(const char* hostName);
136  uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection = true, wifi_mode_t mode = WIFI_MODE_STA);
137  void dump();
138  bool isConnectedToAP();
139  static std::string getApMac();
140  static tcpip_adapter_ip_info_t getApIpInfo();
141  static std::string getApSSID();
142  static std::string getApIp();
143  static std::string getApNetmask();
144  static std::string getApGateway();
145  static std::string getMode();
146  static tcpip_adapter_ip_info_t getStaIpInfo();
147  static std::string getStaMac();
148  static std::string getStaSSID();
149  static std::string getStaIp();
150  static std::string getStaNetmask();
151  static std::string getStaGateway();
152  std::vector<WiFiAPRecord> scan();
153  void startAP(const std::string& ssid, const std::string& passwd, wifi_auth_mode_t auth = WIFI_AUTH_OPEN);
154  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);
155  void setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask);
156  void setIPInfo(const char* ip, const char* gw, const char* netmask);
157  void setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask);
158  void setWifiEventHandler(WiFiEventHandler* wifiEventHandler);
159 
160 };
161 
162 #endif /* MAIN_WIFI_H_ */
static std::string getApMac()
Get the MAC address of the AP interface.
Definition: WiFi.cpp:285
static std::string getApSSID()
Get the AP SSID.
Definition: WiFi.cpp:299
std::string toString()
Return a string representation of the WiFi access point record.
Definition: WiFi.cpp:732
static tcpip_adapter_ip_info_t getStaIpInfo()
Get the STA IP Info.
Definition: WiFi.cpp:392
static std::string getApNetmask()
Get the current AP netmask.
Definition: WiFi.cpp:322
void setWifiEventHandler(WiFiEventHandler *wifiEventHandler)
Set the event handler to use to process detected events.
Definition: WiFi.cpp:656
void addDNSServer(const std::string &ip)
Add a reference to a DNS server.
Definition: WiFi.cpp:90
static std::string getMode()
Get the WiFi Mode.
Definition: WiFi.cpp:370
static std::string getStaNetmask()
Get the current STA netmask.
Definition: WiFi.cpp:415
void setDNSServer(int numdns, const std::string &ip)
Set a reference to a DNS server.
Definition: WiFi.cpp:128
static std::string getApGateway()
Get the current AP Gateway IP.
Definition: WiFi.cpp:333
static std::string getApIp()
Get the current ESP32 IP form AP.
Definition: WiFi.cpp:311
WiFi driver.
Definition: WiFi.h:111
wifi_auth_mode_t getAuthMode()
Get the auth mode.
Definition: WiFi.h:56
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.
Definition: WiFi.cpp:680
void startAP(const std::string &ssid, const std::string &passwd, wifi_auth_mode_t auth=WIFI_AUTH_OPEN)
Start being an access point.
Definition: WiFi.cpp:587
static std::string getStaSSID()
Get the STA SSID.
Definition: WiFi.cpp:452
int8_t getRSSI()
Get the RSSI.
Definition: WiFi.h:64
static std::string getStaGateway()
Get the current STA Gateway IP.
Definition: WiFi.cpp:427
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.
Definition: WiFi.cpp:159
std::vector< WiFiAPRecord > scan()
Perform a WiFi scan looking for access points.
Definition: WiFi.cpp:508
WiFi()
Creates and uses a default event handler.
Definition: WiFi.cpp:49
~WiFi()
Deletes the event handler that was used by the class.
Definition: WiFi.cpp:65
Manage mDNS server.
Definition: WiFi.h:48
static std::string getStaIp()
Get the current ESP32 IP form STA.
Definition: WiFi.cpp:403
static std::string getStaMac()
Get the MAC address of the STA interface.
Definition: WiFi.cpp:439
bool isConnectedToAP()
Returns whether wifi is connected to an access point.
Definition: WiFi.cpp:230
Definition: FreeRTOS.h:31
WiFi state event handler.
Definition: WiFiEventHandler.h:98
static tcpip_adapter_ip_info_t getApIpInfo()
Get the AP IP Info.
Definition: WiFi.cpp:273
std::string getSSID()
Get the SSID.
Definition: WiFi.h:72
void dump()
Dump diagnostics to the log.
Definition: WiFi.cpp:217