PumpenSteuerung/src/WifiManager.cpp

167 lines
5.8 KiB
C++
Raw Normal View History

2020-04-10 14:44:18 +02:00
//
// Created by lukas on 10.04.20.
//
#include <EEPROM.h>
2020-04-10 14:44:18 +02:00
#include "WifiManager.h"
void WifiManager::init() {
Serial.print("Setting up Access Point");
// start softap
const bool 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 available at 192.168.4.1
server.begin();
} else {
Serial.println("Wifi Setup failed!");
2020-04-10 14:44:18 +02:00
}
}
WifiManager::WifiManager() : server(80), lastPumpTime(0), lastWaterOutage(0), lastPumpDuration(0) {}
void WifiManager::handleRoot() {
Serial.println("HomePage called");
// read maxpumptime from eeprom
int value = 0;
EEPROM.begin(4096); // init the eeprom
EEPROM.get(0, value);
EEPROM.end(); // stop the eeprom communication
const bool pumptimeset = this->lastPumpTime != 0;
const bool wateroutageset = this->lastWaterOutage != 0;
const bool pumpdurationset = this->lastPumpDuration != 0;
const unsigned long pumptime = pumptimeset ? (millis() - this->lastPumpTime) / 1000 : 0; // in sec
const unsigned long wateroutagetime = wateroutageset ? (millis() - this->lastWaterOutage) / 1000 : 0; // in sec
const unsigned long pumpduration = pumpdurationset ? (this->lastPumpDuration) / 1000 : 0; // in sec
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 Max-Pumpzeit: " + 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";
// append last pump pump cycle
if (pumptimeset) {
index_html += "<div>Zeit seit letztem einschalten: " +
String(pumptime / (60 * 60)) + "Stunden " +
String((pumptime % 3600) / 60) + "Minuten " +
String((pumptime % 60)) + "Sekunden</div>";
} else {
index_html += "<div>Zeit seit letztem einschalten: -</div>";
}
// append last water outage info
if (wateroutageset) {
index_html += "<div>Zeit seit letzem Wasserausfall: " +
String(wateroutagetime / (60 * 60 * 24)) + "Tage " +
String((wateroutagetime % 86400) / (60 * 60)) + "Stunden " +
String((wateroutagetime % 3600) / 60) + "Minuten </div>";
} else {
index_html += "<div>Zeit seit letzem Wasserausfall: -</div>";
}
// append pump run duration
if (pumpdurationset) {
index_html += "<div>Pumpe lief zuletzt: " +
String(pumpduration / 60) + "Minuten " +
String((pumpduration % 60)) + "Sekunden</div>";
} else {
index_html += "<div>Pumpe lief zuletzt: -</div>";
}
// append chip alive time
const unsigned long currtime = millis() / 1000;
index_html += "<div>Chip laeuft seit: " +
String(currtime / (60 * 60 * 24)) + "Tage " +
String((currtime % 86400) / (60 * 60)) + "Stunden " +
String((currtime % 3600) / 60) + "Minuten " +
String((currtime % 60)) + "Sekunden </div></br>";
// read live sensor values
index_html += "<div>Drucksensor: " + String((digitalRead(DruckSensorPin) ? "EIN" : "AUS")) + "</div>";
index_html += "<div>Wassersensor: " + String((digitalRead(WasserSensorPin) ? "EIN" : "AUS")) + "</div>";
index_html += "</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");
const 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") != "") {
2020-06-20 15:10:35 +02:00
// should be hours
int time = (int) (server.arg("time").toFloat() * 3600.0 + 0.5);
//manager.setSSID(message);
2020-06-20 15:10:35 +02:00
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
}
2020-06-20 15:10:35 +02:00
server.send(200, "text/html", s); //Send web page
}
void WifiManager::holdAlive() {
server.handleClient();
}
2020-06-20 15:10:35 +02:00
int WifiManager::getWaitTime() {
int value = 0;
EEPROM.begin(4096); // init the eeprom
EEPROM.get(0, value);
EEPROM.end(); // stop the eeprom communication
return value;
}
void WifiManager::setlastPumpTime(unsigned long lastPumpTime) {
this->lastPumpTime = lastPumpTime;
}
void WifiManager::setlastWaterOutage(unsigned long lastWaterOutage) {
this->lastWaterOutage = lastWaterOutage;
}
void WifiManager::setPumpDuration(unsigned long lastPumpDuration) {
this->lastPumpDuration = lastPumpDuration;
}