safwan092

Untitled

Nov 21st, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <SoftwareSerial.h>
  3. #include <Wire.h>
  4. #include <MPU6050.h>
  5.  
  6. #define buzzer 13
  7. #define btn1 2
  8. static const int RXPin = 6, TXPin = 5;
  9. String s = "www.google.com/maps/dir/";
  10. static const uint32_t GPSBaud = 9600;
  11. String Lat = "";
  12. String Lng = "";
  13. String Number = "+966591772557";
  14. String MSG1 = "newSwitch [Activated]";
  15. String MSG2 = "Flip [Activated]";
  16.  
  17. const size_t BUFSIZE = 300;
  18. char f_buffer[BUFSIZE];
  19. float *f_buf = (float*)f_buffer;
  20.  
  21. TinyGPSPlus gps;// The TinyGPSPlus object
  22. SoftwareSerial ss(RXPin, TXPin);// The serial connection to the GPS device
  23.  
  24. MPU6050 mpu;
  25.  
  26. void setup()
  27. {
  28. Serial.begin(9600);
  29. Wire.begin();
  30. pinMode(btn1, INPUT_PULLUP);
  31. pinMode(buzzer, OUTPUT);
  32. setupA9G();
  33. mpu.initialize();
  34. Serial.println("Setup Executed");
  35. attachInterrupt(digitalPinToInterrupt(btn1), send_gps_data, CHANGE);
  36. }
  37.  
  38. void loop() {
  39. readGPS_Data();
  40. mpu_Data();
  41. //send_gps_data();
  42. }
  43.  
  44. static void smartDelay(unsigned long ms)
  45. {
  46. unsigned long start = millis();
  47. do
  48. {
  49. while (ss.available())
  50. gps.encode(ss.read());
  51. } while (millis() - start < ms);
  52. }
  53.  
  54. void send_gps_data()
  55. {
  56. /*
  57. if (gps.location.lat() == 0 || gps.location.lng() == 0)
  58. {
  59. Serial.println("Return Executed");
  60. return;
  61. }
  62. */
  63. send_SMS_Location(Number, MSG1);
  64. digitalWrite(buzzer, HIGH);
  65. }
  66.  
  67.  
  68. void setupA9G() {
  69.  
  70. ss.begin(GPSBaud);
  71. Serial.println("Starting...");
  72. ss.println("\r");
  73. ss.println("AT\r");
  74. delay(10);
  75. ss.println("\r");
  76. ss.println("AT+GPS=1\r");
  77. delay(100);
  78. ss.println("AT+CREG=2\r");
  79. delay(6000);
  80. //ss.print("AT+CREG?\r");
  81. ss.println("AT+CGATT=1\r");
  82. delay(6000);
  83. ss.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  84. delay(6000);
  85. // ss.println("AT+LOCATION=1\r");
  86. ss.println("AT+CGACT=1,1\r");
  87. delay(6000);
  88. //Initialize ends
  89. //Initialize GPS
  90. ss.println("\r");
  91. ss.println("AT+GPS=1\r");
  92. delay(1000);
  93. //ss.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  94. ss.println("AT+GPSRD=10\r");
  95. delay(100);
  96. // set SMS mode to text mode
  97. ss.println("AT+CMGF=1\r");
  98. delay(1000);
  99. //ss.println("AT+LOCATION=2\r");
  100.  
  101. }
  102.  
  103. void send_SMS_Location(String number, String message) {
  104. Serial.print("Latitude (deg): ");
  105. Lat = String(gps.location.lat(), 6);
  106. Serial.println(Lat);
  107.  
  108. Serial.print("Longitude (deg): ");
  109. Lng = String(gps.location.lng(), 6);
  110. Serial.println(Lng);
  111. Serial.println();
  112.  
  113. s += String(gps.location.lat(), 6);
  114. s += ",";
  115. s += String(gps.location.lng(), 6);
  116. s += "/";
  117. Serial.println(s);
  118. Serial.println("Sending Message");
  119. String numbertosend = "AT+CMGS=\"";
  120. numbertosend += number;
  121. numbertosend += "\"\r";
  122. String messaG = message;
  123. messaG += " \n";
  124. messaG += s;
  125. ss.println("AT+CMGF=1\r");
  126. delay(1000);
  127. ss.println("AT+CNMI=2,2,0,0,0\r");
  128. delay(1000);
  129. ss.print(numbertosend);
  130. delay(1000);
  131. ss.print(messaG);
  132. //ss.print(s);
  133. ss.write(0x1A);
  134. delay(1000);
  135. s = "www.google.com/maps/dir/";
  136. }
  137.  
  138. void sendSMSwithLocation() {
  139. Serial.println("Switch [Activated] - Sending Message");
  140. ss.println("AT + CMGF = 1\r");
  141. delay(1000);
  142. ss.println("AT + CNMI = 2, 2, 0, 0, 0\r");
  143. delay(1000);
  144. ss.print("AT + CMGS = \"+9660591772557\"\r"); //Replace this with your mobile number
  145. delay(1000);
  146. ss.print("Switch [Activated] \n");
  147. ss.print(s);
  148. ss.write(0x1A);
  149. delay(1000);
  150. s = "www.google.com/maps/dir/";
  151. }
  152.  
  153. void sendSMSFlipLocation() {
  154. Serial.println("Flip [Activated] - Sending Message");
  155. ss.println("AT+CMGF=1\r");
  156. delay(1000);
  157. ss.println("AT+CNMI=2,2,0,0,0\r");
  158. delay(1000);
  159. ss.print("AT+CMGS=\"+966591772557\"\r");//Replace this with your mobile number
  160. delay(1000);
  161. ss.print("Flip [Activated] \n");
  162. ss.print(s);
  163. ss.write(0x1A);
  164. delay(1000);
  165. s = "www.google.com/maps/dir/";
  166. }
  167.  
  168. void readGPS_Data() {
  169. smartDelay(2000);
  170. if (millis() > 5000 && gps.charsProcessed() < 10)
  171. Serial.println(F("No GPS data received: check wiring"));
  172.  
  173. }
  174.  
  175. void mpu_Data() {
  176. int16_t ax, ay, az;
  177. mpu.getAcceleration(&ax, &ay, &az);
  178. if (az < 0) {
  179. digitalWrite(buzzer, HIGH);
  180. send_SMS_Location(Number, MSG2);
  181. }
  182. else {
  183. digitalWrite(buzzer, LOW); // Turn off the buzzer
  184. }
  185. Serial.print("X = ");
  186. Serial.print(ax);
  187. Serial.print(" Y = ");
  188. Serial.print(ay);
  189. Serial.print(" Z = ");
  190. Serial.println(az);
  191. //delay(500); // Delay for stability
  192. }
Advertisement
Add Comment
Please, Sign In to add comment