Skip to content

Instantly share code, notes, and snippets.

@GeorgeFlorian
Created February 20, 2019 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgeFlorian/7ce3de245e8a39434a5effc92d8f53a8 to your computer and use it in GitHub Desktop.
Save GeorgeFlorian/7ce3de245e8a39434a5effc92d8f53a8 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
String v[2];
AsyncWebServer server(80);
const char* ssid;
const char* password;
void setup() {
Serial.begin(115200);
delay(1000);
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//--- Read file
File inputs = SPIFFS.open("/inputs.txt");
if(!inputs){
Serial.println("Failed to open file for reading");
return;
}
Serial.println("File Content: ");
int i = 0;
while(inputs.available()){
String line= inputs.readStringUntil('\n');
v[i] = line;
i++;
String string2 = String("Linia ") + i + ": " + line;
Serial.println(string2);
}
inputs.close();
for(i = 0; i<2; i++) {
Serial.println(v[i]);
}
WiFi.begin(v[0].c_str(),v[1].c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println((String)"Connected to " + v[0] + " with IP addres: " + WiFi.localIP().toString());
//--- Check if files exist inside the Flash Memory
Serial.println(SPIFFS.exists("/inputs.txt"));
Serial.println(SPIFFS.exists("/back-image.jpg"));
Serial.println(SPIFFS.exists("/index.html"));
Serial.println(SPIFFS.exists("/index2.html"));
Serial.println(SPIFFS.exists("/login.css"));
Serial.println(SPIFFS.exists("/logo.png"));
server.on("/url", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index2.html", "text/html");
});
server.on("/login", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", "text/html");
});
server.on("/login.css", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/login.css", "text/css");
});
server.on("/back-image.jpg", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/back-image.jpg", "image/jpeg");
});
server.on("/logo.png", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/logo.png", "image/png");
});
server.on("/login", HTTP_POST, [](AsyncWebServerRequest *request) {
if (!request->hasParam("networkName", true) || !request->hasParam("networkPassword", true)) {
request->send(400, "Missing fields");
return;
}
const char *networkName = request->getParam("networkName")->value().c_str();
const char *networkPassword = request->getParam("networkPassword")->value().c_str();
// Do stuff with networkName and networkPassword
ssid = networkName;
password = networkPassword;
Serial.println(ssid);
Serial.println(password);
// Redirect to new page
request->redirect("/url");
});
server.begin();
Serial.println(ssid);
Serial.println(password);
}
void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment