safwan092

Project_10611_code

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