index.h
const char index_html[] PROGMEM = R"rawliteral(<html>...</html>)rawliteral";
esp32_wm_server.ino
#include <WiFiManager.h>
#include <ESPAsyncWebServer.h>
#include <DNSServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include "index.h"
const char* http_username = "username"; // 웾 접속 용
const char* http_password = "password"; // 웹 접속 용
AsyncWebServer server(80); // Create an instance of the AsyncWebServer
int nCounter;
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// put your setup code here, to run once:
Serial.begin(115200);
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
//wm.resetSettings();
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
Serial.println("connected...yeey :)");
}
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
if (!request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send_P(200, "text/html", index_html, NULL);
});
server.on("/adc_value", HTTP_GET, [](AsyncWebServerRequest * request) {
nCounter ++;
request->send(200, "text/html", String(nCounter));
});
server.on("/led_toggle", HTTP_GET, [](AsyncWebServerRequest * request) {
Serial.println("LED Toggled!!!");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
request->send(200, "text/html", "");
});
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {
String inputMessage;
String inputParam;
Serial.println(inputMessage);
request->send(200, "text/html", index_html);
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
}