Skip to content

Commit

Permalink
Added lesson 9
Browse files Browse the repository at this point in the history
  • Loading branch information
luisllamasbinaburo committed Aug 22, 2019
1 parent ab6bce4 commit 3277c0e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 09_Server_SPIFFS/09_Server_SPIFFS.ino
@@ -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();
}

36 changes: 36 additions & 0 deletions 09_Server_SPIFFS/ESP8266_Utils.hpp
@@ -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());
}
67 changes: 67 additions & 0 deletions 09_Server_SPIFFS/ESP8266_Utils_Server.hpp
@@ -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;
}
18 changes: 18 additions & 0 deletions 09_Server_SPIFFS/Server.hpp
@@ -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");
}
7 changes: 7 additions & 0 deletions 09_Server_SPIFFS/config.h
@@ -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);

0 comments on commit 3277c0e

Please sign in to comment.