Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WiFiManager auto connect and start a http web server
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
std::unique_ptr<ESP8266WebServer> server;
void handleRoot() {
server->send(200, "text/plain", "hello from esp8266!");
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server->uri();
message += "\nMethod: ";
message += (server->method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server->args();
message += "\n";
for (uint8_t i = 0; i < server->args(); i++) {
message += " " + server->argName(i) + ": " + server->arg(i) + "\n";
}
server->send(404, "text/plain", message);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//Serial.setDebugOutput(true);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
wifiManager.resetSettings();
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
//wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
server.reset(new ESP8266WebServer(WiFi.localIP(), 80));
server->on("/", handleRoot);
server->on("/inline", []() {
server->send(200, "text/plain", "this works as well");
});
server->onNotFound(handleNotFound);
server->begin();
Serial.println("HTTP server started");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
server->handleClient();
}
@tusharpc

This comment has been minimized.

Copy link

commented Feb 19, 2017

hi Tzapu, I recently discovered your page and i'm going crazy! this is great work. I'm using ESP's as a remote control for model trains and ships. i was worried i would need to reprogram my esp everytime i moved to a new place with new wifi but you wifimanager has been a god send.
i have been using the http web server as a base for my remote, e.g ... if an app sends out 192.168.1.xxx/led on .. it's sets led on using your serve.0n method above.
i was thinking why not have the functionality of resetting the wifi config settings using this same technique, this way, you have wifi config on demand, without the hassle of physically touching a button so.. i came up with this below.... let me know what you think ?

server->on("/reset", {
WiFiManager wifiManager;
wifiManager.resetSettings();
server->send(200, "text/plain", "reset and reconfig");
});

I tried it, it works, but i'm no coding expert so thought you may be able to better use and troubleshoot
appreciate all your effort and contribution

@ttuksa

This comment has been minimized.

Copy link

commented May 16, 2017

Hello! I'm having trouble with setting up an HTTP server after configuration. I tried this example, config portal works, but after it successfully connects to Wifi, and I click "Close" to exit the portal, it just gives me:
HTTP server started
192.168.0.15
But when I try to access the server on that address, I get a timeout. My pc and my phone are connected to that same wifi, not to ESP8266 wifi. What could be the problem?
EDIT:
Sorry, I didn't provide enough info... I'm using WeMos D1 mini, Arduino IDE 1.6.8 (i also tried the latest 1.8.2), ESP8266 core version 2.2.0 in Board Manager. I have no idea why it doesn't work. Please help :(

SOLVED:
I had two versions of Arduino IDE installed, and it seems they can't work together. I uninstalled the 1.8.2 and kept the 1.6.8, I upgraded the ESP8266 core to 2.3.0 and it all works now!
So to anyone with the same problem, stick with one version of Arduino IDE! Cheers!

@RobsonRsilva

This comment has been minimized.

Copy link

commented Jul 2, 2017

When I use the configuration with the network name and password the webserver does not open the pages anymore and the homepage always opens the configuration page.
Ex: wifiManager.autoConnect("ACCESSPOINTNAME" , "ACCESSPOINTPASSWORD");
Can you solve it?

@Natim

This comment has been minimized.

Copy link

commented Nov 26, 2017

Could you add an example of how to handle POST request or QUERYSTRING?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.