From 1578d96a1f1d024b097ce5c31384ef2010fec736 Mon Sep 17 00:00:00 2001 From: "yorick.geoffre" Date: Sun, 28 May 2023 16:35:24 +0200 Subject: [PATCH] initial commit --- .theia/launch.json | 6 ++ web.ino | 25 ++++++++ webserver.h | 143 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 .theia/launch.json create mode 100644 web.ino create mode 100644 webserver.h diff --git a/.theia/launch.json b/.theia/launch.json new file mode 100644 index 0000000..a2ea02c --- /dev/null +++ b/.theia/launch.json @@ -0,0 +1,6 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + "version": "0.2.0", + "configurations": [] +} diff --git a/web.ino b/web.ino new file mode 100644 index 0000000..effd196 --- /dev/null +++ b/web.ino @@ -0,0 +1,25 @@ +#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 new file mode 100644 index 0000000..762897d --- /dev/null +++ b/webserver.h @@ -0,0 +1,143 @@ +#include + +#define MAX_LINE_LENGTH 5012 + +class WebServer { + private: + Stream& atSerial; + Stream& debugSerial; + char lineBuffer[MAX_LINE_LENGTH]; + uint8_t bufferPosition = 0; + + 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: + 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); + + 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 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()) { + 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 ""; + } + +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 +} + + + +};