Created
November 26, 2015 18:03
-
-
Save sticilface/42f6f6d5f706841dace7 to your computer and use it in GitHub Desktop.
Example sketch serving a static directory where / does not find /index.htm
This file contains hidden or 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
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#include <FS.h> | |
#include <ArduinoOTA.h> | |
#include <ESP8266mDNS.h> | |
const char* ssid = "xxx"; | |
const char* password = "xxx"; | |
static const char * webpage = | |
"<html>\ | |
<head>\ | |
<meta http-equiv='refresh' content='5'/>\ | |
<title>ESP8266 Demo</title>\ | |
<style>\ | |
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ | |
</style>\ | |
</head>\ | |
<body>\ | |
<h1>Hello from ESP8266!</h1>\ | |
<p></p>\ | |
\ | |
</body>\ | |
</html>"; | |
ESP8266WebServer server ( 80 ); | |
const int led = 13; | |
void handleNotFound() { | |
digitalWrite ( led, 1 ); | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for ( uint8_t i = 0; i < server.args(); i++ ) { | |
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; | |
} | |
server.send ( 404, "text/plain", message ); | |
} | |
void setup ( void ) { | |
Serial.begin ( 115200 ); | |
WiFi.begin ( ssid, password ); | |
Serial.println ( "" ); | |
SPIFFS.begin(); | |
// Wait for connection | |
while ( WiFi.status() != WL_CONNECTED ) { | |
delay ( 500 ); | |
Serial.print ( "." ); | |
} | |
Serial.println ( "" ); | |
Serial.print ( "Connected to " ); | |
Serial.println ( ssid ); | |
Serial.print ( "IP address: " ); | |
Serial.println ( WiFi.localIP() ); | |
server.serveStatic("/test", SPIFFS, "/test", "max-age=86400"); | |
server.onNotFound ( handleNotFound ); | |
server.begin(); | |
Serial.println ( "HTTP server started" ); | |
ArduinoOTA.begin(); | |
File f; | |
f = SPIFFS.open("/test/index.htm", "w"); | |
delay(1000); | |
if (f) { | |
f.print(webpage); | |
f.close(); | |
Serial.print("Create Page SUCCESS"); | |
} else { | |
Serial.print("Create Page FAILED"); | |
} | |
} | |
void loop ( void ) { | |
server.handleClient(); | |
ArduinoOTA.handle(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment