Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Anelito
#76587 Hello,
I am trying to create a local WiFi server with an ESP8266 NodeMCU development board.
The idea is to have users connect directly to the ESP WiFi network (open) and navigate the local pages, with no internet connection or bridges needed. The users have an option to opt-in to receive a newsletter, the email address is registered on a SD card.

I tried using the CaptivePortalAdvanced example sketch but it doesn't seem to work, mainly because the user has to type in manually the IP address of the main page, there is no automatic redirect.
How to make it work? Thank you

Here is the code I am using:

Code: Select allconst char *ssid = "....";

ESP8266WebServer server(80);
DNSServer dnsServer;

IPAddress apIP(192, 168, 1, 1);
const byte DNS_PORT = 53;

IPAddress subnet(255, 255, 255, 0);

void handleRoot() {
  String s = ".........";
  server.send(200, "text/html", s);
}

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
 
  Serial.print("\nConfiguring wifi access point...");
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP,apIP,IPAddress(255,255,255,0));
  WiFi.softAP(ssid);
  Serial.print("OK");
  delay(500);

  Serial.println("\nConfiguring dns server...");
  dnsServer.setTTL(300);
  dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  dnsServer.start(DNS_PORT, "*", apIP);
  Serial.print("OK");
  delay(500);
 
  server.on("/", handleRoot);
  server.on("/wifi", handleRoot);
  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(handleRoot);
  server.on("/register_newsletter", []() {
    String newsletter = server.arg("email");
   // written to SD card
    server.send(200, "text/html", "<h1>Thank you!</h1>");
  });
  server.begin();
  Serial.println("\nHTTP server started");
}

void loop() {
  server.handleClient();
}