safwan092

Ardunio_Uno_A9G_Buzzer_LED_MPU6050_Car_Flipped_Car_Crash_Sensor

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