Advertisement
safwan092

Untitled

Jan 15th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <SoftwareSerial.h>
  3. #include <Wire.h>
  4. #include <MPU6050.h>
  5.  
  6. #define redLED 12
  7. #define buzzer 13
  8. #define btn1 2
  9. static const int RXPin = 6, TXPin = 5;
  10. String s = "www.google.com/maps/dir/";
  11. static const uint32_t GPSBaud = 9600;
  12. String Lat = "";
  13. String Lng = "";
  14. int SendSMS_Now = 0;
  15. const size_t BUFSIZE = 300;
  16. char f_buffer[BUFSIZE];
  17. float *f_buf = (float*)f_buffer;
  18.  
  19. TinyGPSPlus gps;
  20. SoftwareSerial ss(RXPin, TXPin);
  21. MPU6050 mpu;
  22.  
  23. void setup() {
  24. Serial.begin(9600);
  25. Wire.begin();
  26. pinMode(btn1, INPUT_PULLUP);
  27. attachInterrupt(digitalPinToInterrupt(btn1), send_SMS_NOW, FALLING);
  28. pinMode(buzzer, OUTPUT);
  29. pinMode(redLED, OUTPUT);
  30. digitalWrite(redLED, LOW);
  31. redLEDOK(5);
  32. setupA9G();
  33. mpu.initialize();
  34. Serial.println("Setup Executed");
  35. }
  36.  
  37. void send_SMS_NOW() {
  38. SendSMS_Now = 1;
  39. }
  40. void loop() {
  41. fullGPSSerial();
  42. mpu_Data();
  43. readGPS_Data();
  44. //showLATLON();
  45. if (SendSMS_Now == 1) {
  46. get_gps_data();
  47. digitalWrite(buzzer, HIGH);
  48. sendSMSwithLocation1();
  49. delay(2000);
  50. s = "www.google.com/maps/dir/";
  51. SendSMS_Now = 0;
  52. }
  53. digitalWrite(buzzer, LOW);
  54. }
  55.  
  56. void get_gps_data() {
  57. if (gps.location.lat() == 0 || gps.location.lng() == 0) {
  58. Serial.println("Return Executed");
  59. s = "http://www.google.com/maps/dir/19.204871,42.085309";
  60. Serial.println(s);
  61. return;
  62. }
  63. Serial.print("Latitude (deg): ");
  64. Lat = String(gps.location.lat(), 6);
  65. Serial.println(Lat);
  66. Serial.print("Longitude (deg): ");
  67. Lng = String(gps.location.lng(), 6);
  68. Serial.println(Lng);
  69. Serial.println();
  70. s += String(gps.location.lat(), 6);
  71. s += ",";
  72. s += String(gps.location.lng(), 6);
  73. s += "/";
  74. Serial.println(s);
  75. }
  76.  
  77. void setupA9G() {
  78. ss.begin(GPSBaud);
  79. Serial.println("Starting...");
  80. ss.println("\r");
  81. ss.println("AT\r");
  82. delay(10);
  83. ss.println("\r");
  84. ss.println("AT+GPS=1\r");
  85. delay(100);
  86. ss.println("AT+CREG=2\r");
  87. delay(6000);
  88. //ss.print("AT+CREG?\r");
  89. ss.println("AT+CGATT=1\r");
  90. delay(6000);
  91. ss.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  92. delay(6000);
  93. // ss.println("AT+LOCATION=1\r");
  94. ss.println("AT+CGACT=1,1\r");
  95. delay(6000);
  96. //Initialize ends
  97. //Initialize GPS
  98. ss.println("\r");
  99. ss.println("AT+GPS=1\r");
  100. delay(1000);
  101. //ss.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  102. ss.println("AT+GPSRD=10\r");
  103. delay(100);
  104. // set SMS mode to text mode
  105. ss.println("AT+CMGF=1\r");
  106. delay(1000);
  107. //ss.println("AT+LOCATION=2\r");
  108. Serial.println(F("FullExample.ino"));
  109. Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
  110. Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  111. Serial.println(F("by Mikal Hart"));
  112. Serial.println();
  113. Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  114. Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  115. Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
  116. }
  117.  
  118. /////////////////////////////////////////////////////////
  119. void sendSMSwithLocation1() {
  120. Serial.println("WARNING! Vehicle Collision Detected");
  121. ss.println("AT+CMGF=1\r");
  122. delay(1000);
  123. ss.println("AT+CNMI=2,2,0,0,0\r");
  124. delay(1000);
  125. ss.print("AT+CMGS=\"+966559070115\"\r");//Replace this with your mobile number
  126. delay(1000);
  127. ss.print("WARNING! Vehicle Collision Detected\n");
  128. ss.print(s);
  129. ss.write(0x1A);
  130. delay(1000);
  131. }
  132. ////////////////////////////////////////////////////////////
  133. void sendSMSFlipLocation() {
  134. Serial.println("WARNING! Vehicle Overturned");
  135. ss.println("AT+CMGF=1\r");
  136. delay(1000);
  137. ss.println("AT+CNMI=2,2,0,0,0\r");
  138. delay(1000);
  139. ss.print("AT+CMGS=\"+966559070115\"\r");//Replace this with your mobile number
  140. delay(1000);
  141. ss.print("WARNING! Vehicle Overturned \n");
  142. ss.print(s);
  143. ss.write(0x1A);
  144. delay(1000);
  145. s = "www.google.com/maps/dir/";
  146. }
  147. ////////////////////////////////////////////////////////////
  148. void readGPS_Data() {
  149. smartDelay(2000);
  150. if (millis() > 5000 && gps.charsProcessed() < 10)
  151. Serial.println(F("No GPS data received: check wiring"));
  152. }
  153.  
  154. void mpu_Data() {
  155. int16_t ax, ay, az;
  156. mpu.getAcceleration(&ax, &ay, &az);
  157. if (az < 0) {
  158. digitalWrite(buzzer, HIGH);
  159. get_gps_data();
  160. sendSMSFlipLocation();
  161. }
  162. else {
  163. digitalWrite(buzzer, LOW); // Turn off the buzzer
  164. }
  165. Serial.print("X = ");
  166. Serial.print(ax);
  167. Serial.print(" Y = ");
  168. Serial.print(ay);
  169. Serial.print(" Z = ");
  170. Serial.println(az);
  171. //delay(500); // Delay for stability
  172. }
  173.  
  174. void showLATLON() {
  175. Serial.print("Latitude (deg): ");
  176. Lat = String(gps.location.lat(), 6);
  177. Serial.println(Lat);
  178. Serial.print("Longitude (deg): ");
  179. Lng = String(gps.location.lng(), 6);
  180. Serial.println(Lng);
  181. Serial.println();
  182. }
  183.  
  184.  
  185. void fullGPSSerial() {
  186. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  187. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  188. printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  189. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  190. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  191. printInt(gps.location.age(), gps.location.isValid(), 5);
  192. printDateTime(gps.date, gps.time);
  193. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  194. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  195. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  196. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  197.  
  198. unsigned long distanceKmToLondon =
  199. (unsigned long)TinyGPSPlus::distanceBetween(
  200. gps.location.lat(),
  201. gps.location.lng(),
  202. LONDON_LAT,
  203. LONDON_LON) / 1000;
  204. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  205.  
  206. double courseToLondon =
  207. TinyGPSPlus::courseTo(
  208. gps.location.lat(),
  209. gps.location.lng(),
  210. LONDON_LAT,
  211. LONDON_LON);
  212.  
  213. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  214.  
  215. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  216.  
  217. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  218.  
  219. printInt(gps.charsProcessed(), true, 6);
  220. printInt(gps.sentencesWithFix(), true, 10);
  221. printInt(gps.failedChecksum(), true, 9);
  222. Serial.println();
  223.  
  224. smartDelay(1000);
  225. if (gps.charsProcessed() > 63) {
  226. redLEDOK(3);
  227. }
  228. if (millis() > 5000 && gps.charsProcessed() < 10)
  229. Serial.println(F("No GPS data received: check wiring"));
  230. }
  231.  
  232.  
  233. // This custom version of delay() ensures that the gps object
  234. // is being "fed".
  235. static void smartDelay(unsigned long ms)
  236. {
  237. unsigned long start = millis();
  238. do
  239. {
  240. while (ss.available())
  241. gps.encode(ss.read());
  242. } while (millis() - start < ms);
  243. }
  244.  
  245. static void printFloat(float val, bool valid, int len, int prec)
  246. {
  247. if (!valid)
  248. {
  249. while (len-- > 1)
  250. Serial.print('*');
  251. Serial.print(' ');
  252. }
  253. else
  254. {
  255. Serial.print(val, prec);
  256. int vi = abs((int)val);
  257. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  258. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  259. for (int i = flen; i < len; ++i)
  260. Serial.print(' ');
  261. }
  262. smartDelay(0);
  263. }
  264.  
  265. static void printInt(unsigned long val, bool valid, int len)
  266. {
  267. char sz[32] = "*****************";
  268. if (valid)
  269. sprintf(sz, "%ld", val);
  270. sz[len] = 0;
  271. for (int i = strlen(sz); i < len; ++i)
  272. sz[i] = ' ';
  273. if (len > 0)
  274. sz[len - 1] = ' ';
  275. Serial.print(sz);
  276. smartDelay(0);
  277. }
  278.  
  279. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  280. {
  281. if (!d.isValid())
  282. {
  283. Serial.print(F("********** "));
  284. }
  285. else
  286. {
  287. char sz[32];
  288. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  289. Serial.print(sz);
  290. }
  291.  
  292. if (!t.isValid())
  293. {
  294. Serial.print(F("******** "));
  295. }
  296. else
  297. {
  298. char sz[32];
  299. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  300. Serial.print(sz);
  301. redLEDOK(1);
  302. }
  303.  
  304. printInt(d.age(), d.isValid(), 5);
  305. smartDelay(0);
  306. }
  307.  
  308. static void printStr(const char *str, int len)
  309. {
  310. int slen = strlen(str);
  311. for (int i = 0; i < len; ++i)
  312. Serial.print(i < slen ? str[i] : ' ');
  313. smartDelay(0);
  314. }
  315.  
  316.  
  317. void redLEDOK(int j) {
  318. for (int i = 0; i < j; i++) {
  319. digitalWrite(redLED, HIGH);
  320. delay(100);
  321. digitalWrite(redLED, LOW);
  322. delay(100);
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement