title: ESP: Auto login/accept message by OS with redirect to page like public WIFI portals access points
body: <p><strong>Example:</strong></p>
  
  <p>I do not know how you call this feature so I try to explain it. For example when you connect your phone to a public WiFi hotspot/access point, you receive a message from OS that it is required to accept terms/conditions or you need to logon to be able to use the connection. When you click onto this message it opens the/a browser and launch a landing page.</p>
  
  <p>How do they do this? Is this a https feature? </p>
  
  <hr>
  
  <p><strong>Update:</strong>(1)</p>
  
  <p>It is called a Captive Portal:</p>
  
  <p>The captive portal technique forces an HTTP client on a network to see a special web page (usually for authentication purposes) before using the Internet normally. A captive portal turns a Web browser into an authentication device. This is done by intercepting most packets, regardless of address or port, until the user opens a browser and tries to access the web. At that time the browser is redirected to a web page which may require authentication and/or payment, or simply display an acceptable use policy and require the user to agree. Captive portals are used at many Wi-Fi hotspots, and can be used to control wired access (e.g. apartment houses, hotel rooms, business centers, "open" Ethernet jacks) as well.</p>
  
  <hr>
  
  <p><strong>Situation:</strong></p>
  
  <p>I want to do the same with an ESP (ESP32/ESP8266) because I develop a device that can be configurated via WiFi. It would be very user friendly when the user connect to the device and doesn't have to open a browser first, type in an IP-address etc. Just a message, user can click on it and a main page will be launched. </p>
  
  <p>In AP mode, I have turned on DNS and redirect everything to the main page however is not the best solution (also sometimes doesn't work). To give you some idea what I am doing, here some code, a part of my library:</p>
  
  <pre><code>bool suStartAccessPointNetwork( String  sSSIDNetworkName, String   sSSIDPassword,
                                  IPAddress sStaticIpAddress,
                                  uint16_t iDnsPort = 0,
                                  uint8_t iTries = 20, 
                                  uint16_t iDelay = 500 
                                )
  { 
    WiFi.disconnect();
    suSetNetworkAccessPointMode();
    delay( 1000 );
    WiFi.softAPConfig( sStaticIpAddress,
                         sStaticIpAddress,
                         IPAddress( 255, 255, 255, 0 )
                       ); 
  
    SU_WIFI_AP_ESTABLISHED = WiFi.softAP( sSSIDNetworkName.c_str(), sSSIDPassword.c_str() );
  
    if( iDnsPort &gt; 0 )
    {
         static SUDNSServer* pDnsServer = NULLPTR;
  
         if( !pDnsServer )
          { pDnsServer = new SUDNSServer(); }
  
         // modify TTL, default is 60 seconds
         pDnsServer-&gt;setTTL(60);
  
         // set which return code will be used for all other domains (e.g. sending
         // ServerFailure instead of NonExistentDomain will reduce number of queries
         // sent by clients)
         // default is DNSReplyCode::NonExistentDomain
         pDnsServer-&gt;setErrorReplyCode( DNSReplyCode::NoError );
         pDnsServer-&gt;start( iDnsPort, 
                          //SCDMS_MDNS_ACCESS_NAME, 
                          "*", 
                          sStaticIpAddress
                         );
  
         Serial.print("DNS server started at " );
         Serial.println( sStaticIpAddress ); 
  
         suSetDnsServer( pDnsServer );
    } 
  
  
    Serial.print("[AP]:");
  
    bool bConnected = false;
  
    if( iTries == 0 )
     { ++iTries; } 
  
     // Try to connect, give WiFi 10 seconds to launch a network as access point.
    while( !(bConnected = suIsNetworkEstablished()) &amp;&amp; (iTries--) )
    {
        Serial.print('.');
        //Serial.print(WiFi.status());
        delay(500);
    }
  
    if( bConnected )
     { SU_WIFI_AP_ESTABLISHED = bConnected; }
  
    Serial.println( bConnected?" ((o))":" o?" );
  
  
    return bConnected;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Question:</strong></p>
  
  <p>How can I implement such feature like discribed in the example above? Any ideas? Is it possible anyway? </p>
  
  <h2>--------------------</h2>
  
  <p><strong>Update 2:</strong></p>
  
  <p>@gre_gor came up with the same examples I also found after knowing the name of the service (see also update 1). Implement the Url paths etc like in the example, doesn't work as described above. Okay, tried the full example, same result, no OS popup/message, no redirection. Entering the IP-adress at the browser, this is working however the same I already had.  </p>
  
  <p><em>One of the extended demo's i have tried:</em></p>
  
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  #include &lt;WiFiClient.h&gt;
  #include &lt;ESP8266WebServer.h&gt;
  #include &lt;DNSServer.h&gt;
  #include &lt;ESP8266mDNS.h&gt;
  #include &lt;EEPROM.h&gt;
  
  /*
     This example serves a "hello world" on a WLAN and a SoftAP at the same time.
     The SoftAP allow you to configure WLAN parameters at run time. They are not setup in the sketch but saved on EEPROM.
     Connect your computer or cell phone to wifi network ESP_ap with password 12345678. A popup may appear and it allow you to go to WLAN config. If it does not then navigate to http://192.168.4.1/wifi and config it there.
     Then wait for the module to connect to your wifi and take note of the WLAN IP it got. Then you can disconnect from ESP_ap and return to your regular WLAN.
     Now the ESP8266 is in your network. You can reach it through http://192.168.x.x/ (the IP you took note of) or maybe at http://esp8266.local too.
     This is a captive portal because through the softAP it will redirect any http request to http://192.168.4.1/
  */
  
  /* Set these to your desired softAP credentials. They are not configurable at runtime */
  #ifndef APSSID
  #define APSSID "ESP_ap"
  #define APPSK  "12345678"
  #endif
  
  const char *softAP_ssid = APSSID;
  const char *softAP_password = APPSK;
  
  /* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
  const char *myHostname = "esp8266";
  
  /* Don't set this wifi credentials. They are configurated at runtime and stored on EEPROM */
  char ssid[32] = "";
  char password[32] = "";
  
  // DNS server
  const byte DNS_PORT = 53;
  DNSServer dnsServer;
  
  // Web server
  ESP8266WebServer server(80);
  
  /* Soft AP network parameters */
  IPAddress apIP(192, 168, 4, 1);
  IPAddress netMsk(255, 255, 255, 0);
  
  
  /** Should I connect to WLAN asap? */
  boolean connect;
  
  /** Last time I tried to connect to WLAN */
  unsigned long lastConnectTry = 0;
  
  /** Current WLAN status */
  unsigned int status = WL_IDLE_STATUS;
  
  /** Is this an IP? */
  boolean isIp(String str) {
    for (size_t i = 0; i &lt; str.length(); i++) {
      int c = str.charAt(i);
      if (c != '.' &amp;&amp; (c &lt; '0' || c &gt; '9')) {
        return false;
      }
    }
    return true;
  }
  
  /** IP to String? */
  String toStringIp(IPAddress ip) {
    String res = "";
    for (int i = 0; i &lt; 3; i++) {
      res += String((ip &gt;&gt; (8 * i)) &amp; 0xFF) + ".";
    }
    res += String(((ip &gt;&gt; 8 * 3)) &amp; 0xFF);
    return res;
  }
  
  /** Load WLAN credentials from EEPROM */
  void loadCredentials() {
    EEPROM.begin(512);
    EEPROM.get(0, ssid);
    EEPROM.get(0 + sizeof(ssid), password);
    char ok[2 + 1];
    EEPROM.get(0 + sizeof(ssid) + sizeof(password), ok);
    EEPROM.end();
    if (String(ok) != String("OK")) {
      ssid[0] = 0;
      password[0] = 0;
    }
    Serial.println("Recovered credentials:");
    Serial.println(ssid);
    Serial.println(strlen(password) &gt; 0 ? "********" : "&lt;no password&gt;");
  }
  
  /** Store WLAN credentials to EEPROM */
  void saveCredentials() {
    EEPROM.begin(512);
    EEPROM.put(0, ssid);
    EEPROM.put(0 + sizeof(ssid), password);
    char ok[2 + 1] = "OK";
    EEPROM.put(0 + sizeof(ssid) + sizeof(password), ok);
    EEPROM.commit();
    EEPROM.end();
  }
  
  /** Handle root or redirect to captive portal */
  void handleRoot() {
    if (captivePortal()) { // If caprive portal redirect instead of displaying the page.
      return;
    }
    server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    server.sendHeader("Pragma", "no-cache");
    server.sendHeader("Expires", "-1");
  
    String Page;
    Page += F(
              "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;"
              "&lt;h1&gt;HELLO WORLD!!&lt;/h1&gt;");
    if (server.client().localIP() == apIP) {
      Page += String(F("&lt;p&gt;You are connected through the soft AP: ")) + softAP_ssid + F("&lt;/p&gt;");
    } else {
      Page += String(F("&lt;p&gt;You are connected through the wifi network: ")) + ssid + F("&lt;/p&gt;");
    }
    Page += F(
              "&lt;p&gt;You may want to &lt;a href='/wifi'&gt;config the wifi connection&lt;/a&gt;.&lt;/p&gt;"
              "&lt;/body&gt;&lt;/html&gt;");
  
    server.send(200, "text/html", Page);
  }
  
  /** Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */
  boolean captivePortal() {
    if (!isIp(server.hostHeader()) &amp;&amp; server.hostHeader() != (String(myHostname) + ".local")) {
      Serial.println("Request redirected to captive portal");
      server.sendHeader("Location", String("http://") + toStringIp(server.client().localIP()), true);
      server.send(302, "text/plain", "");   // Empty content inhibits Content-length header so we have to close the socket ourselves.
      server.client().stop(); // Stop is needed because we sent no content length
      return true;
    }
    return false;
  }
  
  /** Wifi config page handler */
  void handleWifi() {
    server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    server.sendHeader("Pragma", "no-cache");
    server.sendHeader("Expires", "-1");
  
    String Page;
    Page += F(
              "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;"
              "&lt;h1&gt;Wifi config&lt;/h1&gt;");
    if (server.client().localIP() == apIP) {
      Page += String(F("&lt;p&gt;You are connected through the soft AP: ")) + softAP_ssid + F("&lt;/p&gt;");
    } else {
      Page += String(F("&lt;p&gt;You are connected through the wifi network: ")) + ssid + F("&lt;/p&gt;");
    }
    Page +=
      String(F(
               "\r\n&lt;br /&gt;"
               "&lt;table&gt;&lt;tr&gt;&lt;th align='left'&gt;SoftAP config&lt;/th&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;SSID ")) +
      String(softAP_ssid) +
      F("&lt;/td&gt;&lt;/tr&gt;"
        "&lt;tr&gt;&lt;td&gt;IP ") +
      toStringIp(WiFi.softAPIP()) +
      F("&lt;/td&gt;&lt;/tr&gt;"
        "&lt;/table&gt;"
        "\r\n&lt;br /&gt;"
        "&lt;table&gt;&lt;tr&gt;&lt;th align='left'&gt;WLAN config&lt;/th&gt;&lt;/tr&gt;"
        "&lt;tr&gt;&lt;td&gt;SSID ") +
      String(ssid) +
      F("&lt;/td&gt;&lt;/tr&gt;"
        "&lt;tr&gt;&lt;td&gt;IP ") +
      toStringIp(WiFi.localIP()) +
      F("&lt;/td&gt;&lt;/tr&gt;"
        "&lt;/table&gt;"
        "\r\n&lt;br /&gt;"
        "&lt;table&gt;&lt;tr&gt;&lt;th align='left'&gt;WLAN list (refresh if any missing)&lt;/th&gt;&lt;/tr&gt;");
    Serial.println("scan start");
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n &gt; 0) {
      for (int i = 0; i &lt; n; i++) {
        Page += String(F("\r\n&lt;tr&gt;&lt;td&gt;SSID ")) + WiFi.SSID(i) + ((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? F(" ") : F(" *")) + F(" (") + WiFi.RSSI(i) + F(")&lt;/td&gt;&lt;/tr&gt;");
      }
    } else {
      Page += F("&lt;tr&gt;&lt;td&gt;No WLAN found&lt;/td&gt;&lt;/tr&gt;");
    }
    Page += F(
              "&lt;/table&gt;"
              "\r\n&lt;br /&gt;&lt;form method='POST' action='wifisave'&gt;&lt;h4&gt;Connect to network:&lt;/h4&gt;"
              "&lt;input type='text' placeholder='network' name='n'/&gt;"
              "&lt;br /&gt;&lt;input type='password' placeholder='password' name='p'/&gt;"
              "&lt;br /&gt;&lt;input type='submit' value='Connect/Disconnect'/&gt;&lt;/form&gt;"
              "&lt;p&gt;You may want to &lt;a href='/'&gt;return to the home page&lt;/a&gt;.&lt;/p&gt;"
              "&lt;/body&gt;&lt;/html&gt;");
    server.send(200, "text/html", Page);
    server.client().stop(); // Stop is needed because we sent no content length
  }
  
  /** Handle the WLAN save form and redirect to WLAN config page again */
  void handleWifiSave() {
    Serial.println("wifi save");
    server.arg("n").toCharArray(ssid, sizeof(ssid) - 1);
    server.arg("p").toCharArray(password, sizeof(password) - 1);
    server.sendHeader("Location", "wifi", true);
    server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    server.sendHeader("Pragma", "no-cache");
    server.sendHeader("Expires", "-1");
    server.send(302, "text/plain", "");    // Empty content inhibits Content-length header so we have to close the socket ourselves.
    server.client().stop(); // Stop is needed because we sent no content length
    saveCredentials();
    connect = strlen(ssid) &gt; 0; // Request WLAN connect with new credentials if there is a SSID
  }
  
  void handleNotFound() {
    if (captivePortal()) { // If caprive portal redirect instead of displaying the error page.
      return;
    }
    String message = F("File Not Found\n\n");
    message += F("URI: ");
    message += server.uri();
    message += F("\nMethod: ");
    message += (server.method() == HTTP_GET) ? "GET" : "POST";
    message += F("\nArguments: ");
    message += server.args();
    message += F("\n");
  
    for (uint8_t i = 0; i &lt; server.args(); i++) {
      message += String(F(" ")) + server.argName(i) + F(": ") + server.arg(i) + F("\n");
    }
    server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    server.sendHeader("Pragma", "no-cache");
    server.sendHeader("Expires", "-1");
    server.send(404, "text/plain", message);
  }
  
  void setup() {
    delay(1000);
    Serial.begin(9600);
    Serial.println();
    Serial.println("Configuring access point...");
    /* You can remove the password parameter if you want the AP to be open. */
    WiFi.softAPConfig(apIP, apIP, netMsk);
    WiFi.softAP(softAP_ssid, softAP_password);
    delay(500); // Without delay I've seen the IP address blank
    Serial.print("AP IP address: ");
    Serial.println(WiFi.softAPIP());
  
    /* Setup the DNS server redirecting all the domains to the apIP */
    dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
    dnsServer.start(DNS_PORT, "*", apIP);
  
    /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */
    server.on("/", handleRoot);
    server.on("/wifi", handleWifi);
    server.on("/wifisave", handleWifiSave);
    server.on("/generate_204", handleRoot);  //Android captive portal. Maybe not needed. Might be handled by notFound handler.
    server.on("/fwlink", handleRoot);  //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
    server.onNotFound(handleNotFound);
    server.begin(); // Web server start
    Serial.println("HTTP server started");
    loadCredentials(); // Load WLAN credentials from network
    connect = strlen(ssid) &gt; 0; // Request WLAN connect if there is a SSID
  }
  
  void connectWifi() {
    Serial.println("Connecting as wifi client...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    int connRes = WiFi.waitForConnectResult();
    Serial.print("connRes: ");
    Serial.println(connRes);
  }
  
  void loop() {
    if (connect) {
      Serial.println("Connect requested");
      connect = false;
      connectWifi();
      lastConnectTry = millis();
    }
    {
      unsigned int s = WiFi.status();
      if (s == 0 &amp;&amp; millis() &gt; (lastConnectTry + 60000)) {
        /* If WLAN disconnected and idle try to connect */
        /* Don't set retry time too low as retry interfere the softAP operation */
        connect = true;
      }
      if (status != s) { // WLAN status change
        Serial.print("Status: ");
        Serial.println(s);
        status = s;
        if (s == WL_CONNECTED) {
          /* Just connected to WLAN */
          Serial.println("");
          Serial.print("Connected to ");
          Serial.println(ssid);
          Serial.print("IP address: ");
          Serial.println(WiFi.localIP());
  
          // Setup MDNS responder
          if (!MDNS.begin(myHostname)) {
            Serial.println("Error setting up MDNS responder!");
          } else {
            Serial.println("mDNS responder started");
            // Add service to MDNS-SD
            MDNS.addService("http", "tcp", 80);
          }
        } else if (s == WL_NO_SSID_AVAIL) {
          WiFi.disconnect();
        }
      }
      if (s == WL_CONNECTED) {
        MDNS.update();
      }
    }
    // Do work:
    //DNS
    dnsServer.processNextRequest();
    //HTTP
    server.handleClient();
  }
  </code></pre>