master
Yorick GEOFFRE 2 years ago
parent 1578d96a1f
commit ccb6b26d4d

@ -0,0 +1,77 @@
#include "webserver.h"
#include <atomic>
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);*/
}

@ -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();
}

@ -1,6 +1,9 @@
#include <Arduino.h>
#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 "<html>"
"<body>"
"<img src='https://wallpapercave.com/wp/wp9414303.jpg' alt='Image' />"
"<button onclick='location.href=\"/forward\"'>Forward</button>"
"<button onclick='location.href=\"/backward\"'>Backward</button>"
"<button onclick='location.href=\"/left\"'>Left</button>"
"<button onclick='location.href=\"/right\"'>Right</button>"
"<input type='range' min='0' max='100' value='" + String(cm) + "' disabled>"
"</body>"
"</html>";
}
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 =
"<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…
Cancel
Save