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 <ESP8266WebServer.h>
#include <FS.h>
#include <stdio.h>
ESP8266WebServer server(80);
//https://steve.fi/Hardware/d1-flash/
void handleNotFound() {
// If we could serve from flash we're good.
Serial.println(server.uri());
return;
//if (loadFromSpiffs(server.uri()))
// return;
// Otherwise a generic 404.
String message = "File Not Detected\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 +=
" NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
bool loadConfig(String fileName) {
File dataFile = SPIFFS.open(fileName, "r");
char * pch;
while (dataFile.available()) {
// Lets read line by line from the file
String line = dataFile.readStringUntil('\n');
//char line[] = dataFile.readStringUntil('\n');
pch = strtok(line,"=");
Serial.println(line);
Serial.println(pch);
//pch = strtok(NULL," ");
}
server.send(404, "text/plain", message);
Serial.println(message);
return true;
}
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() {
@ -45,6 +120,8 @@ void setup_mode() {
server.onNotFound(handleNotFound);
server.begin();
Serial.println("Server started");
loadConfig("/config.txt");
}
void connect() {
@ -97,5 +174,4 @@ void setup() {
//ESP.restart();
void loop() {
server.handleClient();
//delay(2000);
}

View File

@ -1,8 +1,12 @@
<HTML>
<head>
<link rel="stylesheet" href="tachyon.css" type="text/css" media="screen" />
</head>
<body>
Welcome
</body>
<!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 rel="stylesheet" href="tachyon.css"/>
</head>
<body style="height:100%;">
<div id="app" style="height:100%;">Loading</div>
<script src="main.js" type="text/javascript"></script>
</body>
</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"}
org.clojure/clojurescript {:mvn/version "1.10.520"}
reagent {:mvn/version "0.8.1"}
reagent-utils {:mvn/version "0.3.3"}
cljs-ajax {:mvn/version "0.8.0"}
pez/clerk {:mvn/version "1.0.0"}
metosin/reitit {:mvn/version "0.3.1"}
venantius/accountant {:mvn/version "0.2.4"}
devcards {:mvn/version "0.2.6"}
com.bhauman/figwheel-main {:mvn/version "0.2.1-SNAPSHOT"}
cider/piggieback {:mvn/version "0.4.0"}
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 "resources/public/cljs-out/dev-main.js"
{:output-to "app/data/dev-main.js"
:output-dir "app/data/dev"
:asset-path "app/data/dev",
:optimizations :none
:pretty-print true
:source-map true
:devcards true
:main DEMOAPP.core}
:main DEMOAPP.core}

View File

@ -1,5 +1,19 @@
{
:target-dir "resources"
{:target-dir "app/data"
: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/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!)