| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //String message[3][6]; amount of messages read per time (update_id, name_id, name, lastname, chat_id, text)
- //bot.message[0][0] = ""; All messages have been replied - reset new messages
- //--------------------------------------------------------------------------------------------------------------
- // Libaries
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <ESP8266TelegramBOT.h>
- #include <ArduinoJson.h>
- #include <ESP8266Ping.h>
- #include <WiFiUdp.h>
- #include <NTPClient.h>
- // Telegram-Variablen
- #define BOTtoken "1214264165:AAHxjThsX6UAisiCDu1C02nAv3ErZNbFnfM"
- #define BOTname "BobbyBot"
- #define BOTusername "@BobbyTheOneAndOnlyBot"
- TelegramBOT bot(BOTtoken, BOTname, BOTname);
- // Wifi-Variablen
- String chat_id = "426600237";
- String message_text = "";
- const char* wifi_ssid = "SchneckenAufKaffee";
- const char* wifi_password = "hf8CnyjPwrhc";
- WiFiClientSecure net_ssl;
- const IPAddress google_ping_ip(192, 168, 0, 1);
- // Program-Variablen
- boolean internet_access = true;
- boolean wifi_access = true;
- // Zeit-Variablen
- const long utc_offset_ms = 7200;
- char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- String t1, t2;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", utc_offset_ms);
- //--------------------------------------------------------------------------------------------------------------
- // Main Code
- void setup()
- {
- Serial.begin(115200);
- delay(3000);
-
- // Connect to Wifi network
- Serial.print("Connecting to wifi ");
- Serial.println(wifi_ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(wifi_ssid, wifi_password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.println("No WiFi-Access!");
- }
- Serial.println("Wifi connected");
-
- // Test internet connection
- if (Ping.ping(google_ping_ip))
- {
- Serial.println("Internet connection successful");
- }
- else
- {
- Serial.println("Internet connection unsuccessful");
- }
- //Time-Server Begin
- timeClient.begin();
-
- // Telegram-Bot Bootup
- Serial.println("Booting telegram-bot");
- delay(1000);
- bot.begin();
- message_text = "Bot online";
- bot.sendMessage(chat_id, message_text, "");
- Serial.println("Telegram-Bot online");
- Serial.println("");
- Serial.println("Beginning Work...");
- }
- //--------------------------------------------------------------------------------------------------------------
- void loop()
- {
- // Uhrzeit abfragen
- timeClient.update();
- t1 = String(timeClient.getHours()) + ":" + String(timeClient.getMinutes()) + ":" + String(timeClient.getSeconds());
- Serial.println(t1);
-
- // Internetverbindung prüfen
- while (Ping.ping(google_ping_ip) != true)
- {
- internet_access = false;
- Serial.println("No Ping-Response!");
-
- // WLAN-Verbindung prüfen
- while (WiFi.status() != WL_CONNECTED)
- {
- wifi_access = false;
- Serial.println("No WiFi-Access!");
- WiFi.disconnect();
- delay(5000);
- WiFi.begin(wifi_ssid, wifi_password);
- delay(5000);
- }
- delay(5000);
- }
- timeClient.update();
- t2 = String(timeClient.getHours()) + ":" + String(timeClient.getMinutes()) + ":" + String(timeClient.getSeconds());
- // Störungsmeldung zusammensetzen, falls eine Störung vorlag
- if (wifi_access == false or internet_access == false)
- {
- message_text = "Verbindungsstörung erkannt! ";
- if (wifi_access = false)
- {
- message_text += "WiFi-Verbinung ";
- }
- else
- {
- message_text += "Internet-Verbindung ";
- }
- message_text += "ausgefallen zwischen ";
- message_text += String(t1);
- message_text += "ms und ";
- message_text += String(t2);
- message_text += "ms.";
- // Nachricht absenden
- bot.sendMessage(chat_id, message_text, "");
- // Fehlervariablen zurücksetzen
- wifi_access = true;
- internet_access = true;
- }
- //Sleep
- delay(55 000);
- }
|