| 2 |
raymond |
1 |
#include <FS.h>
|
|
|
2 |
#include <LittleFS.h>
|
|
|
3 |
#include "AsyncFsWebServer.h"
|
|
|
4 |
|
|
|
5 |
#include "index_htm.h"
|
|
|
6 |
|
|
|
7 |
#define FILESYSTEM LittleFS
|
|
|
8 |
|
|
|
9 |
char const* hostname = "fsbrowser";
|
|
|
10 |
AsyncFsWebServer server(FILESYSTEM, 80, hostname);
|
|
|
11 |
|
|
|
12 |
#ifndef LED_BUILTIN
|
|
|
13 |
#define LED_BUILTIN 2
|
|
|
14 |
#endif
|
|
|
15 |
#define BOOT_BUTTON 0
|
|
|
16 |
|
|
|
17 |
// Log messages both on Serial and WebSocket clients
|
|
|
18 |
void wsLogPrintf(bool toSerial, const char* format, ...) {
|
|
|
19 |
char buffer[128];
|
|
|
20 |
va_list args;
|
|
|
21 |
va_start(args, format);
|
|
|
22 |
vsnprintf(buffer, 128, format, args);
|
|
|
23 |
va_end(args);
|
|
|
24 |
server.wsBroadcast(buffer);
|
|
|
25 |
if (toSerial)
|
|
|
26 |
Serial.println(buffer);
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
// In this example a custom websocket event handler is used instead default
|
|
|
30 |
void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
|
|
|
31 |
switch (type) {
|
|
|
32 |
case WS_EVT_CONNECT:
|
|
|
33 |
client->printf("{\"Websocket connected\": true, \"clients\": %u}", client->id());
|
|
|
34 |
Serial.printf("Websocket client %u connected\n", client->id());
|
|
|
35 |
break;
|
|
|
36 |
|
|
|
37 |
case WS_EVT_DISCONNECT:
|
|
|
38 |
Serial.printf("Websocket client %u connected\n", client->id());
|
|
|
39 |
break;
|
|
|
40 |
|
|
|
41 |
case WS_EVT_DATA:
|
|
|
42 |
{
|
|
|
43 |
AwsFrameInfo* info = (AwsFrameInfo*)arg;
|
|
|
44 |
if (info->opcode == WS_TEXT) {
|
|
|
45 |
char msg[len+1];
|
|
|
46 |
msg[len] = '\0';
|
|
|
47 |
memcpy(msg, data, len);
|
|
|
48 |
Serial.printf("Received message \"%s\"\n", msg);
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
break;
|
|
|
52 |
|
|
|
53 |
default:
|
|
|
54 |
break;
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Test "config" values
|
|
|
59 |
String option1 = "Test option String";
|
|
|
60 |
uint32_t option2 = 1234567890;
|
|
|
61 |
uint8_t ledPin = LED_BUILTIN;
|
|
|
62 |
|
|
|
63 |
// Timezone definition to get properly time from NTP server
|
|
|
64 |
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
|
|
|
65 |
struct tm Time;
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
//////////////////////////////// NTP Time /////////////////////////////////////
|
|
|
69 |
void getUpdatedtime(const uint32_t timeout) {
|
|
|
70 |
uint32_t start = millis();
|
|
|
71 |
Serial.print("Sync time...");
|
|
|
72 |
while (millis() - start < timeout && Time.tm_year <= (1970 - 1900)) {
|
|
|
73 |
time_t now = time(nullptr);
|
|
|
74 |
Time = *localtime(&now);
|
|
|
75 |
delay(5);
|
|
|
76 |
}
|
|
|
77 |
Serial.println(" done.");
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
//////////////////////////////// Filesystem /////////////////////////////////////////
|
|
|
82 |
bool startFilesystem() {
|
|
|
83 |
if (FILESYSTEM.begin()) {
|
|
|
84 |
server.printFileList(FILESYSTEM, "/", 1, Serial);
|
|
|
85 |
return true;
|
|
|
86 |
} else {
|
|
|
87 |
Serial.println("ERROR on mounting filesystem. It will be reformatted!");
|
|
|
88 |
FILESYSTEM.format();
|
|
|
89 |
ESP.restart();
|
|
|
90 |
}
|
|
|
91 |
return false;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
//////////////////// Load and save application configuration from filesystem ////////////////////
|
|
|
96 |
bool loadApplicationConfig() {
|
|
|
97 |
if (FILESYSTEM.exists(server.getConfiFileName())) {
|
|
|
98 |
File file = server.getConfigFile("r");
|
|
|
99 |
String content = file.readString();
|
|
|
100 |
file.close();
|
|
|
101 |
CJSON::Json json;
|
|
|
102 |
if (!json.parse(content)) {
|
|
|
103 |
Serial.println(F("Failed to parse JSON configuration."));
|
|
|
104 |
return false;
|
|
|
105 |
}
|
|
|
106 |
String str;
|
|
|
107 |
double num;
|
|
|
108 |
if (json.getString("Option 1", str)) option1 = str;
|
|
|
109 |
if (json.getNumber("Option 2", num)) option2 = (uint32_t)num;
|
|
|
110 |
if (json.getNumber("LED Pin", num)) ledPin = (uint8_t)num;
|
|
|
111 |
return true;
|
|
|
112 |
}
|
|
|
113 |
return false;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
void setup() {
|
|
|
118 |
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
119 |
pinMode(BOOT_BUTTON, INPUT_PULLUP);
|
|
|
120 |
|
|
|
121 |
Serial.begin(115200);
|
|
|
122 |
delay(1000);
|
|
|
123 |
|
|
|
124 |
// FILESYSTEM INIT
|
|
|
125 |
if (startFilesystem()) {
|
|
|
126 |
// Load configuration (if not present, default will be created when webserver will start)
|
|
|
127 |
if (loadApplicationConfig()) {
|
|
|
128 |
Serial.println(F("\nApplication option loaded"));
|
|
|
129 |
Serial.printf(" LED Pin: %d\n", ledPin);
|
|
|
130 |
Serial.printf(" Option 1: %s\n", option1.c_str());
|
|
|
131 |
Serial.printf(" Option 2: %u\nn", option2);
|
|
|
132 |
}
|
|
|
133 |
else
|
|
|
134 |
Serial.println(F("Application options NOT loaded!"));
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// Try to connect to WiFi (will start AP if not connected after timeout)
|
|
|
138 |
if (!server.startWiFi(10000)) {
|
|
|
139 |
Serial.println("\nWiFi not connected! Starting AP mode...");
|
|
|
140 |
server.startCaptivePortal("ESP_AP", "123456789", "/setup");
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
// Configure /setup page
|
|
|
144 |
server.addOptionBox("My Options");
|
|
|
145 |
server.addOption("LED Pin", ledPin);
|
|
|
146 |
server.addOption("Option 1", option1.c_str());
|
|
|
147 |
server.addOption("Option 2", option2);
|
|
|
148 |
|
|
|
149 |
// Add custom page handlers
|
|
|
150 |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
151 |
request->send(200, "text/html", homepage);
|
|
|
152 |
});
|
|
|
153 |
|
|
|
154 |
// Enable ACE FS file web editor and add FS info callback function
|
|
|
155 |
server.enableFsCodeEditor();
|
|
|
156 |
|
|
|
157 |
// Init with custom WebSocket event handler and start server
|
|
|
158 |
server.init(onWsEvent);
|
|
|
159 |
|
|
|
160 |
Serial.print(F("ESP Web Server started on IP Address: "));
|
|
|
161 |
Serial.println(server.getServerIP());
|
|
|
162 |
Serial.println(F(
|
|
|
163 |
"This is \"withWebSocket.ino\" example.\n"
|
|
|
164 |
"Open /setup page to configure optional parameters.\n"
|
|
|
165 |
"Open /edit page to view, edit or upload example or your custom webserver source files."
|
|
|
166 |
));
|
|
|
167 |
|
|
|
168 |
// Set hostname
|
|
|
169 |
WiFi.setHostname(hostname);
|
|
|
170 |
configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
|
|
|
171 |
|
|
|
172 |
// Start MDSN responder
|
|
|
173 |
if (WiFi.status() == WL_CONNECTED) {
|
|
|
174 |
if (MDNS.begin(hostname)) {
|
|
|
175 |
Serial.println(F("MDNS responder started."));
|
|
|
176 |
Serial.printf("You should be able to connect with address http://%s.local/\n", hostname);
|
|
|
177 |
// Add service to MDNS-SD
|
|
|
178 |
MDNS.addService("http", "tcp", 80);
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
void loop() {
|
|
|
185 |
|
|
|
186 |
if (digitalRead(BOOT_BUTTON) == LOW) {
|
|
|
187 |
wsLogPrintf(true, "Button on GPIO %d clicked", BOOT_BUTTON);
|
|
|
188 |
delay(1000);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// Send ESP system time (epoch) to WS client
|
|
|
192 |
static uint32_t sendToClientTime;
|
|
|
193 |
if (millis() - sendToClientTime > 1000) {
|
|
|
194 |
sendToClientTime = millis();
|
|
|
195 |
time_t now = time(nullptr);
|
|
|
196 |
wsLogPrintf(false, "{\"esptime\": %d}", (int)now);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
delay(10);
|
|
|
200 |
}
|