#include #define MAX_LINE_LENGTH 5012 //#define IGNORE_DETECT long cm; class WebServer { private: Stream& atSerial; Stream& debugSerial; char lineBuffer[MAX_LINE_LENGTH]; uint8_t bufferPosition = 0; public: void sendHttpResponse(String content) { atSerial.println("AT+CIPSEND=0," + String(content.length() + 90)); delay(500); // Wait for "> " atSerial.print("HTTP/1.1 200 OK\r\n"); atSerial.print("Content-Type: text/html\r\n"); atSerial.print("Connection: close\r\n"); atSerial.print("Content-Length: "); atSerial.print(content.length()); atSerial.print("\r\n\r\n"); // End of HTTP headers atSerial.print(content); delay(500); // Wait for sending to complete atSerial.println("AT+CIPCLOSE=0"); // Close the connection } String generateWebPage() { return "" "" "Image" "" "" "" "" "" "" ""; } void handleRequest(String request) { if (request == "GET /forward ") { // TODO: Implement forward movement } else if (request == "GET /backward ") { // TODO: Implement backward movement } else if (request == "GET /left ") { // TODO: Implement left movement } else if (request == "GET /right ") { // TODO: Implement right movement } else if (request == "GET / ") { // Root URL was requested } sendHttpResponse(generateWebPage()); } public: WebServer(Stream& atSerial, Stream& debugSerial) : atSerial(atSerial), debugSerial(debugSerial) {} void handleClient() { while (atSerial.available()) { char incomingByte = atSerial.read(); debugSerial.write(incomingByte); // Forwarding response to debug Serial if (incomingByte == '\n') { debugSerial.println(lineBuffer); handleRequest(lineBuffer); memset(lineBuffer, 0, sizeof(lineBuffer)); bufferPosition = 0; } else if (bufferPosition < MAX_LINE_LENGTH - 1) { lineBuffer[bufferPosition++] = incomingByte; } } } bool findAP(const char* ssid) { while (atSerial.available()) {atSerial.read();} atSerial.println("AT+CWLAP"); bool ssidFound = false; bool endOfResponse = false; unsigned long startMillis = millis(); unsigned long currentMillis = startMillis; while (!endOfResponse && (currentMillis - startMillis < 10000)) { // 5 seconds timeout while (atSerial.available()) { char incomingByte = atSerial.read(); debugSerial.write(incomingByte); // Forwarding response to debug Serial if (incomingByte == '\n') { if (strstr(lineBuffer, ssid) != nullptr) { ssidFound = true; } if (strstr(lineBuffer, "OK") != nullptr || strstr(lineBuffer, "ERROR") != nullptr) { endOfResponse = true; } memset(lineBuffer, 0, sizeof(lineBuffer)); bufferPosition = 0; } else if (bufferPosition < MAX_LINE_LENGTH - 1) { lineBuffer[bufferPosition++] = incomingByte; } } delay(10); // Short pause to allow buffer to fill up currentMillis = millis(); } return ssidFound; } public: void setup(const char* ssid, const char* password) { debugSerial.println("Setting up web server..."); if (!findAP(ssid)) { debugSerial.println("The specified AP could not be found. Please check the SSID and try again."); return; } atSerial.println("AT+CWMODE=3"); delay(1000); atSerial.print("AT+CWJAP=\""); atSerial.print(ssid); atSerial.print("\",\""); atSerial.print(password); atSerial.println("\""); delay(5000); atSerial.println("AT+CIPSERVER=1,80"); delay(1000); debugSerial.println("Web server setup complete."); } void handleSerialCommands() { while (debugSerial.available()) { char incomingByte = debugSerial.read(); atSerial.write(incomingByte); // Forwarding command to AT Serial } } String getLocalIP() { debugSerial.println("Fetching local IP..."); atSerial.println("AT+CIPSTA?"); unsigned long startTime = millis(); while (millis() - startTime < 2000) { while (atSerial.available()) { char incomingByte = atSerial.read(); debugSerial.write(incomingByte); // This line prints the raw output } } return ""; } };