PumpenSteuerung/src/WifiManager.cpp
2020-06-20 15:10:35 +02:00

101 lines
3.1 KiB
C++

//
// Created by lukas on 10.04.20.
//
#include <EEPROM.h>
#include "WifiManager.h"
void WifiManager::init() {
Serial.print("Setting up Access Point");
// start softap
boolean result = WiFi.softAP("PumpenSteuerung-Heiligenbrunner", "1qayxsw2");
if (result == true) {
Serial.println("Wifi Ready");
Serial.println( WiFi.softAPIP());
server.on("/", HTTP_GET, [this](){
handleRoot();
});
server.on("/get", [this](){
handleGet();
});
server.onNotFound([this](){
handleNotFound();
});
server.begin();
} else {
Serial.println("Wifi Setup failed!");
}
}
WifiManager::WifiManager() : server(80) {}
void WifiManager::handleRoot() {
Serial.println("HomePage called");
int value = 0;
EEPROM.begin(4096); // init the eeprom
EEPROM.get(0, value);
EEPROM.end(); // stop the eeprom communication
const String index_html = "<!DOCTYPE HTML><html><head>\n"
" <title>Wastinfoboard-ConfigurationPage</title>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
" </head><body>\n"
" <div>Aktuelle Zeit: "+ String(value / 60) +" Minuten</div>\n"
" <form action=\"/get\">\n"
" Zeit setzen: <input placeholder=\"Abschaltzeit in h\" type=\"text\" name=\"time\">\n"
" <input type=\"submit\" value=\"Submit\">\n"
" </form><br>\n"
"</body></html>";
server.send(200, "text/html", index_html);
}
void WifiManager::handleNotFound() {
server.send(404, "text/plain",
"404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
void WifiManager::handleGet() {
Serial.println("get called");
String s = "<!DOCTYPE HTML><html><head>\n"
" <title>Wastinfoboard-ConfigurationPage</title>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
" <meta http-equiv=\"refresh\" content=\"0; URL=/\">\n"
" </head><body>\n"
"</body></html>";
Serial.println(server.arg("time"));
if (server.arg("time") != "") {
// should be hours
int time = (int) (server.arg("time").toFloat() * 3600.0 + 0.5);
//manager.setSSID(message);
if (time == 0) {
time = 7200;
}
EEPROM.begin(4096); // init the eeprom
EEPROM.put(0, time);
EEPROM.commit(); // write the changes to the eeprom
EEPROM.end(); // stop the eeprom communication
}
server.send(200, "text/html", s); //Send web page
}
void WifiManager::holdAlive() {
server.handleClient();
}
int WifiManager::getWaitTime() {
int value = 0;
EEPROM.begin(4096); // init the eeprom
EEPROM.get(0, value);
EEPROM.end(); // stop the eeprom communication
return value;
}