Advertisement
safwan092

A9G - Simple Code To Send SMS with Location Data

May 19th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include <SoftwareSerial.h>
  3.  
  4. static const int RXPin = 6, TXPin = 5;
  5. String s = "www.google.com/maps/dir/";
  6.  
  7. unsigned long interval = 10000;
  8. static const uint32_t GPSBaud = 9600;
  9. unsigned long previousMillis = 0;
  10. int data_counter;
  11.  
  12. const size_t BUFSIZE = 300;
  13. char f_buffer[BUFSIZE];
  14. float *f_buf = (float*)f_buffer;
  15.  
  16. TinyGPSPlus gps;// The TinyGPSPlus object
  17. SoftwareSerial ss(RXPin, TXPin);// The serial connection to the GPS device
  18.  
  19.  
  20. void setup()
  21. {
  22. Serial.begin(9600);
  23. ss.begin(GPSBaud);
  24.  
  25. Serial.println("Starting...");
  26. ss.println("\r");
  27. ss.println("AT\r");
  28. delay(10);
  29.  
  30. ss.println("\r");
  31. ss.println("AT+GPS=1\r");
  32.  
  33. delay(100);
  34. ss.println("AT+CREG=2\r");
  35. delay(6000);
  36.  
  37. //ss.print("AT+CREG?\r");
  38. ss.println("AT+CGATT=1\r");
  39. delay(6000);
  40.  
  41. ss.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  42. delay(6000);
  43.  
  44. // ss.println("AT+LOCATION=1\r");
  45. ss.println("AT+CGACT=1,1\r");
  46. delay(6000);
  47.  
  48. //Initialize ends
  49. //Initialize GPS
  50. ss.println("\r");
  51. ss.println("AT+GPS=1\r");
  52. delay(1000);
  53.  
  54. //ss.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  55. ss.println("AT+GPSRD=10\r");
  56. delay(100);
  57.  
  58. // set SMS mode to text mode
  59. ss.println("AT+CMGF=1\r");
  60. delay(1000);
  61.  
  62. //ss.println("AT+LOCATION=2\r");
  63.  
  64. Serial.println("Setup Executed");
  65. }
  66.  
  67. void loop() {
  68. // if (Serial.available()) { // If anything comes in Serial (USB),
  69. // ss.write(Serial.read()); // read it and send it out ss (pins 0 & 1)
  70. // }
  71. //
  72. // if (ss.available()) { // If anything comes in ss (pins 0 & 1)
  73. // Serial.write(ss.read()); // read it and send it out Serial (USB)
  74. // }
  75.  
  76. smartDelay(2000);
  77.  
  78. if (millis() > 5000 && gps.charsProcessed() < 10)
  79. Serial.println(F("No GPS data received: check wiring"));
  80.  
  81. unsigned long currentMillis = millis();
  82.  
  83. if ((unsigned long)(currentMillis - previousMillis) >= interval) {
  84.  
  85. send_gps_data();
  86. previousMillis = currentMillis;
  87. }
  88. }
  89.  
  90. static void smartDelay(unsigned long ms)
  91. {
  92. unsigned long start = millis();
  93. do
  94. {
  95. while (ss.available())
  96. gps.encode(ss.read());
  97. } while (millis() - start < ms);
  98. }
  99.  
  100. void send_gps_data()
  101. {
  102. if (gps.location.lat() == 0 || gps.location.lng() == 0)
  103. {
  104. Serial.println("Return Executed");
  105. return;
  106. }
  107.  
  108. data_counter++;
  109.  
  110. Serial.print("Latitude (deg): ");
  111. f_buf[data_counter] = gps.location.lat();
  112. Serial.println(f_buf[data_counter]);
  113.  
  114. Serial.print("Longitude (deg): ");
  115. f_buf[data_counter + 1] = gps.location.lng();
  116. Serial.println(f_buf[data_counter + 1]);
  117.  
  118. Serial.println(data_counter);
  119. Serial.println();
  120.  
  121. s += String(gps.location.lat(), 6);
  122. s += ",";
  123. s += String(gps.location.lng(), 6);
  124. s += "/";
  125.  
  126. Serial.println(s);
  127.  
  128. if (data_counter >= 10)
  129. {
  130. data_counter = 0;
  131.  
  132. Serial.println("Sending Message");
  133.  
  134. ss.println("AT+CMGF=1\r");
  135. delay(1000);
  136.  
  137. ss.println("AT+CNMI=2,2,0,0,0\r");
  138. delay(1000);
  139.  
  140. ss.print("AT+CMGS=\"+91xxxxxxxxxx\"\r");//Replace this with your mobile number
  141. delay(1000);
  142. ss.print(s);
  143. ss.write(0x1A);
  144. delay(1000);
  145. s = "www.google.com/maps/dir/";
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement