Skip to content

Instantly share code, notes, and snippets.

@tzapu
Created February 8, 2016 07:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tzapu/ecc0759829d30d5a6152 to your computer and use it in GitHub Desktop.
Save tzapu/ecc0759829d30d5a6152 to your computer and use it in GitHub Desktop.
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
Copy link

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

@tomica00
Copy link

tomica00 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
Copy link

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
Copy link

Natim commented Nov 26, 2017

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

@ayasystems
Copy link

Whats is the way to use httpUpdateServer with std::unique_ptr server definition ??

@Ahmed-farag36
Copy link

One thing that works for me if you have the ability to use port other than 80 is to use different port for every server.

@davidcbittner
Copy link

This works great! However if I power down the ESP and power it back up again it starts the process completely over again where i have to go through the process of connecting my phone setting up/selecting the SSID and entering in my password. How do you keep it from trying to run the set up ever time it's powered down and back up again.

@rodri16
Copy link

rodri16 commented Dec 13, 2022

Do you have a similar example running on ESP32?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment