safwan092

Project_10611_Code

Jan 8th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <TinyGPSPlus.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5.  
  6. #define redLED 4
  7. #define trigPin 5
  8. #define echoPin 18
  9. #define maxTrash 6 // in CM
  10. #define SOUND_SPEED 0.034 // in CM/Sec
  11. #define WIFI_SSID "network"
  12. #define WIFI_PASSWORD "123456789"
  13. #define BOT_TOKEN "5653695564:AAGB9tL88hoW_TZGhFijirJ5DDDxtxaRHEM"
  14. String chat_id = "473975732";
  15.  
  16. String lon;
  17. String latt;
  18. bool flag = 0;
  19. long duration;
  20. float distanceCm;
  21. bool Start = false;
  22. String stringMsg = "";
  23. char messageBuffer[70];
  24. //unsigned long bot_lasttime;
  25. //const unsigned long BOT_MTBS = 1000;
  26. unsigned long interval = 60000; // 1 min
  27.  
  28. TinyGPSPlus gps;
  29. WiFiClientSecure secured_client;
  30. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  31.  
  32. void setup() {
  33. Serial.begin(9600);
  34. Serial2.begin(9600);
  35. pinMode(trigPin, OUTPUT);
  36. pinMode(echoPin, INPUT);
  37. pinMode(redLED, OUTPUT);
  38. digitalWrite(redLED, LOW);
  39. connectToWiFi();
  40. bot.sendMessage(chat_id, "Trash_10611 Online");
  41. }
  42.  
  43.  
  44. void loop() {
  45. readUS_Data();
  46. readGPS_Data();
  47. if (distanceCm < 6) {
  48. digitalWrite(redLED, HIGH);
  49. sendTelegramMessage();
  50. }
  51. else {
  52. digitalWrite(redLED, LOW);
  53. }
  54. }
  55.  
  56. void sendTelegramMessage() {
  57. bot.sendMessage(chat_id, messageBuffer);
  58. delay(interval);
  59. }
  60.  
  61. void readUS_Data() {
  62. // Clears the trigPin
  63. digitalWrite(trigPin, LOW);
  64. delayMicroseconds(2);
  65. // Sets the trigPin on HIGH state for 10 micro seconds
  66. digitalWrite(trigPin, HIGH);
  67. delayMicroseconds(10);
  68. digitalWrite(trigPin, LOW);
  69.  
  70. // Reads the echoPin, returns the sound wave travel time in microseconds
  71. duration = pulseIn(echoPin, HIGH);
  72.  
  73. // Calculate the distance
  74. distanceCm = duration * SOUND_SPEED / 2;
  75. //Serial.print("Distance (cm): ");
  76. //Serial.println(distanceCm);
  77. }
  78.  
  79. void connectToWiFi() {
  80. Serial.print("Connecting to Wifi SSID ");
  81. Serial.print(WIFI_SSID);
  82. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  83. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  84. while (WiFi.status() != WL_CONNECTED)
  85. {
  86. Serial.print(".");
  87. delay(500);
  88. }
  89. Serial.print("\n");
  90. Serial.print("WiFi connected. IP address: ");
  91. Serial.println(WiFi.localIP());
  92.  
  93.  
  94. Serial.print("Retrieving time: ");
  95. configTime(0, 0, "pool.ntp.org");
  96. time_t now = time(nullptr);
  97. while (now < 24 * 3600)
  98. {
  99. Serial.print(".");
  100. delay(100);
  101. now = time(nullptr);
  102. }
  103. Serial.println(now);
  104.  
  105. }
  106.  
  107. void readGPS_Data() {
  108. while (Serial2.available() > 0)
  109. if (gps.encode(Serial2.read()))
  110. displayInfo();
  111. if (millis() > 5000 && gps.charsProcessed() < 10) {
  112. Serial.println(F("No GPS detected: check wiring."));
  113. while (true);
  114. }
  115. }
  116.  
  117. void displayInfo() {
  118. Serial.print(F("Location: "));
  119. if (gps.location.isValid()) {
  120. lon = String(gps.location.lng(), 6);
  121. latt = String(gps.location.lat(), 6);
  122. Serial.print("Lat: ");
  123. Serial.print(latt);
  124. Serial.print(F(","));
  125. Serial.print("Lng: ");
  126. Serial.print(lon);
  127. Serial.println();
  128. stringMsg = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + latt + "+" + lon;
  129. stringMsg.toCharArray(messageBuffer, 70);
  130. }
  131. else {
  132. Serial.print(F("INVALID"));
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment