You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.0 KiB

#include <Arduino.h>
#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 =
"<html>"
"<body>"
"<img src='https://wallpapercave.com/wp/wp9414303.jpg' alt='Image' />"
"</body>"
"</html>";
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
}
};