commit
1578d96a1f
@ -0,0 +1,6 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
"version": "0.2.0",
|
||||
"configurations": []
|
||||
}
|
@ -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();
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
#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
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
Loading…
Reference in new issue