safwan092

Untitled

Oct 19th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.25 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <TinyGPSPlus.h>
  3.  
  4. //////////////////////
  5. /*
  6. get_gps_data();
  7. sendSMSLocation();
  8. */
  9. //////////////////////
  10. ////////////////////////////////////////////////
  11. String s = "www.google.com/maps/dir/";
  12. static const uint32_t GPSBaud = 9600;
  13. String Lat = "";
  14. String Lng = "";
  15. int SendSMS_Now = 0;
  16. const size_t BUFSIZE = 300;
  17. char f_buffer[BUFSIZE];
  18. float *f_buf = (float*)f_buffer;
  19. TinyGPSPlus gps;
  20. ////////////////////////////////////////////////
  21.  
  22. ////////////////////////////////////////////////
  23. const int MPU_ADDR = 0x68;
  24. // MPU6050 register addresses
  25. const int PWR_MGMT_1 = 0x6B;
  26. const int ACCEL_XOUT_H = 0x3B;
  27. const int GYRO_XOUT_H = 0x43;
  28. const int TEMP_OUT_H = 0x41;
  29. // Calibration offsets
  30. float accelOffsetX = 0;
  31. float accelOffsetY = 0;
  32. float accelOffsetZ = 0;
  33. float gyroOffsetX = 0;
  34. float gyroOffsetY = 0;
  35. float gyroOffsetZ = 0;
  36. // Adaptive thresholds - will adjust based on normal motion
  37. float adaptiveAccelThreshold = 3.0; // Start with reasonable value
  38. float adaptiveGyroThreshold = 50.0; // Start with reasonable value
  39. // Impact detection
  40. const unsigned long IMPACT_WINDOW = 100; // Time to analyze impact (ms)
  41. const unsigned long HIT_COOLDOWN = 2000; // Prevent multiple detections
  42. const float SUDDEN_CHANGE_FACTOR = 3.0; // Must be 3x normal motion
  43. // Fall detection parameters
  44. const float FREE_FALL_THRESHOLD = 0.5; // Low G-force for free fall
  45. const float FALL_IMPACT_THRESHOLD = 4.0; // G-force for fall impact
  46. const unsigned long FALL_TIME_WINDOW = 3000; // Max time for fall sequence
  47. const unsigned long FALL_COOLDOWN = 5000; // Cooldown between fall detections
  48. // State variables
  49. bool hitDetected = false;
  50. bool fallDetected = false;
  51. unsigned long lastHitTime = 0;
  52. unsigned long lastFallTime = 0;
  53. unsigned long impactStartTime = 0;
  54. bool analyzingImpact = false;
  55. // Fall detection states
  56. bool inFreeFall = false;
  57. unsigned long fallStartTime = 0;
  58. bool waitingForFallImpact = false;
  59. // For adaptive threshold calculation
  60. float normalAccelMax = 0;
  61. float normalGyroMax = 0;
  62. unsigned long lastNormalUpdate = 0;
  63. const unsigned long NORMAL_LEARNING_TIME = 5000; // Learn normal motion for 5 sec
  64. // Impact analysis
  65. float impactAccelPeaks[10] = {0};
  66. float impactGyroPeaks[10] = {0};
  67. int peakCount = 0;
  68. // Filtering
  69. const int MOVING_AVERAGE_SAMPLES = 5;
  70. float accelBuffer[MOVING_AVERAGE_SAMPLES] = {0};
  71. float gyroBuffer[MOVING_AVERAGE_SAMPLES] = {0};
  72. int bufferIndex = 0;
  73. bool calibrated = false;
  74. const int CALIBRATION_SAMPLES = 100;
  75. bool learningPhase = true;
  76. ////////////////////////////////////////////////
  77.  
  78.  
  79.  
  80. void setup() {
  81. Serial.begin(115200);
  82. Wire.begin();
  83.  
  84. // Wake up MPU6050
  85. Wire.beginTransmission(MPU_ADDR);
  86. Wire.write(PWR_MGMT_1);
  87. Wire.write(0);
  88. Wire.endTransmission(true);
  89.  
  90. Serial.println("HEAD IMPACT & FALL DETECTION SYSTEM");
  91. Serial.println("Learning normal motion for 5 seconds...");
  92. Serial.println("Please walk around normally");
  93. calibrateSensor();
  94. Serial.println("Calibration complete!");
  95. Serial.println("======================================");
  96. lastNormalUpdate = millis();
  97.  
  98. setupA9G();
  99. Serial.println("Setup Executed");
  100. }
  101.  
  102. void send_SMS_NOW() {
  103. SendSMS_Now = 1;
  104. }
  105.  
  106. void calibrateSensor() {
  107. float accelXSum = 0, accelYSum = 0, accelZSum = 0;
  108. float gyroXSum = 0, gyroYSum = 0, gyroZSum = 0;
  109.  
  110. for (int i = 0; i < CALIBRATION_SAMPLES; i++) {
  111. int16_t accelX = readSensor(ACCEL_XOUT_H);
  112. int16_t accelY = readSensor(ACCEL_XOUT_H + 2);
  113. int16_t accelZ = readSensor(ACCEL_XOUT_H + 4);
  114. int16_t gyroX = readSensor(GYRO_XOUT_H);
  115. int16_t gyroY = readSensor(GYRO_XOUT_H + 2);
  116. int16_t gyroZ = readSensor(GYRO_XOUT_H + 4);
  117.  
  118. accelXSum += accelX / 16384.0;
  119. accelYSum += accelY / 16384.0;
  120. accelZSum += accelZ / 16384.0;
  121. gyroXSum += gyroX / 131.0;
  122. gyroYSum += gyroY / 131.0;
  123. gyroZSum += gyroZ / 131.0;
  124.  
  125. delay(10);
  126. }
  127.  
  128. accelOffsetX = accelXSum / CALIBRATION_SAMPLES;
  129. accelOffsetY = accelYSum / CALIBRATION_SAMPLES;
  130. accelOffsetZ = accelZSum / CALIBRATION_SAMPLES - 1.0;
  131.  
  132. gyroOffsetX = gyroXSum / CALIBRATION_SAMPLES;
  133. gyroOffsetY = gyroYSum / CALIBRATION_SAMPLES;
  134. gyroOffsetZ = gyroZSum / CALIBRATION_SAMPLES;
  135.  
  136. calibrated = true;
  137. }
  138.  
  139. float movingAverage(float buffer[], float newValue) {
  140. buffer[bufferIndex] = newValue;
  141. bufferIndex = (bufferIndex + 1) % MOVING_AVERAGE_SAMPLES;
  142.  
  143. float sum = 0;
  144. for (int i = 0; i < MOVING_AVERAGE_SAMPLES; i++) {
  145. sum += buffer[i];
  146. }
  147. return sum / MOVING_AVERAGE_SAMPLES;
  148. }
  149.  
  150.  
  151. //////////////////////////////////////////////////////////
  152. /////////////////// LOOP //////////////////////////
  153. //////////////////////////////////////////////////////////
  154. void loop() {
  155. fullGPSSerial();
  156. readGPS_Data();
  157. showLATLON();
  158.  
  159. if (SendSMS_Now == 1) {
  160. get_gps_data();
  161. sendSMSLocation();
  162. delay(2000);
  163. s = "www.google.com/maps/dir/";
  164. SendSMS_Now = 0;
  165. }
  166. // Read sensor data
  167. int16_t rawAccelX = readSensor(ACCEL_XOUT_H);
  168. int16_t rawAccelY = readSensor(ACCEL_XOUT_H + 2);
  169. int16_t rawAccelZ = readSensor(ACCEL_XOUT_H + 4);
  170.  
  171. int16_t rawGyroX = readSensor(GYRO_XOUT_H);
  172. int16_t rawGyroY = readSensor(GYRO_XOUT_H + 2);
  173. int16_t rawGyroZ = readSensor(GYRO_XOUT_H + 4);
  174.  
  175. // Convert and apply calibration
  176. float accelX_g = rawAccelX / 16384.0 - accelOffsetX;
  177. float accelY_g = rawAccelY / 16384.0 - accelOffsetY;
  178. float accelZ_g = rawAccelZ / 16384.0 - accelOffsetZ;
  179.  
  180. float gyroX_deg = rawGyroX / 131.0 - gyroOffsetX;
  181. float gyroY_deg = rawGyroY / 131.0 - gyroOffsetY;
  182. float gyroZ_deg = rawGyroZ / 131.0 - gyroOffsetZ;
  183.  
  184. // Calculate magnitudes with moving average filtering
  185. float accelMagnitude = sqrt(accelX_g * accelX_g + accelY_g * accelY_g + accelZ_g * accelZ_g);
  186. float gyroMagnitude = sqrt(gyroX_deg * gyroX_deg + gyroY_deg * gyroY_deg + gyroZ_deg * gyroZ_deg);
  187.  
  188. float filteredAccel = movingAverage(accelBuffer, accelMagnitude);
  189. float filteredGyro = movingAverage(gyroBuffer, gyroMagnitude);
  190.  
  191. unsigned long currentTime = millis();
  192.  
  193. // Learning phase - understand normal motion
  194. if (learningPhase) {
  195. updateNormalMotionLevels(filteredAccel, filteredGyro, currentTime);
  196. }
  197.  
  198. // Run both detection systems
  199. checkForImpact(filteredAccel, filteredGyro, currentTime);
  200. checkForFall(filteredAccel, currentTime);
  201.  
  202. // Print status
  203. static unsigned long lastPrint = 0;
  204. if (currentTime - lastPrint > 1000) {
  205. printStatus(accelX_g, accelY_g, accelZ_g, gyroX_deg, gyroY_deg, gyroZ_deg,
  206. filteredAccel, filteredGyro, currentTime);
  207. lastPrint = currentTime;
  208. }
  209.  
  210. delay(20);
  211.  
  212. }// end of LOOP
  213. ///////////////////////////////////////////////////////////////
  214. ///////////////////////////////////////////////////////////////
  215.  
  216. ////////////////////////////////////////////////////////////
  217. void sendSMSLocation() {
  218. Serial.println("WARNING! Helmit Impact Detected");
  219.  
  220.  
  221. /////
  222. Serial2.println("AT+CMGF=1\r");
  223. delay(1000);
  224.  
  225. Serial2.println("AT+CNMI=2,2,0,0,0\r");
  226. delay(1000);
  227.  
  228. Serial2.print("AT+CMGS=\"+966580224645\"\r");//Replace this with your mobile number
  229. delay(1000);
  230. Serial2.print(s);
  231. Serial2.write(0x1A);
  232. delay(3000);
  233. /////
  234. /*
  235. Serial2.println("AT+CMGF=1\r");
  236. delay(1000);
  237. Serial2.println("AT+CNMI=2,2,0,0,0\r");
  238. delay(1000);
  239. Serial2.print("AT+CMGS=\"+966554418546\"\r");//Replace this with your mobile number
  240. delay(1000);
  241. Serial2.print("WARNING! Helmit Impact Detected\n");
  242. //Serial2.print(s);
  243. Serial2.write(0x1A);
  244. */
  245. Serial.println("Sending SMS is Done check your Phone!!!!!!!!!!!!!!!");
  246. delay(1000);
  247. s = "www.google.com/maps/dir/";
  248. }
  249. ////////////////////////////////////////////////////////////
  250.  
  251. void updateNormalMotionLevels(float accel, float gyro, unsigned long currentTime) {
  252. // Update maximum normal motion levels during learning phase
  253. if (accel > normalAccelMax) normalAccelMax = accel;
  254. if (gyro > normalGyroMax) normalGyroMax = gyro;
  255.  
  256. // End learning phase after 5 seconds
  257. if (currentTime - lastNormalUpdate > NORMAL_LEARNING_TIME) {
  258. learningPhase = false;
  259.  
  260. // Set adaptive thresholds based on learned normal motion
  261. adaptiveAccelThreshold = normalAccelMax * SUDDEN_CHANGE_FACTOR;
  262. adaptiveGyroThreshold = normalGyroMax * SUDDEN_CHANGE_FACTOR;
  263.  
  264. // Ensure minimum thresholds
  265. if (adaptiveAccelThreshold < 2.0) adaptiveAccelThreshold = 2.0;
  266. if (adaptiveGyroThreshold < 30.0) adaptiveGyroThreshold = 30.0;
  267.  
  268. Serial.println("\n*** LEARNING PHASE COMPLETE ***");
  269. Serial.print("Normal motion - Max Accel: "); Serial.print(normalAccelMax, 2); Serial.println("g");
  270. Serial.print("Normal motion - Max Gyro: "); Serial.print(normalGyroMax, 1); Serial.println("Β°/s");
  271. Serial.print("Adaptive thresholds - Accel: "); Serial.print(adaptiveAccelThreshold, 2); Serial.println("g");
  272. Serial.print("Adaptive thresholds - Gyro: "); Serial.print(adaptiveGyroThreshold, 1); Serial.println("Β°/s");
  273. Serial.println("Now monitoring for impacts and falls...");
  274. }
  275. }
  276.  
  277. void checkForImpact(float accel, float gyro, unsigned long currentTime) {
  278. // Check if we have a sudden change that might be an impact
  279. bool suddenChange = (accel > adaptiveAccelThreshold) || (gyro > adaptiveGyroThreshold);
  280.  
  281. if (suddenChange && !analyzingImpact) {
  282. // Start analyzing potential impact
  283. analyzingImpact = true;
  284. impactStartTime = currentTime;
  285. peakCount = 0;
  286. impactAccelPeaks[0] = accel;
  287. impactGyroPeaks[0] = gyro;
  288. Serial.println("πŸ’₯ Potential impact detected - analyzing...");
  289. }
  290.  
  291. if (analyzingImpact) {
  292. // Track peaks during impact window
  293. if (accel > impactAccelPeaks[peakCount]) impactAccelPeaks[peakCount] = accel;
  294. if (gyro > impactGyroPeaks[peakCount]) impactGyroPeaks[peakCount] = gyro;
  295.  
  296. // Check if impact analysis period is over
  297. if (currentTime - impactStartTime > IMPACT_WINDOW) {
  298. analyzeImpactPattern(currentTime);
  299. analyzingImpact = false;
  300. }
  301.  
  302. // Move to next peak every 20ms
  303. if (currentTime - impactStartTime > (peakCount + 1) * 20 && peakCount < 9) {
  304. peakCount++;
  305. impactAccelPeaks[peakCount] = accel;
  306. impactGyroPeaks[peakCount] = gyro;
  307. }
  308. }
  309.  
  310. // Reset hit detection after cooldown
  311. if (currentTime - lastHitTime > HIT_COOLDOWN) {
  312. hitDetected = false;
  313. }
  314. }
  315.  
  316. void analyzeImpactPattern(unsigned long currentTime) {
  317. // Find maximum values during impact window
  318. float maxAccel = 0;
  319. float maxGyro = 0;
  320.  
  321. for (int i = 0; i <= peakCount; i++) {
  322. if (impactAccelPeaks[i] > maxAccel) maxAccel = impactAccelPeaks[i];
  323. if (impactGyroPeaks[i] > maxGyro) maxGyro = impactGyroPeaks[i];
  324. }
  325.  
  326. // Determine if this is a real head impact
  327. bool isHeadImpact = verifyHeadImpact(maxAccel, maxGyro);
  328.  
  329. if (isHeadImpact && !hitDetected) {
  330. hitDetected = true;
  331. lastHitTime = currentTime;
  332.  
  333.  
  334. Serial.println("🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨");
  335. Serial.println("🚨 HEAD IMPACT DETECTED! 🚨");
  336. Serial.println("🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨");
  337. Serial.print("Impact Force: "); Serial.print(maxAccel, 1); Serial.println("g");
  338.  
  339. if (maxAccel > 1) {//2.1
  340. SendSMS_Now = 1;
  341. Serial.println("Sending SMS - Please Wait ... ");
  342. Serial.println("Sending SMS - Please Wait ... ");
  343. Serial.println("Sending SMS - Please Wait ... ");
  344. delay(5000);
  345. }
  346. /*
  347. Serial.print("Rotational Force: "); Serial.print(maxGyro, 1); Serial.println("Β°/s");
  348. Serial.print("Thresholds: "); Serial.print(adaptiveAccelThreshold, 1);
  349. Serial.print("g / "); Serial.print(adaptiveGyroThreshold, 1); Serial.println("Β°/s");
  350. Serial.println("⚠️ CHECK USER FOR CONCUSSION SYMPTOMS!");
  351. Serial.println();
  352. */
  353. } else if (!isHeadImpact) {
  354.  
  355. Serial.print("❌ False alarm: ");
  356. Serial.print(maxAccel, 1); Serial.print("g, ");
  357. Serial.print(maxGyro, 1); Serial.println("Β°/s - Normal motion pattern");
  358.  
  359. if (maxAccel > 1) {//2.1
  360. SendSMS_Now = 1;
  361. Serial.println("Sending SMS - Please Wait ... ");
  362. Serial.println("Sending SMS - Please Wait ... ");
  363. Serial.println("Sending SMS - Please Wait ... ");
  364. delay(5000);
  365. }
  366. }
  367. }
  368.  
  369. bool verifyHeadImpact(float maxAccel, float maxGyro) {
  370. // Real head impacts typically have:
  371. // 1. Very high acceleration OR very high rotation
  372. // 2. Both values significantly above normal motion
  373. // 3. Specific pattern characteristics
  374.  
  375. bool highAccel = maxAccel > (normalAccelMax * 4.0); // 4x normal motion
  376. bool highGyro = maxGyro > (normalGyroMax * 4.0); // 4x normal motion
  377.  
  378. // Head impact usually has both linear and rotational components
  379. bool hasBothComponents = (maxAccel > adaptiveAccelThreshold * 1.2) &&
  380. (maxGyro > adaptiveGyroThreshold * 1.2);
  381.  
  382. // Or extremely high values in one component
  383. bool extremeSingleComponent = (maxAccel > adaptiveAccelThreshold * 2.0) ||
  384. (maxGyro > adaptiveGyroThreshold * 2.0);
  385.  
  386. return (hasBothComponents || extremeSingleComponent) && (highAccel || highGyro);
  387. }
  388.  
  389. void checkForFall(float accel, unsigned long currentTime) {
  390. // Fall detection logic: Free fall followed by impact
  391.  
  392. // Step 1: Detect free fall (low acceleration)
  393. if (accel < FREE_FALL_THRESHOLD && !inFreeFall && !waitingForFallImpact) {
  394. inFreeFall = true;
  395. fallStartTime = currentTime;
  396. Serial.println("⚠️ Free fall detected! Monitoring for impact...");
  397. }
  398.  
  399. // Step 2: If we're in free fall, wait for impact
  400. if (inFreeFall) {
  401. // Check if free fall period is too long (might not be a real fall)
  402. if (currentTime - fallStartTime > FALL_TIME_WINDOW) {
  403. inFreeFall = false;
  404. Serial.println("❌ Free fall too long - probably not a real fall");
  405. }
  406.  
  407. // Check for impact after free fall
  408. if (accel > FALL_IMPACT_THRESHOLD) {
  409. // This is the impact after a fall
  410. if (!fallDetected && (currentTime - lastFallTime > FALL_COOLDOWN)) {
  411. fallDetected = true;
  412. lastFallTime = currentTime;
  413. inFreeFall = false;
  414. waitingForFallImpact = false;
  415.  
  416.  
  417. Serial.println("🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨");
  418. Serial.println("🚨 FALL DETECTED! 🚨");
  419. Serial.println("🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨");
  420. Serial.print("Fall Impact: "); Serial.print(accel, 1); Serial.println("g");
  421. Serial.print("Free fall duration: ");
  422. Serial.print(currentTime - fallStartTime);
  423. Serial.println("ms");
  424. Serial.println("⚠️ USER MAY HAVE FALLEN - CHECK IMMEDIATELY!");
  425. Serial.println();
  426.  
  427. }
  428. }
  429. }
  430.  
  431. // Reset fall detection after cooldown
  432. if (currentTime - lastFallTime > FALL_COOLDOWN) {
  433. fallDetected = false;
  434. }
  435.  
  436. // Reset free fall state if acceleration returns to normal
  437. if (inFreeFall && accel > FREE_FALL_THRESHOLD * 2.0) {
  438. inFreeFall = false;
  439. waitingForFallImpact = true;
  440. // Wait a bit to see if impact comes
  441. if (currentTime - fallStartTime > 1000) {
  442. waitingForFallImpact = false;
  443. }
  444. }
  445. }
  446.  
  447. void printStatus(float accelX, float accelY, float accelZ, float gyroX, float gyroY, float gyroZ,
  448. float accelMag, float gyroMag, unsigned long currentTime) {
  449. /*
  450. Serial.print("Accel: ");
  451. Serial.print(accelX, 2); Serial.print("g, ");
  452. Serial.print(accelY, 2); Serial.print("g, ");
  453. Serial.print(accelZ, 2); Serial.print("g");
  454. Serial.print(" | Total: "); Serial.print(accelMag, 2); Serial.println("g");
  455.  
  456. Serial.print("Gyro: ");
  457. Serial.print(gyroX, 1); Serial.print("Β°/s, ");
  458. Serial.print(gyroY, 1); Serial.print("Β°/s, ");
  459. Serial.print(gyroZ, 1); Serial.print("Β°/s");
  460. Serial.print(" | Total: "); Serial.print(gyroMag, 1); Serial.println("Β°/s");
  461. */
  462. if (learningPhase) {
  463. float remaining = (NORMAL_LEARNING_TIME - (currentTime - lastNormalUpdate)) / 1000.0;
  464. /*
  465. Serial.print("πŸ“š Learning normal motion: ");
  466. Serial.print(remaining, 0); Serial.println("s remaining");
  467. Serial.print("Current max - Accel: "); Serial.print(normalAccelMax, 2);
  468. Serial.print("g, Gyro: "); Serial.print(normalGyroMax, 1); Serial.println("Β°/s");
  469. */
  470. } else {
  471. /*
  472. Serial.print("🎯 Thresholds: "); Serial.print(adaptiveAccelThreshold, 1);
  473. Serial.print("g / "); Serial.print(adaptiveGyroThreshold, 1); Serial.println("Β°/s");
  474. */
  475. // Status display for both systems
  476. if (hitDetected) {
  477. //Serial.println("🚨 HEAD IMPACT DETECTED - CHECK USER!");
  478. } else if (fallDetected) {
  479. //Serial.println("🚨 FALL DETECTED - USER MAY HAVE FALLEN!");
  480. } else if (inFreeFall) {
  481. //Serial.println("⚠️ Free fall detected - watching for impact...");
  482. } else if (analyzingImpact) {
  483. //Serial.println("πŸ’₯ Analyzing potential impact...");
  484. } else {
  485. //Serial.println("βœ… Normal - Monitoring impacts & falls");
  486. }
  487. }
  488.  
  489. Serial.println("-----------------------");
  490. }
  491.  
  492. int16_t readSensor(uint8_t reg) {
  493. Wire.beginTransmission(MPU_ADDR);
  494. Wire.write(reg);
  495. Wire.endTransmission(false);
  496. Wire.requestFrom(MPU_ADDR, 2, true);
  497. return (Wire.read() << 8) | Wire.read();
  498. }
  499.  
  500.  
  501. void get_gps_data() {
  502. if (gps.location.lat() == 0 || gps.location.lng() == 0) {
  503. Serial.println("Return Executed");
  504. s = "http://www.google.com/maps/dir/19.204871,42.085309";
  505. Serial.println(s);
  506. return;
  507. }
  508. Serial.print("Latitude (deg): ");
  509. Lat = String(gps.location.lat(), 6);
  510. Serial.println(Lat);
  511. Serial.print("Longitude (deg): ");
  512. Lng = String(gps.location.lng(), 6);
  513. Serial.println(Lng);
  514. Serial.println();
  515. s += String(gps.location.lat(), 6);
  516. s += ",";
  517. s += String(gps.location.lng(), 6);
  518. s += "/";
  519. Serial.println(s);
  520. }
  521.  
  522. void setupA9G() {
  523. Serial2.begin(GPSBaud);
  524. Serial.println("Starting...");
  525. Serial2.println("\r");
  526. Serial2.println("AT\r");
  527. delay(10);
  528. Serial2.println("\r");
  529. Serial2.println("AT+GPS=1\r");
  530. delay(100);
  531. Serial2.println("AT+CREG=2\r");
  532. delay(6000);
  533. //ss.print("AT+CREG?\r");
  534. Serial2.println("AT+CGATT=1\r");
  535. delay(6000);
  536. Serial2.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  537. delay(6000);
  538. // ss.println("AT+LOCATION=1\r");
  539. Serial2.println("AT+CGACT=1,1\r");
  540. delay(6000);
  541. //Initialize ends
  542. //Initialize GPS
  543. Serial2.println("\r");
  544. Serial2.println("AT+GPS=1\r");
  545. delay(1000);
  546. //ss.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  547. Serial2.println("AT+GPSRD=10\r");
  548. delay(100);
  549. // set SMS mode to text mode
  550. Serial2.println("AT+CMGF=1\r");
  551. delay(1000);
  552. //ss.println("AT+LOCATION=2\r");
  553. Serial.println(F("FullExample.ino"));
  554. Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
  555. Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  556. Serial.println(F("by Mikal Hart"));
  557. Serial.println();
  558. Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  559. Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  560. Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
  561. }
  562.  
  563. void readGPS_Data() {
  564. smartDelay(200);
  565. if (millis() > 5000 && gps.charsProcessed() < 10)
  566. Serial.println(F("No GPS data received: check wiring"));
  567. }
  568.  
  569. void showLATLON() {
  570. Serial.print("Latitude (deg): ");
  571. Lat = String(gps.location.lat(), 6);
  572. Serial.println(Lat);
  573. Serial.print("Longitude (deg): ");
  574. Lng = String(gps.location.lng(), 6);
  575. Serial.println(Lng);
  576. Serial.println();
  577. }
  578.  
  579.  
  580. void fullGPSSerial() {
  581. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  582. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  583. printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  584. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  585. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  586. printInt(gps.location.age(), gps.location.isValid(), 5);
  587. printDateTime(gps.date, gps.time);
  588. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  589. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  590. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  591. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  592.  
  593. unsigned long distanceKmToLondon =
  594. (unsigned long)TinyGPSPlus::distanceBetween(
  595. gps.location.lat(),
  596. gps.location.lng(),
  597. LONDON_LAT,
  598. LONDON_LON) / 1000;
  599. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  600.  
  601. double courseToLondon =
  602. TinyGPSPlus::courseTo(
  603. gps.location.lat(),
  604. gps.location.lng(),
  605. LONDON_LAT,
  606. LONDON_LON);
  607.  
  608. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  609.  
  610. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  611.  
  612. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  613.  
  614. printInt(gps.charsProcessed(), true, 6);
  615. printInt(gps.sentencesWithFix(), true, 10);
  616. printInt(gps.failedChecksum(), true, 9);
  617. Serial.println();
  618.  
  619. smartDelay(100);
  620. if (gps.charsProcessed() > 63) {
  621. }
  622. if (millis() > 5000 && gps.charsProcessed() < 10)
  623. Serial.println(F("No GPS data received: check wiring"));
  624. }
  625.  
  626.  
  627. // This custom version of delay() ensures that the gps object
  628. // is being "fed".
  629. static void smartDelay(unsigned long ms)
  630. {
  631. unsigned long start = millis();
  632. do
  633. {
  634. while (Serial2.available())
  635. gps.encode(Serial2.read());
  636. } while (millis() - start < ms);
  637. }
  638.  
  639. static void printFloat(float val, bool valid, int len, int prec)
  640. {
  641. if (!valid)
  642. {
  643. while (len-- > 1) {}
  644. //Serial.print('*');
  645. //Serial.print(' ');
  646. }
  647. else
  648. {
  649. //Serial.print(val, prec);
  650. int vi = abs((int)val);
  651. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  652. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  653. for (int i = flen; i < len; ++i) {}
  654. //Serial.print(' ');
  655. }
  656. smartDelay(0);
  657. }
  658.  
  659. static void printInt(unsigned long val, bool valid, int len)
  660. {
  661. char sz[32] = "*****************";
  662. if (valid)
  663. sprintf(sz, "%ld", val);
  664. sz[len] = 0;
  665. for (int i = strlen(sz); i < len; ++i)
  666. sz[i] = ' ';
  667. if (len > 0)
  668. sz[len - 1] = ' ';
  669. //Serial.print(sz);
  670. smartDelay(0);
  671. }
  672.  
  673. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  674. {
  675. if (!d.isValid())
  676. {
  677. //Serial.print(F("********** "));
  678. }
  679. else
  680. {
  681. char sz[32];
  682. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  683. //Serial.print(sz);
  684. }
  685.  
  686. if (!t.isValid())
  687. {
  688. //Serial.print(F("******** "));
  689. }
  690. else
  691. {
  692. char sz[32];
  693. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  694. //Serial.print(sz);
  695. }
  696.  
  697. printInt(d.age(), d.isValid(), 5);
  698. smartDelay(0);
  699. }
  700.  
  701. static void printStr(const char *str, int len)
  702. {
  703. int slen = strlen(str);
  704. for (int i = 0; i < len; ++i)
  705. //Serial.print(i < slen ? str[i] : ' ');
  706. smartDelay(0);
  707. }
  708.  
  709.  
  710.  
Advertisement
Add Comment
Please, Sign In to add comment