Files
ESP-Network-Status-Bot/main.ino
2025-10-03 21:07:37 +02:00

142 lines
3.9 KiB
C++

//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);
}