207 lines
5.2 KiB
C++
207 lines
5.2 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <WiFiClient.h>
|
|
//#include <ESP8266HTTPClient.h>
|
|
#include <ESP8266WebServer.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
|
|
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;
|
|
}
|
|
|
|
|
|
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() {
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.hostname("flash-httpd");
|
|
WiFi.begin("WEMOS", "SNAPWIRE");
|
|
// Wait until we're connected.
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("WiFi connected");
|
|
// Print the IP address
|
|
Serial.print("Use this URL to connect: ");
|
|
Serial.print("http://");
|
|
Serial.print(WiFi.localIP());
|
|
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();
|
|
}
|
|
|
|
|
|
//ESP.restart();
|
|
void loop() {
|
|
server.handleClient();
|
|
}
|