-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnarchoEarrings.ino
115 lines (89 loc) · 3.13 KB
/
AnarchoEarrings.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// @file WebServer.ino
// @brief Example implementation using the ESP8266 WebServer.
//
// See also README.md for instructions and hints.
//
// Changelog:
// 21.07.2021 creation, first version
#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <LittleFS.h> // This file system is used.
const IPAddress apIp(192, 168, 0, 1);
DNSServer dnsServer;
// mark parameters not used in example
#define UNUSED __attribute__((unused))
// TRACE output simplified, can be deactivated here
#define TRACE(...) Serial.printf(__VA_ARGS__)
// name of the server. You reach it using http://anarchy.lan
#define HOSTNAME "anarchy.lan"
// need a WebServer for http access on port 80.
ESP8266WebServer server(80);
// The text of builtin files are in this header file
#include "builtinfiles.h"
void handleRedirect() // redirect to the main page
{
server.sendHeader("Location", "http://anarchy.lan/index.html", true);
server.send(302, "text/plain", "");
}
// This function is called when the sysInfo service was requested.
void handleSysInfo() {
String result;
FSInfo fs_info;
LittleFS.info(fs_info);
result += "{\n";
result += " \"flashSize\": " + String(ESP.getFlashChipSize()) + ",\n";
result += " \"freeHeap\": " + String(ESP.getFreeHeap()) + ",\n";
result += " \"fsTotalBytes\": " + String(fs_info.totalBytes) + ",\n";
result += " \"fsUsedBytes\": " + String(fs_info.usedBytes) + ",\n";
result += "}";
server.sendHeader("Cache-Control", "no-cache");
server.send(200, "text/javascript; charset=utf-8", result);
} // handleSysInfo()
// Setup everything to make the webserver work.
void setup(void) {
delay(3000); // wait for serial monitor to start completely.
// Use Serial port for some trace information from the example
Serial.begin(115200);
Serial.setDebugOutput(false);
TRACE("Starting WebServer...\n");
TRACE("Mounting the filesystem...\n");
if (!LittleFS.begin()) {
TRACE("could not mount the filesystem...\n");
delay(2000);
ESP.restart();
}
// start WiFI
WiFi.mode(WIFI_AP);
WiFi.persistent(false);
WiFi.disconnect(true);
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
WiFi.softAP("anarchy_earring", nullptr, 1);
// allow to address the device by the given name e.g. http://webserver
WiFi.setHostname(HOSTNAME);
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", apIp);
TRACE("Connect to WiFi...\n");
TRACE("Register service handlers...\n");
// register a redirect handler when only domain name is given.
server.on("/", HTTP_GET, handleRedirect);
// register REST service for system info
server.on("/$sysinfo", HTTP_GET, handleSysInfo);
// enable CORS header in webserver results
server.enableCORS(true);
TRACE("Serving static...\n");
// serve all static files
server.serveStatic("/", LittleFS, "/");
TRACE("Served...\n");
// handle cases when file is not found
server.onNotFound(handleRedirect);
TRACE("Registered onNotFound...\n");
server.begin();
TRACE("Server begun...");
} // setup
// run the server...
void loop(void) {
dnsServer.processNextRequest();
server.handleClient();
} // loop()
// end.