Quang Anh Le / GPS-Tracking-Velo

Dependencies:   MODSERIAL SDBlockDevice GPS

Files at this revision

API Documentation at this revision

Comitter:
QuangAnhLe
Date:
Tue Apr 30 10:08:59 2019 +0000
Parent:
3:eb739df911ef
Commit message:
first commit

Changed in this revision

GPS.lib Show annotated file Show diff for this revision Revisions of this file
MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os-example-fat-filesystem-master/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GPS.lib	Thu Apr 18 15:17:23 2019 +0000
+++ b/GPS.lib	Tue Apr 30 10:08:59 2019 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/fookies/code/GPS/#2b9befe69d07
+https://os.mbed.com/users/QuangAnhLe/code/GPS/#2b9befe69d07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Tue Apr 30 10:08:59 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Sissors/code/MODSERIAL/#da0788f0bd77
--- a/mbed-os-example-fat-filesystem-master/main.cpp	Thu Apr 18 15:17:23 2019 +0000
+++ b/mbed-os-example-fat-filesystem-master/main.cpp	Tue Apr 30 10:08:59 2019 +0000
@@ -1,29 +1,41 @@
 #include "mbed.h"
 #include "GPS.h"
+#include "MODSERIAL.h"
 #include <iostream>
 #include <sstream>
 #include <bitset>
 #include <string>
 #include "FATFileSystem.h"
-//#include "HeapBlockDevice.h"
 #include "SDBlockDevice.h"
 #include <stdio.h>
 #include <errno.h>
+#include <stdio.h>
 
-//HeapBlockDevice bd(128 * 512, 512);
-//SDBlockDevice bd(PinName mosi, PinName miso, PinName sclk, PinName cs);
+
 SDBlockDevice bd(D11, D12, D13, D10);
 Serial pc(PA_2,PA_3);
-Serial SIM900(PA_9,PA_10);                // (TX,RX)                                                   
+Serial SIM900(PA_9,PA_10);                // (TX,RX)    
+//MODSERIAL pc(PA_2,PA_3);
+//MODSERIAL SIM900(PA_9,PA_10);
 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  GPRSbuffer[200];
+char resp[6]; 
 char  NUMBER[14];
+char str1[20];
+char str2[20];
+char str3[25];
+char str4[25];
+char passwort[100];
 string MESSAGE;
+bool smsRequest = false; 
 int index;
 int i=0;
+Timer timeCnt;
+
+#define DEFAULT_TIMEOUT         5
 
 FATFileSystem fs("fs");
 string result;
@@ -53,50 +65,170 @@
     while (SIM900.readable()) {
       x = SIM900.getc();
       result += x;
-    pc.putc(x); // print the answer from SIM900
+      pc.putc(x); // print the answer from SIM900
  
        }
 }
