safwan092

مشروع نداء الاستغاثة A9G مع ESP32 XIAO

Nov 20th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include "WiFi.h"
  2.  
  3. #define SOS D3
  4. #define SLEEP_PIN D2 // Make this pin HIGH to make A9G board to go to sleep mode
  5.  
  6.  
  7. boolean stringComplete = false;
  8. String inputString = "";
  9. String fromGSM = "";
  10. int c = 0;
  11. String SOS_NUM = "+966554418546";
  12.  
  13. int SOS_Time = 5; // Press the button 5 sec
  14.  
  15. bool CALL_END = 1;
  16. char* response = " ";
  17. String res = "";
  18. void setup()
  19. {
  20.  
  21. // Making Radio OFF for power saving
  22. WiFi.mode(WIFI_OFF); // WiFi OFF
  23. btStop(); // Bluetooth OFF
  24.  
  25. pinMode(SOS, INPUT_PULLUP);
  26.  
  27. pinMode(SLEEP_PIN, OUTPUT);
  28.  
  29. Serial.begin(115200); // For Serial Monitor
  30. Serial1.begin(115200, SERIAL_8N1, D0, D1); // For XIAO C3 Board
  31.  
  32. // Waiting for A9G to setup everything for 20 sec
  33. delay(20000);
  34.  
  35.  
  36. digitalWrite(SLEEP_PIN, LOW); // Sleep Mode OFF
  37.  
  38. Serial1.println("AT"); // Just Checking
  39. delay(1000);
  40.  
  41. Serial1.println("AT+GPS = 1"); // Turning ON GPS
  42. delay(1000);
  43.  
  44. Serial1.println("AT+GPSLP = 2"); // GPS low power
  45. delay(1000);
  46.  
  47. Serial1.println("AT+SLEEP = 1"); // Configuring Sleep Mode to 1
  48. delay(1000);
  49.  
  50. digitalWrite(SLEEP_PIN, HIGH); // Sleep Mode ON
  51.  
  52.  
  53. }
  54.  
  55. void loop()
  56. {
  57. //listen from GSM Module
  58. if (Serial1.available())
  59. {
  60. char inChar = Serial1.read();
  61.  
  62. if (inChar == '\n') {
  63.  
  64. //check the state
  65. if (fromGSM == "OK\r") {
  66. Serial.println("---------IT WORKS-------");
  67. }
  68. else if (fromGSM == "RING\r") {
  69. digitalWrite(SLEEP_PIN, LOW); // Sleep Mode OFF
  70. Serial.println("---------ITS RINGING-------");
  71. Serial1.println("ATA");
  72. }
  73. else if (fromGSM == "ERROR\r") {
  74. Serial.println("---------IT DOESNT WORK-------");
  75. }
  76.  
  77. else if (fromGSM == "NO CARRIER\r") {
  78. Serial.println("---------CALL ENDS-------");
  79. CALL_END = 1;
  80. digitalWrite(SLEEP_PIN, HIGH);// Sleep Mode ON
  81. }
  82.  
  83. //write the actual response
  84. Serial.println(fromGSM);
  85. //clear the buffer
  86. fromGSM = "";
  87.  
  88. } else {
  89. fromGSM += inChar;
  90. }
  91. delay(20);
  92. }
  93.  
  94. // read from port 0, send to port 1:
  95. if (Serial.available()) {
  96. int inByte = Serial.read();
  97. Serial1.write(inByte);
  98. }
  99.  
  100. // When SOS button is pressed
  101. if (digitalRead(SOS) == LOW && CALL_END == 1)
  102. {
  103. Serial.print("Calling In.."); // Waiting for 5 sec
  104. for (c = 0; c < SOS_Time; c++)
  105. {
  106. Serial.println((SOS_Time - c));
  107. delay(1000);
  108. if (digitalRead(SOS) == HIGH)
  109. break;
  110. }
  111. if (c == 5)
  112. {
  113. //------------------------------------- Getting Location and making Google Maps link of it
  114.  
  115. digitalWrite(SLEEP_PIN, LOW);
  116. delay(1000);
  117. Serial1.println("AT+LOCATION = 2");
  118. Serial.println("AT+LOCATION = 2");
  119.  
  120. while (!Serial1.available());
  121. while (Serial1.available())
  122. {
  123. char add = Serial1.read();
  124. res = res + add;
  125. delay(1);
  126. }
  127.  
  128. res = res.substring(17, 38);
  129. response = &res[0];
  130.  
  131. Serial.print("Recevied Data - "); Serial.println(response); // printin the String in lower character form
  132. Serial.println("\n");
  133.  
  134. if (strstr(response, "GPS NOT"))
  135. {
  136. Serial.println("No Location data");
  137. }
  138. else
  139. {
  140.  
  141. int i = 0;
  142. while (response[i] != ',')
  143. i++;
  144.  
  145. String location = (String)response;
  146. String lat = location.substring(2, i);
  147. String longi = location.substring(i + 1);
  148. Serial.println(lat);
  149. Serial.println(longi);
  150.  
  151. String Gmaps_link = ( "http://maps.google.com/maps?q=" + lat + "+" + longi); //http://maps.google.com/maps?q=38.9419+-78.3020
  152.  
  153.  
  154. //------------------------------------- Sending SMS with Google Maps Link with our Location
  155. Serial1.println("AT+CMGF=1");
  156. delay(1000);
  157. Serial1.println("AT+CMGS=\"" + SOS_NUM + "\"\r");
  158. delay(1000);
  159.  
  160. Serial1.println ("I'm here " + Gmaps_link);
  161. delay(1000);
  162. Serial1.println((char)26);
  163. delay(1000);
  164. }
  165. response = "";
  166. res = "";
  167.  
  168. //------------------------------------- Calling on that same number after sending SMS
  169. Serial.println("Calling Now");
  170. Serial1.println("ATD" + SOS_NUM);
  171. CALL_END = 0;
  172. }
  173. }
  174.  
  175. //only write a full message to the GSM module
  176. if (stringComplete) {
  177. Serial1.print(inputString);
  178. inputString = "";
  179. stringComplete = false;
  180. }
  181.  
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment