Unendlichkeitsmaschine/vscode/Unendlichkeitsmaschine/src/capportal.cpp

85 lines
2.7 KiB
C++

#include "capportal.hpp"
#include <TimeLib.h>
WebServer CapServer;
String html_content = "";
static void rootPage() {
CapServer.send(302, "text/html", html_content);
}
void Cap::begin() {
Portal = new AutoConnect(CapServer);
Config = new AutoConnectConfig();
Config->autoReset = false; // Not reset the module even by intentional disconnection using AutoConnect menu.
Config->portalTimeout = 1000; // Do not block the sketch and exit after 1000ms
//Config->immediateStart = true;
Config->autoReconnect = true; // Reconnect to known access points.
Config->reconnectInterval = 6; // Reconnection attempting interval is 3[min].
Config->retainPortal = true; // Keep the captive portal open.
Config->apid = "Unendlichkeit_AP";
Config->menuItems = Config->menuItems | AC_MENUITEM_UPDATE;
Config->hostName = "Unendlichkeit";
Config->ota = AC_OTA_BUILTIN;
Portal->config(*Config);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
//content = "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"0; url='/_ac'\" /></head><body></body></html>";
CapServer.on("/", rootPage);
if (Portal->begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString());
}
else{
Serial.println("No configured Wifi connection. Connect to '" + Config->apid + "' and configure network connections.");
}
}
String Cap::localIP()
{
return WiFi.localIP().toString();
}
void Cap::handle(){
Portal->handleClient();
}
// Function that gets current epoch time
unsigned long Cap::epochTime() {
time_t now;
struct tm timeinfo;
if ( !getLocalTime(&timeinfo)) {
//Serial.println("Failed to obtain time");
return(0);
}
time(&now);
//Serial.printf("Epochtime: %lu\n", now);
return now;
}
String Cap::localTime(){
String dateTime = "";
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
//Serial.println("Failed to obtain time");
return(dateTime);
}
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
dateTime = String(timeinfo.tm_year + 1900) + "-" + String(timeinfo.tm_mon + 1) + "-" + String(timeinfo.tm_mday) + "+" + String(timeinfo.tm_hour) + ":" + String(timeinfo.tm_min) + ":" + String(timeinfo.tm_sec);
//Serial.printf("%s\n", dateTime.c_str());
return(dateTime);
}
String Cap::localTime(time_t epochtime){
String dateTime = "";
dateTime = String(year(epochtime)) + "-" + String(month(epochtime)) + "-" + String(day(epochtime)) + "+" + String(hour(epochtime)) + ":" + String(minute(epochtime)) + ":" + String(second(epochtime));
Serial.printf("%s\n", dateTime.c_str());
return(dateTime);
}