Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab6bce4
commit 3277c0e
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <ESP8266WiFi.h> | ||
#include <ESP8266WebServer.h> | ||
#include <FS.h> // Include the SPIFFS library | ||
|
||
#include "config.h" // Sustituir con datos de vuestra red | ||
#include "Server.hpp" | ||
#include "ESP8266_Utils.hpp" | ||
|
||
void setup(void) | ||
{ | ||
Serial.begin(115200); | ||
SPIFFS.begin(); | ||
|
||
ConnectWiFi_STA(); | ||
|
||
InitServer(); | ||
} | ||
|
||
void loop(void) | ||
{ | ||
server.handleClient(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
void ConnectWiFi_STA(bool useStaticIP = false) | ||
{ | ||
Serial.println(""); | ||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(ssid, password); | ||
if(useStaticIP) WiFi.config(ip, gateway, subnet); | ||
while (WiFi.status() != WL_CONNECTED) | ||
{ | ||
delay(100); | ||
Serial.print('.'); | ||
} | ||
|
||
Serial.println(""); | ||
Serial.print("Iniciado STA:\t"); | ||
Serial.println(ssid); | ||
Serial.print("IP address:\t"); | ||
Serial.println(WiFi.localIP()); | ||
} | ||
|
||
void ConnectWiFi_AP(bool useStaticIP = false) | ||
{ | ||
Serial.println(""); | ||
WiFi.mode(WIFI_AP); | ||
while(!WiFi.softAP(ssid, password)) | ||
{ | ||
Serial.println("."); | ||
delay(100); | ||
} | ||
if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); | ||
|
||
Serial.println(""); | ||
Serial.print("Iniciado AP:\t"); | ||
Serial.println(ssid); | ||
Serial.print("IP address:\t"); | ||
Serial.println(WiFi.softAPIP()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
String GetContentType(String filename) | ||
{ | ||
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"; | ||
} | ||
|
||
void ServeFile(String path) | ||
{ | ||
File file = SPIFFS.open(path, "r"); | ||
size_t sent = server.streamFile(file, GetContentType(path)); | ||
file.close(); | ||
} | ||
|
||
void ServeFile(String path, String contentType) | ||
{ | ||
File file = SPIFFS.open(path, "r"); | ||
size_t sent = server.streamFile(file, contentType); | ||
file.close(); | ||
} | ||
|
||
bool HandleFileRead(String path) | ||
{ | ||
if (path.endsWith("/")) path += "index.html"; | ||
Serial.println("handleFileRead: " + path); | ||
|
||
if (SPIFFS.exists(path)) | ||
{ | ||
ServeFile(path); | ||
return true; | ||
} | ||
Serial.println("\tFile Not Found"); | ||
return false; | ||
} | ||
|
||
bool HandleFileReadGzip(String path) | ||
{ | ||
if (path.endsWith("/")) path += "index.html"; | ||
Serial.println("handleFileRead: " + path); | ||
|
||
if (SPIFFS.exists(path)) | ||
{ | ||
ServeFile(path, GetContentType(path)); | ||
return true; | ||
} | ||
else | ||
{ | ||
String pathWithGz = path + ".gz"; | ||
if (SPIFFS.exists(pathWithGz)) | ||
{ | ||
ServeFile(pathWithGz, GetContentType(path)); | ||
return true; | ||
} | ||
} | ||
Serial.println("\tFile Not Found"); | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
ESP8266WebServer server(80); | ||
|
||
#include "ESP8266_Utils_Server.hpp" | ||
|
||
void handleNotFound() { | ||
server.send(404, "text/plain", "Not found"); | ||
} | ||
|
||
void InitServer() | ||
{ | ||
server.onNotFound([]() { // If the client requests any URI | ||
if (!HandleFileRead(server.uri())) // send it if it exists | ||
handleNotFound(); // otherwise, respond with a 404 (Not Found) error | ||
}); | ||
|
||
server.begin(); | ||
Serial.println("HTTP server started"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const char* ssid = "ssid"; | ||
const char* password = "password"; | ||
const char* hostname = "ESP8266_1"; | ||
|
||
IPAddress ip(192, 168, 1, 200); | ||
IPAddress gateway(192, 168, 1, 1); | ||
IPAddress subnet(255, 255, 255, 0); |