Working file serving from wemos.

This commit is contained in:
Oliver Marks 2019-09-03 22:17:39 +01:00
parent 3fd100daf1
commit 9da8e2202e
11 changed files with 1119 additions and 61 deletions

View File

@ -3,34 +3,109 @@
//#include <ESP8266HTTPClient.h> //#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <FS.h> #include <FS.h>
#include <stdio.h>
ESP8266WebServer server(80); ESP8266WebServer server(80);
//https://steve.fi/Hardware/d1-flash/ bool loadConfig(String fileName) {
void handleNotFound() { File dataFile = SPIFFS.open(fileName, "r");
// If we could serve from flash we're good. char * pch;
Serial.println(server.uri()); while (dataFile.available()) {
return; // Lets read line by line from the file
//if (loadFromSpiffs(server.uri())) String line = dataFile.readStringUntil('\n');
// return; //char line[] = dataFile.readStringUntil('\n');
pch = strtok(line,"=");
// Otherwise a generic 404. Serial.println(line);
String message = "File Not Detected\n\n"; Serial.println(pch);
message += "URI: "; //pch = strtok(NULL," ");
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 +=
" NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
} }
server.send(404, "text/plain", message); return true;
Serial.println(message); }
bool handleFile(String fileName, String dataType) {
Serial.println(fileName);
File dataFile = SPIFFS.open(fileName, "r");
if(!dataFile){
server.send(404, "text/plain", "Sorry file not found");
return false;
}
server.streamFile(dataFile, dataType);
dataFile.close();
return true;
}
const char *form-fields[] = {"device-id" "wifi-ssid" "wifi-password"};
bool handlePayload() {
Serial.println(server.args());
if(server.args()!=3)
server.send(404, "text/plain", "Missing params");
Serial.println("saving");
File dataFile = SPIFFS.open("/config.txt", "w");
for (int i = 0; i < server.args(); i++) {
if(server.argName(i) == "device-id") {
dataFile.print(server.argName(i));
dataFile.print("=");
dataFile.println(server.arg(i));
}
if(server.argName(i) == "wifi-ssid") {
dataFile.print(server.argName(i));
dataFile.print("=");
dataFile.println(server.arg(i));
}
if(server.argName(i) == "wifi-password") {
dataFile.print(server.argName(i));
dataFile.print("=");
dataFile.println(server.arg(i));
}
}
dataFile.close();
loadConfig("/config.txt");
server.send(200, "text/plain", "success");
return true;
}
//https://steve.fi/Hardware/d1-flash/
bool handleNotFound() {
// If we could serve from flash we're good.
Serial.println("handle file");
if(server.args()){
Serial.println("handle payload");
return handlePayload();
}
if(server.method() == HTTP_POST){
Serial.println("POST data");
Serial.println(server.args());
}
// jut hard code one js css and html file
// may handle multiple images
if (server.uri().endsWith(".js"))
return handleFile("/main.js", "text/javascript");
if (server.uri().endsWith(".css"))
return handleFile("/tachyon.css", "text/css");
if (server.uri().endsWith(".html"))
return handleFile("/index.html", "text/html");
if (server.uri().endsWith(".txt"))
return handleFile("/config.txt", "text/plain");
if (server.uri().endsWith(".png"))
return handleFile(server.uri(), "image/png");
File dataFile = SPIFFS.open("/index.html", "r");
if(!dataFile)
return false;
server.streamFile(dataFile, "text/html");
dataFile.close();
} }
void setup_mode() { void setup_mode() {
@ -45,6 +120,8 @@ void setup_mode() {
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.begin(); server.begin();
Serial.println("Server started");
loadConfig("/config.txt");
} }
void connect() { void connect() {
@ -97,5 +174,4 @@ void setup() {
//ESP.restart(); //ESP.restart();
void loop() { void loop() {
server.handleClient(); server.handleClient();
//delay(2000);
} }

View File

@ -1,8 +1,12 @@
<HTML> <!DOCTYPE html>
<head> <html style="height:100%;">
<link rel="stylesheet" href="tachyon.css" type="text/css" media="screen" /> <head>
</head> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<body> <meta charset="UTF-8">
Welcome <link rel="stylesheet" href="tachyon.css"/>
</body> </head>
<body style="height:100%;">
<div id="app" style="height:100%;">Loading</div>
<script src="main.js" type="text/javascript"></script>
</body>
</html> </html>

975
app/data/main.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,15 +1,13 @@
{:deps {org.clojure/clojure {:mvn/version "1.10.0"} {:deps {org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.520"} org.clojure/clojurescript {:mvn/version "1.10.520"}
reagent {:mvn/version "0.8.1"} reagent {:mvn/version "0.8.1"}
reagent-utils {:mvn/version "0.3.3"} reagent-utils {:mvn/version "0.3.3"}
cljs-ajax {:mvn/version "0.8.0"} cljs-ajax {:mvn/version "0.8.0"}
pez/clerk {:mvn/version "1.0.0"} pez/clerk {:mvn/version "1.0.0"}
metosin/reitit {:mvn/version "0.3.1"} metosin/reitit {:mvn/version "0.3.1"}
venantius/accountant {:mvn/version "0.2.4"} venantius/accountant {:mvn/version "0.2.4"}
devcards {:mvn/version "0.2.6"}
com.bhauman/figwheel-main {:mvn/version "0.2.1-SNAPSHOT"} com.bhauman/figwheel-main {:mvn/version "0.2.1-SNAPSHOT"}
cider/piggieback {:mvn/version "0.4.0"} cider/piggieback {:mvn/version "0.4.0"}
com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}} com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}}
:paths ["resources" "src"]} :resource-paths ["app/data"]
:paths ["app/data" "src"]}

View File

@ -1,7 +1,7 @@
^{:extra-main-files {:devcards {:main cards.core}}} {:output-to "app/data/dev-main.js"
{:output-to "resources/public/cljs-out/dev-main.js" :output-dir "app/data/dev"
:asset-path "app/data/dev",
:optimizations :none :optimizations :none
:pretty-print true :pretty-print true
:source-map true :source-map true
:devcards true
:main DEMOAPP.core} :main DEMOAPP.core}

View File

@ -1,5 +1,19 @@
{ {:target-dir "app/data"
:target-dir "resources"
:watch-dirs ["src"] :watch-dirs ["src"]
:css-dirs ["resources/public/css"] :ring-stack-options {:params
} {:urlencoded true, :multipart true, :nested true, :keywordize true},
:cookies true,
:session
{:flash true, :cookie-attrs {:http-only true, :same-site :strict}},
:static {:app "data"},
:responses {:content-types true, :default-charset "utf-8"},
:http-server-root "data"
:figwheel.server.ring/dev
{:figwheel.server.ring/fix-index-mime-type true,
:figwheel.server.ring/resource-root-index true,
:figwheel.server.ring/wrap-no-cache true,
:figwheel.server.ring/cljsjs-resources false,
:ring.middleware.not-modified/wrap-not-modified true,
:co.deps.ring-etag-middleware/wrap-file-etag true,
:ring.middleware.cors/wrap-cors true,
:ring.middleware.stacktrace/wrap-stacktrace true}}}

6
prod.cljs.edn Normal file
View File

@ -0,0 +1,6 @@
{:output-to "app/data/main.js"
:output-dir "app/data/prod"
:asset-path "app/data/prod",
:optimizations :advanced
:source-map nil
:main DEMOAPP.core}

View File

@ -19,3 +19,6 @@ clojure -m figwheel.main --build dev --repl
http://localhost:9500/ http://localhost:9500/
http://localhost:9500/figwheel-extra-main/devcards http://localhost:9500/figwheel-extra-main/devcards
app config
clojure -m figwheel.main --print-config --build dev

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html style="height:100%;">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<link href="/css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.10.0/css/tachyons.min.css"/>
</head>
<body style="height:100%;">
<div id="app" style="height:100%;">Loading</div>
<script src="cljs-out/dev-main.js" type="text/javascript"></script>
</body>
</html>

View File

@ -1,5 +0,0 @@
(ns cards.core
(:require
[devcards.core]
[DEMOAPP.core]))
(devcards.core/start-devcard-ui!)