+void getlineGSM()
+{
+    //char c;
+  timeCnt.start();  // start timer
+    while(1) {
+        while (SIM900.readable()) {
+            char c = SIM900.getc();
+            if (c == '\r' || c == '\n') c = '$';
+            GPRSbuffer[i++] = c;
+            if(i > 512)break;
+        }
+        if(i > 512)break;
+        if(timeCnt.read() > DEFAULT_TIMEOUT) {
+            timeCnt.stop();
+            timeCnt.reset();
+            break;
+        }
+    }
+    wait(0.5);
+    while(SIM900.readable()) {  // display the other thing..
+        char c = SIM900.getc();
+    }
+}
+
+int readBuffer(char *buffer,int count)
+{
+    int i = 0;
+    timeCnt.start();  // start timer
+    while(1) {
+        while (SIM900.readable()) {
+            char c = SIM900.getc();
+            if (c == '\r' || c == '\n') c = '$';
+            buffer[i++] = c;
+            if(i > count)break;
+        }
+        if(i > count)break;
+        if(timeCnt.read() > DEFAULT_TIMEOUT) {
+            timeCnt.stop();
+            timeCnt.reset();
+            break;
+        }
+    }
+    wait(0.5);
+    while(SIM900.readable()) {  // display the other thing..
+        char c = SIM900.getc();
+    }
+    return 0;
+}
+void cleanBuffer(char *buffer, int count)
+{
+    for(int i=0; i < count; i++) {
+        buffer[i] = '\0';
+    }
+}
+void readSMS()
+{
+       // If theres a char waiting in the MODSERIAL buffer
+        /*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++;
+    }*/
+    
+       if (SIM900.readable()) {
+            readBuffer(GPRSbuffer,110);
+            pc.printf("%s\r\n",GPRSbuffer);
+            for(i=0;i<5;i++)
+            {
+            resp[i]=GPRSbuffer[i];
+            }
+        }
+    //Uncomment the following to debug
+            //pc.printf("\nbuffer = %s", GPRSbuffer);
+           
+            printf("read SMS\r\n");
+            //SIM900.attach(&callback_rx);
+            SIM900.attach(&callback_rx, Serial::RxIrq);
+            SIM900.baud(9600);
+            clearString();
+            SIM900.printf("AT+CMGF=1\r"); //at command for send sms
+            wait_ms(1000);
+            clearString();
+            wait_ms(1000);
+            SIM900.printf("AT+CMGL=");
+            SIM900.putc('"');
+            SIM900.printf("ALL");
+            SIM900.putc('"');
+            SIM900.printf("\r");
+            printf("\n");
+            //SIM900.printf("AT+CMGR=1\r\n");
+            wait_ms(1000);
+            //SIM900.printf("AT+CMGD=2");
+            /*SIM900.putc('"');
+            SIM900.printf("DEL ALL");
+            SIM900.putc('"');
+            SIM900.printf("\r");
+            printf("\n");*/
+            //SIM900.printf("AT+CMGR=1\r\n");
+            wait_ms(1000);
+            //clearString();
+            //cleanBuffer(GPRSbuffer,200);
+            readBuffer(GPRSbuffer,200);
+            readBuffer(GPRSbuffer,200);
+            getlineGSM();
+            wait_ms(1000);
+            printf("Buffer: %s\r\n",GPRSbuffer);
+            wait_ms(1000);
+            if (sscanf(GPRSbuffer,"+CMTI: \"SM\",%d", &index)>0)
+            {
+                pc.printf("Index: %d\r\n", index);
+            }
+            if(sscanf(GPRSbuffer,"+CMGL: 1\"%*[^\"]\",\"%[^\"]",str3)>0)
+    //"+CMGL: %d,\"%*[^\"]\",\"%[^\"]"
+    //if(sscanf(GPRSbuffer,"+CMTI: \"SM \",%d",&index)>0)
+    //{
+    //    SIM900.printf("AT+CMGR=%d\r\n",index);
+    //}
+    //if(sscanf(GPRSbuffer,"+CMGR: \"%[^\"]\",\"%[^\"]\",,\"%[^\"]\"\r\n%s\r\n", str1, str2, str3, passwort)>0)
+    {
+        //if(passwort == "Position")
+        //{
+        //    smsRequest = true;
+        //}
+        printf("Phone number is %s\n",str3);
+    }
+    wait_ms(1000);
+    //SIM900.printf("AT+CMGD=1\r");
+    //printf("Delete SMS\r\n");
+    wait_ms(1000);
+}
+
 void sendSMS()
 {
-clearString();
-SIM900.printf("AT+CMGF=1\r"); //at command for send sms
-wait_ms(1000);
-clearString();
-wait_ms(1000);
-SIM900.printf("AT+CMGS=");
-SIM900.putc('"');
-SIM900.printf("+4917645651571");
-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("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras);
-wait_ms(1000);
-SIM900.putc(0x1A);
-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);*/
+   
+        printf("send SMS\r\n");
+        SIM900.attach(&callback_rx, Serial::RxIrq);
+        SIM900.baud(9600);
+        clearString();
+        SIM900.printf("AT+CMGF=1\r"); //at command for send sms
+        wait_ms(1000);
+        clearString();
+        wait_ms(1000);
+        SIM900.printf("AT+CMGS=");
+        SIM900.putc('"');
+        SIM900.printf("+4917645651571");
+        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("Find me at....\n %s%f,%f%s", GoogleChunk, gps.latitude, gps.longitude, GoogleExtras);
+        wait_ms(1000);
+        SIM900.putc(0x1A);
+        wait_ms(60000);
+        //smsRequest=false;
+    
 }
 
 void parseSMS()
