adcpcontrol.html 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. <!doctype html>
  2. <html lang="en" class="no-js">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <meta name="description" content="ESP8266/ESP32 WLAN configuration at run time with web interface">
  7. <link rel="canonical" href="https://Hieromon.github.io/AutoConnect/adcpcontrol.html">
  8. <meta name="author" content="Hieromon Ikasamo">
  9. <link rel="shortcut icon" href="assets/images/favicon.png">
  10. <meta name="generator" content="mkdocs-1.1.2, mkdocs-material-6.1.6">
  11. <title>Captive portal control - AutoConnect for ESP8266/ESP32</title>
  12. <link rel="stylesheet" href="assets/stylesheets/main.6910b76c.min.css">
  13. <link rel="stylesheet" href="assets/stylesheets/palette.196e0c26.min.css">
  14. <meta name="theme-color" content="#4051b5">
  15. <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
  16. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
  17. <style>body,input{font-family:"Roboto",-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono",SFMono-Regular,Consolas,Menlo,monospace}</style>
  18. <link rel="stylesheet" href="css/paragraph.css">
  19. <link rel="stylesheet" href="css/extra.css">
  20. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
  21. <script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","UA-116150854-1","auto"),ga("set","anonymizeIp",!0),ga("send","pageview"),document.addEventListener("DOMContentLoaded",function(){document.forms.search&&document.forms.search.query.addEventListener("blur",function(){if(this.value){var e=document.location.pathname;ga("send","pageview",e+"?q="+this.value)}})}),document.addEventListener("DOMContentSwitch",function(){ga("send","pageview",document.location.pathname)})</script>
  22. <script async src="https://www.google-analytics.com/analytics.js"></script>
  23. </head>
  24. <body dir="ltr" data-md-color-scheme="" data-md-color-primary="indigo" data-md-color-accent="indigo">
  25. <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
  26. <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
  27. <label class="md-overlay" for="__drawer"></label>
  28. <div data-md-component="skip">
  29. <a href="#captive-portal-start-control" class="md-skip">
  30. Skip to content
  31. </a>
  32. </div>
  33. <div data-md-component="announce">
  34. </div>
  35. <header class="md-header" data-md-component="header">
  36. <nav class="md-header-nav md-grid" aria-label="Header">
  37. <a href="https://Hieromon.github.io/AutoConnect/" title="AutoConnect for ESP8266/ESP32" class="md-header-nav__button md-logo" aria-label="AutoConnect for ESP8266/ESP32">
  38. <img src="images/arduino-logo.svg" alt="logo">
  39. </a>
  40. <label class="md-header-nav__button md-icon" for="__drawer">
  41. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
  42. </label>
  43. <div class="md-header-nav__title" data-md-component="header-title">
  44. <div class="md-header-nav__ellipsis">
  45. <span class="md-header-nav__topic md-ellipsis">
  46. AutoConnect for ESP8266/ESP32
  47. </span>
  48. <span class="md-header-nav__topic md-ellipsis">
  49. Captive portal control
  50. </span>
  51. </div>
  52. </div>
  53. <label class="md-header-nav__button md-icon" for="__search">
  54. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0116 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 019.5 16 6.5 6.5 0 013 9.5 6.5 6.5 0 019.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
  55. </label>
  56. <div class="md-search" data-md-component="search" role="dialog">
  57. <label class="md-search__overlay" for="__search"></label>
  58. <div class="md-search__inner" role="search">
  59. <form class="md-search__form" name="search">
  60. <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" data-md-state="active" required>
  61. <label class="md-search__icon md-icon" for="__search">
  62. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0116 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 019.5 16 6.5 6.5 0 013 9.5 6.5 6.5 0 019.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
  63. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
  64. </label>
  65. <button type="reset" class="md-search__icon md-icon" aria-label="Clear" data-md-component="search-reset" tabindex="-1">
  66. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>
  67. </button>
  68. </form>
  69. <div class="md-search__output">
  70. <div class="md-search__scrollwrap" data-md-scrollfix>
  71. <div class="md-search-result" data-md-component="search-result">
  72. <div class="md-search-result__meta">
  73. Initializing search
  74. </div>
  75. <ol class="md-search-result__list"></ol>
  76. </div>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. <div class="md-header-nav__source">
  82. <a href="https://github.com/Hieromon/AutoConnect" title="Go to repository" class="md-source">
  83. <div class="md-source__icon md-icon">
  84. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M439.55 236.05L244 40.45a28.87 28.87 0 00-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 01-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 000 40.81l195.61 195.6a28.86 28.86 0 0040.8 0l194.69-194.69a28.86 28.86 0 000-40.81z"/></svg>
  85. </div>
  86. <div class="md-source__repository">
  87. Hieromon/AutoConnect
  88. </div>
  89. </a>
  90. </div>
  91. </nav>
  92. </header>
  93. <div class="md-container" data-md-component="container">
  94. <main class="md-main" data-md-component="main">
  95. <div class="md-main__inner md-grid">
  96. <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
  97. <div class="md-sidebar__scrollwrap">
  98. <div class="md-sidebar__inner">
  99. <nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  100. <label class="md-nav__title" for="__drawer">
  101. <a href="https://Hieromon.github.io/AutoConnect/" title="AutoConnect for ESP8266/ESP32" class="md-nav__button md-logo" aria-label="AutoConnect for ESP8266/ESP32">
  102. <img src="images/arduino-logo.svg" alt="logo">
  103. </a>
  104. AutoConnect for ESP8266/ESP32
  105. </label>
  106. <div class="md-nav__source">
  107. <a href="https://github.com/Hieromon/AutoConnect" title="Go to repository" class="md-source">
  108. <div class="md-source__icon md-icon">
  109. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M439.55 236.05L244 40.45a28.87 28.87 0 00-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 01-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 000 40.81l195.61 195.6a28.86 28.86 0 0040.8 0l194.69-194.69a28.86 28.86 0 000-40.81z"/></svg>
  110. </div>
  111. <div class="md-source__repository">
  112. Hieromon/AutoConnect
  113. </div>
  114. </a>
  115. </div>
  116. <ul class="md-nav__list" data-md-scrollfix>
  117. <li class="md-nav__item">
  118. <a href="index.html" class="md-nav__link">
  119. Overview
  120. </a>
  121. </li>
  122. <li class="md-nav__item">
  123. <a href="gettingstarted.html" class="md-nav__link">
  124. Getting started
  125. </a>
  126. </li>
  127. <li class="md-nav__item">
  128. <a href="menu.html" class="md-nav__link">
  129. AutoConnect menu
  130. </a>
  131. </li>
  132. <li class="md-nav__item">
  133. <a href="basicusage.html" class="md-nav__link">
  134. Basic usage
  135. </a>
  136. </li>
  137. <li class="md-nav__item md-nav__item--active md-nav__item--nested">
  138. <input class="md-nav__toggle md-toggle" data-md-toggle="nav-5" type="checkbox" id="nav-5" checked>
  139. <label class="md-nav__link" for="nav-5">
  140. Advanced usage
  141. <span class="md-nav__icon md-icon"></span>
  142. </label>
  143. <nav class="md-nav" aria-label="Advanced usage" data-md-level="1">
  144. <label class="md-nav__title" for="nav-5">
  145. <span class="md-nav__icon md-icon"></span>
  146. Advanced usage
  147. </label>
  148. <ul class="md-nav__list" data-md-scrollfix>
  149. <li class="md-nav__item">
  150. <a href="advancedusage.html" class="md-nav__link">
  151. Advanced usage
  152. </a>
  153. </li>
  154. <li class="md-nav__item">
  155. <a href="adconnection.html" class="md-nav__link">
  156. AutoConnect WiFi connection control
  157. </a>
  158. </li>
  159. <li class="md-nav__item md-nav__item--active">
  160. <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc">
  161. <label class="md-nav__link md-nav__link--active" for="__toc">
  162. Captive portal control
  163. <span class="md-nav__icon md-icon"></span>
  164. </label>
  165. <a href="adcpcontrol.html" class="md-nav__link md-nav__link--active">
  166. Captive portal control
  167. </a>
  168. <nav class="md-nav md-nav--secondary" aria-label="Table of contents">
  169. <label class="md-nav__title" for="__toc">
  170. <span class="md-nav__icon md-icon"></span>
  171. Table of contents
  172. </label>
  173. <ul class="md-nav__list" data-md-scrollfix>
  174. <li class="md-nav__item">
  175. <a href="#captive-portal-start-control" class="md-nav__link">
  176. Captive portal start control
  177. </a>
  178. </li>
  179. <li class="md-nav__item">
  180. <a href="#captive-portal-start-detection" class="md-nav__link">
  181. Captive portal start detection
  182. </a>
  183. </li>
  184. <li class="md-nav__item">
  185. <a href="#captive-portal-timeout-control" class="md-nav__link">
  186. Captive portal timeout control
  187. </a>
  188. </li>
  189. <li class="md-nav__item">
  190. <a href="#disable-the-captive-portal" class="md-nav__link">
  191. Disable the captive portal
  192. </a>
  193. </li>
  194. <li class="md-nav__item">
  195. <a href="#launch-the-captive-portal-on-demand-by-external-trigger" class="md-nav__link">
  196. Launch the captive portal on demand by external trigger
  197. </a>
  198. </li>
  199. <li class="md-nav__item">
  200. <a href="#launch-the-captive-portal-on-demand-at-losing-wifi" class="md-nav__link">
  201. Launch the captive portal on-demand at losing WiFi
  202. </a>
  203. </li>
  204. <li class="md-nav__item">
  205. <a href="#shutdown-the-captive-portal" class="md-nav__link">
  206. Shutdown the captive portal
  207. </a>
  208. </li>
  209. <li class="md-nav__item">
  210. <a href="#sketch-execution-during-the-captive-portal-loop" class="md-nav__link">
  211. Sketch execution during the captive portal loop
  212. </a>
  213. </li>
  214. </ul>
  215. </nav>
  216. </li>
  217. <li class="md-nav__item">
  218. <a href="adnetwork.html" class="md-nav__link">
  219. Settings and controls for network and WiFi
  220. </a>
  221. </li>
  222. <li class="md-nav__item">
  223. <a href="adauthentication.html" class="md-nav__link">
  224. Authentication settings
  225. </a>
  226. </li>
  227. <li class="md-nav__item">
  228. <a href="adcredential.html" class="md-nav__link">
  229. Credential accesses
  230. </a>
  231. </li>
  232. <li class="md-nav__item">
  233. <a href="adexterior.html" class="md-nav__link">
  234. Customizing page appearance
  235. </a>
  236. </li>
  237. <li class="md-nav__item">
  238. <a href="adothers.html" class="md-nav__link">
  239. Other operation settings and controls
  240. </a>
  241. </li>
  242. </ul>
  243. </nav>
  244. </li>
  245. <li class="md-nav__item md-nav__item--nested">
  246. <input class="md-nav__toggle md-toggle" data-md-toggle="nav-6" type="checkbox" id="nav-6" >
  247. <label class="md-nav__link" for="nav-6">
  248. Custom Web pages
  249. <span class="md-nav__icon md-icon"></span>
  250. </label>
  251. <nav class="md-nav" aria-label="Custom Web pages" data-md-level="1">
  252. <label class="md-nav__title" for="nav-6">
  253. <span class="md-nav__icon md-icon"></span>
  254. Custom Web pages
  255. </label>
  256. <ul class="md-nav__list" data-md-scrollfix>
  257. <li class="md-nav__item">
  258. <a href="acintro.html" class="md-nav__link">
  259. Custom Web pages with AutoConnect
  260. </a>
  261. </li>
  262. <li class="md-nav__item">
  263. <a href="acelements.html" class="md-nav__link">
  264. AutoConnectElements
  265. </a>
  266. </li>
  267. <li class="md-nav__item">
  268. <a href="acjson.html" class="md-nav__link">
  269. Custom Web pages with JSON
  270. </a>
  271. </li>
  272. <li class="md-nav__item">
  273. <a href="achandling.html" class="md-nav__link">
  274. Handling the custom Web pages
  275. </a>
  276. </li>
  277. </ul>
  278. </nav>
  279. </li>
  280. <li class="md-nav__item md-nav__item--nested">
  281. <input class="md-nav__toggle md-toggle" data-md-toggle="nav-7" type="checkbox" id="nav-7" >
  282. <label class="md-nav__link" for="nav-7">
  283. OTA Updates
  284. <span class="md-nav__icon md-icon"></span>
  285. </label>
  286. <nav class="md-nav" aria-label="OTA Updates" data-md-level="1">
  287. <label class="md-nav__title" for="nav-7">
  288. <span class="md-nav__icon md-icon"></span>
  289. OTA Updates
  290. </label>
  291. <ul class="md-nav__list" data-md-scrollfix>
  292. <li class="md-nav__item">
  293. <a href="otaupdate.html" class="md-nav__link">
  294. OTA Updates
  295. </a>
  296. </li>
  297. <li class="md-nav__item">
  298. <a href="otabrowser.html" class="md-nav__link">
  299. Using Web Browser
  300. </a>
  301. </li>
  302. <li class="md-nav__item">
  303. <a href="otaserver.html" class="md-nav__link">
  304. Using Update Server
  305. </a>
  306. </li>
  307. </ul>
  308. </nav>
  309. </li>
  310. <li class="md-nav__item md-nav__item--nested">
  311. <input class="md-nav__toggle md-toggle" data-md-toggle="nav-8" type="checkbox" id="nav-8" >
  312. <label class="md-nav__link" for="nav-8">
  313. Library APIs
  314. <span class="md-nav__icon md-icon"></span>
  315. </label>
  316. <nav class="md-nav" aria-label="Library APIs" data-md-level="1">
  317. <label class="md-nav__title" for="nav-8">
  318. <span class="md-nav__icon md-icon"></span>
  319. Library APIs
  320. </label>
  321. <ul class="md-nav__list" data-md-scrollfix>
  322. <li class="md-nav__item">
  323. <a href="api.html" class="md-nav__link">
  324. AutoConnect API
  325. </a>
  326. </li>
  327. <li class="md-nav__item">
  328. <a href="apiaux.html" class="md-nav__link">
  329. AutoConnectAux API
  330. </a>
  331. </li>
  332. <li class="md-nav__item">
  333. <a href="apiconfig.html" class="md-nav__link">
  334. AutoConnectConfig API
  335. </a>
  336. </li>
  337. <li class="md-nav__item">
  338. <a href="apielements.html" class="md-nav__link">
  339. AutoConnectElements API
  340. </a>
  341. </li>
  342. <li class="md-nav__item">
  343. <a href="apiupdate.html" class="md-nav__link">
  344. AutoConnectUpdate API
  345. </a>
  346. </li>
  347. <li class="md-nav__item">
  348. <a href="apiextra.html" class="md-nav__link">
  349. Something extra
  350. </a>
  351. </li>
  352. </ul>
  353. </nav>
  354. </li>
  355. <li class="md-nav__item md-nav__item--nested">
  356. <input class="md-nav__toggle md-toggle" data-md-toggle="nav-9" type="checkbox" id="nav-9" >
  357. <label class="md-nav__link" for="nav-9">
  358. Examples
  359. <span class="md-nav__icon md-icon"></span>
  360. </label>
  361. <nav class="md-nav" aria-label="Examples" data-md-level="1">
  362. <label class="md-nav__title" for="nav-9">
  363. <span class="md-nav__icon md-icon"></span>
  364. Examples
  365. </label>
  366. <ul class="md-nav__list" data-md-scrollfix>
  367. <li class="md-nav__item">
  368. <a href="howtoembed.html" class="md-nav__link">
  369. How to embed
  370. </a>
  371. </li>
  372. <li class="md-nav__item">
  373. <a href="datatips.html" class="md-nav__link">
  374. Tips for data conversion
  375. </a>
  376. </li>
  377. <li class="md-nav__item">
  378. <a href="menuize.html" class="md-nav__link">
  379. Attach the menus
  380. </a>
  381. </li>
  382. <li class="md-nav__item">
  383. <a href="wojson.html" class="md-nav__link">
  384. Custom Web pages w/o JSON
  385. </a>
  386. </li>
  387. </ul>
  388. </nav>
  389. </li>
  390. <li class="md-nav__item md-nav__item--nested">
  391. <input class="md-nav__toggle md-toggle" data-md-toggle="nav-10" type="checkbox" id="nav-10" >
  392. <label class="md-nav__link" for="nav-10">
  393. Appendix
  394. <span class="md-nav__icon md-icon"></span>
  395. </label>
  396. <nav class="md-nav" aria-label="Appendix" data-md-level="1">
  397. <label class="md-nav__title" for="nav-10">
  398. <span class="md-nav__icon md-icon"></span>
  399. Appendix
  400. </label>
  401. <ul class="md-nav__list" data-md-scrollfix>
  402. <li class="md-nav__item">
  403. <a href="lsbegin.html" class="md-nav__link">
  404. Inside AutoConnect::begin
  405. </a>
  406. </li>
  407. <li class="md-nav__item">
  408. <a href="credit.html" class="md-nav__link">
  409. Saved credentials access
  410. </a>
  411. </li>
  412. <li class="md-nav__item">
  413. <a href="acupload.html" class="md-nav__link">
  414. File upload handler
  415. </a>
  416. </li>
  417. <li class="md-nav__item">
  418. <a href="colorized.html" class="md-nav__link">
  419. Custom colorized
  420. </a>
  421. </li>
  422. <li class="md-nav__item">
  423. <a href="changelabel.html" class="md-nav__link">
  424. Change label text
  425. </a>
  426. </li>
  427. </ul>
  428. </nav>
  429. </li>
  430. <li class="md-nav__item">
  431. <a href="faq.html" class="md-nav__link">
  432. FAQ
  433. </a>
  434. </li>
  435. <li class="md-nav__item">
  436. <a href="changelog.html" class="md-nav__link">
  437. Change log
  438. </a>
  439. </li>
  440. <li class="md-nav__item">
  441. <a href="license.html" class="md-nav__link">
  442. License
  443. </a>
  444. </li>
  445. </ul>
  446. </nav>
  447. </div>
  448. </div>
  449. </div>
  450. <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
  451. <div class="md-sidebar__scrollwrap">
  452. <div class="md-sidebar__inner">
  453. <nav class="md-nav md-nav--secondary" aria-label="Table of contents">
  454. <label class="md-nav__title" for="__toc">
  455. <span class="md-nav__icon md-icon"></span>
  456. Table of contents
  457. </label>
  458. <ul class="md-nav__list" data-md-scrollfix>
  459. <li class="md-nav__item">
  460. <a href="#captive-portal-start-control" class="md-nav__link">
  461. Captive portal start control
  462. </a>
  463. </li>
  464. <li class="md-nav__item">
  465. <a href="#captive-portal-start-detection" class="md-nav__link">
  466. Captive portal start detection
  467. </a>
  468. </li>
  469. <li class="md-nav__item">
  470. <a href="#captive-portal-timeout-control" class="md-nav__link">
  471. Captive portal timeout control
  472. </a>
  473. </li>
  474. <li class="md-nav__item">
  475. <a href="#disable-the-captive-portal" class="md-nav__link">
  476. Disable the captive portal
  477. </a>
  478. </li>
  479. <li class="md-nav__item">
  480. <a href="#launch-the-captive-portal-on-demand-by-external-trigger" class="md-nav__link">
  481. Launch the captive portal on demand by external trigger
  482. </a>
  483. </li>
  484. <li class="md-nav__item">
  485. <a href="#launch-the-captive-portal-on-demand-at-losing-wifi" class="md-nav__link">
  486. Launch the captive portal on-demand at losing WiFi
  487. </a>
  488. </li>
  489. <li class="md-nav__item">
  490. <a href="#shutdown-the-captive-portal" class="md-nav__link">
  491. Shutdown the captive portal
  492. </a>
  493. </li>
  494. <li class="md-nav__item">
  495. <a href="#sketch-execution-during-the-captive-portal-loop" class="md-nav__link">
  496. Sketch execution during the captive portal loop
  497. </a>
  498. </li>
  499. </ul>
  500. </nav>
  501. </div>
  502. </div>
  503. </div>
  504. <div class="md-content">
  505. <article class="md-content__inner md-typeset">
  506. <h1>Captive portal control</h1>
  507. <p>The default behavior of AutoConnect is to launch the captive portal if 1<sup>st</sup>-WiFi.begin attempting inside <a href="api.html#begin">AutoConnect::begin</a> fails. You can change this default behavior through the <a href="apiconfig.html">AutoConnectConfig</a> settings join together with Sketch code that implements to control the WiFi connection attempting.</p>
  508. <ul>
  509. <li><a href="#captive-portal-start-control">Captive portal start control</a></li>
  510. <li><a href="#captive-portal-start-detection">Captive portal start detection</a></li>
  511. <li><a href="#captive-portal-timeout-control">Captive portal timeout control</a></li>
  512. <li><a href="#disable-the-captive-portal">Disable the captive portal</a></li>
  513. <li><a href="#launch-the-captive-portal-on-demand-by-external-trigger">Launch the captive portal on demand by external trigger</a></li>
  514. <li><a href="#launch-the-captive-portal-on-demand-at-losing-wifi">Launch the captive portal on-demand at losing WiFi</a></li>
  515. <li><a href="#shutdown-the-captive-portal">Shutdown the captive portal</a></li>
  516. <li><a href="#sketch-execution-during-the-captive-portal-loop">Sketch execution during the captive portal loop</a></li>
  517. </ul>
  518. <h2 id="captive-portal-start-control">Captive portal start control<a class="headerlink" href="#captive-portal-start-control" title="Permanent link">&para;</a></h2>
  519. <p>AutoConnect will launch the captive portal based on the <a href="apiconfig.html">AutoConnectConfig</a> settings when the WiFi connection status will become to certain conditions. <a href="apiconfig.html#autorise"><em>AutoConnectConfig::autoRise</em></a> and <a href="apiconfig.html#immediatestart"><em>AutoConnectConfig::immediateStart</em></a> are concern the conditions to launch the captive portal. Also, the <a href="apiconfig.html#retainportal"><em>AutoConnectConfig::retainPortal</em></a> controls the continuity of the captive portal state and allows the Sketch to launch the captive portal dynamically even while in a <a href="api.html#handleclient"><strong>handleClient</strong></a> loop.</p>
  520. <p>The <a href="apiconfig.html#autorise"><strong>autoRise</strong></a> allows or disallows the launch of the captive portal. AutoConnect launches the captive portal only if the autoRise is <strong>true</strong>. If the autoRise is <strong>false</strong>, the captive portal will not start even if the WiFi connection is lost.</p>
  521. <p>Basically, the captive portal initiation is triggered by the result of 1<sup>st</sup>-WiFi.begin, but Sketch can control it according to direct the following four actions by configuring AutoConnectConfig with two parameters, the <a href="apiconfig.html#immediatestart"><strong>immediateStart</strong></a> and the <a href="apiconfig.html#autorise"><strong>autoRise</strong></a>.</p>
  522. <table>
  523. <tr>
  524. <th rowspan="2" align="center">AutoConnectConfig<br>::immediateStart</th>
  525. <th colspan="2" align="center">AutoConnectConfig::autoRise</th>
  526. </tr>
  527. <tr>
  528. <td>true</td>
  529. <td>false</td>
  530. </tr>
  531. <tr>
  532. <td>true</td>
  533. <td>Skip 1st-WiFi.begin<br>ESP module becomes SoftAP and the captive portal starts immediately.<br></td>
  534. <td>Not attempt WiFi connection.<br>Only WebServer will start in STA mode.</td>
  535. </tr>
  536. <tr>
  537. <td>false</td>
  538. <td>Attempts WiFi connection in STA mode.<br>In some cases, the autoReconnect may restore the connection even if 1st-WiFiBeing fails.<br>If the connection is completely lost, the captive portal will be launched.<br><b>This is the default.</b></td>
  539. <td>Attempts WiFi connection in STA mode.<br>In some cases, the autoReconnect may restore the connection even if 1st-WiFiBeing fails.<br>ESP module stays in STA mode and WebServer will start.</td>
  540. </tr>
  541. </table>
  542. <p>The <a href="apiconfig.html#retainportal"><strong>retainPortal</strong></a> specifies the continuity of the captive portal after <a href="api.html#begin">AutoConnect::begin</a>, allowing the captive portal would be launched after the Sketch execution has transitioned into the <strong>loop</strong> function. When AutoConnect establishes a WiFi connection while in the captive portal within <a href="api.html#begin">AutoConnect::begin</a>, it stops the DNS server to close the captive portal with SoftAP still running. In this situation, if the Sketch loses the established WiFi connection while executing the loop function, it can reopen the captive portal.</p>
  543. <p>However, this behavior can be redundant depending on the Sketch characteristics. In such a case, you can prevent to starting the captive portal during the <code>loop()</code> by <a href="apiconfig.html#autorise"><strong>autoRise</strong></a> setting to <strong>false</strong>.</p>
  544. <h2 id="captive-portal-start-detection">Captive portal start detection<a class="headerlink" href="#captive-portal-start-detection" title="Permanent link">&para;</a></h2>
  545. <p>The captive portal will only be activated if 1<sup>st</sup>-WiFi::begin fails. Sketch can detect with the <a href="api.html#ondetect">AutoConnect::onDetect</a> function 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>
  546. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">Portal;</span>
  547. <span style="background-color: #49483e"><span style="color: #66d9ef">bool</span> <span style="color: #a6e22e">startCP</span><span style="color: #f8f8f2">(IPAddress</span><span style="color: #f92672">&amp;</span> <span style="color: #f8f8f2">ip)</span> <span style="color: #f8f8f2">{</span>
  548. </span> <span style="color: #f8f8f2">digitalWrite(BUILTIN_LED,</span> <span style="color: #f8f8f2">HIGH);</span>
  549. <span style="color: #f8f8f2">Serial.println(</span><span style="color: #e6db74">&quot;C.P. started, IP:&quot;</span> <span style="color: #f92672">+</span> <span style="color: #f8f8f2">WiFi.localIP().toString());</span>
  550. <span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">true;</span>
  551. <span style="color: #f8f8f2">}</span>
  552. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  553. <span style="color: #f8f8f2">Serial.begin(</span><span style="color: #ae81ff">115200</span><span style="color: #f8f8f2">);</span>
  554. <span style="color: #f8f8f2">pinMode(BUILTIN_LED,</span> <span style="color: #f8f8f2">OUTPUT);</span>
  555. <span style="color: #f8f8f2">digitalWrite(BUILTIN_LED,</span> <span style="color: #f8f8f2">LOW);</span>
  556. <span style="background-color: #49483e"> <span style="color: #f8f8f2">Portal.onDetect(startCP);</span>
  557. </span> <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(Portal.begin())</span> <span style="color: #f8f8f2">{</span>
  558. <span style="color: #f8f8f2">digitalWrite(BUILTIN_LED,</span> <span style="color: #f8f8f2">LOW);</span>
  559. <span style="color: #f8f8f2">}</span>
  560. <span style="color: #f8f8f2">}</span>
  561. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  562. <span style="color: #f8f8f2">Portal.handleClient();</span>
  563. <span style="color: #f8f8f2">}</span>
  564. </code></pre></div>
  565. <h2 id="captive-portal-timeout-control">Captive portal timeout control<a class="headerlink" href="#captive-portal-timeout-control" title="Permanent link">&para;</a></h2>
  566. <p>Once AutoConnect has entered the captive portal state due to the above conditions, the default behavior is that <a href="api.html#begin">AutoConnect::begin</a> will not exit until a WiFi connection is established. Captive portal timeout control prevents AutoConnect from blocking the Sketch progress. The Sketch can abort AutoConnect::begin and returns control to the Sketch. </p>
  567. <p>AutoConnect has two parameters for timeout control. The first is the timeout value used when trying to connect to the specified AP. It works like a typical timeout control for connection attempts with WiFi.begin. This setting is specified by the <a href="apiconfig.html#begintimeout"><em>AutoConnectConfig::beginTimeout</em></a> or third argument of the <a href="api.html#begin">AutoConnect::begin</a> function. The default value is the macro defined by <a href="https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnectDefs.h#L103"><strong>AUTOCONNECT_TIMEOUT</strong></a> in the <a href="https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnectDefs.h"><code>AutoConnectDefs.h</code></a> header file.</p>
  568. <p>Another timeout control is for the captive portal itself. It is useful if you want to keep the Sketch offline running even if a WiFi connection is not possible. You can also combine its setting with the <a href="#launch-the-captive-portal-on-demand-by-external-trigger"><strong>immediateStart</strong></a> option to create highly mobile sketches. The timeout of the captive portal is specified by the <a href="apiconfig.html#portaltimeout"><em>AutoConnectConfig::portalTimeout</em></a> as follows.</p>
  569. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #75715e">#include</span> <span style="color: #75715e">&lt;ESP8266WiFi.h&gt;</span>
  570. <span style="color: #75715e">#include</span> <span style="color: #75715e">&lt;ESP8266WebServer.h&gt;</span>
  571. <span style="color: #75715e">#include</span> <span style="color: #75715e">&lt;AutoConnect.h&gt;</span>
  572. <span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">portal;</span>
  573. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">config;</span>
  574. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  575. <span style="background-color: #49483e"> <span style="color: #f8f8f2">config.portalTimeout</span> <span style="color: #f92672">=</span> <span style="color: #ae81ff">60000</span><span style="color: #f8f8f2">;</span> <span style="color: #75715e">// It will time out in 60 seconds</span>
  576. </span> <span style="color: #f8f8f2">portal.config(config);</span>
  577. <span style="color: #f8f8f2">portal.begin();</span>
  578. <span style="color: #f8f8f2">}</span>
  579. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  580. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">==</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  581. <span style="color: #75715e">// Some sketch code for the connected scene is here.</span>
  582. <span style="color: #f8f8f2">}</span>
  583. <span style="color: #66d9ef">else</span> <span style="color: #f8f8f2">{</span>
  584. <span style="color: #75715e">// Some sketch code for not connected scene is here.</span>
  585. <span style="color: #f8f8f2">}</span>
  586. <span style="color: #f8f8f2">portal.handleClient();</span>
  587. <span style="color: #f8f8f2">}</span>
  588. </code></pre></div>
  589. <p>Also, if you want to stop AutoConnect completely when the captive portal is timed out, you need to call the <a href="api.html#end">AutoConnect::end</a> function. It looks like the following code:</p>
  590. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #66d9ef">bool</span> <span style="color: #f8f8f2">acEnable;</span>
  591. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  592. <span style="color: #f8f8f2">config.portalTimeout</span> <span style="color: #f92672">=</span> <span style="color: #ae81ff">60000</span><span style="color: #f8f8f2">;</span> <span style="color: #75715e">// It will time out in 60 seconds</span>
  593. <span style="color: #f8f8f2">portal.config(config);</span>
  594. <span style="color: #f8f8f2">acEnable</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">portal.begin();</span>
  595. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(</span><span style="color: #f92672">!</span><span style="color: #f8f8f2">acEnable)</span> <span style="color: #f8f8f2">{</span>
  596. <span style="color: #f8f8f2">portal.end();</span>
  597. <span style="color: #f8f8f2">}</span>
  598. <span style="color: #f8f8f2">}</span>
  599. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  600. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">==</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  601. <span style="color: #75715e">// Some sketch code for the connected scene is here.</span>
  602. <span style="color: #f8f8f2">}</span>
  603. <span style="color: #66d9ef">else</span> <span style="color: #f8f8f2">{</span>
  604. <span style="color: #75715e">// Some sketch code for not connected scene is here.</span>
  605. <span style="color: #f8f8f2">}</span>
  606. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(acEnable)</span> <span style="color: #f8f8f2">{</span>
  607. <span style="color: #f8f8f2">portal.handleClient();</span>
  608. <span style="color: #f8f8f2">}</span>
  609. <span style="color: #f8f8f2">}</span>
  610. </code></pre></div>
  611. <p>AutoConnectConfig has another option related to timeouts that you can enable to take advantage of the captive portal feature after a timeout occurrence. The <a href="apiconfig.html#retainportal"><em>AutoConnectConfig::retainPortal</em></a> option will not shut down SoftAP and the internal DNS server even though <a href="api.html#begin">AutoConnect::begin</a> has aborted due to a timeout occurrence. Even after the captive portal times out, you can always try to connect to the AP while keeping the Sketch running offline.</p>
  612. <p>The following sample code is the Sketch above with the <a href="apiconfig.html#retainportal"><strong>retainPortal</strong></a> setting added. As you can see, the implementation for captive portal continuation does not affect the main logic of the Sketch.</p>
  613. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #75715e">#include</span> <span style="color: #75715e">&lt;ESP8266WiFi.h&gt;</span>
  614. <span style="color: #75715e">#include</span> <span style="color: #75715e">&lt;ESP8266WebServer.h&gt;</span>
  615. <span style="color: #75715e">#include</span> <span style="color: #75715e">&lt;AutoConnect.h&gt;</span>
  616. <span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">portal;</span>
  617. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">config;</span>
  618. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  619. <span style="color: #f8f8f2">config.portalTimeout</span> <span style="color: #f92672">=</span> <span style="color: #ae81ff">60000</span><span style="color: #f8f8f2">;</span> <span style="color: #75715e">// It will time out in 60 seconds</span>
  620. <span style="background-color: #49483e"> <span style="color: #f8f8f2">config.retainPortal</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  621. </span> <span style="color: #f8f8f2">portal.config(config);</span>
  622. <span style="color: #f8f8f2">portal.begin();</span>
  623. <span style="color: #f8f8f2">}</span>
  624. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  625. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">==</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  626. <span style="color: #75715e">// Some sketch code for the connected scene is here.</span>
  627. <span style="color: #f8f8f2">}</span>
  628. <span style="color: #66d9ef">else</span> <span style="color: #f8f8f2">{</span>
  629. <span style="color: #75715e">// Some sketch code for not connected scene is here.</span>
  630. <span style="color: #f8f8f2">}</span>
  631. <span style="color: #f8f8f2">portal.handleClient();</span>
  632. <span style="color: #f8f8f2">}</span>
  633. </code></pre></div>
  634. <h2 id="disable-the-captive-portal">Disable the captive portal<a class="headerlink" href="#disable-the-captive-portal" title="Permanent link">&para;</a></h2>
  635. <p>It can also prevent the captive portal from starting even if the connection at the 1<sup>st</sup>-WiFi.begin fails. In this case, <a href="api.html#begin">AutoConnect::begin</a> behaves same as <em>WiFi.begin</em>.</p>
  636. <p>For disabling the captive portal, <a href="apiconfig.html#autorise"><strong>autoRise</strong></a> sets to false with <a href="apiconfig.html">AutoConnectConfig</a>.</p>
  637. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">portal;</span>
  638. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">acConfig;</span>
  639. <span style="background-color: #49483e"><span style="color: #f8f8f2">acConfig.autoRise</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">false;</span>
  640. </span><span style="color: #f8f8f2">portal.config(acConfig);</span>
  641. <span style="color: #f8f8f2">portal.begin();</span>
  642. </code></pre></div>
  643. <h2 id="launch-the-captive-portal-on-demand-by-external-trigger">Launch the captive portal on demand by external trigger<a class="headerlink" href="#launch-the-captive-portal-on-demand-by-external-trigger" title="Permanent link">&para;</a></h2>
  644. <p>The <a href="lsbegin.html">default behavior</a> of <a href="api.html#begin">AutoConnect::begin</a> gives priority to connect to the least recently established access point. In general, We expect this behavior in most situations, but will intentionally launch the captive portal on some occasion.</p>
  645. <p>Here section describes how to launch on demand the captive portal, and suggests two templates that you can use to implement it.</p>
  646. <ol>
  647. <li>
  648. <p>Offline for usual operation, connect to WiFi with an external switch</p>
  649. <p>You can use this template if the ESP module does not connect to WiFi at an ordinal situation and need to establish by a manual trigger. In this case, it is desirable that AutoConnect not start until an external switch fires. This behavior is similar to the <a href="https://github.com/tzapu/WiFiManager#on-demand-configuration-portal">WiFiManager's startConfigPortal</a> function.<br />
  650. <a href="apiconfig.html#immediatestart"><em>AutoConnectConfig::immediateStart</em></a> is an option to launch the portal by the SoftAP immediately without attempting 1<sup>st</sup>-WiFi.begin. Also, by setting the <a href="apiconfig.html#autorise"><em>AutoConnectConfig::autoRise</em></a> option to false, it is possible to suppress unintended automatic pop-ups of the portal screen when connecting to an ESP module SSID.<br />
  651. To implement this, execute AutoConnect::config within the <strong>setup()</strong> function as usual, and handle AutoConnect::begin inside the <strong>loop()</strong> function.</p>
  652. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #75715e">#define TRIGGER_PIN 5 // Trigger switch should be LOW active.</span>
  653. <span style="color: #75715e">#define HOLD_TIMER 3000</span>
  654. <span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">Portal;</span>
  655. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">Config;</span>
  656. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  657. <span style="color: #f8f8f2">pinMode(</span><span style="color: #ae81ff">5</span><span style="color: #f8f8f2">,</span> <span style="color: #f8f8f2">INPUT_PULLUP);</span>
  658. <span style="background-color: #49483e"> <span style="color: #f8f8f2">Config.immediateStart</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  659. </span> <span style="color: #75715e">// Config.autoRise = false; // If you don&#39;t need to automatically pop-up the portal when connected to the ESP module&#39;s SSID.</span>
  660. <span style="color: #f8f8f2">Portal.config(Config);</span>
  661. <span style="color: #f8f8f2">}</span>
  662. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  663. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(digitalRead(TRIGGER_PIN)</span> <span style="color: #f92672">==</span> <span style="color: #f8f8f2">LOW)</span> <span style="color: #f8f8f2">{</span>
  664. <span style="color: #66d9ef">unsigned</span> <span style="color: #66d9ef">long</span> <span style="color: #f8f8f2">tm</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">millis();</span>
  665. <span style="color: #66d9ef">while</span> <span style="color: #f8f8f2">(digitalRead(TRIGGER_PIN)</span> <span style="color: #f92672">==</span> <span style="color: #f8f8f2">LOW)</span> <span style="color: #f8f8f2">{</span>
  666. <span style="color: #f8f8f2">yield();</span>
  667. <span style="color: #f8f8f2">}</span>
  668. <span style="color: #75715e">// Hold the switch while HOLD_TIMER time to start connect.</span>
  669. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(millis()</span> <span style="color: #f92672">-</span> <span style="color: #f8f8f2">tm</span> <span style="color: #f92672">&gt;</span> <span style="color: #f8f8f2">HOLD_TIMER)</span>
  670. <span style="color: #f8f8f2">Portal.begin();</span>
  671. <span style="color: #f8f8f2">}</span>
  672. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">==</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  673. <span style="color: #75715e">// Here, what to do if the module has connected to a WiFi access point</span>
  674. <span style="color: #f8f8f2">}</span>
  675. <span style="color: #75715e">// Main process of your sketch</span>
  676. <span style="color: #f8f8f2">Portal.handleClient();</span> <span style="color: #75715e">// If WiFi is not connected, handleClient will do nothing.</span>
  677. <span style="color: #f8f8f2">}</span>
  678. </code></pre></div>
  679. <div class="admonition info">
  680. <p class="admonition-title">It will not be automatic reconnect</p>
  681. <p>The above example does not connect to WiFi until TRIGGER_PIN goes LOW. When TRIGGER_PIN goes LOW, the captive portal starts and you can connect to WiFi. Even if you reset the module, it will not automatically reconnect.</p>
  682. </div>
  683. </li>
  684. <li>
  685. <p>Register new access points on demand</p>
  686. <p>The following template is useful for controlling the registration of unknown access points. In this case, the ESP module establishes a WiFi connection using WiFi.begin natively without relying on AutoConnect.<br />
  687. Known access point credentials are saved by AutoConnect, to the ESP module can use the saved credentials to handle WiFi.begin natively. This means that you can explicitly register available access points when needed, and the ESP module will not use unknown access points under normal situations.</p>
  688. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span><span style="color: #f92672">*</span> <span style="color: #f8f8f2">portal</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">nullptr</span><span style="color: #f8f8f2">;</span>
  689. <span style="color: #66d9ef">bool</span> <span style="color: #a6e22e">detectSwitch</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  690. <span style="color: #75715e">/*</span>
  691. <span style="color: #75715e"> Returns true if an external switch to configure is active.</span>
  692. <span style="color: #75715e"> */</span>
  693. <span style="color: #f8f8f2">}</span>
  694. <span style="color: #66d9ef">bool</span> <span style="color: #a6e22e">connectWiFi</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">const</span> <span style="color: #66d9ef">char</span><span style="color: #f92672">*</span> <span style="color: #f8f8f2">ssid,</span> <span style="color: #66d9ef">const</span> <span style="color: #66d9ef">char</span><span style="color: #f92672">*</span> <span style="color: #f8f8f2">password,</span> <span style="color: #66d9ef">unsigned</span> <span style="color: #66d9ef">long</span> <span style="color: #f8f8f2">timeout)</span> <span style="color: #f8f8f2">{</span>
  695. <span style="color: #f8f8f2">WiFi.mode(WiFi_STA);</span>
  696. <span style="color: #f8f8f2">delay(</span><span style="color: #ae81ff">100</span><span style="color: #f8f8f2">);</span>
  697. <span style="color: #f8f8f2">WiFi.begin(ssid,</span> <span style="color: #f8f8f2">password);</span>
  698. <span style="color: #66d9ef">unsigned</span> <span style="color: #66d9ef">long</span> <span style="color: #f8f8f2">tm</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">millis();</span>
  699. <span style="color: #66d9ef">while</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">!=</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  700. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(millis()</span> <span style="color: #f92672">-</span> <span style="color: #f8f8f2">tm</span> <span style="color: #f92672">&gt;</span> <span style="color: #f8f8f2">timeout)</span>
  701. <span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">false;</span>
  702. <span style="color: #f8f8f2">}</span>
  703. <span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">true;</span>
  704. <span style="color: #f8f8f2">}</span>
  705. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  706. <span style="color: #f8f8f2">AutoConnectCredential</span> <span style="color: #f8f8f2">credt;</span>
  707. <span style="color: #f8f8f2">station_config_t</span> <span style="color: #f8f8f2">config;</span>
  708. <span style="color: #66d9ef">for</span> <span style="color: #f8f8f2">(</span><span style="color: #66d9ef">int8_t</span> <span style="color: #f8f8f2">e</span> <span style="color: #f92672">=</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span> <span style="color: #f8f8f2">e</span> <span style="color: #f92672">&lt;</span> <span style="color: #f8f8f2">credt.entries();</span> <span style="color: #f8f8f2">e</span><span style="color: #f92672">++</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
  709. <span style="color: #f8f8f2">credt.load(e,</span> <span style="color: #f92672">&amp;</span><span style="color: #f8f8f2">config);</span>
  710. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(connectWiFi(config.ssid,</span> <span style="color: #f8f8f2">config.password,</span> <span style="color: #ae81ff">30000</span><span style="color: #f8f8f2">))</span>
  711. <span style="color: #66d9ef">break</span><span style="color: #f8f8f2">;</span>
  712. <span style="color: #f8f8f2">}</span>
  713. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">!=</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  714. <span style="color: #75715e">// Here, do something when WiFi cannot reach.</span>
  715. <span style="color: #f8f8f2">}</span>
  716. <span style="color: #f8f8f2">}</span>
  717. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  718. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(detectSwitch())</span> <span style="color: #f8f8f2">{</span>
  719. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">config;</span>
  720. <span style="color: #f8f8f2">config.immediateStart</span><span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  721. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(</span><span style="color: #f92672">!</span><span style="color: #f8f8f2">portal)</span> <span style="color: #f8f8f2">{</span>
  722. <span style="color: #f8f8f2">portal</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">new</span> <span style="color: #f8f8f2">AutoConnect;</span>
  723. <span style="color: #f8f8f2">}</span>
  724. <span style="color: #f8f8f2">portal</span><span style="color: #f92672">-&gt;</span><span style="color: #f8f8f2">config(config);</span>
  725. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(portal</span><span style="color: #f92672">-&gt;</span><span style="color: #f8f8f2">begin())</span> <span style="color: #f8f8f2">{</span>
  726. <span style="color: #f8f8f2">portal</span><span style="color: #f92672">-&gt;</span><span style="color: #f8f8f2">end();</span>
  727. <span style="color: #66d9ef">delete</span> <span style="color: #f8f8f2">portal;</span>
  728. <span style="color: #f8f8f2">portal</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">nullptr</span><span style="color: #f8f8f2">;</span>
  729. <span style="color: #f8f8f2">}</span>
  730. <span style="color: #f8f8f2">}</span>
  731. <span style="color: #75715e">// Here, ordinary sketch logic.</span>
  732. <span style="color: #f8f8f2">}</span>
  733. </code></pre></div>
  734. </li>
  735. </ol>
  736. <h2 id="launch-the-captive-portal-on-demand-at-losing-wifi">Launch the captive portal on-demand at losing WiFi<a class="headerlink" href="#launch-the-captive-portal-on-demand-at-losing-wifi" title="Permanent link">&para;</a></h2>
  737. <p>If the ESP module loses the established WiFi connection during the loop of <a href="api.html#handleclient"><strong>handleClient</strong></a>, you can prevent the ESP module from going absolutely standalone by launching the captive portal on demand.</p>
  738. <p>When <a href="apiconfig.html#retainportal"><strong>retainPortal</strong></a> and <a href="apiconfig.html#autorise"><strong>autoRise</strong></a> settings are enabled, AutoConnect will launch SoftAP and start DNS when it detects a WiFi disconnect with the router during a handleClient loop. This behavior will occur caused by a WiFi disconnect detection even if the WiFi mode is STA.</p>
  739. <p><strong>Since AutoConnect v1.2.0</strong>, An improved <a href="apiconfig.html#retainportal"><strong>retainPortal</strong></a> option allows the captive portal to be restarted during a handleClient loop even if it is closed once in <a href="api.html#begin">AutoConnect::begin</a>. In this case, the Sketch execution stage has already transitioned into the loop function, so the Sketch process seems running concurrently with the captive portal loop. Its captive portal launched from inside handleClient does not block the execution of the Sketch, unlike that launched from <a href="api.html#begin">AutoConnect::begin</a>.</p>
  740. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">Portal;</span>
  741. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">Config;</span>
  742. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  743. <span style="background-color: #49483e"> <span style="color: #f8f8f2">Config.autoRise</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span> <span style="color: #75715e">// It&#39;s the default, no setting is needed explicitly.</span>
  744. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">Config.retainPortal</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  745. </span> <span style="color: #f8f8f2">Portal.config(Config);</span>
  746. <span style="color: #f8f8f2">Portal.begin();</span>
  747. <span style="color: #f8f8f2">}</span>
  748. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  749. <span style="color: #f8f8f2">Portal.handleClient();</span>
  750. <span style="color: #f8f8f2">}</span>
  751. </code></pre></div>
  752. <div class="admonition note">
  753. <p class="admonition-title">Need autoRise enabled</p>
  754. <p>Need <a href="apiconfig.html#autorise"><em>AutoConnectConfig::autoRise</em></a> setting enabled to start the captive portal on demand during a handleClient loop.</p>
  755. </div>
  756. <p>Although the Sketch above specifies the <a href="apiconfig.html#retainportal"><strong>retainPortal</strong></a>, it does not instruct starts the captive portal always. AutoConnect will try WiFi.begin once in <a href="api.html#begin">AutoConnect::begin</a> unless the <a href="apiconfig.html#immediatestart"><strong>immediateStart</strong></a> option is specified. If AutoConnect fails the 1<sup>st</sup>-WiFi.begin, the captive portal will be launched at that point and the Sketch execution will stay within the <a href="api.html#begin">AutoConnect::begin</a> function.</p>
  757. <p>There is also a way to avoid starting the captive portal inside <a href="api.html#begin">AutoConnect::begin</a> and start the captive portal according to the WiFi connection status after the Sketch execution transitions to a <strong>handleClient loop</strong>. Adjusting the timing of <a href="apiconfig.html#autorise"><strong>autoRise</strong></a> activation will allow the captive portal to start only from inside <a href="api.html#handleclient">AutoConnect::handleClient</a> function.</p>
  758. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">Portal;</span>
  759. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">Config;</span>
  760. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  761. <span style="background-color: #49483e"> <span style="color: #f8f8f2">Config.retainPortal</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  762. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">Config.autoRise</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">false;</span> <span style="color: #75715e">// Suppresses the launch of the captive portal from AutoConnect::begin.</span>
  763. </span> <span style="color: #f8f8f2">Portal.config(Config);</span> <span style="color: #75715e">// Don&#39;t forget it.</span>
  764. <span style="color: #f8f8f2">Portal.begin();</span>
  765. <span style="background-color: #49483e"> <span style="color: #f8f8f2">Config.autoRise</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span> <span style="color: #75715e">// Enable the launch of the captive portal.</span>
  766. </span> <span style="color: #f8f8f2">Portal.config(Config);</span> <span style="color: #75715e">// Don&#39;t forget it.</span>
  767. <span style="color: #f8f8f2">}</span>
  768. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  769. <span style="color: #f8f8f2">Portal.handleClient();</span>
  770. <span style="color: #f8f8f2">}</span>
  771. </code></pre></div>
  772. <p>The <a href="apiconfig.html#retainportal"><em>retainPortal</em></a> option will keep SoftAP even if WiFi has established a connection as a client with the router. Since it has the effect of stopping the DNS server, the phenomenon that the portal screen will not pop up automatically even though SoftAP is in action occur.<br>
  773. This is a legacy behavior to ensure backward compatibility with up to AutoConnect v1.1.7. To stop SoftAP on escape from the on-demand captive portal, you need to explicitly call <strong><a href="https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/soft-access-point-class.html#softapdisconnect"><code>WiFi.softAPdisconnect(true)</code></a></strong> and <strong><a href="https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html?highlight=enableAP#other-function-calls"><code>WiFi.enableAP(false)</code></a></strong> in the Sketch.</p>
  774. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">Portal;</span>
  775. <span style="color: #f8f8f2">AutoConnectConfig</span> <span style="color: #f8f8f2">Config;</span>
  776. <span style="color: #66d9ef">bool</span> <span style="color: #f8f8f2">Connected;</span>
  777. <span style="color: #66d9ef">unsigned</span> <span style="color: #66d9ef">long</span> <span style="color: #f8f8f2">Elapsed;</span>
  778. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">onConnect</span><span style="color: #f8f8f2">(IPAddress</span><span style="color: #f92672">&amp;</span> <span style="color: #f8f8f2">clientIP)</span> <span style="color: #f8f8f2">{</span>
  779. <span style="color: #f8f8f2">Connected</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  780. <span style="color: #f8f8f2">Elapsed</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">millis();</span>
  781. <span style="color: #f8f8f2">}</span>
  782. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  783. <span style="color: #f8f8f2">Config.retainPortal</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">true;</span>
  784. <span style="color: #f8f8f2">Portal.config(Config);</span>
  785. <span style="color: #f8f8f2">Portal.onConnect(onConnect);</span>
  786. <span style="color: #f8f8f2">Portal.begin();</span>
  787. <span style="color: #f8f8f2">}</span>
  788. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  789. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.status()</span> <span style="color: #f92672">!=</span> <span style="color: #f8f8f2">WL_CONNECTED)</span> <span style="color: #f8f8f2">{</span>
  790. <span style="color: #f8f8f2">connected</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">false;</span>
  791. <span style="color: #f8f8f2">Elapsed</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">millis();</span>
  792. <span style="color: #f8f8f2">}</span>
  793. <span style="background-color: #49483e"> <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">((WiFi.getMode()</span> <span style="color: #f92672">&amp;</span> <span style="color: #f8f8f2">WIFI_AP)</span> <span style="color: #f92672">&amp;&amp;</span> <span style="color: #f8f8f2">Connected)</span> <span style="color: #f8f8f2">{</span>
  794. </span><span style="background-color: #49483e"> <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(millis()</span> <span style="color: #f92672">-</span> <span style="color: #f8f8f2">Elapsed</span> <span style="color: #f92672">&gt;</span> <span style="color: #ae81ff">30000</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
  795. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">WiFi.softAPdisconnect(true);</span>
  796. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">WiFi.enableAP(false);</span>
  797. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">}</span>
  798. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">}</span>
  799. </span>
  800. <span style="color: #75715e">// Actual sketch process is here.</span>
  801. <span style="color: #f8f8f2">Portal.handleClient();</span>
  802. <span style="color: #f8f8f2">}</span>
  803. </code></pre></div>
  804. <p>The above sketch will shutdown the SoftAP after elapsed time exceeds 30 seconds since the connection was re-established. Its logic is a bit tricky and does not stop SoftAP immediately after the connection established, which has several seconds delay. Doing it ensures that AutoConnect can send the HTML response.</p>
  805. <div class="admonition info">
  806. <p class="admonition-title">Stopped SoftAP is still displayed</p>
  807. <p>After SoftAP stopped, there is a time lag before it disappears from the detected access points list on the client device.</p>
  808. </div>
  809. <h2 id="shutdown-the-captive-portal">Shutdown the captive portal<a class="headerlink" href="#shutdown-the-captive-portal" title="Permanent link">&para;</a></h2>
  810. <p>There is some complexity in the conditions under which AutoConnect shuts down the captive portal. Making a sketch that activates SoftAP only when needed can seem tedious.<br />
  811. But there is a reason why. Even if AutoConnect could establish a connection using a captive portal, your cell phone as a client device would still have to keep connected to the ESP module-generated SoftAP in order to send the page for notifying the connection successful to a user. At that point, your client device that opened the captive portal still needs a connection with SoftAP.</p>
  812. <p>What happens, after all, is as follows:</p>
  813. <ol>
  814. <li>You made a connection to the access point such as WiFi router using the captive portal and took a successful page.</li>
  815. <li>Your sketch will rush into the loop function and starts to works well, hooray!</li>
  816. <li>Oops. Don't celebrate yet. I can see SoftAP ID on my cell phone. But the AutoConnect page never pops up automatically. Why?</li>
  817. </ol>
  818. <p>Because, for the above reasons, we can not promptly shut down the SoftAP. (However, DNS will stop)</p>
  819. <p>So, If you want to stop SoftAP after connecting to the access point using the captive portal, you need to implement the shutdown process with Sketch explicitly. A template of the basic sketch that can stop the SoftAP after the connection is the following:</p>
  820. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">Portal;</span>
  821. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  822. <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(Portal.begin())</span> <span style="color: #f8f8f2">{</span>
  823. <span style="background-color: #49483e"> <span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(WiFi.getMode()</span> <span style="color: #f92672">&amp;</span> <span style="color: #f8f8f2">WIFI_AP)</span> <span style="color: #f8f8f2">{</span>
  824. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">WiFi.softAPdisconnect(true);</span>
  825. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">WiFi.enableAP(false);</span>
  826. </span><span style="background-color: #49483e"> <span style="color: #f8f8f2">}</span>
  827. </span> <span style="color: #f8f8f2">}</span>
  828. <span style="color: #f8f8f2">}</span>
  829. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">loop</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  830. <span style="color: #f8f8f2">Portal.handleClient();</span>
  831. <span style="color: #f8f8f2">}</span>
  832. </code></pre></div>
  833. <div class="admonition note">
  834. <p class="admonition-title">If you stop SoftAP, the connection will be lost</p>
  835. <p>If you stop SoftAP immediately after the <a href="api.html#begin">AutoConnect::begin</a> successful, will part with the connection and cannot see the result notifying on your client device.<br />
  836. You can expect to receive result notifications if you run <a href="api.html#handleclient"><strong>handleClient</strong></a> before stopping SoftAP. (although you may not always succeed; it will not work if the WiFi radio signal is weak)</p>
  837. </div>
  838. <h2 id="sketch-execution-during-the-captive-portal-loop">Sketch execution during the captive portal loop<a class="headerlink" href="#sketch-execution-during-the-captive-portal-loop" title="Permanent link">&para;</a></h2>
  839. <p>With <a href="api.html#begin">AutoConnect::begin</a>, once the captive portal is started without being able to connect to a known WiFi access point, control will not return to sketch until the WiFi connection is established or times out. This behavior helps to pin the ESP module's network participation as a WiFi client (that is, AutoConnect::begin is an alternative to WiFi.begin) but it can not rush into the loop function of the Sketch. Therefore, while the ESP module is in the captive portal state and waiting for a connection operation to the access point, the behavior of the Sketch will be restrained by the <a href="lsbegin.html">escape conditions from AutoConnect::begin</a>.</p>
  840. <p>The <strong>whileCaptivePortal exit</strong> allows the Sketch to continue the process temporarily while the ESP module remains standalone and the captive portal is open. <a href="api.html#whilecaptiveportal">AutConnect::whileCaptivePortal</a> function registers the user's sketch function to be called by <a href="api.html#begin">AutoConnect::begin</a> or <a href="api.html#handleclient">AutoConnect::handleClient</a> during the execution of the captive portal session loop.</p>
  841. <p>The whileCaptivePortal exit can be registered by following: </p>
  842. <div class="highlight" style="background: #272822"><pre style="line-height: 125%; margin: 0;"><span></span><code><span style="color: #f8f8f2">AutoConnect</span> <span style="color: #f8f8f2">portal;</span>
  843. <span style="color: #66d9ef">bool</span> <span style="color: #a6e22e">whileCP</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">void</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
  844. <span style="color: #66d9ef">bool</span> <span style="color: #f8f8f2">rc;</span>
  845. <span style="color: #75715e">// Here, something to process while the captive portal is open.</span>
  846. <span style="color: #75715e">// To escape from the captive portal loop, this exit function returns false.</span>
  847. <span style="color: #75715e">// rc = true;, or rc = false;</span>
  848. <span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">rc;</span>
  849. <span style="color: #f8f8f2">}</span>
  850. <span style="color: #66d9ef">void</span> <span style="color: #a6e22e">setup</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
  851. <span style="color: #f8f8f2">...</span>
  852. <span style="background-color: #49483e"> <span style="color: #f8f8f2">portal.whileCaptivePortal(whileCP);</span>
  853. </span> <span style="color: #f8f8f2">portal.begin();</span>
  854. <span style="color: #f8f8f2">...</span>
  855. <span style="color: #f8f8f2">}</span>
  856. </code></pre></div>
  857. <p>AutoConnect will open the captive portal in the <a href="api.html#bdgin">AutoConnect::begin</a> and <a href="api.html#handleclient">AutoConnect::handleClient</a> scenes, but the whileCaptive portal exit will be called repeatedly from <a href="api.html#begin">AutoConnect::begin</a> until exits from it. The whileCaptivePortal exit will be called repeatedly while the captive portal is open until WiFi connected or times out occurs. In the Sketch, returning a <strong>FALSE</strong> value from the whileCaptivePortal function allows the control to escape from the captive portal loop even before the session elapsed time exceeds the limits.</p>
  858. </article>
  859. </div>
  860. </div>
  861. </main>
  862. <footer class="md-footer">
  863. <div class="md-footer-nav">
  864. <nav class="md-footer-nav__inner md-grid" aria-label="Footer">
  865. <a href="adconnection.html" class="md-footer-nav__link md-footer-nav__link--prev" rel="prev">
  866. <div class="md-footer-nav__button md-icon">
  867. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
  868. </div>
  869. <div class="md-footer-nav__title">
  870. <div class="md-ellipsis">
  871. <span class="md-footer-nav__direction">
  872. Previous
  873. </span>
  874. AutoConnect WiFi connection control
  875. </div>
  876. </div>
  877. </a>
  878. <a href="adnetwork.html" class="md-footer-nav__link md-footer-nav__link--next" rel="next">
  879. <div class="md-footer-nav__title">
  880. <div class="md-ellipsis">
  881. <span class="md-footer-nav__direction">
  882. Next
  883. </span>
  884. Settings and controls for network and WiFi
  885. </div>
  886. </div>
  887. <div class="md-footer-nav__button md-icon">
  888. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4z"/></svg>
  889. </div>
  890. </a>
  891. </nav>
  892. </div>
  893. <div class="md-footer-meta md-typeset">
  894. <div class="md-footer-meta__inner md-grid">
  895. <div class="md-footer-copyright">
  896. <div class="md-footer-copyright__highlight">
  897. Copyright &copy; 2018-2020 Hieromon Ikasamo
  898. </div>
  899. Made with
  900. <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
  901. Material for MkDocs
  902. </a>
  903. </div>
  904. <div class="md-footer-social">
  905. <a href="https://github.com/Hieromon" target="_blank" rel="noopener" title="github.com" class="md-footer-social__link">
  906. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
  907. </a>
  908. <a href="https://twitter.com/hieromon" target="_blank" rel="noopener" title="twitter.com" class="md-footer-social__link">
  909. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/></svg>
  910. </a>
  911. </div>
  912. </div>
  913. </div>
  914. </footer>
  915. </div>
  916. <script src="assets/javascripts/vendor.fd16492e.min.js"></script>
  917. <script src="assets/javascripts/bundle.7836ba4d.min.js"></script><script id="__lang" type="application/json">{"clipboard.copy": "Copy to clipboard", "clipboard.copied": "Copied to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.placeholder": "Type to start searching", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.term.missing": "Missing"}</script>
  918. <script>
  919. app = initialize({
  920. base: ".",
  921. features: [],
  922. search: Object.assign({
  923. worker: "assets/javascripts/worker/search.4ac00218.min.js"
  924. }, typeof search !== "undefined" && search)
  925. })
  926. </script>
  927. <script src="js/gifffer.min.js"></script>
  928. </body>
  929. </html>