safwan092

Project_15054

Nov 23rd, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. int state = 0;
  5. int counter = 0;
  6. static const int button_pin = 4;
  7. static const int RXPin = 6, TXPin = 5;
  8. static const uint32_t GPSBaud = 9600;
  9.  
  10. String stringMessage = "";
  11. String lonGPS = "";
  12. String latGPS = "";
  13. char Buf[70];
  14.  
  15. TinyGPSPlus gps;
  16. SoftwareSerial ss(RXPin, TXPin);
  17.  
  18. void setup() {
  19. Serial.begin(9600);
  20. Serial.println("Starting...");
  21. pinMode(button_pin, INPUT);
  22. a9g_init();
  23. Serial.println("Setup Executed");
  24. }
  25.  
  26. void loop() {
  27. smartDelay(2000);//read GPS Data
  28. Serial.println(gps.location.lat(),6);
  29. state = digitalRead(button_pin);
  30. Serial.println(state);
  31. if (state == 0) {
  32. counter++;
  33. delay(500);
  34. if (counter > 5) {
  35. a9g_loop();//read GPS data and Send SMS
  36. counter = 0;
  37. }
  38. }
  39. else {
  40. counter = 0;
  41. }
  42. }
  43.  
  44.  
  45. static void smartDelay(unsigned long ms) {
  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. if (gps.location.lat() == 0 || gps.location.lng() == 0) {
  56. Serial.println("Return Executed");
  57. //21.913677,39.263130
  58. ///////////////////////////////////////////////////////////////////////////////////////
  59. Serial.println("Sending Message");
  60. ss.println("AT+CMGF=1\r");
  61. delay(1000);
  62. ss.println("AT+CNMI=2,2,0,0,0\r");
  63. delay(1000);
  64. ss.print("AT+CMGS=\"+966544092008\"\r");//Replace this with your mobile number
  65. delay(1000);
  66. ss.print("[SOS] Please Help me I'm at this location: ");
  67. ss.print("http://www.google.com/maps/place/21.913677,39.263130");
  68. ss.write(0x1A);
  69. delay(1000);
  70. stringMessage = "";
  71. ///////////////////////////////////////////////////////////////////////////////////////
  72. return;
  73. }
  74. lonGPS = String(gps.location.lng(), 6);
  75. latGPS = String(gps.location.lat(), 6);
  76. Serial.print("Lat: ");
  77. Serial.print(latGPS);
  78. Serial.print(F(","));
  79. Serial.print("Lng: ");
  80. Serial.print(lonGPS);
  81. Serial.println();
  82. stringMessage = "http://www.google.com/maps/place/" + latGPS + "," + lonGPS;
  83. stringMessage.toCharArray(Buf, 70);
  84. ///////////////////////////////////////////////////////////////////////////////////////
  85. Serial.println("Sending Message");
  86. ss.println("AT+CMGF=1\r");
  87. delay(1000);
  88. ss.println("AT+CNMI=2,2,0,0,0\r");
  89. delay(1000);
  90. ss.print("AT+CMGS=\"+966544092008\"\r");//Replace this with your mobile number
  91. delay(1000);
  92. ss.print("[SOS] Please Help me I'm at this location: ");
  93. ss.print(Buf);
  94. ss.write(0x1A);
  95. delay(1000);
  96. stringMessage = "";
  97. ///////////////////////////////////////////////////////////////////////////////////////
  98.  
  99. }
  100.  
  101. void a9g_init() {
  102. ss.begin(GPSBaud);
  103. ss.println("\r");
  104. ss.println("AT\r");
  105. delay(10);
  106. ss.println("\r");
  107. ss.println("AT+GPS=1\r");
  108. delay(100);
  109. ss.println("AT+CREG=2\r");
  110. delay(6000);
  111. //ss.print("AT+CREG?\r");
  112. ss.println("AT+CGATT=1\r");
  113. delay(6000);
  114. ss.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  115. delay(6000);
  116. // ss.println("AT+LOCATION=1\r");
  117. ss.println("AT+CGACT=1,1\r");
  118. delay(6000);
  119. //Initialize ends
  120. //Initialize GPS
  121. ss.println("\r");
  122. ss.println("AT+GPS=1\r");
  123. delay(1000);
  124. //ss.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  125. ss.println("AT+GPSRD=10\r");
  126. delay(100);
  127. // set SMS mode to text mode
  128. ss.println("AT+CMGF=1\r");
  129. delay(1000);
  130. //ss.println("AT+LOCATION=2\r");
  131. }
  132.  
  133. void a9g_loop() {
  134. if (millis() > 5000 && gps.charsProcessed() < 10)
  135. Serial.println(F("No GPS data received: check wiring"));
  136. send_gps_data();//Send SMS with Location Data
  137. }
Advertisement
Add Comment
Please, Sign In to add comment