CPP 200
Garagedoor_final By pgeher on 19th December 2021 01:06:22 PM
  1. /*
  2.    Copyright (c) 2021 Peter Geher.  All rights reserved.
  3.    See the bottom of this file for the license terms.
  4.    peter.geher@nsa2go.de
  5. */
  6.  
  7. // Version 2.3.final - 15.12.2021 / 06:00
  8. // All-in-One Version!
  9.  
  10. /*
  11.     Drin/Erledigt ist:
  12.     WWW-Server für Token ansehen/neu setzen
  13.     Token in spiffs speichern
  14.     Token aus spiffs lesen und verwenden
  15.     Wifi-Connector
  16.     Wifi-Connector-Deleter per Pin D7 an Ground
  17.     NTP-Client
  18.     OTA für Entwicklung!
  19.     Delete aller Nutzer mit einen Klick. Erledigt,
  20.       Zwei Seiten zum zum Löschen reichen.
  21.     User-Limit auf auf userN1-5.txt aus spiffs
  22.     Code aufraeumen und etwas Fehlerbereinigung...
  23.     Status von Relais2 anzeigen und warnen,
  24.       falls es nochmals ein/ausgeschaltet werden soll
  25.  
  26.     Fallback: Da gerade von allen Seiten gegen Telegram geschossen
  27.     wird, habe ich eine Manuelle Tasterung eingebaut. Laesst sich
  28.     auch per curl o.Ae. triggern.
  29.  
  30.     Weiterhin fehlen immer noch Party-Alpakas...
  31.     Und Bier... Ja, das ist nun vorhanden!!
  32. */
  33.  
  34. #include <ESP8266WiFi.h>
  35. #include <ESP8266WebServer.h>
  36. #include <ESP8266mDNS.h>
  37. #include <ESP8266HTTPUpdateServer.h>
  38. #include <WiFiClientSecure.h>
  39. #include <DNSServer.h>
  40. #include <WiFiClient.h>
  41. #include <WiFiManager.h>
  42. #include <UniversalTelegramBot.h>
  43. #include <time.h>
  44. #include <LittleFS.h>
  45. #include <Effortless_SPIFFS.h>
  46. #include <WiFiUdp.h>
  47. #include <ArduinoOTA.h>
  48. #include "uptime_formatter.h"
  49. #include "uptime.h"
  50.  
  51. #define BOT_TOKEN ""
  52. const unsigned long BOT_MTBS = 500;
  53.  
  54. X509List cert(TELEGRAM_CERTIFICATE_ROOT);
  55. WiFiClientSecure secured_client;
  56. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  57. unsigned long bot_lasttime;
  58.  
  59. FS& gfs = LittleFS;
  60. eSPIFFS fileSystem;
  61.  
  62. const int lichtPin = D2;
  63. const int torPin = D5;
  64. int tastdauer = 150; // in Millisekunden
  65. int rtaster = D7; // Taster fuer Wifi-Reset!
  66. int tasterstatus = 0;
  67.  
  68. void handleNewMessages(int numNewMessages) {
  69.   for (int i = 0; i < numNewMessages; i++)
  70.   {
  71.     String chat_id = bot.messages[i].chat_id;
  72.     String text = bot.messages[i].text;
  73.     String from_name = bot.messages[i].from_name;
  74.     String from_id = bot.messages[i].from_id;
  75.  
  76.     // ChatID Geraffel
  77.     String user1String;
  78.     String user2String;
  79.     String user3String;
  80.     String user4String;
  81.     String user5String;
  82.  
  83.     fileSystem.openFromFile("/user1.txt", user1String);
  84.     fileSystem.openFromFile("/user2.txt", user2String);
  85.     fileSystem.openFromFile("/user3.txt", user3String);
  86.     fileSystem.openFromFile("/user4.txt", user4String);
  87.     fileSystem.openFromFile("/user5.txt", user5String);
  88.  
  89.     if (chat_id != user1String) {
  90.       if (chat_id != user2String) {
  91.         if (chat_id != user3String) {
  92.           if (chat_id != user4String) {
  93.             if (chat_id != user5String) {
  94.               bot.sendMessage(chat_id, "Du nicht!", "");
  95.               continue;
  96.             }
  97.           }
  98.         }
  99.       }
  100.     }
  101.  
  102.     // ChatID Geraffel
  103.  
  104.     if (text == "/tor")
  105.     {
  106.       int tord1String;
  107.       fileSystem.openFromFile("/tord.txt", tord1String);
  108.       digitalWrite(torPin, LOW); // Relais 1
  109.       bot.sendMessage(chat_id, " Tor wird bewegt ", "");
  110.       delay(tord1String);
  111.       digitalWrite(torPin, HIGH);
  112.     }
  113.  
  114.     if (text == "tor") // Relais 1
  115.     {
  116.       int tord1String;
  117.       fileSystem.openFromFile("/tord.txt", tord1String);
  118.       digitalWrite(torPin, LOW); // Relais 1
  119.       bot.sendMessage(chat_id, " Tor wird bewegt ", "");
  120.       delay(tord1String);
  121.       digitalWrite(torPin, HIGH);
  122.     }
  123.  
  124.     if (text == "/an") // Relais 2
  125.     {
  126.       if (digitalRead(lichtPin)) {
  127.         String welcome = "Relais2 wird eingeschaltet\n";
  128.         digitalWrite(lichtPin, LOW);
  129.         bot.sendMessage(chat_id, welcome, "Markdown");
  130.       } else {
  131.         String welcome = "Relais2 ist schon eingeschaltet!\n";
  132.         bot.sendMessage(chat_id, welcome, "Markdown");
  133.       }
  134.     }
  135.  
  136.     if (text == "an") // Relais 2
  137.     {
  138.       if (digitalRead(lichtPin)) {
  139.         String welcome = "Relais2 wird eingeschaltet\n";
  140.         digitalWrite(lichtPin, LOW);
  141.         bot.sendMessage(chat_id, welcome, "Markdown");
  142.       } else {
  143.         String welcome = "Relais2 ist schon eingeschaltet!\n";
  144.         bot.sendMessage(chat_id, welcome, "Markdown");
  145.       }
  146.     }
  147.  
  148.     if (text == "aus") // Relais 2
  149.     {
  150.       if (digitalRead(lichtPin)) {
  151.         String welcome = "Relais2 ist schon ausgeschaltet!\n";
  152.         bot.sendMessage(chat_id, welcome, "Markdown");
  153.       } else {
  154.         String welcome = "Relais2 wird ausgeschaltet\n";
  155.         digitalWrite(lichtPin, HIGH);
  156.         bot.sendMessage(chat_id, welcome, "Markdown");
  157.       }
  158.     }
  159.  
  160.  
  161.     if (text == "/aus") // Relais 2
  162.     {
  163.       if (digitalRead(lichtPin)) {
  164.         String welcome = "Relais2 ist schon ausgeschaltet!\n";
  165.         bot.sendMessage(chat_id, welcome, "Markdown");
  166.       } else {
  167.         String welcome = "Relais2 wird ausgeschaltet\n";
  168.         digitalWrite(lichtPin, HIGH);
  169.         bot.sendMessage(chat_id, welcome, "Markdown");
  170.       }
  171.     }
  172.  
  173.     if (text == "/start")
  174.     {
  175.       int tord1String;
  176.       fileSystem.openFromFile("/tord.txt", tord1String);
  177.  
  178.       String welcome = "Willkommen beim Garagentor, " + from_name + ".\n\n";
  179.       welcome += "/tor    Tastert den Tor-Taster\n";
  180.       welcome += "/an     Schaltet Relais2 an \n";
  181.       welcome += "/aus    Schaltet Relais2 aus \n";
  182.       welcome += "/users  Zeigt aktuell gespeicherte Benutzer an\n";
  183.       welcome += "/ip     zeigt die IP-Adresse vom ESP an\n";
  184.       welcome += "/status     zeigt den Status von Relais2 an\n";
  185.       welcome += "/uptime   Zeigt die Laufzeit vom ESP an\n";
  186.       welcome += " \n";
  187.       welcome += "'tor' 'an' 'aus' und 'status' sind auch ohne '/' möglich. \n";
  188.       bot.sendMessage(chat_id, welcome, "Markdown");
  189.     }
  190.  
  191.     if (text == "/hilfe")
  192.     {
  193.       int tord1String;
  194.       fileSystem.openFromFile("/tord.txt", tord1String);
  195.  
  196.       String welcome = "Willkommen beim Garagentor, " + from_name + ".\n\n";
  197.       welcome += "/tor    Tastert den Tor-Taster\n";
  198.       welcome += "/an     Schaltet Relais2 an \n";
  199.       welcome += "/aus    Schaltet Relais2 aus \n";
  200.       welcome += "/users  Zeigt aktuell gespeicherte Benutzer an\n";
  201.       welcome += "/ip     zeigt die IP-Adresse vom ESP an\n";
  202.       welcome += "/status     zeigt den Status von Relais2 an\n";
  203.       welcome += "/uptime   Zeigt die Laufzeit vom ESP an\n";
  204.       welcome += " \n";
  205.       welcome += "'tor' 'an' und 'aus' sind auch ohne '/' möglich. \n";
  206.       bot.sendMessage(chat_id, welcome, "Markdown");
  207.     }
  208.  
  209.     if (text == "/users")
  210.     {
  211.       String user1String;
  212.       String user2String;
  213.       String user3String;
  214.       String user4String;
  215.       String user5String;
  216.       fileSystem.openFromFile("/user1.txt", user1String);
  217.       fileSystem.openFromFile("/user2.txt", user2String);
  218.       fileSystem.openFromFile("/user3.txt", user3String);
  219.       fileSystem.openFromFile("/user4.txt", user4String);
  220.       fileSystem.openFromFile("/user5.txt", user5String);
  221.       String welcome = "Aktuell registrierte UserIDs:\n";
  222.       welcome += user1String + "\n";
  223.       welcome += user2String + "\n";
  224.       welcome += user3String + "\n";
  225.       welcome += user4String + "\n";
  226.       welcome += user5String + "\n";
  227.       bot.sendMessage(chat_id, welcome, "Markdown");
  228.     }
  229.  
  230.     if (text == "/ip")
  231.     {
  232.       String IPaddress = WiFi.localIP().toString();
  233.       String welcome = "Das Tor hat die IP:\n";
  234.       welcome += IPaddress + "\n";
  235.       bot.sendMessage(chat_id, welcome, "Markdown");
  236.     }
  237.  
  238.     if (text == "status")
  239.     {
  240.       String welcome = "Relais2 ist ";
  241.       if (digitalRead(lichtPin)) {
  242.         welcome += "aus\n";
  243.       } else {
  244.         welcome += "an\n";
  245.       }
  246.       bot.sendMessage(chat_id, welcome, "Markdown");
  247.       Serial.println("status");
  248.     }
  249.  
  250.     if (text == "/status")
  251.     {
  252.       String welcome = "Relais2 ist ";
  253.       if (digitalRead(lichtPin)) {
  254.         welcome += "aus\n";
  255.       } else {
  256.         welcome += "an\n";
  257.       }
  258.       bot.sendMessage(chat_id, welcome, "Markdown");
  259.     }
  260.  
  261.     if (text == "/uptime")
  262.     {
  263.       uptime::calculateUptime();
  264.       String welcome = "Laufzeit:\n";
  265.       welcome += uptime_formatter::getUptime() + "\n";
  266.       bot.sendMessage(chat_id, welcome, "Markdown");
  267.     }
  268.  
  269.     if (text == "/about")
  270.     {
  271.       String welcome = "Garage-Controller - ";
  272.       welcome += "This little code controls your garage door using ";
  273.       welcome += "one (or two) simple relays and a Wemos D1-Mini. ";
  274.       welcome += "\n\n";
  275.       welcome += "It is forbidden to use this code ";
  276.       welcome += "commercially/profitably or for police/military purposes! ";
  277.       welcome += "\n\n ";
  278.       welcome += "2022 by Peter Geher\n";
  279.       welcome += "peter.geher@nsa2go.de\n";
  280.       welcome += "http://www.layers-gedanken.de\n";
  281.       welcome += " \n";
  282.       welcome += "Have a lot of Fun! \n";
  283.       bot.sendMessage(chat_id, welcome, "Markdown");
  284.     }
  285.   }
  286. }
  287.  
  288. unsigned long previousMillis = 0;
  289. const long blinkinterval = 125;
  290.  
  291. ESP8266WebServer httpServer(80);
  292. ESP8266HTTPUpdateServer httpUpdater;
  293.  
  294. String Argument_Name, Clients_Response1, Clients_Response2;
  295. const char* host = "garagen-esp";
  296. const char* update_path = "/firmware";
  297. const char* update_username = "admin";
  298. const char* update_password = "admin";
  299.  
  300. void WifiVerbinder() {
  301.   WiFi.mode(WIFI_STA);
  302.   Serial.begin(115200);
  303.   WiFiManager wm;
  304.   bool res;
  305.   res = wm.autoConnect("Garage");
  306.  
  307.   if (!res) {
  308.     Serial.println("Failed to connect");
  309.   }
  310.   else {
  311.     Serial.println("Verbunden...yay :)");
  312.   }
  313. }
  314.  
  315. void WifiDeleter() {
  316.  
  317.   // Wird nicht wirklich gebraucht, daher ist es auch im Loop auskommentiert!
  318.  
  319.   unsigned long currentMillis = millis();
  320.   tasterstatus = digitalRead(rtaster);
  321.   if (currentMillis - previousMillis >= blinkinterval) {
  322.     previousMillis = currentMillis;
  323.     if (tasterstatus == HIGH) {
  324.     } else {
  325.       Serial.println("Flash-Reset getastet!!!");
  326.       ESP.eraseConfig();
  327.       delay(100);
  328.       ESP.reset();
  329.     }
  330.   }
  331. }
  332.  
  333. void OTA() {
  334.   ArduinoOTA.setHostname("garagen8266");
  335.   ArduinoOTA.onStart([]() {
  336.     String type;
  337.     type = "sketch";
  338.     Serial.println("Start updating " + type);
  339.   });
  340.  
  341.   ArduinoOTA.onEnd([]() {
  342.     Serial.println("\nEnd");
  343.   });
  344.  
  345.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  346.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  347.   });
  348.  
  349.   ArduinoOTA.onError([](ota_error_t error) {
  350.     Serial.printf("Error[%u]: ", error);
  351.     if (error == OTA_AUTH_ERROR) {
  352.       Serial.println("Auth Failed");
  353.     } else if (error == OTA_BEGIN_ERROR) {
  354.       Serial.println("Begin Failed");
  355.     } else if (error == OTA_CONNECT_ERROR) {
  356.       Serial.println("Connect Failed");
  357.     } else if (error == OTA_RECEIVE_ERROR) {
  358.       Serial.println("Receive Failed");
  359.     } else if (error == OTA_END_ERROR) {
  360.       Serial.println("End Failed");
  361.     }
  362.   });
  363.  
  364.   ArduinoOTA.begin();
  365. }
  366.  
  367. void ntp() {
  368.   // NTP-Zeiten beziehen für Telegram-Cert...
  369.   configTime(0, 0, "194.25.134.196");   // T-Online-Zeitserver
  370.   time_t now = time(nullptr);
  371.   while (now < 24 * 3600)
  372.   {
  373.  
  374.     delay(100);
  375.     now = time(nullptr);
  376.   }
  377. }
  378.  
  379. void webupdater() {
  380.   httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  381.   MDNS.addService("http", "tcp", 80);
  382.   Serial.printf("HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username '%s' and password '%s'\n", host, update_path, update_username, update_password);
  383. }
  384.  
  385. void tokenGetter() {
  386.   const char* blah;
  387.   fileSystem.openFromFile("/token.txt", blah);
  388.   void updateToken(const char* blah);
  389.   bot.updateToken(blah);
  390. }
  391.  
  392. void Hauptseite() {
  393.   String user1String;
  394.   String user2String;
  395.   String user3String;
  396.   String user4String;
  397.   String user5String;
  398.   String tord1String;
  399.   fileSystem.openFromFile("/user1.txt", user1String);
  400.   fileSystem.openFromFile("/user2.txt", user2String);
  401.   fileSystem.openFromFile("/user3.txt", user3String);
  402.   fileSystem.openFromFile("/user4.txt", user4String);
  403.   fileSystem.openFromFile("/user5.txt", user5String);
  404.   fileSystem.openFromFile("/tord.txt", tord1String);
  405.  
  406.   String IPaddress = WiFi.localIP().toString();
  407.   String Hauptseite;
  408.   Hauptseite =  "<html>";
  409.   Hauptseite += "<head><title>GaragenESP </title>";
  410.   Hauptseite += "<center>";
  411.   Hauptseite += "<style>";
  412.   Hauptseite += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  413.   Hauptseite += "</style>";
  414.   Hauptseite += "</head>";
  415.   Hauptseite += "<body>";
  416.   Hauptseite += "<h1><br>Übersicht v2.3</h1>";
  417.   Hauptseite += "<a href=http://" + IPaddress + "/st" + ">Token anzeigen</a><br>";
  418.   Hauptseite += "<a href=http://" + IPaddress + "/SetToken" + ">Neues Token setzen</a><br><br>";
  419.   Hauptseite += "<a href=http://" + IPaddress + "/user1" + ">Benutzer 1 ändern</a> (" + user1String + ") <br>";
  420.   Hauptseite += "<a href=http://" + IPaddress + "/user2" + ">Benutzer 2 ändern</a> (" + user2String + ") <br>";
  421.   Hauptseite += "<a href=http://" + IPaddress + "/user3" + ">Benutzer 3 ändern</a> (" + user3String + ") <br>";
  422.   Hauptseite += "<a href=http://" + IPaddress + "/user4" + ">Benutzer 4 ändern</a> (" + user4String + ") <br>";
  423.   Hauptseite += "<a href=http://" + IPaddress + "/user5" + ">Benutzer 5 ändern</a> (" + user5String + ") <br><br>";
  424.   Hauptseite += "<a href=http://" + IPaddress + "/predelete" + ">Alle Benutzer löschen</a><br><br>";
  425.   Hauptseite += "<a href=http://" + IPaddress + "/tord" + ">Tastdauer ändern</a> (" + tord1String + "ms)<br>";
  426.   Hauptseite += "<br><br><a href=http://" + IPaddress + "/firmware" + ">Firmware Aktualisieren</a><br>(Passowrt wird benötigt!)<br><br>";
  427.   Hauptseite += "Uptime: <br>";
  428.   Hauptseite += "" + uptime_formatter::getUptime() +  "";
  429.   Hauptseite += "<br><br><br><a href=http://" + IPaddress + "/manuell" + ">Tor Tastern</a><br><br>";
  430.   Hauptseite += "</body>";
  431.   Hauptseite += "</center>";
  432.   Hauptseite += "</html>";
  433.   httpServer.send(200, "text/html", Hauptseite);
  434. }
  435.  
  436. void SetToken() {
  437.   String IPaddress = WiFi.localIP().toString();
  438.   String SetTokenString;
  439.   fileSystem.openFromFile("/token.txt", SetTokenString);
  440.   String SetToken;
  441.   SetToken =  "<html>";
  442.   SetToken += "<head><title>Neues Token speichern... </title>";
  443.   SetToken += "<center>";
  444.   SetToken += "<style>";
  445.   SetToken += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  446.   SetToken += "</style>";
  447.   SetToken += "</head>";
  448.   SetToken += "<body>";
  449.   SetToken += "<h1><br>Token ( " + SetTokenString + " )</h1>";
  450.   SetToken += "Bitte Token 2x eingeben!";
  451.   SetToken += "<br><br><br>Beide Felder leer lassen zum löschen";
  452.   SetToken += "<form action='http://" + IPaddress + "/SetToken' method='POST'>";
  453.   SetToken += "ID: <input type='text' name='name_input' size=30><br>";
  454.   SetToken += "ID: <input type='text' name='name_input1' size=30><br><br>";
  455.   SetToken += "<input type='submit' value='Token Speichern'>";
  456.   SetToken += "</form>";
  457.   SetToken += "<a href=http://" + IPaddress + "/SetToken" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  458.   SetToken += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  459.   SetToken += "</body>";
  460.   SetToken += "</center>";
  461.   SetToken += "</html>";
  462.   httpServer.send(200, "text/html", SetToken);
  463.   if (httpServer.args() > 0 ) {
  464.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  465.       Argument_Name = httpServer.argName(i);
  466.       if (httpServer.argName(i) == "name_input") {
  467.         if (httpServer.arg(0) == httpServer.arg(1)) {
  468.           auto f = gfs.open("/token.txt", "w");
  469.           f.print(httpServer.arg(i));
  470.           f.close();
  471.           Serial.println("gleich");
  472.           delay(1000);
  473.           ESP.restart();
  474.         }
  475.         else
  476.         {
  477.           Serial.println("Ungleich");
  478.         }
  479.       }
  480.     }
  481.   }
  482. }
  483.  
  484. void user1() {
  485.   String IPaddress = WiFi.localIP().toString();
  486.   String user1String;
  487.   fileSystem.openFromFile("/user1.txt", user1String);
  488.   String user1;
  489.   user1 =  "<html>";
  490.   user1 += "<head><title>Benutzer #1 speichern... </title>";
  491.   user1 += "<center>";
  492.   user1 += "<style>";
  493.   user1 += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  494.   user1 += "</style>";
  495.   user1 += "</head>";
  496.   user1 += "<body>";
  497.   user1 += "<h1><br>Benutzer ID #1 ( " + user1String + " )</h1>";
  498.   user1 += "Bitte ID 2x eingeben!";
  499.   user1 += "<br><br><br>Beide Felder leer lassen zum löschen";
  500.   user1 += "<form action='http://" + IPaddress + "/user1' method='POST'>";
  501.   user1 += "ID: <input type='text' name='name_input' size=30><br>";
  502.   user1 += "ID: <input type='text' name='name_input1' size=30><br><br>";
  503.   user1 += "<input type='submit' value='Benutzer ID Speichern'>";
  504.   user1 += "</form>";
  505.   user1 += "<a href=http://" + IPaddress + "/user1" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  506.   user1 += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  507.   user1 += "</body>";
  508.   user1 += "</center>";
  509.   user1 += "</html>";
  510.   httpServer.send(200, "text/html", user1);
  511.   if (httpServer.args() > 0 ) {
  512.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  513.       Argument_Name = httpServer.argName(i);
  514.       if (httpServer.argName(i) == "name_input") {
  515.         if (httpServer.arg(0) == httpServer.arg(1)) {
  516.           auto f = gfs.open("/user1.txt", "w");
  517.           f.print(httpServer.arg(i));
  518.           f.close();
  519.           Serial.println("gleich");
  520.         }
  521.         else
  522.         {
  523.           Serial.println("Ungleich");
  524.         }
  525.       }
  526.     }
  527.   }
  528. }
  529.  
  530. void user2() {
  531.   String IPaddress = WiFi.localIP().toString();
  532.   String user2String;
  533.   fileSystem.openFromFile("/user2.txt", user2String);
  534.   String user2;
  535.   user2 =  "<html>";
  536.   user2 += "<head><title>Benutzer #2 speichern... </title>";
  537.   user2 += "<center>";
  538.   user2 += "<style>";
  539.   user2 += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  540.   user2 += "</style>";
  541.   user2 += "</head>";
  542.   user2 += "<body>";
  543.   user2 += "<h1><br>Benutzer ID #2 ( " + user2String + " )</h1>";
  544.   user2 += "Bitte ID 2x eingeben!";
  545.   user2 += "<br><br><br>Beide Felder leer lassen zum löschen";
  546.   user2 += "<form action='http://" + IPaddress + "/user2' method='POST'>";
  547.   user2 += "ID: <input type='text' name='name_input' size=30><br>";
  548.   user2 += "ID: <input type='text' name='name_input1' size=30><br><br>";
  549.   user2 += "<input type='submit' value='Benutzer ID Speichern'>";
  550.   user2 += "</form>";
  551.   user2 += "<a href=http://" + IPaddress + "/user2" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  552.   user2 += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  553.   user2 += "</body>";
  554.   user2 += "</center>";
  555.   user2 += "</html>";
  556.   httpServer.send(200, "text/html", user2);
  557.  
  558.   if (httpServer.args() > 0 ) {
  559.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  560.       Argument_Name = httpServer.argName(i);
  561.       if (httpServer.argName(i) == "name_input") {
  562.  
  563.         // Writer...
  564.         if (httpServer.arg(0) == httpServer.arg(1)) {
  565.           auto f = gfs.open("/user2.txt", "w");
  566.           f.print(httpServer.arg(i));
  567.           f.close();
  568.           Serial.println("gleich");
  569.         }
  570.         else
  571.         {
  572.           Serial.println("Ungleich");
  573.         }
  574.       }
  575.     }
  576.   }
  577. }
  578.  
  579. void user3() {
  580.   String IPaddress = WiFi.localIP().toString();
  581.   String user3String;
  582.   fileSystem.openFromFile("/user3.txt", user3String);
  583.   String user3;
  584.   user3 =  "<html>";
  585.   user3 += "<head><title>Benutzer #3 speichern... </title>";
  586.   user3 += "<center>";
  587.   user3 += "<style>";
  588.   user3 += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  589.   user3 += "</style>";
  590.   user3 += "</head>";
  591.   user3 += "<body>";
  592.   user3 += "<h1><br>Benutzer ID #3 ( " + user3String + " )</h1>";
  593.   user3 += "Bitte ID 2x eingeben!";
  594.   user3 += "<br><br><br>Beide Felder leer lassen zum löschen";
  595.   user3 += "<form action='http://" + IPaddress + "/user3' method='POST'>";
  596.   user3 += "ID: <input type='text' name='name_input' size=30><br>";
  597.   user3 += "ID: <input type='text' name='name_input1' size=30><br><br>";
  598.   user3 += "<input type='submit' value='Benutzer ID Speichern'>";
  599.   user3 += "</form>";
  600.   user3 += "<a href=http://" + IPaddress + "/user3" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  601.   user3 += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  602.   user3 += "</body>";
  603.   user3 += "</center>";
  604.   user3 += "</html>";
  605.   httpServer.send(200, "text/html", user3);
  606.   if (httpServer.args() > 0 ) {
  607.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  608.       Argument_Name = httpServer.argName(i);
  609.       if (httpServer.argName(i) == "name_input") {
  610.  
  611.         // Writer...
  612.         if (httpServer.arg(0) == httpServer.arg(1)) {
  613.           auto f = gfs.open("/user3.txt", "w");
  614.           f.print(httpServer.arg(i));
  615.           f.close();
  616.           Serial.println("gleich");
  617.         }
  618.         else
  619.         {
  620.           Serial.println("Ungleich");
  621.         }
  622.       }
  623.     }
  624.   }
  625. }
  626.  
  627. void user4() {
  628.   String IPaddress = WiFi.localIP().toString();
  629.   String user4String;
  630.   fileSystem.openFromFile("/user4.txt", user4String);
  631.  
  632.   String user4;
  633.   user4 =  "<html>";
  634.   user4 += "<head><title>Benutzer #4 speichern... </title>";
  635.   user4 += "<center>";
  636.   user4 += "<style>";
  637.   user4 += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  638.   user4 += "</style>";
  639.   user4 += "</head>";
  640.   user4 += "<body>";
  641.   user4 += "<h1><br>Benutzer ID #4 ( " + user4String + " )</h1>";
  642.   user4 += "Bitte ID 2x eingeben!";
  643.   user4 += "<br><br><br>Beide Felder leer lassen zum löschen";
  644.   user4 += "<form action='http://" + IPaddress + "/user4' method='POST'>";
  645.   user4 += "ID: <input type='text' name='name_input' size=30><br>";
  646.   user4 += "ID: <input type='text' name='name_input1' size=30><br><br>";
  647.   user4 += "<input type='submit' value='Benutzer ID Speichern'>";
  648.   user4 += "</form>";
  649.   user4 += "<a href=http://" + IPaddress + "/user4" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  650.   user4 += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  651.   user4 += "</body>";
  652.   user4 += "</center>";
  653.   user4 += "</html>";
  654.   httpServer.send(200, "text/html", user4);
  655.  
  656.   if (httpServer.args() > 0 ) {
  657.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  658.       Argument_Name = httpServer.argName(i);
  659.       if (httpServer.argName(i) == "name_input") {
  660.         if (httpServer.arg(0) == httpServer.arg(1)) {
  661.           auto f = gfs.open("/user4.txt", "w");
  662.           f.print(httpServer.arg(i));
  663.           f.close();
  664.           Serial.println("gleich");
  665.         }
  666.         else
  667.         {
  668.           Serial.println("Ungleich");
  669.         }
  670.       }
  671.     }
  672.   }
  673. }
  674.  
  675. void user5() {
  676.   String IPaddress = WiFi.localIP().toString();
  677.   String user5String;
  678.   fileSystem.openFromFile("/user5.txt", user5String);
  679.  
  680.   String user5;
  681.   user5 =  "<html>";
  682.   user5 += "<head><title>Benutzer #5 speichern... </title>";
  683.   user5 += "<center>";
  684.   user5 += "<style>";
  685.   user5 += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  686.   user5 += "</style>";
  687.   user5 += "</head>";
  688.   user5 += "<body>";
  689.   user5 += "<h1><br>Benutzer ID #5 ( " + user5String + " )</h1>";
  690.   user5 += "Bitte ID 2x eingeben!";
  691.   user5 += "<br><br><br>Beide Felder leer lassen zum löschen";
  692.   user5 += "<form action='http://" + IPaddress + "/user5' method='POST'>";
  693.   user5 += "ID: <input type='text' name='name_input' size=30><br>";
  694.   user5 += "ID: <input type='text' name='name_input1' size=30><br><br>";
  695.   user5 += "<input type='submit' value='Benutzer ID Speichern'>";
  696.   user5 += "</form>";
  697.   user5 += "<a href=http://" + IPaddress + "/user5" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  698.   user5 += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  699.   user5 += "</body>";
  700.   user5 += "</center>";
  701.   user5 += "</html>";
  702.   httpServer.send(200, "text/html", user5);
  703.  
  704.   if (httpServer.args() > 0 ) {
  705.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  706.       Argument_Name = httpServer.argName(i);
  707.       if (httpServer.argName(i) == "name_input") {
  708.         if (httpServer.arg(0) == httpServer.arg(1)) {
  709.           auto f = gfs.open("/user5.txt", "w");
  710.           f.print(httpServer.arg(i));
  711.           f.close();
  712.           Serial.println("gleich");
  713.         }
  714.         else
  715.         {
  716.           Serial.println("Ungleich");
  717.         }
  718.       }
  719.     }
  720.   }
  721. }
  722.  
  723. void ShowUsers() { // showUsers
  724.   String IPaddress = WiFi.localIP().toString();
  725.   String user1String;
  726.   String user2String;
  727.   String user3String;
  728.   String user4String;
  729.   String user5String;
  730.   fileSystem.openFromFile("/user1.txt", user1String);
  731.   fileSystem.openFromFile("/user2.txt", user2String);
  732.   fileSystem.openFromFile("/user3.txt", user3String);
  733.   fileSystem.openFromFile("/user4.txt", user4String);
  734.   fileSystem.openFromFile("/user5.txt", user5String);
  735.   String userspage;
  736.   userspage =  "<html>";
  737.   userspage += "<head><title>Benutzer anzeigen... </title>";
  738.   userspage += "<center>";
  739.   userspage += "<style>";
  740.   userspage += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  741.   userspage += "</style>";
  742.   userspage += "</head>";
  743.   userspage += "<body>";
  744.   userspage += "<h1><br>Gespeicherte Benutzer </h1><br>";
  745.   userspage += "Benutzer 1:<br>" + user1String + "<br><br>";
  746.   userspage += "Benutzer 2:<br>" + user2String + "<br><br>";
  747.   userspage += "Benutzer 3:<br>" + user3String + "<br><br>";
  748.   userspage += "Benutzer 4:<br>" + user4String + "<br><br>";
  749.   userspage += "Benutzer 5:<br>" + user5String + "<br><br><br>";
  750.   userspage += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  751.   userspage += "</body>";
  752.   userspage += "</center>";
  753.   userspage += "</html>";
  754.   httpServer.send(200, "text/html", userspage);
  755. }
  756.  
  757. void predelete() { // Delete alle User
  758.   String IPaddress = WiFi.localIP().toString();
  759.   String predelete;
  760.   predelete =  "<html>";
  761.   predelete += "<head><title>GaragenESP </title>";
  762.   predelete += "<center>";
  763.   predelete += "<style>";
  764.   predelete += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  765.   predelete += "</style>";
  766.   predelete += "</head>";
  767.   predelete += "<body>";
  768.   predelete += "<h1><br>Wirklich alle Benutzer Löschen?</h1>";
  769.   predelete += "<br><a href=http://" + IPaddress + "/deleter" + ">JA, wirklich ALLE Benutzer löschen</a><br><br>";
  770.   predelete += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  771.   predelete += "</body>";
  772.   predelete += "</center>";
  773.   predelete += "</html>";
  774.   httpServer.send(200, "text/html", predelete); // Send a response to the client asking for input
  775. }
  776.  
  777. void deleter() {
  778.   auto a = gfs.open("/user1.txt", "w");
  779.   a.close();
  780.   auto b = gfs.open("/user2.txt", "w");
  781.   b.close();
  782.   auto c = gfs.open("/user3.txt", "w");
  783.   c.close();
  784.   auto d = gfs.open("/user4.txt", "w");
  785.   d.close();
  786.   auto e = gfs.open("/user5.txt", "w");
  787.   e.close();
  788.   String IPaddress = WiFi.localIP().toString();
  789.   String deleter;
  790.   deleter =  "<html>";
  791.   deleter += "<head><title>GaragenESP </title>";
  792.   deleter += "<center>";
  793.   deleter += "<style>";
  794.   deleter += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  795.   deleter += "</style>";
  796.   deleter += "</head>";
  797.   deleter += "<body>";
  798.   deleter += "<h1><br>Alle benutzer Gelöscht!</h1>";
  799.   deleter += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  800.   deleter += "</body>";
  801.   deleter += "</center>";
  802.   deleter += "</html>";
  803.   httpServer.send(200, "text/html", deleter); // Send a response to the client asking for input
  804. }
  805.  
  806. void st() { // ShowToken
  807.   String IPaddress = WiFi.localIP().toString();
  808.   String tokenString;
  809.   fileSystem.openFromFile("/token.txt", tokenString);
  810.  
  811.   String stpage;
  812.   stpage =  "<html>";
  813.   stpage += "<head><title>Gespeicherten Token anzeigen... </title>";
  814.   stpage += "<center>";
  815.   stpage += "<style>";
  816.   stpage += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  817.   stpage += "</style>";
  818.   stpage += "</head>";
  819.   stpage += "<body>";
  820.   stpage += "<h1><br>Garagen Token</h1>";
  821.   stpage += "Gespeicherter Token:<br><br><h1><br>" + tokenString + "</h1><br><br>";
  822.   stpage += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  823.   stpage += "</body>";
  824.   stpage += "</center>";
  825.   stpage += "</html>";
  826.   httpServer.send(200, "text/html", stpage);
  827. }
  828.  
  829. void tord() { // Tor Tastdauer
  830.   String IPaddress = WiFi.localIP().toString();
  831.   String tordString;
  832.   fileSystem.openFromFile("/tord.txt", tordString);
  833.   String tord;
  834.   tord =  "<html>";
  835.   tord += "<head><title>Tastzeit Tor</title>";
  836.   tord += "<center>";
  837.   tord += "<style>";
  838.   tord += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  839.   tord += "</style>";
  840.   tord += "</head>";
  841.   tord += "<body>";
  842.   tord += "<h1><br>Tastdauer</h1>";
  843.   tord += "Aktuelle Tastdauer: " + tordString + "ms<br><br>";
  844.   tord += "<form action='http://" + IPaddress + "/tord' method='POST'>";
  845.   tord += "Neue Tastdauer: <br><input type='text' name='name_input' size=5>ms<br>";
  846.   tord += "<input type='submit' value='Tastdauer Speichern'>";
  847.   tord += "</form>";
  848.   tord += "<a href=http://" + IPaddress + "/tord" + ">Seite neu laden. Bitte kein Reload benutzen!</a><br><br>";
  849.   tord += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  850.   tord += "</body>";
  851.   tord += "</center>";
  852.   tord += "</html>";
  853.   httpServer.send(200, "text/html", tord);
  854.   if (httpServer.args() > 0 ) {
  855.     for ( uint8_t i = 0; i < httpServer.args(); i++ ) {
  856.       Argument_Name = httpServer.argName(i);
  857.       auto f = gfs.open("/tord.txt", "w");
  858.       f.print(httpServer.arg(0));
  859.       f.close();
  860.     }
  861.   }
  862. }
  863.  
  864. void manuell() {
  865.   int tord1String;
  866.   String IPaddress = WiFi.localIP().toString();
  867.   String manuell;
  868.   manuell =  "<html>";
  869.   manuell += "<head><title>GaragenESP </title>";
  870.   manuell += "<center>";
  871.   manuell += "<style>";
  872.   manuell += "body { background-color: #E6E6FA; font-family: Arial, Helvetica, Sans-Serif; Color: blue;}";
  873.   manuell += "</style>";
  874.   manuell += "</head>";
  875.   manuell += "<body>";
  876.   manuell += "<h1><br>Tor wurde getastert!</h1>";
  877.   manuell += "<a href=http://" + IPaddress + "/" + ">Zurück zur Hauptseite</a>";
  878.   manuell += "</body>";
  879.   manuell += "</center>";
  880.   manuell += "</html>";
  881.   httpServer.send(200, "text/html", manuell); // Send a response to the client asking for input
  882.   fileSystem.openFromFile("/tord.txt", tord1String);
  883.   digitalWrite(torPin, LOW); // Relais 1
  884.   delay(tord1String);
  885.   digitalWrite(torPin, HIGH);
  886. }
  887.  
  888. void setup() {
  889.   MDNS.begin(host);
  890.   Serial.begin(115200);
  891.   secured_client.setInsecure();
  892.   Serial.println(" ");
  893.   pinMode(rtaster, INPUT_PULLUP);
  894.   pinMode(torPin, OUTPUT);
  895.   pinMode(lichtPin, OUTPUT);
  896.   digitalWrite(lichtPin, HIGH);
  897.   digitalWrite(torPin, HIGH);
  898.   delay(10);
  899.   WifiVerbinder();
  900.   //  Serial.println(WiFi.localIP());
  901.   secured_client.setTrustAnchors(&cert);
  902.   Serverstarten();
  903.   ntp();
  904.   tokenGetter();
  905.   webupdater();
  906.   OTA();
  907.  
  908.   //  String tordString;
  909.   //  fileSystem.openFromFile("/tord.txt", tordString);
  910.   //  Serial.println(tordString);
  911.  
  912. }
  913.  
  914. void Serverstarten() {
  915.   httpServer.begin();
  916.   httpServer.on("/", Hauptseite);
  917.   httpServer.on("/st", st); // Show Token
  918.   httpServer.on("/SetToken", SetToken);
  919.   httpServer.on("/user1", user1);
  920.   httpServer.on("/user2", user2);
  921.   httpServer.on("/user3", user3);
  922.   httpServer.on("/user4", user4);
  923.   httpServer.on("/user5", user5);
  924.   httpServer.on("/ShowUsers", ShowUsers);
  925.   httpServer.on("/predelete", predelete);
  926.   httpServer.on("/deleter", deleter);
  927.   httpServer.on("/tord", tord);
  928.   httpServer.on("/manuell", manuell);
  929. }
  930.  
  931. void loop() {
  932.   {
  933.     if (millis() - bot_lasttime > BOT_MTBS)
  934.     {
  935.       int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  936.       while (numNewMessages)
  937.       {
  938.         handleNewMessages(numNewMessages);
  939.         numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  940.       }
  941.       bot_lasttime = millis();
  942.     }
  943.   }
  944.   httpServer.handleClient();
  945.   ArduinoOTA.handle(); // Auskommentieren!
  946.   // WifiDeleter();
  947. }
  948.  
  949. // Version 2.3.final - 15.12.2021 / 06:00
  950. // All-in-One Version!
  951.  
  952.  
  953. /*
  954.    Copyright (c) 2021 Peter Geher
  955.  
  956.    This Code is free software; you can redistribute it and/or
  957.    modify it under the terms of the GNU Lesser General Public
  958.    License as published by the Free Software Foundation; either
  959.    version 2.1 of the License, or (at your option) any later version.
  960.  
  961.    This Code is distributed in the hope that it will be useful,
  962.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  963.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  964.    Lesser General Public License for more details.
  965.  
  966.    You should have received a copy of the GNU Lesser General Public
  967.    License along with this Code; if not, write to the Free Software
  968.    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  969. */

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.