@@ -114,19 +246,20 @@
         
     }
     // Uncomment the following to debug
-    // pc.printf("\nbuffer = %s", GPRSbuffer);
+     
     
     // If we get an SMS notification
-    if (sscanf(GPRSbuffer,"$$+CMTI: \"SM\",%d", &index)>0)
+    if (sscanf(GPRSbuffer,"+CMTI: \"SM\",%d", &index)>0)
     {
         pc.printf("\nSMS recieved @ index [%d]", index);
-        memset(GPRSbuffer, '0', 511);
+        pc.printf("\nbuffer = %s", GPRSbuffer);
+        //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 
+    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 
@@ -137,15 +270,16 @@
         MESSAGE = pch;
         pc.printf("\nDone! ");
         // Send the location
-        sendSMS();
+        //sendSMS();
         // Reset the GPRS buffer
-        memset(GPRSbuffer, '0', 511);
+         // memset(GPRSbuffer, '0', 511);
         // Reset the char counter
-        i = 0;    
+        i = 0;
+        pc.printf("\nbuffer = %s", GPRSbuffer);    
     }
      if (strncmp(GPRSbuffer, "$$+CMGS",7) == 0) {
         // Reset the GPRS buffer
-        memset(GPRSbuffer, '0', 511);
+        //memset(GPRSbuffer, '0', 511);
         // Reset the char counter
         i = 0;
     }
@@ -153,24 +287,25 @@
         SIM900.printf("ATH0\r\n");
         pc.printf("\nCall bounced!...");
         // Do the send SMS routine...
-        sendSMS();
+        //sendSMS();
         // Reset the GPRS buffer
-        memset(GPRSbuffer, '0', 511);
+        //memset(GPRSbuffer, '0', 511);
         // Reset the char counter
         i = 0;
     }
+    //pc.printf("\nbuffer = %s", GPRSbuffer);
     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);
