/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-web-server-outputs-momentary-switch/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ #ifdef ESP32 #include #include #else #include #include #endif #include // REPLACE WITH YOUR NETWORK CREDENTIALS const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; const int output = 2; // HTML web page const char index_html[] PROGMEM = R"rawliteral( ESP Pushbutton Web Server

ESP Pushbutton Web Server

)rawliteral"; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } AsyncWebServer server(80); void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("WiFi Failed!"); return; } Serial.println(); Serial.print("ESP IP Address: http://"); Serial.println(WiFi.localIP()); pinMode(output, OUTPUT); digitalWrite(output, LOW); // Send web page to client server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html); }); // Receive an HTTP GET request server.on("/on", HTTP_GET, [] (AsyncWebServerRequest *request) { digitalWrite(output, HIGH); request->send(200, "text/plain", "ok"); }); // Receive an HTTP GET request server.on("/off", HTTP_GET, [] (AsyncWebServerRequest *request) { digitalWrite(output, LOW); request->send(200, "text/plain", "ok"); }); server.onNotFound(notFound); server.begin(); } void loop() { }