Advertisement
safwan092

Untitled

Sep 7th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. //----------------------------------------------------------------
  2. // Include Dependencies and Libraries
  3. //----------------------------------------------------------------
  4. #include <TinyGPSPlus.h>
  5. #include <SoftwareSerial.h>
  6. //----------------------------------------------------------------
  7.  
  8. //**************************************************************************
  9.  
  10. //----------------------------------------------------------------
  11. // Define Connections
  12. //----------------------------------------------------------------
  13. #define buttonPin 14 //D5 = GPIO14 (ESP8266 - NodeMCU)
  14. //#define RXPin 4 //D2 = GPIO4 (ESP8266 - NodeMCU)
  15. //#define TXPin 5 //D1 = GPIO5 (ESP8266 - NodeMCU)
  16. static const int RXPin = 4, TXPin = 5;
  17. //----------------------------------------------------------------
  18.  
  19. //**************************************************************************
  20.  
  21. //----------------------------------------------------------------
  22. // Define Global Variables
  23. //----------------------------------------------------------------
  24. String emergencyContactNumber = "+966538060336";
  25. String s = "www.google.com/maps/dir/";
  26. String LAT, LON, sms;
  27. char charBuf[450];
  28. //----------------------------------------------------------------
  29.  
  30. //**************************************************************************
  31.  
  32. //----------------------------------------------------------------
  33. // Define Global Objects
  34. //----------------------------------------------------------------
  35. // The TinyGPSPlus object
  36. TinyGPSPlus gps;
  37.  
  38. // The serial connection to the GPS device
  39. SoftwareSerial mySerial(RXPin, TXPin);
  40. //----------------------------------------------------------------
  41.  
  42. //**************************************************************************
  43.  
  44. //----------------------------------------------------------------
  45. // Main Setup Function
  46. //----------------------------------------------------------------
  47. void setup() {
  48. Serial.begin(9600);
  49. initialize_panic_button();
  50. initialize_A9G_GSM_GPS();
  51. }
  52. //----------------------------------------------------------------
  53.  
  54. //**************************************************************************
  55.  
  56. //----------------------------------------------------------------
  57. // Main Loop Function
  58. //----------------------------------------------------------------
  59. void loop() {
  60.  
  61. readData_from_A9G_Modem_SMS_and_GPS(5000);
  62.  
  63. }//end of loop
  64. //----------------------------------------------------------------
  65.  
  66. //**************************************************************************
  67.  
  68. //----------------------------------------------------------------
  69. // Functions
  70. //----------------------------------------------------------------
  71. //________________________________________________________________
  72.  
  73. // Function to Send SMS to Emergancy Contact if Button is Pressed
  74.  
  75. void ICACHE_RAM_ATTR isr() {
  76. send_gps_data_via_SMS(emergencyContactNumber);
  77. }
  78.  
  79. //________________________________________________________________
  80.  
  81. // Function to Setup A9G Modem To Read GPS Data and Receive SMS
  82.  
  83. void initialize_A9G_GSM_GPS() {
  84. mySerial.begin(9600);
  85. delay(1000);
  86. mySerial.println("\r");
  87. mySerial.println("AT\r");
  88. delay(10);
  89. mySerial.println("\r");
  90. mySerial.println("AT+GPS=1\r");
  91. delay(100);
  92. mySerial.println("AT+CREG=2\r");
  93. delay(6000);
  94. mySerial.println("AT+CGATT=1\r");
  95. delay(6000);
  96. mySerial.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  97. delay(6000);
  98. mySerial.println("AT+CGACT=1,1\r");
  99. delay(6000);
  100. //Initialize ends
  101. //Initialize GPS
  102. mySerial.println("\r");
  103. mySerial.println("AT+GPS=1\r");
  104. delay(1000);
  105. mySerial.println("AT+GPSRD=10\r");
  106. delay(100);
  107. // set SMS mode to text mode
  108. mySerial.println("AT+CMGF=1\r");
  109. delay(1000);
  110. }
  111.  
  112. //________________________________________________________________
  113.  
  114. // Function to Connect an Interrupt for the Panic Button
  115.  
  116. void initialize_panic_button() {
  117. pinMode(buttonPin, INPUT);
  118. attachInterrupt(D5, isr, RISING);
  119. }
  120.  
  121. //________________________________________________________________
  122.  
  123. // Function to Send SMS with Location Data
  124.  
  125. void send_gps_data_via_SMS(String NUMBER) {
  126.  
  127. if (gps.location.lat() == 0 || gps.location.lng() == 0)
  128. {
  129. Serial.println("Return Executed - GPS Satellite [No Signal]");
  130. // LAT, LON
  131. // 21.330791, 39.945985
  132. LAT = "21.330791";
  133. LON = "39.945985";
  134. //return;
  135. }
  136.  
  137. Serial.print(LAT);
  138. Serial.print(" , ");
  139. Serial.println(LON);
  140. Serial.println();
  141. Serial.println();
  142. Serial.println();
  143. Serial.println();
  144. Serial.println();
  145. s += LAT;
  146. s += ",";
  147. s += LON;
  148. s += "/";
  149.  
  150. //s = "new_new_24-08-2023";
  151. Serial.println(s);
  152.  
  153. Serial.println("Sending Message");
  154.  
  155. mySerial.println("AT+CMGF=1\r");
  156. delay(1000);
  157.  
  158. mySerial.println("AT+CNMI=2,2,0,0,0\r");
  159. delay(1000);
  160.  
  161. mySerial.print("AT+CMGS=\"");
  162. mySerial.print(NUMBER);
  163. mySerial.print("\"\r");
  164. delay(1000);
  165. mySerial.print(s);
  166. mySerial.write(0x1A);
  167. delay(1000);
  168. s = "www.google.com/maps/dir/";
  169.  
  170. }
  171.  
  172. //________________________________________________________________
  173.  
  174. // Function to read Incoming SMS messages and Read GPS Data
  175.  
  176. void readData_from_A9G_Modem_SMS_and_GPS(unsigned long T) {
  177. unsigned long Start = millis();
  178. do
  179. {
  180. while (mySerial.available())
  181. sms = mySerial.readString();
  182. Serial.println(sms);
  183.  
  184. readGPS_Data_and_Show_on_Serial_Monitor();
  185. readSMS_if_SMS_Requested_Send_Location_To_Sender();
  186.  
  187. } while (millis() - Start < T);
  188. }
  189.  
  190. //________________________________________________________________
  191.  
  192. // Function to Read GPS data and Save values as (Latitude,Longitude)
  193.  
  194. void readGPS_Data_and_Show_on_Serial_Monitor() {
  195. sms.toCharArray(charBuf, 450);
  196. for (int i = 0; i < 450; i++) {
  197. gps.encode(charBuf[i]);
  198. }
  199. LAT = String(gps.location.lat(), 6);
  200. LON = String(gps.location.lng(), 6);
  201. Serial.print(LAT);
  202. Serial.print(" , ");
  203. Serial.println(LON);
  204. }
  205.  
  206. //________________________________________________________________
  207.  
  208. // Function to Respond with Location SMS if A9G modem receives an SMS
  209.  
  210. void readSMS_if_SMS_Requested_Send_Location_To_Sender() {
  211. if (sms.indexOf("+CMT:") != -1) {
  212.  
  213. // Get the sender's phone number
  214. String sender = sms.substring(sms.indexOf("+966"), sms.indexOf("\",,\"20"));
  215. Serial.println("Sender Number: ");
  216. Serial.println(sender);//e.g. "+966554433221"
  217.  
  218. Serial.println("|||||||||||||||||||||||||||||||||||||||||||||||||||");
  219. Serial.println("GPS SMS Detected");
  220. Serial.println("|||||||||||||||||||||||||||||||||||||||||||||||||||");
  221. send_gps_data_via_SMS(sender);
  222. delay(5000);
  223. }
  224. }
  225.  
  226. //________________________________________________________________
  227.  
  228. //----------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement