174 lines
4.9 KiB
C++
174 lines
4.9 KiB
C++
#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();
|
|
}
|
|
}
|