Advertisement
safwan092

Untitled

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