Advertisement
safwan092

Untitled

Feb 13th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4.  
  5. #define WIFI_SSID "network"
  6. #define WIFI_PASSWORD "123456789"
  7. #define BOT_TOKEN "6168635844:AAGoo6CZQ-AjTwSeS_-D0SrABzKMZDX8E4A"
  8. String chat_id = "473975732";
  9.  
  10. #define gasSensorPin 34
  11. #define ledPin 15
  12.  
  13. WiFiClientSecure secured_client;
  14. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  15.  
  16.  
  17. void setup() {
  18. Serial.begin(9600);
  19. pinMode(gasSensorPin, INPUT);
  20. pinMode(ledPin, OUTPUT);
  21. digitalWrite(ledPin, 0);
  22. connectToWiFi();
  23. bot.sendMessage(chat_id, "ESP32 is Online");
  24. delay(1000);
  25. }
  26.  
  27. void loop() {
  28. int gasSensor = analogRead(34);
  29. Serial.println(gasSensor);
  30. if (gasSensor > 2000) {
  31. digitalWrite(ledPin, 1);
  32. bot.sendMessage(chat_id, "Alert Gas Detected!!");
  33. delay(1000);
  34. }
  35. else {
  36. digitalWrite(ledPin, 0);
  37. }
  38. delay(1000);
  39. }
  40.  
  41.  
  42. void connectToWiFi() {
  43. Serial.print("Connecting to Wifi SSID ");
  44. Serial.print(WIFI_SSID);
  45. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  46. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  47. while (WiFi.status() != WL_CONNECTED)
  48. {
  49. Serial.print(".");
  50. delay(500);
  51. }
  52. Serial.print("\n");
  53. Serial.print("WiFi connected. IP address: ");
  54. Serial.println(WiFi.localIP());
  55.  
  56.  
  57. Serial.print("Retrieving time: ");
  58. configTime(0, 0, "pool.ntp.org");
  59. time_t now = time(nullptr);
  60. while (now < 24 * 3600)
  61. {
  62. Serial.print(".");
  63. delay(100);
  64. now = time(nullptr);
  65. }
  66. Serial.println(now);
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement