2020-04-10 14:44:18 +02:00
|
|
|
//
|
|
|
|
// Created by lukas on 10.04.20.
|
|
|
|
//
|
|
|
|
|
2020-06-20 12:12:27 +02:00
|
|
|
#include <EEPROM.h>
|
2020-04-10 14:44:18 +02:00
|
|
|
#include "WifiManager.h"
|
|
|
|
|
|
|
|
void WifiManager::init() {
|
2020-04-11 10:23:02 +02:00
|
|
|
Serial.print("Setting up Access Point");
|
2020-06-20 12:12:27 +02:00
|
|
|
// start softap
|
2020-08-02 11:31:36 +00:00
|
|
|
const bool result = WiFi.softAP("PumpenSteuerung-Heiligenbrunner", "1qayxsw2");
|
2020-06-20 12:12:27 +02:00
|
|
|
if (result == true) {
|
2020-04-11 10:23:02 +02:00
|
|
|
Serial.println("Wifi Ready");
|
2020-08-01 18:00:36 +02:00
|
|
|
Serial.println(WiFi.softAPIP());
|
2020-06-20 12:12:27 +02:00
|
|
|
|
|
|
|
|
2020-08-01 18:00:36 +02:00
|
|
|
server.on("/", HTTP_GET, [this]() {
|
2020-06-20 12:12:27 +02:00
|
|
|
handleRoot();
|
|
|
|
});
|
|
|
|
|
2020-08-01 18:00:36 +02:00
|
|
|
server.on("/get", [this]() {
|
2020-06-20 12:12:27 +02:00
|
|
|
handleGet();
|
|
|
|
});
|
2020-08-01 18:00:36 +02:00
|
|
|
server.onNotFound([this]() {
|
2020-06-20 12:12:27 +02:00
|
|
|
handleNotFound();
|
|
|
|
});
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
// server available at 192.168.4.1
|
2020-06-20 12:12:27 +02:00
|
|
|
server.begin();
|
|
|
|
} else {
|
2020-04-11 10:23:02 +02:00
|
|
|
Serial.println("Wifi Setup failed!");
|
2020-04-10 14:44:18 +02:00
|
|
|
}
|
2020-08-22 19:13:50 +02:00
|
|
|
|
|
|
|
// init temperature sensor
|
|
|
|
temp.init();
|
2020-04-10 14:44:18 +02:00
|
|
|
}
|
2020-06-20 12:12:27 +02:00
|
|
|
|
2020-08-01 18:00:36 +02:00
|
|
|
WifiManager::WifiManager() : server(80), lastPumpTime(0), lastWaterOutage(0), lastPumpDuration(0) {}
|
2020-06-20 12:12:27 +02:00
|
|
|
|
|
|
|
void WifiManager::handleRoot() {
|
|
|
|
Serial.println("HomePage called");
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
// read maxpumptime from eeprom
|
2020-06-20 12:12:27 +02:00
|
|
|
int value = 0;
|
|
|
|
EEPROM.begin(4096); // init the eeprom
|
|
|
|
EEPROM.get(0, value);
|
|
|
|
EEPROM.end(); // stop the eeprom communication
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
const bool pumptimeset = this->lastPumpTime != 0;
|
|
|
|
const bool wateroutageset = this->lastWaterOutage != 0;
|
|
|
|
const bool pumpdurationset = this->lastPumpDuration != 0;
|
2020-08-01 18:00:36 +02:00
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
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
|
2020-08-01 18:00:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
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"
|
2020-08-02 11:31:36 +00:00
|
|
|
" <div>Aktuelle Max-Pumpzeit: " + String(value / 60) +
|
2020-08-01 18:00:36 +02:00
|
|
|
" 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";
|
2020-08-02 11:31:36 +00:00
|
|
|
|
|
|
|
// append last pump pump cycle
|
2020-08-01 18:00:36 +02:00
|
|
|
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>";
|
|
|
|
}
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
// append last water outage info
|
2020-08-01 18:00:36 +02:00
|
|
|
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>";
|
|
|
|
}
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
// append pump run duration
|
2020-08-01 18:00:36 +02:00
|
|
|
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>";
|
|
|
|
}
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
// append chip alive time
|
|
|
|
const unsigned long currtime = millis() / 1000;
|
2020-08-01 18:00:36 +02:00
|
|
|
index_html += "<div>Chip laeuft seit: " +
|
|
|
|
String(currtime / (60 * 60 * 24)) + "Tage " +
|
|
|
|
String((currtime % 86400) / (60 * 60)) + "Stunden " +
|
|
|
|
String((currtime % 3600) / 60) + "Minuten " +
|
2020-08-02 11:31:36 +00:00
|
|
|
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>";
|
|
|
|
|
2020-08-22 19:13:50 +02:00
|
|
|
index_html += "</br><div>Temperatur: " + String(this->temp.getTemp()) + "C</div>";
|
|
|
|
index_html += "<div>Relative Luftfeuchtigkeit: " + String(this->temp.getHum()) + "%</div>";
|
|
|
|
|
2020-08-02 11:31:36 +00:00
|
|
|
index_html += "</body></html>";
|
2020-06-20 12:12:27 +02:00
|
|
|
|
|
|
|
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");
|
2020-08-02 11:31:36 +00:00
|
|
|
const String s = "<!DOCTYPE HTML><html><head>\n"
|
2020-06-20 12:12:27 +02:00
|
|
|
" <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);
|
2020-06-20 12:12:27 +02:00
|
|
|
//manager.setSSID(message);
|
2020-06-20 15:10:35 +02:00
|
|
|
if (time == 0) {
|
|
|
|
time = 7200;
|
|
|
|
}
|
2020-06-20 12:12:27 +02:00
|
|
|
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
|
|
|
|
2020-06-20 12:12:27 +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;
|
|
|
|
}
|
2020-08-01 18:00:36 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|