Merge pull request #8 from Hieromon/feature/reconnect_past_ssid

Supports autoReconnect
pull/14/head
Hieromon Ikasamo 7 years ago committed by GitHub
commit 8a6944367d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      README.md
  2. 25
      docs/advancedusage/index.html
  3. 32
      docs/api/index.html
  4. 23
      docs/changelog/index.html
  5. 27
      docs/search/search_index.json
  6. 20
      docs/sitemap.xml
  7. 9
      examples/Simple/Simple.ino
  8. 15
      mkdocs/advancedusage.md
  9. 20
      mkdocs/api.md
  10. 4
      mkdocs/changelog.md
  11. 60
      src/AutoConnect.cpp
  12. 12
      src/AutoConnect.h

@ -68,8 +68,9 @@ Full documentation is available on https://Hieromon.github.io/AutoConnect, some
## Change log ## Change log
### [0.9.4] April 22, 2018. ### [0.9.4] May 5, 2018.
- Automatically focus passphrase after selecting SSID with Configure New AP. - Automatically focus passphrase after selecting SSID with Configure New AP.
- Supports AutoConnectConfig::autoReconnect option, it will scan the WLAN when it can not connect to the default SSID, apply the applicable credentials if it is saved, and try reconnecting.
### [0.9.3] March 23, 2018. ### [0.9.3] March 23, 2018.
- Supports a static IP address assignment. - Supports a static IP address assignment.

@ -365,6 +365,13 @@
404 handler 404 handler
</a> </a>
</li>
<li class="md-nav__item">
<a href="#automatic-reconnect" title=" Automatic reconnect" class="md-nav__link">
Automatic reconnect
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@ -578,6 +585,13 @@
404 handler 404 handler
</a> </a>
</li>
<li class="md-nav__item">
<a href="#automatic-reconnect" title=" Automatic reconnect" class="md-nav__link">
Automatic reconnect
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@ -711,6 +725,17 @@
<h2 id="advanced-usage">Advanced usage<a class="headerlink" href="#advanced-usage" title="Permanent link">&para;</a></h2> <h2 id="advanced-usage">Advanced usage<a class="headerlink" href="#advanced-usage" title="Permanent link">&para;</a></h2>
<h3 id="404-handler"><i class="fa fa-caret-right"></i> 404 handler<a class="headerlink" href="#404-handler" title="Permanent link">&para;</a></h3> <h3 id="404-handler"><i class="fa fa-caret-right"></i> 404 handler<a class="headerlink" href="#404-handler" title="Permanent link">&para;</a></h3>
<p>Registering the "not found" handler is a different way than ESP8266WebServer. The <em>onNotFound</em> of ESP8266WebServer does not work with AutoConnect. AutoConnect overrides <em>ESP8266WebServer::onNotFound</em> to handle a captive portal. To register "not found" handler, use <a href="../api/index.html#onnotfound"><em>AutoConnect::onNotFound</em></a>.</p> <p>Registering the "not found" handler is a different way than ESP8266WebServer. The <em>onNotFound</em> of ESP8266WebServer does not work with AutoConnect. AutoConnect overrides <em>ESP8266WebServer::onNotFound</em> to handle a captive portal. To register "not found" handler, use <a href="../api/index.html#onnotfound"><em>AutoConnect::onNotFound</em></a>.</p>
<h3 id="automatic-reconnect"><i class="fa fa-caret-right"></i> Automatic reconnect<a class="headerlink" href="#automatic-reconnect" title="Permanent link">&para;</a></h3>
<p>When the captive portal is started, SoftAP starts and the STA is disconnected. The current SSID setting memorized in ESP8266 will be lost.<br />
If the <a href="../api/index.html#autoreconnect"><strong>autoReconnect</strong></a> option of the <a href="../api/index.html#autoconnectconfig-api"><strong>AutoConnectConfig</strong></a> class is enabled, it automatically attempts to reconnect to the disconnected past access point. When the autoReconnect option is specified, AutoConnect will not start SoftAP immediately if the first WiFi.begin fails. It will scan WiFi signal and the same connection information as the detected BSSID is stored in EEPROM as AutoConnect's credentials, explicitly apply it with WiFi.begin and rerun.</p>
<div class="codehilite"><pre><span></span><span class="n">AutoConnect</span> <span class="n">Portal</span><span class="p">;</span>
<span class="n">AutoConnectConfig</span> <span class="n">Config</span><span class="p">;</span>
<span class="hll"><span class="n">Config</span><span class="p">.</span><span class="n">autoReconnect</span> <span class="o">=</span> <span class="kr">true</span><span class="p">;</span>
</span><span class="n">Portal</span><span class="p">.</span><span class="nf">config</span><span class="p">(</span><span class="n">Config</span><span class="p">);</span>
<span class="n">Portal</span><span class="p">.</span><span class="nf">begin</span><span class="p">();</span>
</pre></div>
<p>An autoReconnect option is available to <em>AutoConnect::begin</em> without SSID and pass passphrase.</p>
<h3 id="auto-save-credential"><i class="fa fa-caret-right"></i> Auto save Credential<a class="headerlink" href="#auto-save-credential" title="Permanent link">&para;</a></h3> <h3 id="auto-save-credential"><i class="fa fa-caret-right"></i> Auto save Credential<a class="headerlink" href="#auto-save-credential" title="Permanent link">&para;</a></h3>
<p>By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the <a href="../api/index.html#autosave"><strong>autoSave</strong></a> parameter specified by <a href="../api/index.html#autoconnectconfig"><strong>AutoConnectConfig</strong></a>.</p> <p>By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the <a href="../api/index.html#autosave"><strong>autoSave</strong></a> parameter specified by <a href="../api/index.html#autoconnectconfig"><strong>AutoConnectConfig</strong></a>.</p>
<div class="codehilite"><pre><span></span><span class="n">AutoConnect</span> <span class="n">Portal</span><span class="p">;</span> <div class="codehilite"><pre><span></span><span class="n">AutoConnect</span> <span class="n">Portal</span><span class="p">;</span>