+    //memset(GPRSbuffer, '0', 511);
     // Reset the char counter
     i = 0;
 }
  
 int main() {
-  /**/
-  memset(GPRSbuffer, '0', 511);
+  /**/     
+  /*memset(GPRSbuffer, '0', 511);
   pc.printf("\r\n GSM 900 Test\n");
   //SIM900.attach(&callback_rx);
   SIM900.baud(9600);
@@ -182,9 +317,44 @@
   SIM900.printf("AT+CMGDA=\"DEL ALL\"\r\n");
     wait(1);
     pc.printf("\nMessages Cleared...\n");
-    wait(1);
+    wait(1);*/
   //sendSMS();
-  wait_ms(100);
+    //printf("read SMS\r\n");
+    /*SIM900.printf("AT+CMGF=1\r"); //at command for send sms
+    wait_ms(1000);
+    SIM900.printf("AT+CMGL=\"ALL\"\r\n");
+  
+    wait_ms(1000);
+    wait_ms(100);*/
+    /*printf("read SMS\r\n");
+            SIM900.attach(&callback_rx, Serial::RxIrq);
+            SIM900.baud(9600);
+            clearString();
+            SIM900.printf("AT+CMGF=1\r"); //at command for send sms
+            wait_ms(1000);
+            clearString();
+            wait_ms(1000);
+            SIM900.printf("AT+CMGL=");
+            SIM900.putc('"');
+            SIM900.printf("ALL");
+            SIM900.putc('"');
+            SIM900.printf("\r");
+            printf("\n");
+            wait_ms(30000);
+            if (sscanf(GPRSbuffer,"+CMTI: \"SM\",%d", &index)>0)
+            {
+                pc.printf("Index: %d\r\n", index);
+            }*/
+            //if(sscanf(GPRSbuffer,"+CMGR: 2,\"REC READ\",\"+4917645651571\",,\"2019/04/26,15:57:07+02\"\r\n%s\r\n",passwort)>0)
+            
+                //pc.printf("passwort: %s\r\n",passwort);
+            
+    //sendSMS();
+    
+        readSMS();
+        //parseSMS();
+     
+ 
   /**/
   int error = 0;
   printf("Welcome to the filesystem example.\r\n"
@@ -199,14 +369,7 @@
   printf("Opening a new file, numbers.txt.");
   FILE* fd = fopen("/fs/numbers.txt", "a+");
   errno_error(fd);
- 
-  //for (int i = 0; i < 20; i++){
-  // printf("Writing decimal numbers to a file (%d/20)\r", i);
-  //fprintf(fd, "%d\r\n", i);
-  //}
-   fprintf(fd,"Latitude, Longitude,Altitude,Speed in knots, Date, Time,Number of satellites being tracked, IDs of satellites ins use, Fix, Fix mode\r\n");
-  //printf("Writing decimal numbers to a file (20/20) done.\r\n");
- 
+  fprintf(fd,"Latitude, Longitude,Altitude,Speed in knots, Date, Time,Number of satellites being tracked, IDs of satellites ins use, Fix, Fix mode\r\n");
   printf("Closing file.");
   fclose(fd);
   printf(" done.\r\n");
@@ -249,11 +412,7 @@
           float latitude    = gps.latitude;
           float longitude   = gps.longitude;
           float altitude    = gps.altitude;
-          //float direction   =0.0;
           float utc = gps.utc+20000;
-          //int sek=gps.sek;
-          //int min=gps.min;
-          //int hour=gps.hour;
           int sat_tracked=gps. sat_tracked;
           int fix=gps.fix;
           int fix_3d=gps.fix_3d;
@@ -269,20 +428,8 @@
           int id10=gps.id10;
           int id11=gps.id11;
           int id12=gps.id12;
-          //double hdop=gps.hdop;
-          //double pdop=gps.pdop;
           float speed=gps.speed;
           int   date=gps.date;
-          //char aut=gps.aut;
-          //float vdop=gps.vdop;
-          //int chek_data=gps.check_data;
-          //pc.printf("Latitude, Longitude,Altitude, Direction, Speed in knots, Date, Time, Satellites in use, Satellites in view, IDs of satellites ins use, Fix, Fix mode, PDOP, HDOP\r\n");
-          //pc.printf("%0.6f,%0.6f,%0.3f,%0.2f,%0.2f,%d,%0.0f,%d,",latitude,longitude,altitude,direction,speed,date,utc, sat_tracked);
-          //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("Longitude:%0.6f\r\n",longitude);
@@ -296,7 +443,7 @@
           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+");
@@ -316,15 +463,17 @@
              //errno_error(fd);
           
           wait(1);
-          //parseSMS();
-          sendSMS();
+               //parseSMS();
+               //readSMS();
+          //printf("Passwort: %s\r\n",passwort);
+            
+            
+         wait_ms(3000);
           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);
-  //}
-   //fprintf(fd,"Latitude, Longitude,Altitude,Speed in knots, Date, Time,Number of satellites being tracked, IDs of satellites ins use, Fix, Fix mode,\r\n");
-  //fprintf(fd,"%0.6f,%0.6f,%0.3f,%0.2f,%d,%0.0f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",latitude, longitude, altitude, speed, date, utc, sat_tracked, id1,id2,id3,id4,id5,id6,id7,id8,id9,id10,id11,id12, fix,fix_3d);
+          
+          //sendSMS();
+          
+          wait(1);
       }
     }
 }
\ No newline at end of file