Skip to content

Commit

Permalink
Create 3-WebServer.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
GlitchedPanda authored Jan 12, 2024
1 parent 8bd16d2 commit 5bc992f
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Programs/3-WebServer/3-WebServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <WiFi.h>
#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

const char* SSID = "ssid";
const char* PASSWORD = "password";

WiFiServer server(80);
String header;

unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;

void setup() {
// put your setup code here, to run once:
tft.init();
tft.setRotation(1);

tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);

String connectingMsg = "Connecting to " + String(SSID) + "...";
tft.drawString(connectingMsg, 10, 10, 2);

WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.begin();

tft.fillScreen(TFT_BLACK);

String connectedMsg = "Connected to " + String(SSID);
tft.drawString(connectedMsg, 10, 10, 2);

String localIPMsg = "Local IP: " + WiFi.localIP().toString();
tft.drawString(localIPMsg, 10, 30, 2);
}

void loop() {
WiFiClient client = server.available();

if(client) {
currentTime = millis();
previousTime = currentTime;
String currentLine = "";

while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if(client.available()) {
char data = client.read();
header += data;
if (data == '\n') {
if(currentLine.length() == 0) {
// Headers to tell the client what to do
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();

// Website content
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>");
client.println("<body><p>Hello, World!</p></body></html>");

// It has to end with a blank line
client.println();
break;
} else {
currentLine = "";
}
} else if (data != '\r') {
currentLine += data;
}
}
}

header = "";
client.stop();
}
}

0 comments on commit 5bc992f

Please sign in to comment.