Skip to content

Instantly share code, notes, and snippets.

@tablatronix
Forked from tzapu/AutoConnectWithHTTPServer
Last active March 4, 2018 15:14
Show Gist options
  • Save tablatronix/e57dc3096ea311dd0e59aa512b8fe6e7 to your computer and use it in GitHub Desktop.
Save tablatronix/e57dc3096ea311dd0e59aa512b8fe6e7 to your computer and use it in GitHub Desktop.
WiFiManager auto connect and start a http web server
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
#if defined(ESP32)
#include <WebServer.h>
using my_WebServer = WebServer;
#else
#include <ESP8266WebServer.h>
using my_WebServer = ESP8266WebServer;
#endif
std::unique_ptr<my_WebServer> 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
if(!wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
server.reset(new my_WebServer(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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment