safwan092

Untitled

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