Massively improved and split out the config loader.
This commit is contained in:
parent
296112c2b0
commit
aab0689ff8
186
app/app.ino
186
app/app.ino
|
@ -4,156 +4,29 @@
|
|||
#include <ESP8266WebServer.h>
|
||||
#include <FS.h>
|
||||
#include <stdio.h>
|
||||
#include "setup.h"
|
||||
|
||||
ESP8266WebServer server(80);
|
||||
// define your expected field names here
|
||||
/* #define cfg_device_id 0 */
|
||||
/* #define cfg_wifi_ssid 1 */
|
||||
/* #define cfg_wifi_key 2 */
|
||||
/* #define cfg_server 3 */
|
||||
/* const char *form_fields[] = {"device-id" "wifi-ssid" "wifi-key" "server"}; */
|
||||
/* int number_of_fields = sizeof(form_fields); */
|
||||
/* const char *config[4] = {}; */
|
||||
|
||||
//int number_of_fields = 4; //sizeof(form_fields) - 1;
|
||||
|
||||
#define cfg_device_id 0
|
||||
#define cfg_wifi_ssid 1
|
||||
#define cfg_wifi_key 2
|
||||
#define cfg_server 3
|
||||
const char *form_fields[] = {"device-id" "wifi-ssid" "wifi-key" "server"};
|
||||
int number_of_fields = sizeof(form_fields);
|
||||
const char *config[4] = {};
|
||||
|
||||
bool loadConfig(String fileName) {
|
||||
File dataFile = SPIFFS.open(fileName, "r");
|
||||
char * pch;
|
||||
const char * config_key;
|
||||
char * config_value;
|
||||
while (dataFile.available()) {
|
||||
// Lets read line by line from the file
|
||||
String line = dataFile.readStringUntil('\n');
|
||||
int str_len = line.length() + 1;
|
||||
char char_array[str_len];
|
||||
line.toCharArray(char_array, str_len);
|
||||
//char line[] = dataFile.readStringUntil('\n');
|
||||
config_key = strtok(char_array,"=");
|
||||
config_value = strtok(NULL,"=");
|
||||
config[0] = config_value;
|
||||
Serial.println(config_key);
|
||||
Serial.println(config_value);
|
||||
|
||||
for (int i=0; i<3;i++){
|
||||
if(strcmp(form_fields[i],config_key)==0){
|
||||
config[i] = config_value;
|
||||
Serial.println("------");
|
||||
Serial.println(config[i]);
|
||||
}
|
||||
}
|
||||
//Serial.println(config[1]);
|
||||
}
|
||||
|
||||
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-key", "server"};
|
||||
char config[4][50] = {};
|
||||
|
||||
|
||||
bool handlePayload() {
|
||||
Serial.println(server.args());
|
||||
|
||||
if(server.args()!=4)
|
||||
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-key") {
|
||||
dataFile.print(server.argName(i));
|
||||
dataFile.print("=");
|
||||
dataFile.println(server.arg(i));
|
||||
}
|
||||
if(server.argName(i) == "server") {
|
||||
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(".woff2"))
|
||||
return handleFile("/fa-solid-900.woff2", "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() {
|
||||
WiFi.softAP("snapwire", "snapwire"); // Start the access point
|
||||
Serial.print("Access Point \"");
|
||||
// Serial.print(ssid);
|
||||
Serial.println("\" started");
|
||||
|
||||
Serial.print("IP address:\t");
|
||||
Serial.println(
|
||||
WiFi.softAPIP()); // Send the IP address of the ESP8266 to the computer
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
server.begin();
|
||||
Serial.println("Server started");
|
||||
loadConfig("/config.txt");
|
||||
}
|
||||
|
||||
void connect() {
|
||||
void wifi_join_network() {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.hostname("flash-httpd");
|
||||
WiFi.begin("WEMOS", "SNAPWIRE");
|
||||
|
@ -170,37 +43,18 @@ void connect() {
|
|||
Serial.println("/");
|
||||
}
|
||||
|
||||
void startup() {
|
||||
File f = SPIFFS.open("/index.html", "r");
|
||||
while (f.available()) {
|
||||
// Lets read line by line from the file
|
||||
String line = f.readStringUntil('\n');
|
||||
Serial.println(line);
|
||||
}
|
||||
|
||||
f = SPIFFS.open("/config.txt", "r");
|
||||
if (!f.available()) {
|
||||
Serial.println("Launching in access point mode.");
|
||||
return;
|
||||
}
|
||||
Serial.println("Reading existing config.");
|
||||
|
||||
while (f.available()) {
|
||||
// Lets read line by line from the file
|
||||
String line = f.readStringUntil('\n');
|
||||
Serial.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
SPIFFS.begin();
|
||||
startup();
|
||||
setup_mode();
|
||||
config_startup("/config.txt", form_fields, config, sizeof(form_fields));
|
||||
/* startup(); */
|
||||
/* setup_mode(); */
|
||||
}
|
||||
|
||||
|
||||
//ESP.restart();
|
||||
void loop() {
|
||||
server.handleClient();
|
||||
// dummy does nothing unless in access point mode
|
||||
config_handler();
|
||||
//server.handleClient();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,173 @@
|
|||
#include <ESP8266WebServer.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
//#include <ESP8266WiFi.h>
|
||||
//#include <WiFiClient.h>
|
||||
//#include <ESP8266HTTPClient.h>
|
||||
//#include <ESP8266WebServer.h>
|
||||
#include "setup.h"
|
||||
#include <FS.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// ESP8266WebServer server(80);
|
||||
// define your expected field names here
|
||||
// #define cfg_device_id 0
|
||||
// #define cfg_wifi_ssid 1
|
||||
// #define cfg_wifi_key 2
|
||||
// #define cfg_server 3
|
||||
|
||||
bool config_mode = CONFIG_MODE_NORMAL;
|
||||
// const char *form_fields[] = {"device-id", "wifi-ssid", "wifi-key", "server"};
|
||||
// int number_of_fields = 4; // sizeof(form_fields) - 1;
|
||||
// char config[4][50] = {};
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
bool loadConfig(String fileName, const char *form_fields[], char (*config)[50],
|
||||
int number_of_fields) {
|
||||
File dataFile = SPIFFS.open(fileName, "r");
|
||||
// no file so fail loadings
|
||||
if (!dataFile)
|
||||
return false;
|
||||
char *config_key;
|
||||
while (dataFile.available()) {
|
||||
// Lets read line by line from the file
|
||||
String line = dataFile.readStringUntil('\n');
|
||||
int str_len = line.length() + 1;
|
||||
char char_array[str_len];
|
||||
line.toCharArray(char_array, str_len);
|
||||
config_key = strtok(char_array, "=");
|
||||
Serial.println("++++++++++++");
|
||||
for (int i = 0; i < number_of_fields; i++) {
|
||||
if (strcmp(form_fields[i], config_key) == 0) {
|
||||
// config[i] = config_value;
|
||||
strcpy(config[i], strtok(NULL, "="));
|
||||
Serial.println("############");
|
||||
}
|
||||
}
|
||||
}
|
||||
Serial.println("++ Loaded config");
|
||||
for (int i = 0; i < number_of_fields; i++) {
|
||||
Serial.println(i);
|
||||
Serial.println(form_fields[i]);
|
||||
Serial.println(config[i]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
bool handlePayload() {
|
||||
Serial.println(server.args());
|
||||
|
||||
if (server.args() != 4)
|
||||
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-key") {
|
||||
dataFile.print(server.argName(i));
|
||||
dataFile.print("=");
|
||||
dataFile.println(server.arg(i));
|
||||
}
|
||||
if (server.argName(i) == "server") {
|
||||
dataFile.print(server.argName(i));
|
||||
dataFile.print("=");
|
||||
dataFile.println(server.arg(i));
|
||||
}
|
||||
}
|
||||
dataFile.close();
|
||||
server.send(200, "text/plain", "success");
|
||||
ESP.restart();
|
||||
return true;
|
||||
}
|
||||
|
||||
// https://steve.fi/Hardware/d1-flash/
|
||||
bool handleRequestedPage() {
|
||||
// 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(".woff2"))
|
||||
return handleFile("/fa-solid-900.woff2", "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 access_point_setup_mode() {
|
||||
WiFi.softAP("snapwire", "snapwire"); // Start the access point
|
||||
Serial.print("IP address:\t");
|
||||
// Send the IP address of the ESP8266 to the computer
|
||||
Serial.println(WiFi.softAPIP());
|
||||
|
||||
|
||||
//[a]() { handleRequestedPage(a); } ();
|
||||
|
||||
server.onNotFound(handleRequestedPage);
|
||||
server.begin();
|
||||
}
|
||||
|
||||
int config_startup(String fileName, const char *fields[], char (*config)[50],
|
||||
int number_of_fields) {
|
||||
if (loadConfig(fileName, fields, config, number_of_fields) == true) {
|
||||
Serial.println("Config detected and loaded");
|
||||
return config_mode;
|
||||
}
|
||||
config_mode = CONFIG_MODE_ACCESS_POINT;
|
||||
Serial.println("Access point launched, please connect to configure device.");
|
||||
access_point_setup_mode();
|
||||
return config_mode;
|
||||
}
|
||||
|
||||
void config_handler() {
|
||||
if (config_mode == CONFIG_MODE_ACCESS_POINT) {
|
||||
server.handleClient();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef device_setup_H
|
||||
#define device_setup_H
|
||||
|
||||
#define CONFIG_MODE_NORMAL 0
|
||||
#define CONFIG_MODE_ACCESS_POINT 1
|
||||
|
||||
|
||||
bool loadConfig(String fileName, char (*config)[50], int number_of_elements);
|
||||
bool handleRequestedPage();
|
||||
void config_via_access_point();
|
||||
int config_startup(String fileName, const char *fields[], char (*config)[50], int number_of_fields);
|
||||
void config_handler();
|
||||
|
||||
#endif
|
|
@ -1,17 +1,18 @@
|
|||
(ns DEMOAPP.core
|
||||
(:require [accountant.core :as accountant]
|
||||
[clojure.spec.alpha :as s]
|
||||
[ajax.core :refer [GET, POST]]
|
||||
|
||||
[reagent.core :as reagent :refer [atom]]
|
||||
[reagent.session :as session]
|
||||
[ajax.core :refer [GET, POST]]
|
||||
[reitit.frontend :as reitit]))
|
||||
|
||||
;;(enable-console-print!)
|
||||
(defonce form-errors (atom nil))
|
||||
(defonce form-title "GASP Sensor setup")
|
||||
(defonce form-data (atom {:device-id "" :wifi-ssid "" :wifi-key ""}))
|
||||
(defonce form-data (atom {:server "" :device-id "" :wifi-ssid "" :wifi-key ""}))
|
||||
(defonce form-layout [{:name "server" :title "Server" :placeholder "127.0.0.1"
|
||||
:data form-data :validator "device-id-type"
|
||||
:data form-data :validator "server-type"
|
||||
:error "Invalid server address"}
|
||||
{:name "device-id" :title "Device Unique ID" :placeholder "AAAA"
|
||||
:data form-data :validator "device-id-type"
|
||||
|
@ -47,15 +48,17 @@
|
|||
[:input#wifi-ssid.pa2.input-reset.ba.bg-near-white.w-100.measure.b--silver
|
||||
{:type (or type "text") :name name
|
||||
:placeholder placeholder :autoFocus (or focus nil)
|
||||
:on-change #(swap! data assoc (keyword name) (-> % .-target .-value))
|
||||
:value ((keyword name) @data)
|
||||
:on-change #(swap! form-data assoc :device-id (-> % .-target .-value))
|
||||
:value (get @form-data :device-id)
|
||||
}]
|
||||
[:p (if (and (not-empty ((keyword name) @data))
|
||||
(s/explain-data
|
||||
(keyword 'DEMOAPP.core validator)
|
||||
(:device-id @data)))
|
||||
((keyword name) @data)))
|
||||
error)]])
|
||||
|
||||
(swap! form-data assoc :device-id "aa")
|
||||
|
||||
(defn handle-failure [{:keys [status status-text response]}]
|
||||
(case status
|
||||
500 (do (reset! form-errors "Something bad happened submitting your data."))
|
||||
|
|
Loading…
Reference in New Issue