#include #include #include #include "ESPAsyncWebServer.h" DNSServer dnsServer; AsyncWebServer server(80); String user_name; String proficiency; bool name_received = false; bool proficiency_received = false; bool ledState = false; const int ledPin = 23; // Pin 21 for the LED const char index_html[] PROGMEM = R"rawliteral( SmartWorkoutz Remote Site

SmartWorkoutz

Klik op Start Game om het spel te starten:



)rawliteral"; class CaptiveRequestHandler : public AsyncWebHandler { public: CaptiveRequestHandler() {} virtual ~CaptiveRequestHandler() {} bool canHandle(AsyncWebServerRequest *request){ return true; } void handleRequest(AsyncWebServerRequest *request) { request->send_P(200, "text/html", index_html); } }; void setupServer(){ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html); Serial.println("Client Connected"); }); server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { String inputMessage; String inputParam; if (request->hasParam("name")) { inputMessage = request->getParam("name")->value(); inputParam = "name"; user_name = inputMessage; Serial.println(inputMessage); name_received = true; } if (request->hasParam("proficiency")) { inputMessage = request->getParam("proficiency")->value(); inputParam = "proficiency"; proficiency = inputMessage; Serial.println(inputMessage); proficiency_received = true; } request->send(200, "text/html", "The values entered by you have been successfully sent to the device
Return to Home Page"); }); server.on("/led", HTTP_GET, [](AsyncWebServerRequest *request) { String state = request->getParam("state")->value(); if (state == "ON") { ledState = true; // Set LED state to ON delay(100); ledState = false; // Set LED state to OFF } else if (state == "OFF") { ledState = false; // Set LED state to OFF } request->send(200, "text/plain", "LED state changed"); }); } void setup(){ Serial.begin(115200); Serial.println(); Serial.println("Setting up AP Mode"); WiFi.mode(WIFI_AP); WiFi.softAP("SmartWorkoutzConnect","SLCB2023"); // Set the SSID and password for the captive portal Serial.print("AP IP address: ");Serial.println(WiFi.softAPIP()); Serial.println("Setting up Async WebServer"); setupServer(); Serial.println("Starting DNS Server"); dnsServer.start(53, "*", WiFi.softAPIP()); server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER); // Only when requested from AP server.begin(); Serial.println("All Done!"); pinMode(ledPin, OUTPUT); // Initialize LED pin as an output digitalWrite(ledPin, LOW); // Initially turn off the LED } void loop(){ dnsServer.processNextRequest(); if (name_received && proficiency_received){ Serial.print("Hello ");Serial.println(user_name); Serial.print("You have stated your proficiency to be ");Serial.println(proficiency); name_received = false; proficiency_received = false; Serial.println("We'll wait for the next client now"); } // Update LED state if necessary static bool prevLedState = false; if (ledState != prevLedState) { digitalWrite(ledPin, ledState ? HIGH : LOW); prevLedState = ledState; } }