-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtvcbrdg.ino
73 lines (69 loc) · 2.07 KB
/
btvcbrdg.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <LittleFS.h>
#include <WiFiClient.h>
// WiFi and user authentication related constants
const char* SSID = "";
const char* WIFI_PASSWORD = "";
const char* HOSTNAME = "btvcbrdg";
const char* USERNAME = "btvcbrdg";
const char* PASSWORD = "btvcbrdg";
// Server object
ESP8266WebServer server(80);
// Setup function
void setup(void) {
// Initialize filesystem
LittleFS.begin();
// Initialize serial and WiFi connection
Serial.begin(115200);
Serial.setTimeout(10000);
Serial.readString();
Serial.setTimeout(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Initialize mDNS
MDNS.begin(HOSTNAME);
// Initialize server
server.on("/", HTTP_GET, [&]() {
if (!server.authenticate(USERNAME, PASSWORD)) {
server.requestAuthentication();
} else {
File file = LittleFS.open("/index.html", "r");
String content;
while (file.available()) {
content += (char) file.read();
}
file.close();
server.send(200, "text/html", content);
}
});
server.on("/", HTTP_POST, [&]() {
if (!server.authenticate(USERNAME, PASSWORD)) {
server.requestAuthentication();
} else {
if (server.hasArg("command")) {
String content = "";
Serial.print(server.arg("command"));
Serial.print('\n');
content = Serial.readStringUntil('\x1a');
server.send(200, "text/plain", content);
} else {
server.send(404, "text/plain", "Invalid request");
}
}
});
server.serveStatic("/styles/dark.min.css", LittleFS, "/styles/dark.min.css");
server.onNotFound([&]() {
server.send(404, "text/plain", "Resource not found");
});
server.begin();
}
// Main function
void loop(void) {
server.handleClient();
MDNS.update();
}