├── .gitattributes ├── .DS_Store └── Code └── Code.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/Multipurpose-IoT-Tracker-/HEAD/.DS_Store -------------------------------------------------------------------------------- /Code/Code.ino: -------------------------------------------------------------------------------- 1 | #include "WiFi.h" 2 | #define DEBUG true 3 | 4 | 5 | //******************* Pin Configurations *******************// 6 | 7 | #define A9G_PON D10 8 | #define A9G_LOWP D2 9 | #define SOS_Button D3 10 | 11 | 12 | //******************* Necessary Variables *******************// 13 | boolean stringComplete = false; 14 | String inputString = ""; 15 | String fromGSM = ""; 16 | bool CALL_END = 1; 17 | char* response = " "; 18 | String res = ""; 19 | int c = 0; 20 | String msg; 21 | String custom_message; 22 | 23 | //******************* SIM Paramaters *******************// 24 | 25 | 26 | String SOS_NUM = "+91xxxxxxxxxx"; 27 | 28 | 29 | 30 | //******************* SOS Button Press *******************// 31 | int SOS_Time = 5; // Press the button 5 sec 32 | 33 | void A9G_Ready_msg(); 34 | 35 | void setup() 36 | { 37 | Serial.begin(115200); 38 | Serial1.begin(115200, SERIAL_8N1, D0, D1); 39 | 40 | pinMode(A9G_PON, OUTPUT);//LOW LEVEL ACTIVE 41 | // pinMode(A9G_RST, OUTPUT);//HIGH LEVEL ACTIVE 42 | pinMode(A9G_LOWP, OUTPUT);//LOW LEVEL ACTIVE 43 | 44 | // digitalWrite(A9G_RST, LOW); 45 | digitalWrite(A9G_LOWP, HIGH); 46 | digitalWrite(A9G_PON, HIGH); 47 | delay(1000); 48 | digitalWrite(A9G_PON, LOW); 49 | delay(10000); 50 | 51 | // Making Radio OFF for power saving 52 | WiFi.mode(WIFI_OFF); // WiFi OFF 53 | btStop(); // Bluetooth OFF 54 | 55 | pinMode(SOS_Button, INPUT_PULLUP); 56 | pinMode(A9G_LOWP, OUTPUT); 57 | 58 | // Waiting for A9G to setup everything for 20 sec 59 | delay(20000); 60 | 61 | 62 | digitalWrite(A9G_LOWP, LOW); // Sleep Mode OFF 63 | 64 | // Just Checking 65 | msg = ""; 66 | msg = sendData("AT", 1000, DEBUG); 67 | while ( msg.indexOf("OK") == -1 ) { 68 | msg = sendData("AT", 1000, DEBUG); 69 | Serial.println("Trying"); 70 | } 71 | 72 | 73 | // Turning ON GPS 74 | msg = ""; 75 | msg = sendData("AT+GPS=1", 2000, DEBUG); 76 | while ( msg.indexOf("OK") == -1 ) { 77 | msg = sendData("AT+GPS=1", 1000, DEBUG); 78 | Serial.println("Trying"); 79 | } 80 | 81 | // GPS low power 82 | msg = ""; 83 | msg = sendData("AT+GPSLP = 2", 2000, DEBUG); 84 | while ( msg.indexOf("OK") == -1 ) { 85 | msg = sendData("AT+GPSLP = 2", 1000, DEBUG); 86 | Serial.println("Trying"); 87 | } 88 | 89 | // Configuring Sleep Mode to 1 90 | msg = ""; 91 | msg = sendData("AT+SLEEP = 1", 2000, DEBUG); 92 | while ( msg.indexOf("OK") == -1 ) { 93 | msg = sendData("AT+SLEEP = 1", 1000, DEBUG); 94 | Serial.println("Trying"); 95 | } 96 | 97 | // For SMS 98 | msg = ""; 99 | msg = sendData("AT+CMGF = 1", 2000, DEBUG); 100 | while ( msg.indexOf("OK") == -1 ) { 101 | msg = sendData("AT+CMGF = 1", 1000, DEBUG); 102 | Serial.println("Trying"); 103 | } 104 | 105 | msg = ""; 106 | msg = sendData("AT+CSMP = 17,167,0,0 ", 2000, DEBUG); 107 | while ( msg.indexOf("OK") == -1 ) { 108 | msg = sendData("AT+CSMP = 17,167,0,0 ", 1000, DEBUG); 109 | Serial.println("Trying"); 110 | } 111 | 112 | msg = ""; 113 | msg = sendData("AT+CPMS = \"SM\",\"ME\",\"SM\" ", 2000, DEBUG); 114 | while ( msg.indexOf("OK") == -1 ) { 115 | msg = sendData("AT+CPMS = \"SM\",\"ME\",\"SM\" ", 1000, DEBUG); 116 | Serial.println("Trying"); 117 | } 118 | 119 | 120 | // For Speaker 121 | msg = ""; 122 | msg = sendData("AT+SNFS=2", 2000, DEBUG); 123 | while ( msg.indexOf("OK") == -1 ) { 124 | msg = sendData("AT+SNFS=2", 1000, DEBUG); 125 | Serial.println("Trying"); 126 | } 127 | 128 | msg = ""; 129 | msg = sendData("AT+CLVL=8", 2000, DEBUG); 130 | while ( msg.indexOf("OK") == -1 ) { 131 | msg = sendData("AT+CLVL=8", 1000, DEBUG); 132 | Serial.println("Trying"); 133 | } 134 | 135 | A9G_Ready_msg(); // Sending Ready Msg to SOS Number 136 | 137 | digitalWrite(A9G_LOWP, HIGH); // Sleep Mode ON 138 | } 139 | 140 | void loop() 141 | { 142 | 143 | { 144 | //listen from GSM Module 145 | if (Serial1.available()) 146 | { 147 | char inChar = Serial1.read(); 148 | 149 | if (inChar == '\n') { 150 | 151 | //check the state 152 | if (fromGSM == "SEND LOCATION\r" || fromGSM == "send location\r" || fromGSM == "Send Location\r") 153 | { 154 | Get_gmap_link(0); // Send Location without Call 155 | digitalWrite(A9G_LOWP, HIGH);// Sleep Mode ON 156 | 157 | } 158 | 159 | //check the state 160 | else if (fromGSM == "BATTERY?\r" || fromGSM == "battery?\r" || fromGSM == "Battery?\r") 161 | { 162 | digitalWrite(A9G_LOWP, LOW); // Sleep Mode OFF 163 | Serial.println("---------Battery Status-------"); 164 | msg = ""; 165 | msg = sendData("AT+CBC?", 2000, DEBUG); 166 | while ( msg.indexOf("OK") == -1 ) { 167 | msg = sendData("AT+CBC?", 1000, DEBUG); 168 | Serial.println("Trying"); 169 | } 170 | 171 | msg = msg.substring(19, 24); 172 | response = &msg[0]; 173 | 174 | Serial.print("Recevied Data - "); Serial.println(response); // printin the String in lower character form 175 | Serial.println("\n"); 176 | 177 | 178 | custom_message = response; 179 | Send_SMS(custom_message); 180 | 181 | 182 | 183 | } 184 | 185 | // For Auto Call Recieve 186 | else if (fromGSM == "RING\r") 187 | { 188 | digitalWrite(A9G_LOWP, LOW); // Sleep Mode OFF 189 | Serial.println("---------ITS RINGING-------"); 190 | Serial1.println("ATA"); 191 | } 192 | 193 | else if (fromGSM == "NO CARRIER\r") 194 | { 195 | Serial.println("---------CALL ENDS-------"); 196 | CALL_END = 1; 197 | digitalWrite(A9G_LOWP, HIGH);// Sleep Mode ON 198 | } 199 | 200 | //write the actual response 201 | Serial.println(fromGSM); 202 | //clear the buffer 203 | fromGSM = ""; 204 | 205 | } 206 | else 207 | { 208 | fromGSM += inChar; 209 | } 210 | delay(20); 211 | } 212 | 213 | // read from port 0, send to port 1: 214 | if (Serial.available()) { 215 | int inByte = Serial.read(); 216 | Serial1.write(inByte); 217 | } 218 | 219 | // When SOS button is pressed 220 | if (digitalRead(SOS_Button) == LOW && CALL_END == 1) 221 | { 222 | Serial.print("Calling In.."); // Waiting for 5 sec 223 | for (c = 0; c < SOS_Time; c++) 224 | { 225 | Serial.println((SOS_Time - c)); 226 | delay(1000); 227 | if (digitalRead(SOS_Button) == HIGH) 228 | break; 229 | } 230 | 231 | if (c == 5) 232 | { 233 | Get_gmap_link(1); // Send Location with Call 234 | } 235 | 236 | //only write a full message to the GSM module 237 | if (stringComplete) 238 | { 239 | Serial1.print(inputString); 240 | inputString = ""; 241 | stringComplete = false; 242 | } 243 | 244 | } 245 | } 246 | } 247 | 248 | 249 | //--------------------------------------------- Getting Location and making Google Maps link of it. Also making call if needed 250 | void Get_gmap_link(bool makeCall) 251 | { 252 | 253 | 254 | digitalWrite(A9G_LOWP, LOW); 255 | delay(1000); 256 | Serial1.println("AT+LOCATION = 2"); 257 | Serial.println("AT+LOCATION = 2"); 258 | 259 | while (!Serial1.available()); 260 | while (Serial1.available()) 261 | { 262 | char add = Serial1.read(); 263 | res = res + add; 264 | delay(1); 265 | } 266 | 267 | res = res.substring(17, 38); 268 | response = &res[0]; 269 | 270 | Serial.print("Recevied Data - "); Serial.println(response); // printin the String in lower character form 271 | Serial.println("\n"); 272 | 273 | if (strstr(response, "GPS NOT")) 274 | { 275 | Serial.println("No Location data"); 276 | //------------------------------------- Sending SMS without any location 277 | custom_message = "Unable to fetch location. Please try again"; 278 | Send_SMS(custom_message); 279 | } 280 | else 281 | { 282 | 283 | int i = 0; 284 | while (response[i] != ',') 285 | i++; 286 | 287 | String location = (String)response; 288 | String lat = location.substring(2, i); 289 | String longi = location.substring(i + 1); 290 | Serial.println(lat); 291 | Serial.println(longi); 292 | 293 | String Gmaps_link = "I'm Here " + ( "http://maps.google.com/maps?q=" + lat + "+" + longi); //http://maps.google.com/maps?q=38.9419+-78.3020 294 | //------------------------------------- Sending SMS with Google Maps Link with our Location 295 | 296 | 297 | custom_message = Gmaps_link; 298 | Send_SMS(custom_message); 299 | 300 | } 301 | response = ""; 302 | res = ""; 303 | if (makeCall) 304 | { 305 | Serial.println("Calling Now"); 306 | Serial1.println("ATD" + SOS_NUM); 307 | CALL_END = 0; 308 | } 309 | } 310 | 311 | void A9G_Ready_msg() 312 | { 313 | 314 | custom_message = "A9G Ready!!"; 315 | Send_SMS(custom_message); 316 | 317 | } 318 | 319 | String sendData(String command, const int timeout, boolean debug) 320 | { 321 | String temp = ""; 322 | Serial1.println(command); 323 | long int time = millis(); 324 | while ( (time + timeout) > millis()) 325 | { 326 | while (Serial1.available()) 327 | { 328 | char c = Serial1.read(); 329 | temp += c; 330 | } 331 | } 332 | if (debug) 333 | { 334 | Serial.print(temp); 335 | } 336 | return temp; 337 | } 338 | 339 | 340 | void Send_SMS(String message) 341 | { 342 | //for (int i = 0; i < Total_Numbers; i++) 343 | { 344 | Serial1.println("AT+CMGF=1"); 345 | delay(1000); 346 | Serial1.println("AT+CMGS=\"" + SOS_NUM + "\"\r"); 347 | delay(1000); 348 | 349 | Serial1.println (message); 350 | delay(1000); 351 | Serial1.println((char)26); 352 | delay(1000); 353 | 354 | Serial1.println("AT+CMGD=1,4"); // delete stored SMS to save memory 355 | delay(3000); 356 | } 357 | } 358 | --------------------------------------------------------------------------------