-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEsp8266-Spiffs-ApWebseverPortal.ino
341 lines (301 loc) · 9.79 KB
/
Esp8266-Spiffs-ApWebseverPortal.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
modified by fryakatkop nov2015
FSWebServer - Example WebServer with SPIFFS backend for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP8266WebServer library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
upload the contents of the data folder with MkSPIFFS Tool ("ESP8266 Sketch Data Upload" in Tools menu in Arduino IDE)
or you can upload the contents of a folder if you CD in that folder and run the following command:
for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done
access the sample web page at http://esp8266fs.local *edit* dns will catch all request and response with 302
edit the page by going to http://esp8266fs.local/edit
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <DNSServer.h>
#include <FS.h>
#define DBG_OUTPUT_PORT Serial
const char *ssid = "ESP-test";
const char *password = "";
/* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
const char *myHostname = "esp8266";
const char *metaRefreshStr = "<head><meta http-equiv=\"refresh\" content=\"3; url=http://192.168.4.1/index.html\" /></head><body><p>redirecting...</p></body>";
// DNS server
const byte DNS_PORT = 53;
DNSServer dnsServer;
// LED Animations
unsigned long previousMillis = 0;
bool ledAniRider = 0;
bool ledAniFlipper = 0;
bool ledAniDir = 0;
int ledAniPos = 0;
int interval = 100; // ms
/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1); // note: update metaRefreshStr string if ip change!
IPAddress netMsk(255, 255, 255, 0);
// Web server
ESP8266WebServer server(80);
// Gpios with LEDs
byte ledIoNames[] = {5, 4, 13, 12, 14, 16 }; // GPIO 5&4 12&13 flipped on my board so dirty patch here 5<->4 12<->13
//format bytes
String formatBytes(size_t bytes){
if (bytes < 1024){
return String(bytes)+"B";
} else if(bytes < (1024 * 1024)){
return String(bytes/1024.0)+"KB";
} else if(bytes < (1024 * 1024 * 1024)){
return String(bytes/1024.0/1024.0)+"MB";
} else {
return String(bytes/1024.0/1024.0/1024.0)+"GB";
}
}
String getContentType(String filename){
if(server.hasArg("download")) return "application/octet-stream";
else if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path){
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
if(path.endsWith("/")) path += "index.html";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
if(SPIFFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
void handleFileList() {
if(!server.hasArg("dir")) {server.send(500, "text/plain", "BAD ARGS"); return;}
String path = server.arg("dir");
DBG_OUTPUT_PORT.println("handleFileList: " + path);
Dir dir = SPIFFS.openDir(path);
path = String();
String output = "[";
while(dir.next()){
File entry = dir.openFile("r");
if (output != "[") output += ',';
bool isDir = false;
output += "{\"type\":\"";
output += (isDir)?"dir":"file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
output += "\"}";
entry.close();
}
output += "]";
server.send(200, "text/json", output);
}
void handleLeds()
{
if(server.arg("LED") ) {
int pin = server.arg("LED").toInt();
bool onOFF = server.arg("state").toInt();
digitalWrite(pin, onOFF);
}
String json = "{ \"leds\": [";
for (int i = 0; i < sizeof(ledIoNames); i++)
{
bool state = digitalRead(ledIoNames[i]);
json += "{ \"" + String(i) + "\": [";
json += "\""+String(ledIoNames[i])+"\", ";
json += "\""+String(state)+"\" ] }";
if(!(i == sizeof(ledIoNames)-1))
json += ",";
}
json += "] }";
server.send(200, "text/json", json);
json = String();
}
void clearAll() {
//stop Animations
ledAniRider = 0;
ledAniFlipper = 0;
ledAniPos = 0;
ledAniDir = 0;
// clear all leds
for (int i = 0; i < sizeof(ledIoNames); i++) {
digitalWrite(ledIoNames[i], LOW);
}
}
void setup(void){
DBG_OUTPUT_PORT.begin(115200);
DBG_OUTPUT_PORT.print("\n");
DBG_OUTPUT_PORT.setDebugOutput(true);
SPIFFS.begin();
{
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
}
DBG_OUTPUT_PORT.printf("\n");
}
// Setup LED pins
for (int i = 0; i < sizeof(ledIoNames); i++) {
pinMode(ledIoNames[i],OUTPUT);
}
//WIFI INIT
DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
DBG_OUTPUT_PORT.println("");
DBG_OUTPUT_PORT.print("Connected! IP address: ");
DBG_OUTPUT_PORT.println ( WiFi.softAPIP() );
/* Setup the DNS server redirecting all the domains to the apIP */
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", apIP);
// Setup MDNS responder
if (!MDNS.begin(myHostname)) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.println("mDNS responder started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
//SERVER INIT
//list directory
server.on("/listFiles", HTTP_GET, handleFileList);
//called when the url is not defined here
//use it to load content from SPIFFS
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.send(302, "text/html", metaRefreshStr);
});
// handle led requests
server.on("/leds", HTTP_GET, handleLeds);
server.on("/clearAll", HTTP_GET, clearAll);
server.on("/rider", HTTP_GET, [](){
ledAniPos = 0;
ledAniDir = 0;
ledAniFlipper = 0;
ledAniRider = 1;
});
server.on("/flipper", HTTP_GET, [](){
ledAniPos = 0;
ledAniDir = 0;
ledAniRider = 0;
ledAniFlipper = 1;
});
//get heap status, analog input value and all GPIO statuses in one json call
server.on("/all", HTTP_GET, [](){
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += ", \"analog\":"+String(analogRead(A0));
json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}";
server.send(200, "text/json", json);
json = String();
});
server.begin();
DBG_OUTPUT_PORT.println("HTTP server started");
}
void loop(void){
//DNS
dnsServer.processNextRequest();
//HTTP
server.handleClient();
// LED Animation
if(ledAniRider || ledAniFlipper)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if(ledAniRider)
{
if(ledAniDir == 0)
{
if(ledAniPos < sizeof(ledIoNames))
{
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos -1], LOW);
ledAniPos++;
}
else
{
ledAniDir = 1;
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos +1], LOW);
ledAniPos--;
}
}
else
{
if(ledAniPos >= 0)
{
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos +1], LOW);
ledAniPos--;
}
else
{
ledAniDir = 0;
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos -1], LOW);
ledAniPos++;
}
}
}
else if(ledAniFlipper)
{
if(ledAniDir == 0)
{
if(ledAniPos < sizeof(ledIoNames))
{
digitalWrite(ledIoNames[ledAniPos], HIGH);
ledAniPos++;
}
else
{
ledAniDir = 1;
digitalWrite(ledIoNames[ledAniPos], LOW);
ledAniPos--;
}
}
else
{
if(ledAniPos >= 0)
{
digitalWrite(ledIoNames[ledAniPos], LOW);
ledAniPos--;
}
else
{
ledAniDir = 0;
digitalWrite(ledIoNames[ledAniPos], HIGH);
ledAniPos++;
}
}
}
}
}
}