From ccb6b26d4d902bec3d7da290ffa6b4e203c78f39 Mon Sep 17 00:00:00 2001 From: "yorick.geoffre" Date: Thu, 15 Jun 2023 10:49:10 +0200 Subject: [PATCH] check bug --- simple-webserver.ino | 77 ++++++++++++++++++++++++++++ web.ino | 25 ---------- webserver.h | 116 +++++++++++++++++++++++++------------------ 3 files changed, 146 insertions(+), 72 deletions(-) create mode 100644 simple-webserver.ino delete mode 100644 web.ino diff --git a/simple-webserver.ino b/simple-webserver.ino new file mode 100644 index 0000000..cd96799 --- /dev/null +++ b/simple-webserver.ino @@ -0,0 +1,77 @@ +#include "webserver.h" +#include + +WebServer webServer(Serial1, Serial); +int trigPin = 10; +int echoPin = 11; +long duration, inches; + +void setup() { + Serial1.setTX(12); + Serial1.setRX(13); + Serial1.begin(115200); + Serial.begin(115200); + delay(2000); + + pinMode(echoPin, INPUT); + pinMode(trigPin, OUTPUT); + pinMode(14, OUTPUT); + pinMode(15, OUTPUT); + pinMode(16, OUTPUT); + pinMode(17, OUTPUT); + + webServer.setup("tmk2", "tk666666"); + + String localIP = webServer.getLocalIP(); + if (localIP.length() > 0) { + Serial.println("Web server IP: " + localIP); + } else { + Serial.println("Could not retrieve the web server IP."); + } +} + +void handleCommand(const unsigned char& mode){ + switch(mode){ + case 0: + Serial.println("front"); + break; + case 1: + Serial.println("front"); + break; + case 2: + Serial.println("front"); + break; + case 3: + Serial.println("front"); + break; + } +} + +void loop() { + webServer.handleClient(); + webServer.handleSerialCommands(); + /*digitalWrite(trigPin, LOW); + delayMicroseconds(5); + digitalWrite(trigPin, HIGH); + delayMicroseconds(10); + digitalWrite(trigPin, LOW); + + duration = pulseIn(echoPin, HIGH); + + cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343 + inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135 + + /*Serial.print(inches); + Serial.print("in, "); + Serial.print(cm); + Serial.print("cm"); + Serial.println();*/ +/* + analogWrite(14, (cm < 100 ? 1000/(cm/10) : LOW)); + analogWrite(15, (cm < 100 ? 1000/(cm/10) : LOW)); + analogWrite(16, (cm < 100 ? LOW : HIGH)); + analogWrite(17, (cm < 100 ? LOW : HIGH)); + + delay(250);*/ + +} \ No newline at end of file diff --git a/web.ino b/web.ino deleted file mode 100644 index effd196..0000000 --- a/web.ino +++ /dev/null @@ -1,25 +0,0 @@ -#include "webserver.h" - -WebServer webServer(Serial1, Serial); - -void setup() { - Serial1.setTX(12); - Serial1.setRX(13); - Serial1.begin(115200); - Serial.begin(115200); - delay(2000); - - webServer.setup("tmk2", "tk666666"); - - String localIP = webServer.getLocalIP(); - if (localIP.length() > 0) { - Serial.println("Web server IP: " + localIP); - } else { - Serial.println("Could not retrieve the web server IP."); - } -} - -void loop() { - webServer.handleClient(); - webServer.handleSerialCommands(); -} \ No newline at end of file diff --git a/webserver.h b/webserver.h index 762897d..e181534 100644 --- a/webserver.h +++ b/webserver.h @@ -1,6 +1,9 @@ #include #define MAX_LINE_LENGTH 5012 +//#define IGNORE_DETECT + +long cm; class WebServer { private: @@ -8,6 +11,72 @@ class WebServer { 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();} @@ -46,17 +115,13 @@ class WebServer { public: - WebServer(Stream& atSerial, Stream& debugSerial) - : atSerial(atSerial), debugSerial(debugSerial) {} 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); @@ -73,25 +138,6 @@ class WebServer { debugSerial.println("Web server setup complete."); } - void handleClient() { - while (atSerial.available()) { - char incomingByte = atSerial.read(); - debugSerial.write(incomingByte); // Forwarding response to debug Serial - - if (incomingByte == '\n') { - debugSerial.println(lineBuffer); - if (strstr(lineBuffer, "GET / ") != nullptr) { - // Root URL was requested - sendHttpResponse(); - } - memset(lineBuffer, 0, sizeof(lineBuffer)); - bufferPosition = 0; - } else if (bufferPosition < MAX_LINE_LENGTH - 1) { - lineBuffer[bufferPosition++] = incomingByte; - } - } - } - void handleSerialCommands() { while (debugSerial.available()) { @@ -115,29 +161,5 @@ class WebServer { return ""; } -void sendHttpResponse() { - const char* content = - "" - "" - "Image" - "" - ""; - - atSerial.println("AT+CIPSEND=0," + String(strlen(content) + 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(strlen(content)); - 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 -} - - };