@ -554,6 +554,13 @@
apip apip
</a> </a>
</li>
<li class="md-nav__item">
<a href="#autoreconnect" title="autoReconnect" class="md-nav__link">
autoReconnect
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@ -952,6 +959,13 @@
apip apip
</a> </a>
</li>
<li class="md-nav__item">
<a href="#autoreconnect" title="autoReconnect" class="md-nav__link">
autoReconnect
</a>
</li> </li>
<li class="md-nav__item"> <li class="md-nav__item">
@ -1186,7 +1200,7 @@ AutoConnect first invokes <em>WiFi.begin</em>. If the case of SSID and Password
<div class="codehilite"><pre><span></span><span class="kt">void</span> <span class="n">handleClient</span><span class="p">()</span> <div class="codehilite"><pre><span></span><span class="kt">void</span> <span class="n">handleClient</span><span class="p">()</span>
</pre></div> </pre></div>
<p>Process the AutoConnect menu interface. It will be processed the client request too contained in the user sketch handler by calling the host <em>handleClient::ESP8266WebServer</em> from the Autoconnect internally.</p> <p>Process the AutoConnect menu interface. It will be processed the client request too contained in the user sketch handler by calling from inside of AutoConnect to the hosted <em>ESP8266WebServer::handleClient</em>.</p>
<h4 id="handlerequest">handleRequest<a class="headerlink" href="#handlerequest" title="Permanent link">&para;</a></h4> <h4 id="handlerequest">handleRequest<a class="headerlink" href="#handlerequest" title="Permanent link">&para;</a></h4>
<div class="codehilite"><pre><span></span><span class="kt">void</span> <span class="n">handleRequest</span><span class="p">()</span> <div class="codehilite"><pre><span></span><span class="kt">void</span> <span class="n">handleRequest</span><span class="p">()</span>
</pre></div> </pre></div>
@ -1275,6 +1289,21 @@ Register the handler function for undefined URL request detected.
<dt><strong>Type</strong></dt> <dt><strong>Type</strong></dt>
<dd><span class="apidef" style="width:230px;">IPAddress</span>The default value is <strong>192.168.244.1</strong></dd> <dd><span class="apidef" style="width:230px;">IPAddress</span>The default value is <strong>192.168.244.1</strong></dd>
</dl></p> </dl></p>
<h4 id="autoreconnect">autoReconnect<a class="headerlink" href="#autoreconnect" title="Permanent link">&para;</a></h4>
<p>Automatically reconnect to past established access point (BSSID) when the current configured SSID in ESP8266 could not be connected. By enabling this option, <em>AutoConnect::begin()</em> function will attempt to reconnect to a known access point using credentials stored in the EEPROM, even if the connection failed by current SSID.<br />
If the connection fails, starts the captive portal in SoftAP + STA mode.<br />
<dl class="apidl">
<dt><strong>Type</strong></dt>
<dd>bool</dd>
<dt><strong>Value</strong></dt>
<dd><span class="apidef" style="width:230px;">true</span>Reconnect automatically.</dd>
<dd><span class="apidef" style="width:230px;">false</span>Starts Captive Portal in SoftAP + STA mode without trying to reconnect. This is the default.</dd>
</dl></p>
<p>When the autoReconnect option is enabled, an automatic connection will behave if the following conditions are satisfied.</p>
<ul>
<li>Invokes <em>AutoConnect::begin</em> without user name and password parameter as <code class="codehilite">begin()</code>.</li>
<li>If one of the saved BSSIDs (not the SSID) of the credentials matches the BSSID detected by the network scan.</li>
</ul>
<h4 id="autoreset">autoReset<a class="headerlink" href="#autoreset" title="Permanent link">&para;</a></h4> <h4 id="autoreset">autoReset<a class="headerlink" href="#autoreset" title="Permanent link">&para;</a></h4>
<p>Reset ESP8266 module automatically after WLAN disconnected. <p>Reset ESP8266 module automatically after WLAN disconnected.
<dl class="apidl"> <dl class="apidl">
@ -1395,6 +1424,7 @@ The default value is 0.
<span class="n">Config</span><span class="p">.</span><span class="n">apip</span> <span class="o">=</span> <span class="nf">IPAddress</span><span class="p">(</span><span class="mi">192</span><span class="p">,</span><span class="mi">168</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">101</span><span class="p">);</span> <span class="c1">// Sets SoftAP IP address</span> <span class="n">Config</span><span class="p">.</span><span class="n">apip</span> <span class="o">=</span> <span class="nf">IPAddress</span><span class="p">(</span><span class="mi">192</span><span class="p">,</span><span class="mi">168</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">101</span><span class="p">);</span> <span class="c1">// Sets SoftAP IP address</span>
<span class="n">Config</span><span class="p">.</span><span class="n">gateway</span> <span class="o">=</span> <span class="nf">IPAddress</span><span class="p">(</span><span class="mi">192</span><span class="p">,</span><span class="mi">168</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// Sets WLAN router IP address</span> <span class="n">Config</span><span class="p">.</span><span class="n">gateway</span> <span class="o">=</span> <span class="nf">IPAddress</span><span class="p">(</span><span class="mi">192</span><span class="p">,</span><span class="mi">168</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// Sets WLAN router IP address</span>
<span class="n">Config</span><span class="p">.</span><span class="n">netmask</span> <span class="o">=</span> <span class="nf">IPAddress</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span> <span class="c1">// Sets WLAN scope</span> <span class="n">Config</span><span class="p">.</span><span class="n">netmask</span> <span class="o">=</span> <span class="nf">IPAddress</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span> <span class="c1">// Sets WLAN scope</span>
<span class="n">Config</span><span class="p">.</span><span class="n">autoReconnect</span> <span class="o">=</span> <span class="kr">true</span><span class="p">;</span> <span class="c1">// Enable auto-reconnect</span>
<span class="n">Config</span><span class="p">.</span><span class="n">autoSave</span> <span class="o">=</span> <span class="n">AC_SAVECREDENTIAL_NEVER</span><span class="p">;</span> <span class="c1">// No save credential</span> <span class="n">Config</span><span class="p">.</span><span class="n">autoSave</span> <span class="o">=</span> <span class="n">AC_SAVECREDENTIAL_NEVER</span><span class="p">;</span> <span class="c1">// No save credential</span>
<span class="n">COnfig</span><span class="p">.</span><span class="n">boundaryOffet</span> <span class="o">=</span> <span class="mi">64</span><span class="p">;</span> <span class="c1">// Reserve 64 bytes for the user data in EEPROM. </span> <span class="n">COnfig</span><span class="p">.</span><span class="n">boundaryOffet</span> <span class="o">=</span> <span class="mi">64</span><span class="p">;</span> <span class="c1">// Reserve 64 bytes for the user data in EEPROM. </span>
<span class="n">Config</span><span class="p">.</span><span class="n">homeUri</span> <span class="o">=</span> <span class="s">&quot;/index.html&quot;</span> <span class="c1">// Sets home path of the sketch application</span> <span class="n">Config</span><span class="p">.</span><span class="n">homeUri</span> <span class="o">=</span> <span class="s">&quot;/index.html&quot;</span> <span class="c1">// Sets home path of the sketch application</span>

@ -101,7 +101,7 @@
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="search"> <input class="md-toggle" data-md-toggle="search" type="checkbox" id="search">
<label class="md-overlay" data-md-component="overlay" for="drawer"></label> <label class="md-overlay" data-md-component="overlay" for="drawer"></label>
<a href="#094-april-22-2018" tabindex="1" class="md-skip"> <a href="#094-may-5-2018" tabindex="1" class="md-skip">
Skip to content Skip to content
</a> </a>
@ -401,14 +401,13 @@
<ul class="md-nav__list" data-md-scrollfix> <ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#094-april-22-2018" title="[0.9.4] April 22, 2018." class="md-nav__link"> <a href="#094-may-5-2018" title="[0.9.4] May 5, 2018." class="md-nav__link">
[0.9.4] April 22, 2018. [0.9.4] May 5, 2018.
</a> </a>
<nav class="md-nav"> </li>
<ul class="md-nav__list">
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#093-march-23-2018" title="[0.9.3] March 23, 2018." class="md-nav__link"> <a href="#093-march-23-2018" title="[0.9.3] March 23, 2018." class="md-nav__link">
[0.9.3] March 23, 2018. [0.9.3] March 23, 2018.
</a> </a>
@ -476,14 +475,13 @@
<ul class="md-nav__list" data-md-scrollfix> <ul class="md-nav__list" data-md-scrollfix>
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#094-april-22-2018" title="[0.9.4] April 22, 2018." class="md-nav__link"> <a href="#094-may-5-2018" title="[0.9.4] May 5, 2018." class="md-nav__link">
[0.9.4] April 22, 2018. [0.9.4] May 5, 2018.
</a> </a>
<nav class="md-nav"> </li>
<ul class="md-nav__list">
<li class="md-nav__item"> <li class="md-nav__item">
<a href="#093-march-23-2018" title="[0.9.3] March 23, 2018." class="md-nav__link"> <a href="#093-march-23-2018" title="[0.9.3] March 23, 2018." class="md-nav__link">
[0.9.3] March 23, 2018. [0.9.3] March 23, 2018.
</a> </a>
@ -528,9 +526,10 @@
<h1>Change log</h1> <h1>Change log</h1>
<h3 id="094-april-22-2018">[0.9.4] April 22, 2018.<a class="headerlink" href="#094-april-22-2018" title="Permanent link">&para;</a></h3> <h4 id="094-may-5-2018">[0.9.4] May 5, 2018.<a class="headerlink" href="#094-may-5-2018" title="Permanent link">&para;</a></h4>
<ul> <ul>
<li>Automatically focus passphrase after selecting SSID with Configure New AP.</li> <li>Automatically focus passphrase after selecting SSID with Configure New AP.</li>
<li>Supports AutoConnectConfig::autoReconnect option, it will scan the WLAN when it can not connect to the default SSID, apply the applicable credentials if it is saved, and try reconnecting.</li>
</ul> </ul>
<h4 id="093-march-23-2018">[0.9.3] March 23, 2018.<a class="headerlink" href="#093-march-23-2018" title="Permanent link">&para;</a></h4> <h4 id="093-march-23-2018">[0.9.3] March 23, 2018.<a class="headerlink" href="#093-march-23-2018" title="Permanent link">&para;</a></h4>
<ul> <ul>

File diff suppressed because one or more lines are too long

@ -4,7 +4,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -12,7 +12,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//gettingstarted/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//gettingstarted/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -20,7 +20,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//menu/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//menu/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -28,7 +28,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//basicusage/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//basicusage/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -36,7 +36,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//advancedusage/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//advancedusage/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -44,7 +44,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//api/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//api/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -52,7 +52,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//examples/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//examples/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -60,7 +60,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//faq/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//faq/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -68,7 +68,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//changelog/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//changelog/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
@ -76,7 +76,7 @@
<url> <url>
<loc>https://Hieromon.github.io/AutoConnect//license/index.html</loc> <loc>https://Hieromon.github.io/AutoConnect//license/index.html</loc>
<lastmod>2018-04-23</lastmod> <lastmod>2018-05-06</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>

@ -14,6 +14,7 @@
ESP8266WebServer Server; ESP8266WebServer Server;
AutoConnect Portal(Server); AutoConnect Portal(Server);
AutoConnectConfig Config; // Enable autoReconnect supported on v0.9.4
#define TIMEZONE (3600 * 9) // Tokyo #define TIMEZONE (3600 * 9) // Tokyo
#define NTPServer1 "ntp.nict.jp" // NICT japan. #define NTPServer1 "ntp.nict.jp" // NICT japan.
@ -51,7 +52,15 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
// Behavior a root path of ESP8266WebServer.
Server.on("/", rootPage); Server.on("/", rootPage);
// Enable saved past credential by autoReconnect option,
// even once it is disconnected.
Config.autoReconnect = true;
Portal.config(Config);
// Establish a connection with an autoReconnect option.
if (Portal.begin()) { if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString()); Serial.println("WiFi connected: " + WiFi.localIP().toString());
configTime(TIMEZONE, 0, NTPServer1, NTPServer2); configTime(TIMEZONE, 0, NTPServer1, NTPServer2);

@ -4,6 +4,21 @@
Registering the "not found" handler is a different way than ESP8266WebServer. The *onNotFound* of ESP8266WebServer does not work with AutoConnect. AutoConnect overrides *ESP8266WebServer::onNotFound* to handle a captive portal. To register "not found" handler, use [*AutoConnect::onNotFound*](api.md#onnotfound). Registering the "not found" handler is a different way than ESP8266WebServer. The *onNotFound* of ESP8266WebServer does not work with AutoConnect. AutoConnect overrides *ESP8266WebServer::onNotFound* to handle a captive portal. To register "not found" handler, use [*AutoConnect::onNotFound*](api.md#onnotfound).
### <i class="fa fa-caret-right"></i> Automatic reconnect
When the captive portal is started, SoftAP starts and the STA is disconnected. The current SSID setting memorized in ESP8266 will be lost.
If the [**autoReconnect**](api.md#autoreconnect) option of the [**AutoConnectConfig**](api.md#autoconnectconfig-api) class is enabled, it automatically attempts to reconnect to the disconnected past access point. When the autoReconnect option is specified, AutoConnect will not start SoftAP immediately if the first WiFi.begin fails. It will scan WiFi signal and the same connection information as the detected BSSID is stored in EEPROM as AutoConnect's credentials, explicitly apply it with WiFi.begin and rerun.
```arduino hl_lines="3"
AutoConnect Portal;
AutoConnectConfig Config;
Config.autoReconnect = true;
Portal.config(Config);
Portal.begin();
```
An autoReconnect option is available to *AutoConnect::begin* without SSID and pass passphrase.
### <i class="fa fa-caret-right"></i> Auto save Credential ### <i class="fa fa-caret-right"></i> Auto save Credential
By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the [**autoSave**](api.md#autosave) parameter specified by [**AutoConnectConfig**](api.md#autoconnectconfig). By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the [**autoSave**](api.md#autosave) parameter specified by [**AutoConnectConfig**](api.md#autoconnectconfig).

@ -113,7 +113,7 @@ Stops AutoConnect captive portal service. Release ESP8266WebServer and DNSServer
void handleClient() void handleClient()
``` ```
Process the AutoConnect menu interface. It will be processed the client request too contained in the user sketch handler by calling the host *handleClient::ESP8266WebServer* from the Autoconnect internally. Process the AutoConnect menu interface. It will be processed the client request too contained in the user sketch handler by calling from inside of AutoConnect to the hosted *ESP8266WebServer::handleClient*.
#### handleRequest #### handleRequest
@ -232,6 +232,23 @@ Sets IP address for Soft AP in captive portal. When AutoConnect fails the initia
<dd><span class="apidef" style="width:230px;">IPAddress</span>The default value is **192.168.244.1**</dd> <dd><span class="apidef" style="width:230px;">IPAddress</span>The default value is **192.168.244.1**</dd>
</dl> </dl>
#### autoReconnect
Automatically reconnect to past established access point (BSSID) when the current configured SSID in ESP8266 could not be connected. By enabling this option, *AutoConnect::begin()* function will attempt to reconnect to a known access point using credentials stored in the EEPROM, even if the connection failed by current SSID.
If the connection fails, starts the captive portal in SoftAP + STA mode.
<dl class="apidl">
<dt>**Type**</dt>
<dd>bool</dd>
<dt>**Value**</dt>
<dd><span class="apidef" style="width:230px;">true</span>Reconnect automatically.</dd>
<dd><span class="apidef" style="width:230px;">false</span>Starts Captive Portal in SoftAP + STA mode without trying to reconnect. This is the default.</dd>
</dl>
When the autoReconnect option is enabled, an automatic connection will behave if the following conditions are satisfied.
- Invokes *AutoConnect::begin* without user name and password parameter as ```begin()```.
- If one of the saved BSSIDs (not the SSID) of the credentials matches the BSSID detected by the network scan.
#### autoReset #### autoReset
Reset ESP8266 module automatically after WLAN disconnected. Reset ESP8266 module automatically after WLAN disconnected.
@ -382,6 +399,7 @@ Config.apid = ESP.hostname(); // Retrieve host name to SotAp ide
Config.apip = IPAddress(192,168,10,101); // Sets SoftAP IP address Config.apip = IPAddress(192,168,10,101); // Sets SoftAP IP address
Config.gateway = IPAddress(192,168,10,1); // Sets WLAN router IP address Config.gateway = IPAddress(192,168,10,1); // Sets WLAN router IP address
Config.netmask = IPAddress(255,255,255,0); // Sets WLAN scope Config.netmask = IPAddress(255,255,255,0); // Sets WLAN scope
Config.autoReconnect = true; // Enable auto-reconnect
Config.autoSave = AC_SAVECREDENTIAL_NEVER; // No save credential Config.autoSave = AC_SAVECREDENTIAL_NEVER; // No save credential
COnfig.boundaryOffet = 64; // Reserve 64 bytes for the user data in EEPROM. COnfig.boundaryOffet = 64; // Reserve 64 bytes for the user data in EEPROM.
Config.homeUri = "/index.html" // Sets home path of the sketch application Config.homeUri = "/index.html" // Sets home path of the sketch application

@ -1,5 +1,7 @@
### [0.9.4] April 22, 2018. #### [0.9.4] May 5, 2018.
- Automatically focus passphrase after selecting SSID with Configure New AP. - Automatically focus passphrase after selecting SSID with Configure New AP.
- Supports AutoConnectConfig::autoReconnect option, it will scan the WLAN when it can not connect to the default SSID, apply the applicable credentials if it is saved, and try reconnecting.
#### [0.9.3] March 23, 2018. #### [0.9.3] March 23, 2018.

@ -2,8 +2,8 @@
* AutoConnect class implementation. * AutoConnect class implementation.
* @file AutoConnect.cpp * @file AutoConnect.cpp
* @author hieromon@gmail.com * @author hieromon@gmail.com
* @version 0.9.3 * @version 0.9.4
* @date 2018-03-23 * @date 2018-05-05
* @copyright MIT license. * @copyright MIT license.
*/ */
@ -69,24 +69,11 @@ bool AutoConnect::begin() {
* @retval false Could not connected, Captive portal started with WIFI_AP_STA mode. * @retval false Could not connected, Captive portal started with WIFI_AP_STA mode.
*/ */
bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long timeout) { bool AutoConnect::begin(const char* ssid, const char* passphrase, unsigned long timeout) {
_portalTimeout = timeout;
return begin(ssid, passphrase);
}
/**
* Starts establishing WiFi connection.
* Before establishing, start the Web server and DNS server for the captive
* portal. Then begins connection establishment in WIFI_STA mode. If
* connection can not established with the specified SSID and password,
* switch to WIFI_AP_STA mode and activate SoftAP.
* @param ssid SSID to be connected.
* @param passphrase Password for connection.
* @retval true Connection established, AutoConnect service started with WIFI_STA mode.
* @retval false Could not connected, Captive portal started with WIFI_AP_STA mode.
*/
bool AutoConnect::begin(const char* ssid, const char* passphrase) {
bool cs; bool cs;
// Overwrite for the current timeout value.
_portalTimeout = timeout;
// Start WiFi connection. // Start WiFi connection.
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
delay(100); delay(100);
@ -113,6 +100,19 @@ bool AutoConnect::begin(const char* ssid, const char* passphrase) {
WiFi.begin(ssid, passphrase); WiFi.begin(ssid, passphrase);
AC_DBG("WiFi.begin(%s%s%s)\n", ssid == nullptr ? "" : ssid, passphrase == nullptr ? "" : ",", passphrase == nullptr ? "" : passphrase); AC_DBG("WiFi.begin(%s%s%s)\n", ssid == nullptr ? "" : ssid, passphrase == nullptr ? "" : ",", passphrase == nullptr ? "" : passphrase);
cs = _waitForConnect(_portalTimeout) == WL_CONNECTED; cs = _waitForConnect(_portalTimeout) == WL_CONNECTED;
// Reconnect with a valid credential as the autoReconnect option is enabled.
if (!cs && _apConfig.autoReconnect && (ssid == nullptr && passphrase == nullptr)) {
// Load a valid credential.
if (_loadAvailCredential()) {
// Try to reconnect with a stored credential.
AC_DBG("Past SSID:%s loaded\n", reinterpret_cast<const char*>(_credential.ssid));
const char* psk = strlen(reinterpret_cast<const char*>(_credential.password)) ? reinterpret_cast<const char*>(_credential.password) : nullptr;
WiFi.begin(reinterpret_cast<const char*>(_credential.ssid), psk);
AC_DBG("WiFi.begin(%s%s%s)\n", _credential.ssid, psk == nullptr ? "" : ",", psk == nullptr ? "" : psk);
cs = _waitForConnect(_portalTimeout) == WL_CONNECTED;
}
}
_currentHostIP = WiFi.localIP(); _currentHostIP = WiFi.localIP();
// It doesn't matter the connection status for launching the Web server. // It doesn't matter the connection status for launching the Web server.
@ -373,6 +373,30 @@ void AutoConnect::onNotFound(ESP8266WebServer::THandlerFunction fn) {
_notFoundHandler = fn; _notFoundHandler = fn;
} }
/**
* Load stored credentials that match nearby WLANs.
* @retval true A matched credential of BSSID was loaded.
*/
bool AutoConnect::_loadAvailCredential() {
AutoConnectCredential credential(_apConfig.boundaryOffset);
if (credential.entries() >= 0) {
// Scan the vicinity only when the saved credentials are existing.
int8_t nn = WiFi.scanNetworks(false, true);
if (nn > 0) {
// Determine valid credentials by BSSID.
for (uint8_t i = 0; i < credential.entries(); i++) {
credential.load(i, &_credential);
for (uint8_t n = 0; n <= nn; n++) {
if (!memcmp(_credential.bssid, WiFi.BSSID(n), sizeof(station_config::bssid)))
return true;
}
}
}
}
return false;
}
/** /**
* Disconnect from the AP and stop the AutoConnect portal. * Disconnect from the AP and stop the AutoConnect portal.
* Stops DNS server and flush tcp sending. * Stops DNS server and flush tcp sending.

@ -2,8 +2,8 @@
* Declaration of AutoConnect class and accompanying AutoConnectConfig class. * Declaration of AutoConnect class and accompanying AutoConnectConfig class.
* @file AutoConnect.h * @file AutoConnect.h
* @author hieromon@gmail.com * @author hieromon@gmail.com
* @version 0.9.3 * @version 0.9.4
* @date 2018-03-23 * @date 2018-05-05
* @copyright MIT license. * @copyright MIT license.
*/ */
@ -119,6 +119,7 @@ class AutoConnectConfig {
uptime(AUTOCONNECT_STARTUPTIME), uptime(AUTOCONNECT_STARTUPTIME),
autoRise(true), autoRise(true),
autoReset(true), autoReset(true),
autoReconnect(false),
homeUri(AUTOCONNECT_HOMEURI), homeUri(AUTOCONNECT_HOMEURI),
staip(0U), staip(0U),
staGateway(0U), staGateway(0U),
@ -141,6 +142,7 @@ class AutoConnectConfig {
uptime(AUTOCONNECT_STARTUPTIME), uptime(AUTOCONNECT_STARTUPTIME),
autoRise(true), autoRise(true),
autoReset(true), autoReset(true),
autoReconnect(false),
homeUri(AUTOCONNECT_HOMEURI), homeUri(AUTOCONNECT_HOMEURI),
staip(0U), staip(0U),
staGateway(0U), staGateway(0U),
@ -163,6 +165,7 @@ class AutoConnectConfig {
uptime = o.uptime; uptime = o.uptime;
autoRise = o.autoRise; autoRise = o.autoRise;
autoReset = o.autoReset; autoReset = o.autoReset;
autoReconnect = o.autoReconnect;
homeUri = o.homeUri; homeUri = o.homeUri;
staip = o.staip; staip = o.staip;
staGateway = o.staGateway; staGateway = o.staGateway;
@ -184,6 +187,7 @@ class AutoConnectConfig {
int uptime; /**< Length of start up time */ int uptime; /**< Length of start up time */
bool autoRise; /**< Automatic starting the captive portal */ bool autoRise; /**< Automatic starting the captive portal */
bool autoReset; /**< Reset ESP8266 module automatically when WLAN disconnected. */ bool autoReset; /**< Reset ESP8266 module automatically when WLAN disconnected. */
bool autoReconnect; /**< Automatic reconnect with past SSID */
String homeUri; /**< A URI of user site */ String homeUri; /**< A URI of user site */
IPAddress staip; /**< Station static IP address */ IPAddress staip; /**< Station static IP address */
IPAddress staGateway; /**< Station gateway address */ IPAddress staGateway; /**< Station gateway address */
@ -201,8 +205,7 @@ class AutoConnect {
bool config(const char* ap, const char* password = nullptr); bool config(const char* ap, const char* password = nullptr);
void home(String uri); void home(String uri);
bool begin(); bool begin();
bool begin(const char* ssid, const char* passphrase); bool begin(const char* ssid, const char* passphrase = nullptr, unsigned long timeout = AUTOCONNECT_TIMEOUT);
bool begin(const char* ssid, const char* passphrase, unsigned long timeout);
void end(); void end();
void handleClient(); void handleClient();
void handleRequest(); void handleRequest();
@ -223,6 +226,7 @@ class AutoConnect {
void _startWebServer(); void _startWebServer();
void _startDNSServer(); void _startDNSServer();
void _handleNotFound(); void _handleNotFound();
bool _loadAvailCredential();
void _stopPortal(); void _stopPortal();
bool _classifyHandle(HTTPMethod mothod, String uri); bool _classifyHandle(HTTPMethod mothod, String uri);
PageElement* _setupPage(String uri); PageElement* _setupPage(String uri);

Loading…
Cancel
Save