#include "web_services.h" #include "RTClib.h" // get hours and minutes #define gethrs(mts) ((mts) / 60) #define getmts(mts) ((mts) % 60) #define DEBUG 1 const char *ssid = "PM-Lüftersteuerung"; //const char *password = "220805"; const byte DNS_PORT = 53; extern uint8_t state; extern String config_file; AsyncWebServer server(80); extern uint16_t start_time[7]; extern uint16_t stop_time[7]; extern uint8_t status[7]; extern RTC_DS3231 rtc; String file_data; uint8_t reponded = 0; const char header_html[] PROGMEM = R"rawliteral( )rawliteral"; const char rem_body[] PROGMEM = R"rawliteral(


)rawliteral"; // enum STATES {STARTUP, IDLE, BTN_PRESSED, AP_ACTIVE, AP_INACTIVE}; // uint32_t uploadsize,uploadtime,start; String get_time_str(uint16_t minu) { String time_str = ""; if (geth(minu) < 10) time_str = "0"; time_str += String(gethrs(minu)) + ":"; if (getmts(minu) < 10) time_str += "0"; time_str += String(getmts(minu)); return time_str; } String get_html_table() { String table; table = "

Lüfter Steuerung

\n"; table += "\n"; table += "\n"; table += "\n"; table += "\n"; table += "\n"; table += "\n"; table += "\n"; table += "
TagStart ZeitStopp ZeitAktiv
Montag
Dienstag
Mittwoch
Donnerstag
Freitag
Samstag
Sonntag
"; return table; } String get_jsdate_fn() { String fnc; uint8_t h, m, d,mo; uint16_t y; DateTime now; now = rtc.now(); h = now.hour(); m = now.minute(); d = now.day(); mo = now.month(); y = now.year(); int th = h%12; String hStr,mStr,dStr,moStr,yStr; //th = h %12; th = h %24; if (th==0) { // hStr = "12"; hStr = "24"; } else if(th<10) hStr = "0"+String(th); else hStr = String(th); mStr=m<10?"0"+String(m):String(m); dStr=d<10?"0"+String(d):String(d); moStr=mo<10?"0"+String(mo):String(mo); yStr = String(y); yStr = String(y); //String dateStr = moStr+"/"+dStr+"/"+yStr+"
"+hStr+":"+mStr; String dateStr = dStr+"."+moStr+"."+yStr+"
"+hStr+":"+mStr; //if(h<12) dateStr += " AM"; //else dateStr += " PM"; dateStr += " Uhr"; fnc = "

"+dateStr+"

\n"; return fnc; } void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { if (!index) { #ifdef DEBUG Serial.printf("UploadStart: %s\n", filename.c_str()); #endif } file_data = ""; for (size_t i = 0; i < len; i++) { #ifdef DEBUG Serial.write(data[i]); #endif file_data += String((char)data[i]); if (data[i - 1] == ']' && data[i] == '}') break; } if (final) { #ifdef DEBUG Serial.printf("UploadEnd: %s, %u B\n", filename.c_str(), index + len); #endif Serial.print(file_data); save_config_file(DEFAULT_CONFIG_FILE, file_data); int r = load_config(start_time, stop_time, status, DEFAULT_CONFIG_FILE); state = STARTUP; request-> redirect("/uploadmsg"); } } void web_services_init() { #ifdef DEBUG Serial.print("Setting AP (Access Point)…"); #endif // Remove the password parameter, if you want the AP (Access Point) to be open WiFi.softAP(ssid); IPAddress IP = WiFi.softAPIP(); Serial.println(); Serial.print("AP IP address: "); Serial.println(IP); // // Print ESP8266 Local IP Address // Serial.println(WiFi.localIP()); // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ state = WAIT4RES; String head = String(header_html); String fn = get_jsdate_fn(); String table = get_html_table(); String rem = String(rem_body); String foot = String(footer_html); String resp = head + fn + table + rem+ foot; request->send_P(200, "text/html", (resp).c_str()); }); // downloadconfig server.on("/downloadconfig", HTTP_GET, [](AsyncWebServerRequest *request){ state = STARTUP; request->send(SPIFFS,DEFAULT_CONFIG_FILE, String(), true); }); // downloadconfig server.on("/uploadmsg", HTTP_GET, [](AsyncWebServerRequest *request){ state = STARTUP; delay(100); String head = String(header_html); String fn = get_jsdate_fn(); String upload_msg = "

Datei gespeichert!

\n"; String table = get_html_table(); String rem = String(rem_body); String foot = String(footer_html); String resp = head + fn + upload_msg +table + rem+ foot; request->send_P(200, "text/html", (resp).c_str()); }); // load selected file for pump timing server.on("/updatertc", HTTP_GET, [](AsyncWebServerRequest *request){ state = STARTUP; uint16_t yr; uint8_t mont, dy, hrs, minu, sec; String thisy = request->getParam("y")->value(); String thismo = request->getParam("mo")->value(); String thisd = request->getParam("d")->value(); String thish = request->getParam("h")->value(); String thism = request->getParam("m")->value(); String thiss = request->getParam("s")->value(); yr = thisy.toInt(); mont = thismo.toInt(); dy = thisd.toInt(); hrs = thish.toInt(); minu = thism.toInt(); sec = thiss.toInt(); delay(10); #ifdef DEBUG Serial.printf("%d\n",yr); #endif rtc.adjust(DateTime(yr, mont, dy, hrs, minu, sec)); int r = load_config(start_time, stop_time, status, DEFAULT_CONFIG_FILE); String head = String(header_html); String fn = get_jsdate_fn(); String table = get_html_table(); String rem = String(rem_body); String foot = String(footer_html); String resp = head + fn + table + rem+ foot; request->send_P(200, "text/html", (resp).c_str()); delay(10); }); // server.on("/handleupload", HTTP_POST, [](AsyncWebServerRequest * request) {}, server.on("/handleupload", HTTP_POST, [](AsyncWebServerRequest *request) { request->send(200); }, handleUpload); // save file server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ state = STARTUP; String myjson; if (request->url() == "/submit") { int i = 0; while (true) { myjson = myjson + String((char)data[i]); if (data[i - 1] == ']' && data[i] == '}') break; i++; } // Serial.println(myjson); #ifdef DEBUG // Serial.println(myjson); // Serial.println(fn); #endif save_config_file(DEFAULT_CONFIG_FILE, myjson); int r = load_config(start_time, stop_time, status, DEFAULT_CONFIG_FILE); String head = String(header_html); String table = get_html_table(); String rem = String(rem_body); String fn = get_jsdate_fn(); String foot = String(footer_html); String resp = head + table + rem + fn + foot; request->send_P(200, "text/html", (resp).c_str()); } }); // Set handler for '/handleupload' // Start server enable_ap(); } void disable_ap() { server.end(); } void enable_ap() { server.begin(); }