Advertisement
safwan092

ESP32_TELEGRAM_BOT_ALERT_MESSAGE

Feb 10th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4.  
  5. #define WIFI_SSID "WIFI_NAME"
  6. #define WIFI_PASSWORD "WIFI_PASSWORD"
  7. #define BOT_TOKEN "BOT_TOKEN_BOT_FATHER"
  8. WiFiClientSecure secured_client;
  9. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  10. String chat_id = "CHAT_ID_IDBOT";
  11.  
  12. void setup()
  13. {
  14. Serial.begin(9600);
  15. initWiFi_and_Time_For_Telegram();
  16. bot.sendMessage(chat_id, "Project Connected to Telegram Server <ONLINE>");
  17. }
  18. void loop() {
  19. //bot.sendMessage(chat_id, "Alert Message");
  20. }
  21.  
  22. void initWiFi_and_Time_For_Telegram() {
  23. Serial.println();
  24. Serial.print("Connecting to Wifi SSID ");
  25. Serial.print(WIFI_SSID);
  26. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  27. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  28. while (WiFi.status() != WL_CONNECTED)
  29. {
  30. Serial.print(".");
  31. delay(500);
  32. }
  33. Serial.print("\nWiFi connected. IP address: ");
  34. Serial.println(WiFi.localIP());
  35.  
  36. Serial.print("Retrieving time: ");
  37. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  38. time_t now = time(nullptr);
  39. while (now < 24 * 3600)
  40. {
  41. Serial.print(".");
  42. delay(100);
  43. now = time(nullptr);
  44. }
  45. Serial.println(now);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement