WiFi Connect  Version 1.0
WiFi connection manager for ESP32 and ESP8266 including OLED support
WiFiConnect.h
Go to the documentation of this file.
1 
24 #ifndef WiFiConnect_h
25 #define WiFiConnect_h
26 #include <Arduino.h>
27 #if defined(ESP8266)
28 #include <ESP8266WiFi.h>
29 #include <ESP8266WebServer.h>
30 #else
31 #include <WiFi.h>
32 #include <WebServer.h>
33 #endif
34 #include <DNSServer.h>
35 #include <memory>
36 
37 #if defined(ESP8266)
38 extern "C" {
39 #include "user_interface.h"
40 }
41 #define ESP_getChipId() (ESP.getChipId())
42 #else
43 #include <esp_wifi.h>
44 #define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
45 #endif
46 
47 #include "WC_AP_HTML.h"
48 #include "WiFiConnectParam.h"
55 };
56 /**************************************************************************/
63 /**************************************************************************/
64 class WiFiConnect {
65  public:
67  WiFiConnect();
68 
69  boolean startConfigurationPortal();
70  boolean startConfigurationPortal(AP_Continue apcontinue);
71  boolean startConfigurationPortal(AP_Continue apcontinue, const char* apName, const char* apPassword = NULL);
72 
73  boolean startParamsPortal();
74  boolean startParamsPortal(AP_Continue apcontinue);
75  boolean startParamsPortal(AP_Continue apcontinue, const char* apName, const char* apPassword = NULL);
76 
78 
79  void setAPName(const char* apName);
80  const char* getAPName();
81 
82  void resetSettings();
83 
84  boolean autoConnect();
85  boolean autoConnect(const char* ssidName, const char* ssidPassword = NULL, WiFiMode_t acWiFiMode = WIFI_STA);
86 
87 
88  //sets a custom ip /gateway /subnet configuration
89  void setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
90  //sets config for a static IP
91  void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
92 
93  //called when AP mode and config portal is started
94  void setAPCallback( void (*func)(WiFiConnect*) );
95  //called when settings have been changed and connection was successful
96  void setSaveConfigCallback( void (*func)(void) );
97 
98  void setDebug(boolean isDebug);
99 
100  void setRetryAttempts(int attempts);
101  void setConnectionTimeoutSecs(int timeout);
102  void setAPModeTimeoutMins(int mins);
103 
104  boolean captivePortal();
105 
106  //helpers
107  const char* statusToString(int state);
108  int getRSSIasQuality(int RSSI);
109  boolean isIp(String str);
110  String toStringIp(IPAddress ip);
111  virtual void displayTurnOFF(int ms = 5000);
112  virtual void displayLoop();
113  virtual void displayON();
114  protected:
115  boolean _debug = false;
116  template <typename Generic>
117  void DEBUG_WC(Generic text);
118  virtual void displayConnecting(int attempt, int totalAttempts);
119  virtual void displayConnected();
120  virtual void displayAP();
121  virtual void displayParams();
122  virtual void displayManualReset();
123 
124  private:
125 
126 
127  int _retryAttempts = 3;
128  int _connectionTimeoutSecs = 10;
129  int _apTimeoutMins = 3;
130  // DNS server
131  const byte DNS_PORT = 53;
132 
133  long _lastAPPage = 0;
134  boolean _removeDuplicateAPs = true;
135  int _minimumQuality = 8;
136  int _paramsCount = 0;
137  boolean _readyToConnect = false;
138  String _ssid = "";
139  String _password = "";
140 
142 
143  std::unique_ptr<DNSServer> dnsServer;
144 #ifdef ESP8266
145  std::unique_ptr<ESP8266WebServer> server;
146 #else
147  std::unique_ptr<WebServer> server;
148 #endif
149 
150  char _apName[32] ;
151  char _apPassword[64] ;
152 
153  IPAddress _ap_static_ip;
154  IPAddress _ap_static_gw;
155  IPAddress _ap_static_sn;
156  IPAddress _sta_static_ip;
157  IPAddress _sta_static_gw;
158  IPAddress _sta_static_sn;
159 
160  void (*_apcallback)(WiFiConnect*) = NULL;
161  void (*_savecallback)(void) = NULL;
162 
163 
164 
165  void handleRoot();
166  void handleParamRoot();
167  void handleParams();
168  void handleWifi(boolean scan);
169  void handleWifiSave();
170  void handleInfo();
171  void handleReset();
172  void handle204();
173  void handleNotFound();
174 
175  template <class T>
176  auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) {
177  return obj->fromString(s);
178  }
179  auto optionalIPFromString(...) -> bool {
180  DEBUG_WC("NO fromString METHOD ON IPAddress, you need ESP8266 core 2.1.0 or newer for Custom IP configuration to work.");
181  return false;
182  }
183 
184 };
185 
186 #endif
void setDebug(boolean isDebug)
Used to turn on or off messages to the serial port/monitor.
Definition: WiFiConnect.cpp:1283
String toStringIp(IPAddress ip)
Function to return a IPAddress as a string representation.
Definition: WiFiConnect.cpp:1268
Stalls execution with an infinate loop.
Definition: WiFiConnect.h:52
virtual void displayON()
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:119
Restarts the chip, allowing it to try to setup again. Handy for sensors when wifi is lost...
Definition: WiFiConnect.h:53
void addParameter(WiFiConnectParam *p)
Adds a custom parameter to be displayed in the accesspoint and parameters portals. Custom parameters can be used to save information that you do not want to hard code i.e. IP address, Port number that can be saved to the file system.
Definition: WiFiConnect.cpp:178
void setAPName(const char *apName)
Sets the name for the Access Point, setting a default based on the ESP chip id if needed...
Definition: WiFiConnect.cpp:142
virtual void displayManualReset()
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:92
void setAPCallback(void(*func)(WiFiConnect *))
Sets the function to be called when we enter Access Point for configuration. Gets passed a the WiFiCo...
Definition: WiFiConnect.cpp:1203
virtual void displayConnected()
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:65
virtual void displayConnecting(int attempt, int totalAttempts)
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:56
Same as AP_RESTART.
Definition: WiFiConnect.h:54
virtual void displayTurnOFF(int ms=5000)
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:101
virtual void displayAP()
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:74
Class that stores a custom parameter.
Definition: WiFiConnectParam.h:44
virtual void displayLoop()
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:110
void setAPModeTimeoutMins(int mins)
Sets the timeout for when the Access Point is idle.
Definition: WiFiConnect.cpp:129
boolean startParamsPortal()
Starts the custom Parameters Portal using default access point name and password. ...
Definition: WiFiConnect.cpp:191
boolean isIp(String str)
Function to check if a given string is an IP Address.
Definition: WiFiConnect.cpp:1250
void setSaveConfigCallback(void(*func)(void))
Sets the function to be called when we need to save the custom parameters/configuration.
Definition: WiFiConnect.cpp:1216
boolean startConfigurationPortal()
Starts the Access Portal for setting SSID and Password for connecting to a network and any custom use...
Definition: WiFiConnect.cpp:363
Class that helps to connect to WiFi networks, that also has captive portal web interface for configur...
Definition: WiFiConnect.h:64
void setConnectionTimeoutSecs(int timeout)
Sets how long to wait for the connection before failing.
Definition: WiFiConnect.cpp:634
virtual void displayParams()
Virtual method overriden in WiFiConnectOLED.
Definition: WiFiConnect.cpp:83
void resetSettings()
A helper to clear the settings for the last connected network. Remember to comment out or remove and ...
Definition: WiFiConnect.cpp:646
boolean _debug
Flag to determine wheter to output mesages or not.
Definition: WiFiConnect.h:115
WiFiConnect()
Create WiFiConnect class.
Definition: WiFiConnect.cpp:45
const char * getAPName()
Gets the Access Point name or the default based on the ESP chip id if not already set...
Definition: WiFiConnect.cpp:161
No action, continues to run code.
Definition: WiFiConnect.h:51
void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn)
Used to set a static ip/gateway and subnet mask for the local network.
Definition: WiFiConnect.cpp:1318
void setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn)
Used to set a static ip/gateway and subnet mask for the access point. IP and DNS Gateway are normally...
Definition: WiFiConnect.cpp:1300
int getRSSIasQuality(int RSSI)
Function to convert RSSI into a usable range 0-100 so we can use it to exclude poor networks...
Definition: WiFiConnect.cpp:1229
AP_Continue
Definition: WiFiConnect.h:50
const char * statusToString(int state)
A helper function to return the string version of a wifi network state.
Definition: WiFiConnect.cpp:671
void setRetryAttempts(int attempts)
Sets how many times we should try to connect to the WiFi network.
Definition: WiFiConnect.cpp:622
#define WiFiConnect_MAX_PARAMS
The maximum size of the param array and how many custom parameters we may have.
Definition: WiFiConnectParam.h:35
void DEBUG_WC(Generic text)
Outputs debug messages.
Definition: WiFiConnect.cpp:701
boolean autoConnect()
Tries to connect to the last connected WiFi network.
Definition: WiFiConnect.cpp:553
boolean captivePortal()
Redirect to captive portal if we get a request for another domain. Return true in that case so the pa...
Definition: WiFiConnect.cpp:1178