// // Created by lukas on 10.04.20. // #include #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!"); } // init temperature sensor temp.init(); } 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 = "\n" " Wastinfoboard-ConfigurationPage\n" " \n" " \n" "
Aktuelle Max-Pumpzeit: " + String(value / 60) + " Minuten
\n" "
\n" " Zeit setzen: \n" " \n" "

\n"; // append last pump pump cycle if (pumptimeset) { index_html += "
Zeit seit letztem einschalten: " + String(pumptime / (60 * 60)) + "Stunden " + String((pumptime % 3600) / 60) + "Minuten " + String((pumptime % 60)) + "Sekunden
"; } else { index_html += "
Zeit seit letztem einschalten: -
"; } // append last water outage info if (wateroutageset) { index_html += "
Zeit seit letzem Wasserausfall: " + String(wateroutagetime / (60 * 60 * 24)) + "Tage " + String((wateroutagetime % 86400) / (60 * 60)) + "Stunden " + String((wateroutagetime % 3600) / 60) + "Minuten
"; } else { index_html += "
Zeit seit letzem Wasserausfall: -
"; } // append pump run duration if (pumpdurationset) { index_html += "
Pumpe lief zuletzt: " + String(pumpduration / 60) + "Minuten " + String((pumpduration % 60)) + "Sekunden
"; } else { index_html += "
Pumpe lief zuletzt: -
"; } // append chip alive time const unsigned long currtime = millis() / 1000; index_html += "
Chip laeuft seit: " + String(currtime / (60 * 60 * 24)) + "Tage " + String((currtime % 86400) / (60 * 60)) + "Stunden " + String((currtime % 3600) / 60) + "Minuten " + String((currtime % 60)) + "Sekunden

"; // read live sensor values index_html += "
Drucksensor: " + String((digitalRead(DruckSensorPin) ? "EIN" : "AUS")) + "
"; index_html += "
Wassersensor: " + String((digitalRead(WasserSensorPin) ? "EIN" : "AUS")) + "
"; index_html += "
Temperatur: " + String(this->temp.getTemp()) + "C
"; index_html += "
Relative Luftfeuchtigkeit: " + String(this->temp.getHum()) + "%
"; index_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 = "\n" " Wastinfoboard-ConfigurationPage\n" " \n" " \n" " \n" ""; 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; } 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; }