As this problem is still not solved, I've simplified the code as much as possible to just read a cookie when connecting, showing it, and create a new one, which should show up at the next logon etc ...
This is the code used
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "ESP8266_SoftAP";
ESP8266WebServer server(80);
void handleRoot() {
String storedCookieValue = "None";
String newCookieValue = String(millis());
if (server.hasHeader("Cookie")) {
String cookie = server.header("Cookie");
int pos = cookie.indexOf("clientID=");
if (pos != -1) {
pos += 9; // Length of "clientID="
int endPos = cookie.indexOf(";", pos);
if (endPos == -1) endPos = cookie.length();
storedCookieValue = cookie.substring(pos, endPos);
}
}
server.sendHeader("Set-Cookie", "clientID=" + newCookieValue + "; Path=/;");
Serial.println("Stored Cookie: " + storedCookieValue);
Serial.println("New Cookie: " + newCookieValue);
String html = "<html>"
"<head>"
"<style>"
"body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; font-family: Arial, sans-serif; background-color: #f0f0f0; }"
"div { text-align: center; background: #fff; padding: 40px; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }"
"h1 { font-size: 3em; margin-bottom: 20px; }"
"p { font-size: 1.5em; margin-bottom: 20px; }"
"</style>"
"</head>"
"<body>"
"<div>"
"<h1>Stored Cookie Value: " + storedCookieValue + "</h1>"
"<p>New Cookie Value: " + newCookieValue + "</p>"
"</div>"
"</body>"
"</html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/", handleRoot);
server.onNotFound([]() {
server.send(404, "text/plain", "Not found");
});
server.begin();
}
void loop() {
server.handleClient();
}
And this is the result on the serial monitor for the first logon (which is strange that it shows it 2 times)
17:07:11.008 -> Stored Cookie: None
17:07:11.008 -> New Cookie: 33970
17:07:29.009 -> Stored Cookie: None
17:07:29.009 -> New Cookie: 51951
And this for the second logon
17:08:35.230 -> Stored Cookie: None
17:08:35.230 -> New Cookie: 118209
I want to use SoftAP on the NODEMCU as a stand-alone device without internet access.
Any help to solve this will be greatly appreciated (ChatGPT doesn't manage to solve the issue :slight_smile