Quang Anh Le / GPS-Tracking-Velo

Dependencies:   MODSERIAL SDBlockDevice GPS

Revision:
3:eb739df911ef
Parent:
2:3d3d272d2df2
Child:
4:793221f865f6
diff -r 3d3d272d2df2 -r eb739df911ef mbed-os-example-fat-filesystem-master/main.cpp
--- a/mbed-os-example-fat-filesystem-master/main.cpp	Tue Apr 16 21:53:13 2019 +0000
+++ b/mbed-os-example-fat-filesystem-master/main.cpp	Thu Apr 18 15:17:23 2019 +0000
@@ -17,6 +17,14 @@
 Serial SIM900(PA_9,PA_10);                // (TX,RX)                                                   
 GPS gps (PA_11,PA_12);
 
+const string GoogleChunk  = "https://maps.google.com.au/maps?q=";   // URL constant
+const string GoogleExtras = "&z=20";                                // Zoom Level
+char  GPRSbuffer[512];
+char  NUMBER[14];
+string MESSAGE;
+int index;
+int i=0;
+
 FATFileSystem fs("fs");
 string result;
 char x;
@@ -62,19 +70,120 @@
 SIM900.putc('"');
 SIM900.printf("\r");
 wait_ms(1000);
-SIM900.printf("Latitude: %0.6f, Longitude: %0.6f,Altitude: %0.3f",gps.latitude,gps.longitude,gps.altitude);
+//SIM900.printf("Latitude: %0.6f, Longitude: %0.6f,Altitude: %0.3f",gps.latitude,gps.longitude,gps.altitude);
+SIM900.printf("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras);
 wait_ms(1000);
 SIM900.putc(0x1A);
-wait_ms(5000);
+wait_ms(30000);
+    //clearString();
+    // Set message mode to ASCII
+    /*SIM900.printf("AT+CMGF=1\r\n");
+    wait(1);
+    //clearString();
+    // Set the phone number
+    SIM900.printf("AT+CMGS=\"%s\"\r\n", +4917645651571);
+    wait(1);
+    // Write out the GPS data in a message
+    SIM900.printf("Latitude: %0.6f, Longitude: %0.6f,Altitude: %0.3f",gps.latitude,gps.longitude,gps.altitude);
+    wait(1);
+    // Send it...
+    SIM900.putc(0x1A);
+    wait(4);
+    SIM900.printf("AT+CMGF=1\r\n");
+    wait(1);
+    SIM900.printf("AT+CMGS=\"%s\"\r\n", +4917645651571);
+    wait(1);
+    SIM900.printf("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras);
+    wait(1);
+    SIM900.putc(0x1A);
+    wait(1);*/
+}
+
+void parseSMS()
+{
+    while(SIM900.readable())
+    {
+        // Assign it to 'c'
+        char c = SIM900.getc();
+        // Replace all returns and or line endings with money
+        if(c == '\r' || c == '\n') c= '$';
+        // Put it in the array
+        GPRSbuffer[i] =c;
+        // Repeat if possible
+        i++;
+        
+    }
+    // Uncomment the following to debug
+    // pc.printf("\nbuffer = %s", GPRSbuffer);
+    
+    // If we get an SMS notification
+    if (sscanf(GPRSbuffer,"$$+CMTI: \"SM\",%d", &index)>0)
+    {
+        pc.printf("\nSMS recieved @ index [%d]", index);
+        memset(GPRSbuffer, '0', 511);
+        i=0;
+        pc.printf("\nOpening message...");
+        // ask GPRS to read the message
+        SIM900.printf("AT+CMGR=%d\r\n", index);
+    }
+    if (strncmp(GPRSbuffer, "$$+CMGR",7) == 0 ) // compared return value = 0 then it indicates str1 is equal to str2 
+    {
+        // Get the number out
+        char *n = strstr(GPRSbuffer,"+49");     // finds the first occurrence of the substring "+49" in the string GPRSbuffer +4917645651571 
+        strncpy(NUMBER, n, 14);                 // this funtion returns the final copy of the copied string
+        char * pch;             
+        pch = strtok (GPRSbuffer, "$$");        // return a pointer to the first token found in the string. A null pointer is returned if there are no tokens left to retrieve
+        pch = strtok (NULL, "$$");
+        MESSAGE = pch;
+        pc.printf("\nDone! ");
+        // Send the location
+        sendSMS();
+        // Reset the GPRS buffer
+        memset(GPRSbuffer, '0', 511);
+        // Reset the char counter
+        i = 0;    
+    }
+     if (strncmp(GPRSbuffer, "$$+CMGS",7) == 0) {
+        // Reset the GPRS buffer
+        memset(GPRSbuffer, '0', 511);
+        // Reset the char counter
+        i = 0;
+    }
+    if (strncmp(GPRSbuffer, "$$RING",6) == 0) {
+        SIM900.printf("ATH0\r\n");
+        pc.printf("\nCall bounced!...");
+        // Do the send SMS routine...
+        sendSMS();
+        // Reset the GPRS buffer
+        memset(GPRSbuffer, '0', 511);
+        // Reset the char counter
+        i = 0;
+    }
+    pc.printf("\n\n\nWaiting for SMS or call...\n");
+    pc.printf("\nThe last number was : %s", NUMBER);
+    pc.printf("\nThe last message was : %s", MESSAGE);
+    // Reset the GPRS buffer
+    memset(GPRSbuffer, '0', 511);
+    // Reset the char counter
+    i = 0;
 }
  
 int main() {
   /**/
+  memset(GPRSbuffer, '0', 511);
   pc.printf("\r\n GSM 900 Test\n");
-  SIM900.attach(&callback_rx);
+  //SIM900.attach(&callback_rx);
   SIM900.baud(9600);
   wait_ms(100);
-  sendSMS();
+  SIM900.printf("ATE0\r\n");
+  pc.printf("\nGPRS echo [OFF]\n");
+    wait(1);
+    // Delete all messages on the sim card
+  SIM900.printf("AT+CMGDA=\"DEL ALL\"\r\n");
+    wait(1);
+    pc.printf("\nMessages Cleared...\n");
+    wait(1);
+  //sendSMS();
   wait_ms(100);
   /**/
   int error = 0;
@@ -172,7 +281,10 @@
           //pc.printf("%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d;%d,",id1,id2,id3,id4,id5,id6,id7,id8,id9,id10,id11,id12);
           //pc.printf("%d,%d,%.2f,%.2f\r\n",fix,fix_3d,pdop,hdop);
           //pc.printf("%d",date);
-          pc.printf("Latitude: %0.6f\r\n",latitude);
+          
+          
+          
+          /*pc.printf("Latitude: %0.6f\r\n",latitude);
           pc.printf("Longitude:%0.6f\r\n",longitude);
           pc.printf("Altitude: %0.3f\r\n",altitude);
           pc.printf("Speed in knots: %0.2f\r\n",speed);
@@ -184,7 +296,8 @@
           pc.printf("ID7:%d,ID8:%d,ID9:%d,ID10:%d,ID11:%d,ID12:%d\r\n",id7,id8,id9,id10,id11,id12);
           pc.printf("Fix quality: %d - value include 0=invalid; 1=GPS fix\r\n",fix);
           pc.printf("3D fix: %d - value include: 1 = no fix, 2 = 2D fix, 3 = 3D fix\r\n\n", fix_3d); 
-          pc.printf("****************************************************************\r\n");   
+          pc.printf("****************************************************************\r\n");*/ 
+          
           
           FILE* fd = fopen("/fs/numbers.txt", "a+");
           //errno_error(fd);
@@ -203,6 +316,9 @@
              //errno_error(fd);
           
           wait(1);
+          //parseSMS();
+          sendSMS();
+          wait(1);
         //for (int i = 0; i < 20; i++){
         // printf("Writing decimal numbers to a file (%d/20)\r", i);
         //fprintf(fd, "%d\r\n", i);