safwan092

Project_10590_Code

Dec 25th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5.  
  6. // Wifi network station credentials
  7. #define WIFI_SSID "network"
  8. #define WIFI_PASSWORD "123456789"
  9. // Telegram BOT Token (Get from Botfather)
  10. #define BOT_TOKEN "5945177836:AAFBjZV7k3jPL84-xEwJQe9PFBg0sIt_4Cc"
  11. String chat_id = "473975732";
  12. long lat_long ;
  13. long lng_long ;
  14. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  15.  
  16. WiFiClientSecure secured_client;
  17. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  18. unsigned long bot_lasttime; // last time messages' scan has been done
  19. bool Start = false;
  20.  
  21. #define Sensor_input 36
  22. int sensor_Aout = 0;
  23. TinyGPSPlus gps;
  24.  
  25. void setup() {
  26. Serial.begin(9600);
  27. Serial2.begin(9600);
  28. pinMode(Sensor_input, INPUT);
  29. delay(3000);
  30. // attempt to connect to Wifi network:
  31. Serial.print("Connecting to Wifi SSID ");
  32. Serial.print(WIFI_SSID);
  33. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  34. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  35. while (WiFi.status() != WL_CONNECTED)
  36. {
  37. Serial.print(".");
  38. delay(500);
  39. }
  40. Serial.print("\nWiFi connected. IP address: ");
  41. Serial.println(WiFi.localIP());
  42.  
  43. Serial.print("Retrieving time: ");
  44. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  45. time_t now = time(nullptr);
  46. while (now < 24 * 3600)
  47. {
  48. Serial.print(".");
  49. delay(100);
  50. now = time(nullptr);
  51. }
  52. Serial.println(now);
  53. bot.sendMessage(chat_id, "123");
  54. }
  55.  
  56.  
  57. void loop() {
  58. sensor_Aout = analogRead(Sensor_input);
  59. Serial.print("Gas Sensor: ");
  60. Serial.println(sensor_Aout);
  61. while (Serial2.available() > 0)
  62. if (gps.encode(Serial2.read()))
  63. displayInfo();
  64. if (millis() > 5000 && gps.charsProcessed() < 10) {
  65. Serial.println(F("No GPS detected: check wiring."));
  66. while (true);
  67. }
  68. if (sensor_Aout > 1800) {
  69. Serial.println("Gas");
  70. //http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=20.123456+20.123456
  71. //Send Alert Telegram Message
  72. String message = "Alert Gas Detected !! \n Sensor Value: " + String(sensor_Aout) + ".\n";
  73. message += "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + String(lat_long,6) + "+" + String(lng_long,6) + ".\n";
  74. bot.sendMessage(chat_id, message);
  75. delay(10000);
  76. }
  77. else {
  78. Serial.println("No Gas");
  79. }
  80.  
  81. }//end of LOOP
  82.  
  83.  
  84. void displayInfo() {
  85. Serial.print(F("Location: "));
  86. if (gps.location.isValid()) {
  87. Serial.print("Lat: ");
  88. Serial.print(gps.location.lat(), 6);
  89. lat_long = gps.location.lat();
  90. Serial.print(F(","));
  91. Serial.print("Lng: ");
  92. Serial.print(gps.location.lng(), 6);
  93. lng_long = gps.location.lng();
  94. Serial.println();
  95. }
  96. else {
  97. Serial.print(F("INVALID"));
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment