WiFiManager icon indicating copy to clipboard operation
WiFiManager copied to clipboard

Wifimanager does not work with esp8266webserver

Open alemori opened this issue 5 years ago • 11 comments

I'm using wifimanager version 0.14 with esp8266webserver version 1.0.0 and when I send html pages with server.send, it doesn't work for me and in the debug appears Request redirected to captive portal. What can it be?

alemori avatar Dec 20 '18 15:12 alemori

probably because wm uses it and you cannot use it twice

tablatronix avatar Dec 20 '18 15:12 tablatronix

and how can I use a server.send that works?

alemori avatar Dec 20 '18 17:12 alemori

the development branch allows it

tablatronix avatar Dec 20 '18 18:12 tablatronix

How is it done? Can you give me an example? Thank you

alemori avatar Dec 20 '18 22:12 alemori

#461

tablatronix avatar Dec 21 '18 00:12 tablatronix

Development branch dev/ondemandconfigportal should have an example in it

tablatronix avatar Dec 21 '18 01:12 tablatronix

I went to Development branch dev/ondemandconfigportal, but only wifimanager is used there. I also wanted to compile the .ino and gave me errors, I guess the error is mine, because I do not know how to work with the development code. What I need is to be able to write the html interface using the ESP8266webserver facility. If there is any example that you can give me, I would appreciate it. I copy the code that I'm trying to make work, and for example when I invoke 192.168.4.1/pepepe, it doesn't give me anything back.

#include <FS.h> // this needs to be first, or it all crashes and burns... #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include "index.h" ESP8266WebServer server2(81); String s1 = MAIN_page; //Read HTML contents

void handleRoot() { //server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/plain", "hello from esp8266!"); } void pepe(){ Serial.print("Entro a handleRoot"); //server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", s1); //Send web page } void handleReset() { //server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/plain", "Resetting!"); //Serial.println("diconnecting client and wifi"); //client.disconnect(); wifi_station_disconnect();

WiFiManager wifiManager; wifiManager.resetSettings(); ESP.reset();

}

void handleNotFound(){ String message = "File Not Found\n\n"; message += "URI: "; message += server2.uri(); message += "\nMethod: "; message += (server2.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server2.args(); message += "\n"; for (uint8_t i=0; i<server2.args(); i++){ message += " " + server2.argName(i) + ": " + server2.arg(i) + "\n"; } server2.send(404, "text/plain", message); }

void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println(); // Serial.setDebugOutput(true);

//WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager;

//exit after config instead of connecting wifiManager.setBreakAfterConfig(true); //reset settings - for testing //wifiManager.resetSettings();

//tries to connect to last known settings //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" with password "password" //and goes into a blocking loop awaiting configuration if (!wifiManager.autoConnect("AutoConnectAP", "password")) { Serial.println("failed to connect, we should reset as see if it connects"); delay(3000); ESP.restart(); delay(5000); }

//if you get here you have connected to the WiFi Serial.println(""); Serial.print("Connected to "); Serial.print("IP address: "); Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); }

server.on("/", handleRoot);

server.on("/inline", { server.send(200, "text/plain", "this works as well"); }); server.on("/resetwifi", handleReset); server.on("/pepe", pepe); server.onNotFound(handleNotFound);

// server.begin(); server.begin(); Serial.println("HTTP server started");

}

void loop() { // put your main code here, to run repeatedly: server.handleClient();

}

===================== INDEX.h

const char MAIN_page[] PROGMEM = R"=====( <! DOCTYPE html>

LED Control

DATOS DEL ATLETA



Nombre/Equipo

Femenino


Masculino


Edad



)=====";

alemori avatar Dec 21 '18 16:12 alemori

The webserver is built in...

You either need to start your own webserver when wm is not running or out of scope, so the webservers do not clash.

or use the webserver provided by wm

It is in the example I noted

https://github.com/tzapu/WiFiManager/blob/development/examples/DEV/OnDemandConfigPortal/OnDemandConfigPortal.ino#L39

wm is used global, and the server is used, else use your own server when wm is unloaded.

also you didnt use code blocks in your comment, code is unreadable,

wm.setWebServerCallback(bindServerCallback); // we use this callback so we know the webserver is running

void bindServerCallback(){
  wm.server->on("/custom",handleRoute);
  // wm.server->on("/info",handleRoute); // can override wm!
}

void handleRoute(){
  Serial.println("[HTTP] handle route");
  wm.server->send(200, "text/plain", "hello from user code");
}

The alternative is do what others have and just change the webserver from private to public in the code an use the stable version of wm

Sorry there are no docs yet on this, it is new and obviously "in development"

tablatronix avatar Dec 21 '18 17:12 tablatronix

I'm going to wait for it to be incorporated into the library, since I don't want to use the development banch. Thank you

alemori avatar Dec 24 '18 01:12 alemori

You just have to change line of code if you want to do the later

tablatronix avatar Dec 24 '18 02:12 tablatronix

I am late on this but experiencing exactly the same problem. Can you please tell me how to change the webserver from private to public in the code?

sarmadsohaib avatar Aug 03 '22 09:08 sarmadsohaib