main.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //String message[3][6]; amount of messages read per time (update_id, name_id, name, lastname, chat_id, text)
  2. //bot.message[0][0] = ""; All messages have been replied - reset new messages
  3. //--------------------------------------------------------------------------------------------------------------
  4. // Libaries
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiClientSecure.h>
  7. #include <ESP8266TelegramBOT.h>
  8. #include <ArduinoJson.h>
  9. #include <ESP8266Ping.h>
  10. #include <WiFiUdp.h>
  11. #include <NTPClient.h>
  12. // Telegram-Variablen
  13. #define BOTtoken "1214264165:AAHxjThsX6UAisiCDu1C02nAv3ErZNbFnfM"
  14. #define BOTname "BobbyBot"
  15. #define BOTusername "@BobbyTheOneAndOnlyBot"
  16. TelegramBOT bot(BOTtoken, BOTname, BOTname);
  17. // Wifi-Variablen
  18. String chat_id = "426600237";
  19. String message_text = "";
  20. const char* wifi_ssid = "SchneckenAufKaffee";
  21. const char* wifi_password = "hf8CnyjPwrhc";
  22. WiFiClientSecure net_ssl;
  23. const IPAddress google_ping_ip(192, 168, 0, 1);
  24. // Program-Variablen
  25. boolean internet_access = true;
  26. boolean wifi_access = true;
  27. // Zeit-Variablen
  28. const long utc_offset_ms = 7200;
  29. char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  30. String t1, t2;
  31. WiFiUDP ntpUDP;
  32. NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", utc_offset_ms);
  33. //--------------------------------------------------------------------------------------------------------------
  34. // Main Code
  35. void setup()
  36. {
  37. Serial.begin(115200);
  38. delay(3000);
  39. // Connect to Wifi network
  40. Serial.print("Connecting to wifi ");
  41. Serial.println(wifi_ssid);
  42. WiFi.mode(WIFI_STA);
  43. WiFi.begin(wifi_ssid, wifi_password);
  44. while (WiFi.status() != WL_CONNECTED)
  45. {
  46. delay(500);
  47. Serial.println("No WiFi-Access!");
  48. }
  49. Serial.println("Wifi connected");
  50. // Test internet connection
  51. if (Ping.ping(google_ping_ip))
  52. {
  53. Serial.println("Internet connection successful");
  54. }
  55. else
  56. {
  57. Serial.println("Internet connection unsuccessful");
  58. }
  59. //Time-Server Begin
  60. timeClient.begin();
  61. // Telegram-Bot Bootup
  62. Serial.println("Booting telegram-bot");
  63. delay(1000);
  64. bot.begin();
  65. message_text = "Bot online";
  66. bot.sendMessage(chat_id, message_text, "");
  67. Serial.println("Telegram-Bot online");
  68. Serial.println("");
  69. Serial.println("Beginning Work...");
  70. }
  71. //--------------------------------------------------------------------------------------------------------------
  72. void loop()
  73. {
  74. // Uhrzeit abfragen
  75. timeClient.update();
  76. t1 = String(timeClient.getHours()) + ":" + String(timeClient.getMinutes()) + ":" + String(timeClient.getSeconds());
  77. Serial.println(t1);
  78. // Internetverbindung prüfen
  79. while (Ping.ping(google_ping_ip) != true)
  80. {
  81. internet_access = false;
  82. Serial.println("No Ping-Response!");
  83. // WLAN-Verbindung prüfen
  84. while (WiFi.status() != WL_CONNECTED)
  85. {
  86. wifi_access = false;
  87. Serial.println("No WiFi-Access!");
  88. WiFi.disconnect();
  89. delay(5000);
  90. WiFi.begin(wifi_ssid, wifi_password);
  91. delay(5000);
  92. }
  93. delay(5000);
  94. }
  95. timeClient.update();
  96. t2 = String(timeClient.getHours()) + ":" + String(timeClient.getMinutes()) + ":" + String(timeClient.getSeconds());
  97. // Störungsmeldung zusammensetzen, falls eine Störung vorlag
  98. if (wifi_access == false or internet_access == false)
  99. {
  100. message_text = "Verbindungsstörung erkannt! ";
  101. if (wifi_access = false)
  102. {
  103. message_text += "WiFi-Verbinung ";
  104. }
  105. else
  106. {
  107. message_text += "Internet-Verbindung ";
  108. }
  109. message_text += "ausgefallen zwischen ";
  110. message_text += String(t1);
  111. message_text += "ms und ";
  112. message_text += String(t2);
  113. message_text += "ms.";
  114. // Nachricht absenden
  115. bot.sendMessage(chat_id, message_text, "");
  116. // Fehlervariablen zurücksetzen
  117. wifi_access = true;
  118. internet_access = true;
  119. }
  120. //Sleep
  121. delay(55 000);
  122. }