178 lines
4.3 KiB
C++
178 lines
4.3 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <WiFiClient.h>
|
|
//#include <ESP8266HTTPClient.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <FS.h>
|
|
#include <stdio.h>
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
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," ");
|
|
}
|
|
|
|
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() {
|
|
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();
|
|
}
|