So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By dylan1951
#64471 Hi,

I have copy pasted / written some code which creates an AP with a simple captive portal that display's all wifi networks available and then gives a box for the password and lets you connect the ESP to a wifi network.

Here is the code:
Code: Select all#include <ESP8266WiFi.h>
#include "./DNSServer.h"                  // Patched lib
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define KEY1 "?Password%3A="
#define KEY2 " HTTP/1.1"

bool wifiConnect = false;
String scanResults;
bool gotClient = false;
WiFiClient client;

ESP8266WebServer  webServer(80);          // HTTP server
byte i = 0;
byte j = 0;
byte position = 0;
int digitalValues[9];
WiFiServer server(80);
String st;

//function prototypes:
void bubbleSort();

String scan() {
  WiFi.disconnect();
  delay(100);
  int n = WiFi.scanNetworks();
  String st = "<ul>";
  for (int i = 0; i < n; ++i)
  {
    st += "<li>";
    st += i + 1;
    st += ": ";
    st += WiFi.SSID(i);
    st += " ";
    st += "<form action=\"/";
    st += WiFi.SSID(i);
    st += "\" method=\"get\"><input type=\"text\" name=\"Password:\" placeholder=\"Password...\"><input type=\"submit\" value=\"Submit\"></form>";
    st += "</li>";
  }
  st += "</ul>";
  return st;
}

const byte        DNS_PORT = 53;          // Capture DNS requests on port 53
IPAddress         apIP(10, 10, 10, 1);    // Private network for server
DNSServer         dnsServer;              // Create the DNS object


void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  scanResults = scan();
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("MY ESP8266 AP");
  dnsServer.start(DNS_PORT, "*", apIP);
  server.begin();
}

String username;
String password;


int getRequest() {

  dnsServer.processNextRequest();
  // Check if a client has connected
  if (!client) {
    return 3;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  gotClient = true;

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);

  // Match the request
  if (strstr(request.c_str(), "Password%3A=") == NULL) {
    Serial.println("Normal request");
    return 2;
  } else {
    Serial.println("GET request");
    char requestArray[50];
    char *requestArray2;
    request.toCharArray(requestArray, 50);
    requestArray2 = requestArray + 4;
    username = strtok(requestArray2, "/?");
    strtok(NULL, " =");
    password = strtok(NULL, " =");
    return 1;
  }
}

void sendResponse(int type) {
  Serial.println("SENDING RESPONSE!");
  String content;
  content = "<!DOCTYPE HTML>";
  content += "<html>";
  if (type == 1) {
    content += "<h2>Connect to a network...</h2><h4>This may take a few minutes...</h4>";
    content += scanResults;
  } else if (type == 2) {
    content += "<h2>Connect to a network...</h2><h4>This may take a few minutes...</h4>";
    content += scanResults;
    content += "<br><h3>Could not connect to ";
    content += username;
    content += " please try again!</h3>";
    content += ("</html>");
    content += ("<style>h3{color: red}</style>");
  } else {
    content += "<h3>ESP8266 is now connected to ";
    content += username;
    content += " and has been setup successfully!</h3>";
    content += ("</html>");
    content += ("<style>h3{color: blue}</style>");
  }

  String header;
  header = "HTTP/1.1 200 OK\r\n";
  header += "Content-Type: text/html; charset=UTF-8\r\n";
  header += "Content-Length: ";
  header += content.length();
  header += "\r\n";
  header += "Connection: close\r\n\r\n";

  client.println((header + content));
}

void wifi() {
  int i = 0;
  WiFi.begin(username.c_str(), password.c_str());
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i == 20) {
      WiFi.disconnect();
      WiFi.setOutputPower(0);
      Serial.println("Timed out!");
      sendResponse(2);
      delay(5000);
      wifiConnect = false;
      return;
    }
    i++;
  }
  sendResponse(3);
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}


void loop() {
  while (wifiConnect == false) {
    client = server.available();
    int stat = getRequest();
    //1 = Got creds 2 = Normal request 3 = No client
    if (stat == 2) {
      sendResponse(1);
    } else if (stat == 1) {
      Serial.println(username);
      Serial.println(password);
      wifiConnect = true;
      wifi();
    }
    client.flush();
  }
}


For some very strange reason which I can't understand at all this works 100% fine when trying to connect to my WIFI network. If I enter the wrong password the response which tells the client it couldn't connect works completely fine on my wifi, and if I enter the correct password it works and I get the right response.

But when I try to connect to my neighbours wifi with the wrong password it doesn't tell me it couldn't connect, nothing happens at all! It just loops around and try's to connect again and again but never actually sending the response! I can't test wether the right password would send the response because I don't have it.

I am very frustrated and can't understand why it will only work on my wifi! If anyone could help I would really appreciate it.

Thanks, Dylan.