<ahref="#5-autoconnectbegin-with-ssid-and-password"title="5. AutoConnect::begin with SSID and Password"class="md-nav__link">
5. AutoConnect::begin with SSID and Password
</a>
</li>
<liclass="md-nav__item">
<ahref="#6-use-esp8266webserveron-to-handle-url"title="6. Use ESP8266WebServer::on to handle URL"class="md-nav__link">
6. Use ESP8266WebServer::on to handle URL
</a>
</li>
<liclass="md-nav__item">
<ahref="#7-use-either-esp8266webserverhandleclient-or-autoconnecthandleclient"title="7. Use either ESP8266WebServer::handleClient() or AutoConnect::handleClient()"class="md-nav__link">
7. Use either ESP8266WebServer::handleClient() or AutoConnect::handleClient()
</a>
</li>
</ul>
</nav>
</li>
<liclass="md-nav__item">
<ahref="#esp8266webserver-hosted-or-parasitic"title=" ESP8266WebServer hosted or parasitic"class="md-nav__link">
<ahref="#5-autoconnectbegin-with-ssid-and-password"title="5. AutoConnect::begin with SSID and Password"class="md-nav__link">
5. AutoConnect::begin with SSID and Password
</a>
</li>
<liclass="md-nav__item">
<ahref="#6-use-esp8266webserveron-to-handle-url"title="6. Use ESP8266WebServer::on to handle URL"class="md-nav__link">
6. Use ESP8266WebServer::on to handle URL
</a>
</li>
<liclass="md-nav__item">
<ahref="#7-use-either-esp8266webserverhandleclient-or-autoconnecthandleclient"title="7. Use either ESP8266WebServer::handleClient() or AutoConnect::handleClient()"class="md-nav__link">
7. Use either ESP8266WebServer::handleClient() or AutoConnect::handleClient()
</a>
</li>
</ul>
</nav>
</li>
<liclass="md-nav__item">
<ahref="#esp8266webserver-hosted-or-parasitic"title=" ESP8266WebServer hosted or parasitic"class="md-nav__link">
<h3id="embed-to-the-sketches"><iclass="fa fa-edit"></i> Embed to the sketches<aclass="headerlink"href="#embed-to-the-sketches"title="Permanent link">¶</a></h3>
<p>How embed the AutoConnect to the sketches you have. Most simple approach to applying AutoConnect for the existing sketches, follow the below steps.</p>
<li>Insert <codeclass="codehilite"><spanclass="cp">#include</span><spanclass="cpf"><AutoConnect.h></span></code> to behind of <codeclass="codehilite"><spanclass="cp">#include</span><spanclass="cpf"><ESP8266WebServer.h></span></code>.</li>
<li>Insert <codeclass="codehilite"><spanclass="na">AutoConnect</span><em>PORTAL(WEBSERVER);</em></code> to behind of <codeclass="codehilite"><spanclass="na">ESP8266WebServer</span><em>WEBSERVER;</em></code> declaration.<supid="fnref:1"><aclass="footnote-ref"href="#fn:1"rel="footnote">1</a></sup></li>
<li>Remove <codeclass="codehilite">WiFi.<spanclass="na">begin</span>(<em>SSID</em>,<em>PSK</em>)</code> and the subsequent logic for the connection status check.</li>
<li>Replace <codeclass="codehilite"><em>WEBSERVER</em>.<spanclass="na">begin</span><spanclass="p">()</span></code> to <codeclass="codehilite"><em>PORTAL</em>.<spanclass="na">begin</span><spanclass="p">()</span></code>.<supid="fnref:2"><aclass="footnote-ref"href="#fn:2"rel="footnote">2</a></sup></li>
<li>Replace <codeclass="codehilite"><em>WEBSERVER</em>.<spanclass="na">handleClient</span><spanclass="p">()</span></code> to <codeclass="codehilite"><em>PORTAL</em>.<spanclass="na">handleClient</span><spanclass="p">()</span></code>.<supid="fnref:3"><aclass="footnote-ref"href="#fn:3"rel="footnote">3</a></sup></li>
<li>If the connection checks logic is needed, you can check the return value according to <codeclass="codehilite"><em>PORTAL</em>.<spanclass="na">begin</span><spanclass="p">()</span></code> with <codeclass="codehilite">true</code> or <codeclass="codehilite">false</code>.</li>
<h3id="basic-logic-sequence-for-the-user-sketches"><iclass="fa fa-caret-right"></i> Basic logic sequence for the user sketches<aclass="headerlink"href="#basic-logic-sequence-for-the-user-sketches"title="Permanent link">¶</a></h3>
<h4id="1-a-typical-logic-sequence">1. A typical logic sequence<aclass="headerlink"href="#1-a-typical-logic-sequence"title="Permanent link">¶</a></h4>
<divclass="admonition note">
<ol>
<li><strong>Include headers,</strong><codeclass="codehilite">ESP8266WebServer.h</code> and <codeclass="codehilite">AutoConnect.h</code></li>
<p><ahref="#esp8266webserver-hosted-or-parasitic">Two options</a> are available for <ahref="../api/index.html#constructors">AutoConnect constructor</a>.</p>
<p><strong>Parameter with ESP8266WebServer variable:</strong> An ESP8266WebServer object variable must be declared in the sketch. AutoConnect uses its variable for handling the AutoConnect menu.</p>
</li>
<li>
<p><strong>With no parameter:</strong> The sketch does not declare ESP8266WebServer object. In this case, AutoConnect allocates an instance of the ESP8266WebServer internally and the logic sequence of the sketch is somewhat different as the above. To register a URL handler function by <em>ESP8266WebServer::on</em> should be performed after <ahref="../api/index.html#begin"><em>AutoConnect::begin</em></a>.</p>
</li>
</ul>
<h4id="3-no-need-wifibegin">3. No need WiFI.begin(...)<aclass="headerlink"href="#3-no-need-wifibegin"title="Permanent link">¶</a></h4>
<p>AutoConnect performs <em>WiFi.begin</em> for establishing a connection with WLAN internally. There is no need for a general process to establish a connection with <em>WiFi.begin</em> in a sketch.</p>
<p><ahref="../api/index.html#begin"><em>AutoConnect::begin</em></a> internally executes <em>ESP8266WebServer::begin</em> too and it starts DNS server to behave as a captive portal. So the sketch does not need to call <em>ESP8266WebServer::begin</em>.</p>
<divclass="admonition info">
<pclass="admonition-title">Why DNS Server Starts</p>
<p>AutoConnect traps the detection of captive portals and directs them to the AutoConnect menu to achieve a connection with the WLAN interactively. In order to trap, it temporarily responds SoftAP address to all DNS queries. When the connection with the WLAN is successfully established, the DNS server will stop.</p>
</div>
<h4id="5-autoconnectbegin-with-ssid-and-password">5. AutoConnect::begin with SSID and Password<aclass="headerlink"href="#5-autoconnectbegin-with-ssid-and-password"title="Permanent link">¶</a></h4>
<p>SSID and Password can also specify by <ahref="../api.me#begin">AutoConnect::begin</a>. ESP8266 uses provided SSID and Password explicitly. If the connection false with specified SSID with Password then a captive portal is activated. SSID and Password are not present, ESP8266 SDK will attempt to connect using the still effectual SSID and password. Usually, it succeeds.</p>
<h4id="6-use-esp8266webserveron-to-handle-url">6. Use ESP8266WebServer::on to handle URL<aclass="headerlink"href="#6-use-esp8266webserveron-to-handle-url"title="Permanent link">¶</a></h4>
<p>AutoConnect is designed to coexist with the process for handling the web pages by user sketches. The page processing function which will send an HTML to the client invoked by the "<strong>on</strong>" function is the same as when using ESP8266WebServer natively.</p>
<h4id="7-use-either-esp8266webserverhandleclient-or-autoconnecthandleclient">7. Use either ESP8266WebServer::handleClient() or AutoConnect::handleClient()<aclass="headerlink"href="#7-use-either-esp8266webserverhandleclient-or-autoconnecthandleclient"title="Permanent link">¶</a></h4>
<p>Both classes member function name is the same: <em>handleClient</em>, but behavior is different. Using the AutoConnect embedded along with ESP8266WebServer::handleClient has limitations. Refer to the below section for details. </p>
<h3id="esp8266webserver-hosted-or-parasitic"><iclass="fa fa-caret-right"></i> ESP8266WebServer hosted or parasitic<aclass="headerlink"href="#esp8266webserver-hosted-or-parasitic"title="Permanent link">¶</a></h3>
<p>The interoperable process with an ESP8266WebServer depends on the parameters of the <ahref="../api/index.html#constructors">AutoConnect constructor</a>.</p>
<table>
<thead>
<tr>
<th>Declaration parameter</th>
<th>Use ESP8266WebServer::handleClient</th>
<th>Use AutoConnect::handleClient</th>
</tr>
</thead>
<tbody>
<tr>
<td>None</td>
<td>AutoConnect menu not available.<br>host() is needed.</td>
<td>AutoConnect menu available.<br>host() is needed.</td>
</tr>
<tr>
<td>Reference to ESP8266WebServer</td>
<td>AutoConnect menu not available.<br>host() not necessary.</td>
<td>AutoConnect menu available.<br>host() not necessary.</td>
</tr>
</tbody>
</table>
<ul>
<li>
<p><strong>By declaration for the AutoConnect variable with no parameter</strong>: The ESP8266WebServer instance is hosted by AutoConnect automatically then the sketches use <ahref="../api/index.html#host"><em>AutoConnect::host</em></a> as API to get it after <ahref="../api/index.html#begin"><em>AutoConnect::begin</em></a> performed.</p>
</li>
<li>
<p><strong>By declaration for the AutoConnect variable with the reference of ESP8266WebServer</strong>: AutoConnect will use it. The sketch can use it is too.</p>
</li>
<li>
<p><strong>In use ESP8266WebServer::handleClient()</strong>: AutoConnect menu can be dispatched but not works normally. It is necessary to call <ahref="../api/index.html#void-handlerequest"><em>AutoConnect::handleRequest</em></a> after <em>ESP8255WebServer::handleClient</em> invoking.</p>
</li>
<li>
<p><strong>In use <ahref="../api/index.html#void-handleclient">AutoConnect::handleClient()</a></strong>: The handleClient() process and the AutoConnect menu is available without calling <em>ESP8266WebServer::handleClient</em>.</p>
</li>
</ul>
<divclass="admonition info">
<pclass="admonition-title">Why AutoConnect::handleRequest is needed when using ESP8266::handleClient</p>
<p>The AutoConnect menu function may affect WiFi connection state. It follows that the menu process must execute outside <em>ESP8266WebServer::handleClient</em>.<br/>
<ahref="../api/index.html#void-handleclient"><em>AutoConnect::handleClient</em></a> is equivalent <em>ESP8266WebServer::handleClient</em> included <ahref="../api/index.html#void-handlerequest"><em>AutoConnect::handleRequest</em></a>.</p>
<p>Registering the "not found" handler is a different way than ESP8266. 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 <ahref="../api/index.html#onnotfound"><em>AutoConnect::onNotFound</em></a>.</p>
<h3id="auto-save-credential"><iclass="fa fa-caret-right"></i> Auto save Credential<aclass="headerlink"href="#auto-save-credential"title="Permanent link">¶</a></h3>
<p>By default, AutoConnect saves the credentials of the established connection in EEPROM. You can disable it with the <strong>autoSave</strong> parameter specified by <ahref="../api/index.html#config"><em>AutoConnect::config</em></a>.</p>
<p>The captive portal will only be activated if the first <em>WiFi::begin</em> fails. Sketch can detect with the <ahref="../api/index.html#ondetect"><em>onDetect</em></a> funciton that the captive portal has started. For example, the sketch can be written like as follows that turns on the LED at the start captive portal.</p>
<h3id="combination-with-mdns"><iclass="fa fa-caret-right"></i> Combination with mDNS<aclass="headerlink"href="#combination-with-mdns"title="Permanent link">¶</a></h3>
<p>With <ahref="https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266mDNS">mDNS library</a>, you can access to ESP8266 by name instead of IP address after connection. The sketch can start the MDNS responder after AutoConnect::begin.</p>
<p>You can output AutoConnect monitor messages to the <strong>Serial</strong>. A monitor message activation switch is in an include header file <ahref="https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnect.h"><em>AutoConnect.h</em></a> of library source. Define <strong>AC_DEBUG</strong> macro to output monitor messages.</p>
<h3id="disable-the-captive-portal"><iclass="fa fa-caret-right"></i> Disable the captive portal<aclass="headerlink"href="#disable-the-captive-portal"title="Permanent link">¶</a></h3>
<p>It can also prevent the captive portal from starting even if the connection at the first <em>WiFi.begin</em> fails. In this case, the behavior of <em>AutoConnect::begin</em> is the same as <em>WiFi.begin</em>.</p>
<p>For disabling the captive portal, <strong>autoRise</strong> sets to false with AutoConnectConfig.</p>
<h3id="refers-the-hosted-esp8266webserver"><iclass="fa fa-caret-right"></i> Refers the hosted ESP8266WebServer<aclass="headerlink"href="#refers-the-hosted-esp8266webserver"title="Permanent link">¶</a></h3>
<p>Constructing an AutoConnect object variable without parameters then creates and starts an ESP8266WebServer inside the AutoConnect. This object variable could be referred by <ahref="../api/index.html#host"><em>AutoConnect::host()</em></a> function to access ESP8266WebServer instance as like below.</p>
<pclass="admonition-title">When host() is valid</p>
<p>The host() can be referred at after <em>AutoConnect::begin</em>.</p>
</div>
<h3id="usage-for-automatically-instantiated-esp8266webserver"><iclass="fa fa-caret-right"></i> Usage for automatically instantiated ESP8266WebServer<aclass="headerlink"href="#usage-for-automatically-instantiated-esp8266webserver"title="Permanent link">¶</a></h3>
<p>The sketch can handle URL requests using ESP8266WebServer that AutoConnect started internally. ESP8266WebServer instantiated dynamically by AutoConnect can be referred to by <ahref="../api/index.html#host"><em>AutoConnect::host</em></a> function. The sketch can use the '<strong>on</strong>' function, '<strong>send</strong>' function, '<strong>client</strong>' function and others by ESP8266WebServer reference of its return value.</p>
</span><spanclass="n">Portal</span><spanclass="p">.</span><spanclass="n">onNotFound</span><spanclass="p">(</span><spanclass="n">handleNotFound</span><spanclass="p">);</span><spanclass="c1">// For only onNotFound.</span>
<spanclass="cm">/* or following one line code is equ.</span>
<spanclass="cm"> Portal.handleClient();</span>
<spanclass="cm"> */</span>
<spanclass="p">}</span>
</pre></div>
<divclass="admonition note">
<pclass="admonition-title">ESP8266WebServer function should be called after AutoConnect::begin</p>
<p>The sketch cannot refer to an instance of ESP8266WebServer until AutoConnect::begin completes successfully.</p>
</div>
<divclass="admonition warning">
<pclass="admonition-title">Do not use with ESP8266WebServer::begin</p>
<p>ESP8266WebServer is already running inside the AutoConnect.</p>
</div>
<h3id="use-with-the-pagebuilder-library"><iclass="fa fa-caret-right"></i> Use with the <ahref="https://github.com/Hieromon/PageBuilder">PageBuilder</a> library<aclass="headerlink"href="#use-with-the-pagebuilder-library"title="Permanent link">¶</a></h3>
<p>In ordinary, the URL handler will respond the request by sending some HTML. <ahref="https://github.com/Hieromon/PageBuilder">PageBuilder</a> library is HTML assembly aid. it can handle predefined HTML as like a template and simplify an HTML string assemble logic, and also the generated HTML send automatically.</p>
<p>An example sketch used with the PageBuilder as follows and it explains how it aids for the HTML generating. Details for <ahref="https://github.com/Hieromon/PageBuilder">Github repository</a>.</p>
<h3id="configuration-for-soft-ap"><iclass="fa fa-caret-right"></i> Configuration for Soft AP<aclass="headerlink"href="#configuration-for-soft-ap"title="Permanent link">¶</a></h3>
<p>AutoConnect will activate SoftAP at failed initial WiFi.Begin. It SoftAP settings are stored in <ahref="../api/index.html#autoconnectconfig-api"><em>AutoConnectConfig</em></a> as the following parameters. The sketch could be configured SoftAP using these parameters, refer <ahref="../api/index.html#autoconnectconfig-api">AutoConnectConfig API</a> for details.</p>
<ul>
<li>IP address of SoftAP activated.</li>
<li>Gateway IP address.</li>
<li>Subnet mask.</li>
<li>SSID for SoftAP.</li>
<li>Password for SoftAP.</li>
<li>Channel.</li>
<li>Hidden attribute.</li>
<li>Auto save credential.</li>
<li>Auto reset after connection establishment.</li>
<li>Home URL of the user sketch application.</li>
</ul>
<h3id="assign-user-sketchs-home-path"><iclass="fa fa-caret-right"></i> Assign user sketch's home path<aclass="headerlink"href="#assign-user-sketchs-home-path"title="Permanent link">¶</a></h3>
<p>"<strong>HOME</strong>" for returning to the user's sketch homepage is displayed at the bottom of the AutoConnect menu. It could be set using the <ahref="../api/index.html#home"><em>AutoConnect::home</em></a> function.</p>
<p><imgsrc="../images/menu_home.png"/></p>
<h3id="relocate-the-autoconnect-home-path"><iclass="fa fa-caret-right"></i> Relocate the AutoConnect home path<aclass="headerlink"href="#relocate-the-autoconnect-home-path"title="Permanent link">¶</a></h3>
<p>A home path of AutoConnect is <strong>_ac</strong> by default. You can access from the browser with http://IPADDRESS/_ac. You can change the home path by revising <strong>AUTOCONNECT_URI</strong> macro in the include header file as <ahref="https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnect.h">AutoConnect.h</a>.</p>
<p>Each <em>VARIABLE</em> conforms to the actual declaration in the sketches. <aclass="footnote-backref"href="#fnref:1"rev="footnote"title="Jump back to footnote 1 in the text">↩</a></p>
</li>
<liid="fn:2">
<p>WiFi SSID and Password can be specified AutoConnect::begin() too. <aclass="footnote-backref"href="#fnref:2"rev="footnote"title="Jump back to footnote 2 in the text">↩</a></p>
</li>
<liid="fn:3">
<p>Replacement the <strong>handleClient</strong> method is not indispensable. AutoConnect can still connect with the captive portal as it is ESP8266WebServer::handleClient. But it can <strong>not valid AutoConnect menu</strong>. <aclass="footnote-backref"href="#fnref:3"rev="footnote"title="Jump back to footnote 3 in the text">↩</a></p>