forked from CarlosRDomin/GeophoneDuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webServer.h
71 lines (58 loc) · 4.53 KB
/
webServer.h
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
/****** Web server config ******/
#ifndef WEB_SERVER_H_
#define WEB_SERVER_H_
#include "main.h" // Global includes and definitions
#include "GPIO.h" // GPIO library so HTTP server can change IO state upon request
#include "WiFi.h" // WiFi library needed for secret settings (to detect which interface a client is connected on, WLAN or AP)
#include <ESPAsyncWebServer.h> // HTTP web server
#include <ESP8266HTTPUpdateServer.h> // OTA (upload firmware through HTTP browser over WiFi)
#include <FS.h> // SPIFFS file system (to read/write to flash)
#include <SPIFFSEditor.h> // Helper that provides the resources to view&edit SPIFFS files through HTTP
#define consolePrintF(s, ...) consolePrintf(String(F(s)).c_str(), ##__VA_ARGS__)
#define CONT(x) String(FPSTR(contentType_P[x]))
#define PORT_WEBSERVER 80 // Port for the webServer
#define PORT_WEBSOCKET_GEOPHONE 81 // Port for the webSocket for real-time geophone streaming purposes
#define PORT_WEBSOCKET_CONSOLE 82 // Port for the webSocket to which debug Serial.print messages are forwarded
#define PORT_WEBSOCKET_FFT 90 // Port for the webSocket for real-time geophone FFT streaming purposes
#define PORT_ARDUINO_OTA 8266 // Port recognized by Arduino IDE which allows remote firmware flashing
#define WEB_FILE_EDITOR_USERNAME SF("PEILab")
#define WEB_FILE_EDITOR_PASS SF("geophone")
#define USE_NATIVE_WEBSOCKET_LIB false // True to use ESPAsyncWebServer's webSocket implementation, false for WebSocketsServer's
#define USE_ARDUINO_OTA false // Whether or not to use Arduino's native IDE remote firmware flasher
#define USE_MDNS false // Whether or not to use mDNS (allows access to the arduino through a name without knowing its IP)
#define UNIQUE_HOSTNAME true // If true, use ESP.getChipId() to create a unique hostname; Otherwise, use "GeophoneDuino"
#define UPLOAD_TEMP_FILENAME "/tmp.file" // Temporary file name given to a file uploaded through the web server. Once we receive its desired path, we'll rename it (move it)
#if DO_FFT
#include "FFT.h" // FFT library in case we also want to stream the FFT of the ADC data
#endif
#if !USE_NATIVE_WEBSOCKET_LIB
#include <WebSocketsServer.h> // WebSockets library
#endif
#if USE_ARDUINO_OTA
#include <ArduinoOTA.h> // OTA (upload firmware through Arduino IDE over WiFi)
#endif
#if USE_MDNS
#include <ESP8266mDNS.h> // DNS (allows access to the arduino through a name without knowing its IP)
#endif
enum {TYPE_PLAIN=0, TYPE_HTML, TYPE_JSON, TYPE_CSS, TYPE_JS, TYPE_PNG, TYPE_GIF, TYPE_JPG, TYPE_ICO, TYPE_XML, TYPE_PDF, TYPE_ZIP, TYPE_GZ, TYPE_DLOAD};
const char* const PROGMEM contentType_P[] = {"text/plain", "text/html", "text/json", "text/css", "application/javascript", "image/png", "image/gif", "image/jpeg", "image/x-icon", "text/xml", "application/x-pdf", "application/x-zip", "application/x-gzip", "application/octet-stream"};
/***************************************************/
/****** SETUP FUNCTIONS ******/
/***************************************************/
void setupWebServer(); // Initializes hostName, mDNS, HTTP server, OTA methods (HTTP-based and IDE-based) and webSockets
/*********************************************************/
/****** Web server related functions ******/
/*********************************************************/
void addNoCacheHeaders(AsyncWebServerResponse* response); // Add specific headers to an http response to avoid caching
void handleFileUpload(AsyncWebServerRequest* request, String filename, size_t index, uint8_t * data, size_t len, bool final); // Allows user to upload a file to the SPIFFS (so we don't have to write the whole Flash via USB)
bool renameFileUpload(String fileName); // Renames the last file uploaded to the new file name provided
void webServerWLANscan(AsyncWebServerRequest* request); // Handles secret HTTP page that scans WLAN networks
void webServerWLANsave(AsyncWebServerRequest* request); // Handles secret HTTP page that saves new WLAN settings
#if USE_NATIVE_WEBSOCKET_LIB
void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t * data, size_t len); // webSocket event callback function
#else
void onWsEvent(WebSocketsServer* ws, uint8_t num, WStype_t type, uint8_t * payload, size_t len); // webSocket event callback function
#endif
void consolePrintf(const char * format, ...); // Log messages through webSocketConsole and Serial
void processWebServer(); // "webServer.loop()" function: handle incoming OTA connections (if any), http requests and webSocket events
#endif