safwan092

Project_10611_Code_Final

Jan 19th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 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 "5758548495:AAHYaV3NnmsFQo_1UjzBgcimSttmsZDp5Os"
  14. String chat_id = "660897003";
  15.  
  16. int flag_ff = 0;
  17. String lon;
  18. String latt;
  19. bool flag = 0;
  20. long duration;
  21. float distanceCm;
  22. bool Start = false;
  23. String stringMsg = "";
  24. char messageBuffer[70];
  25. //unsigned long bot_lasttime;
  26. //const unsigned long BOT_MTBS = 1000;
  27. //unsigned long interval = 60000; // 1 min
  28.  
  29. unsigned long previousMillis = 0; // will store last time LED was updated
  30.  
  31. const long interval = 10000; // interval at which to blink (milliseconds)
  32.  
  33.  
  34.  
  35. TinyGPSPlus gps;
  36. WiFiClientSecure secured_client;
  37. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  38.  
  39. void setup() {
  40. Serial.begin(9600);
  41. Serial2.begin(9600);
  42. pinMode(trigPin, OUTPUT);
  43. pinMode(echoPin, INPUT);
  44. pinMode(redLED, OUTPUT);
  45. digitalWrite(redLED, LOW);
  46. digitalWrite(trigPin, LOW);
  47. connectToWiFi();
  48. bot.sendMessage(chat_id, "Project Online");
  49. delay(20000);
  50. }
  51.  
  52.  
  53. void loop() {
  54. readUS_Data();
  55. readGPS_Data();
  56. if (distanceCm < 10) {
  57. digitalWrite(redLED, HIGH);
  58. sendTelegramMessage();
  59. flag_ff = 1;
  60. }
  61. else if (distanceCm >= 10 && flag_ff == 1) {
  62. flag_ff = 0;
  63. digitalWrite(redLED, LOW);
  64. bot.sendMessage(chat_id, "This Trash is Empty, Thank You.");
  65. }
  66. else if (distanceCm >= 10 && flag_ff == 0) {
  67. digitalWrite(redLED, LOW);
  68. }
  69. }
  70.  
  71. void sendTelegramMessage() {
  72. bot.sendMessage(chat_id, "This Trash is Full");
  73. delay(500);
  74. bot.sendMessage(chat_id, messageBuffer);
  75. delay(10000);//interval
  76. }
  77.  
  78. void readUS_Data() {
  79. if (millis() - previousMillis >= interval) {
  80. // save the last time you blinked the LED
  81. previousMillis = millis();
  82.  
  83. // Clears the trigPin
  84. digitalWrite(trigPin, LOW);
  85. delayMicroseconds(2);
  86. // Sets the trigPin on HIGH state for 10 micro seconds
  87. digitalWrite(trigPin, HIGH);
  88. delayMicroseconds(10);
  89. digitalWrite(trigPin, LOW);
  90.  
  91. // Reads the echoPin, returns the sound wave travel time in microseconds
  92. duration = pulseIn(echoPin, HIGH);
  93.  
  94. // Calculate the distance
  95. distanceCm = duration * SOUND_SPEED / 2;
  96. Serial.print("Distance (cm): ");
  97. Serial.println(distanceCm);
  98. }
  99. }
  100.  
  101. void connectToWiFi() {
  102. Serial.print("Connecting to Wifi SSID ");
  103. Serial.print(WIFI_SSID);
  104. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  105. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  106. while (WiFi.status() != WL_CONNECTED)
  107. {
  108. Serial.print(".");
  109. delay(500);
  110. }
  111. Serial.print("\n");
  112. Serial.print("WiFi connected. IP address: ");
  113. Serial.println(WiFi.localIP());
  114.  
  115.  
  116. Serial.print("Retrieving time: ");
  117. configTime(0, 0, "pool.ntp.org");
  118. time_t now = time(nullptr);
  119. while (now < 24 * 3600)
  120. {
  121. Serial.print(".");
  122. delay(100);
  123. now = time(nullptr);
  124. }
  125. Serial.println(now);
  126.  
  127. }
  128.  
  129. void readGPS_Data() {
  130. while (Serial2.available() > 0)
  131. if (gps.encode(Serial2.read()))
  132. displayInfo();
  133. if (millis() > 5000 && gps.charsProcessed() < 10) {
  134. Serial.println(F("No GPS detected: check wiring."));
  135. while (true);
  136. }
  137. }
  138.  
  139. void displayInfo() {
  140. Serial.print(F("Location: "));
  141. if (gps.location.isValid()) {
  142. lon = String(gps.location.lng(), 6);
  143. latt = String(gps.location.lat(), 6);
  144. Serial.print("Lat: ");
  145. Serial.print(latt);
  146. Serial.print(F(","));
  147. Serial.print("Lng: ");
  148. Serial.print(lon);
  149. Serial.println();
  150. stringMsg = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + latt + "+" + lon;
  151. stringMsg.toCharArray(messageBuffer, 70);
  152. }
  153. else {
  154. Serial.println(F("INVALID"));
  155. stringMsg = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=21.861659+39.067037";// Location if GPS no signal
  156. stringMsg.toCharArray(messageBuffer, 70);
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment