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

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.