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();
}
@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