whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Revision:
32:4b94cb22d338
Parent:
31:f94b8f987b62
Child:
33:4ed19bed18c0
--- a/main.cpp	Sat Nov 21 01:27:50 2015 +0000
+++ b/main.cpp	Wed Nov 25 22:25:14 2015 +0000
@@ -6,6 +6,10 @@
 #include "GPS.h"    // GPS support
 #include "MDM.h"    // Modem support
 
+
+
+
+
 // AT&T Support
 
 //------------------------------------------------------------------------------------
@@ -16,9 +20,23 @@
 #define PASSWORD    NULL            //! Set the password for your APN, or NULL if not needed (which, apparently, it isn't)
 //------------------------------------------------------------------------------------
 
+// PONY refactored code
+#include "PonyLoc.h"
+
+
+
+
+
 // Set pinout for LED
 DigitalOut myled(LED); 
 
+extern const char* NMEA_talkers[];
+
+
+
+
+// MAIN
+
 int main(void) {
     printf("\n\n\r----------\r\nI'm alive!\r\n");
     
@@ -52,7 +70,7 @@
     
     
     
-    
+    /*
     while( (ret = gps.getMessage(buf, sizeof(buf))) < 1 ) {
         if (ret<0)
             printf("WAIT received from GPS\r\n");
@@ -60,49 +78,83 @@
             printf("NOT FOUND received from GPS\r\n");
         myled = !myled;
         wait(1.0);
-    }
+    }*/
+    
+    if (gpsReady(gps, 5) ) {
+        printf("GPS ready");
     
-    float locationLoopWait = 1.0;
+    float locationLoopWait = 0.25;
     int locationLoopIter = 0;
+    
+    // How long will we wait before we accept ANY location value?
+    int locationLoopTimeout = 30;
+    int locationLoopCountout = (float) locationLoopTimeout / locationLoopWait;
+    
+    
     // Get a good GPS message
     
     int len = LENGTH(ret);
     
+    printf("\r\nMessage loop wait=%fs\r\n",locationLoopWait);
     while(true) {
         locationLoopIter++;
         
         ret = gps.getMessage(buf, sizeof(buf));
         
         // Test for location data in this priority: GLL, RMC, GGA
+        #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
         
-        if ( strstr(buf, "GLL") ) {
+        // TODO: Add check that we're getting NMEA-formatted sentences
+        
+        
+        
+        
+        int sentenceType = getNMEAtalker(buf);
+        //char talkerType[3] = NMEA_talkers[sentenceType];
+        printf("Talker type = %s\r\n", NMEA_talkers[sentenceType]);
+        
+        if (sentenceType == 5) {
+            break;
+        }
+        
+        
+        
+        /*
+        if ( _CHECK_TALKER("GLL") ) {
             char ch = '-';
             // FIX: The getNmeaItem isn't working to capture the check character in a valid GLL code, which is unusual; the workaround seems to be that invalid GLL values DOES find a "V"
             if(!gps.getNmeaItem(6,buf,len,ch)) {
-                printf("\n\n\rHave GLL location data after %i seconds and %i requests:\r\n", ( (int) locationLoopWait * locationLoopIter), locationLoopIter );
+                printf("\n\n\rHave GLL location data after %f seconds and %i requests:\r\n", ( locationLoopWait * (float) locationLoopIter ), locationLoopIter );
                 printf(buf);
                 break;
             }
             else {
-                printf("Received invalid GLL data\r\n");
-                printf(buf);   
-                
+                printf("\rGLL inv...");
+                //printf(buf); printf("\r\n");
             }
-                
+        }
+        else if ( (_CHECK_TALKER("RMC") || _CHECK_TALKER("GGA")) && locationLoopIter > locationLoopCountout ) {
+            printf("\r\nTaking non-GLL data due to location timeout\r\n");
+            printf(buf); printf("\r\n");
+            break;
         }
-        else if ( strstr(buf, "RMC") ) {
-            printf("RMC loc...");
-            //printf(buf); printf("\r\n\n");
+        
+        
+        else if ( _CHECK_TALKER("RMC") ) {
             
+            printf("\rRMC loc...");
+            //printf(buf); printf("\r\n\n");
         }
-        else if ( strstr(buf, "GGA") ) {
-            printf("GGA loc...");
+        else if ( _CHECK_TALKER("GGA") ) {
+            printf("\rGGA loc...");
             //printf(buf); printf("\r\n\n");
         }
         else {
-            printf("\n\rNo location data this message\r\n");
+            printf("\rNo loc...");
         }
         
+        */
+        
         wait(locationLoopWait);
     }// LOOP THAT MOFO
     
@@ -114,7 +166,87 @@
     gps.getNmeaAngle(1,buf,len,la);
     gps.getNmeaAngle(3,buf,len,lo);
     
-    printf("\r\n\nLatitude: %G\r\nLongitude: %G\r\n", la, lo);
+    printf("\r\n\nLatitude: %G\r\nLongitude: %G\r\n\n", la, lo);
+    
+    
+    
+    
+    //----- MODEM UP!!!
+    // Now that we know where we are, let's do something with it (modem-wise...)
+    
+    printf("Open modem object mdm\r\n");
+    
+    MDMSerial mdm;
+    mdm.setDebug(4); // enable this for debugging issues 
+    
+    
+    // Create status objects
+    MDMParser::DevStatus devStatus = {};
+    MDMParser::NetStatus netStatus = {};
+    
+    bool mdmOk = mdm.init(SIMPIN, &devStatus);
+    
+    // How we doing so far?
+    printf("\r\n-- mdm.init status dump\r\n");
+    mdm.dumpDevStatus(&devStatus);
+    
+    if (mdmOk) {
+        // wait until we are connected (SGM: Not sure what this means, since this isn't a loop...
+        mdmOk = mdm.registerNet(&netStatus);
+        mdm.dumpNetStatus(&netStatus);
+        
+    }
+    
+    printf("-- mdm.registerNet status dump\r\n");
+    
+    // Open a data connecction
+    if(mdmOk) {
+
+        MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
+        printf("-- mdm.join dump\r\n");
+        
+    
+        // If we secure an IP address, keep going!
+        
+        if (ip != NOIP) {
+            
+            printf("\n\n\rWe got's an IP address!\r\n");    
+            mdm.dumpIp(ip);
+
+        }
+    }
+    
+    // SMS code
+    while(true) {
+        wait(5);
+        if (mdmOk) {
+            // check the network status
+            if (mdm.checkNetStatus(&netStatus)) {
+                printf("\r\n-- mdm.dumpNetStatus\r\n");
+                mdm.dumpNetStatus(&netStatus, fprintf, stdout);
+                break;
+            }
+        }
+    }
+    
+    // Send SMS
+    char num1[32] = "+12062556786";
+    
+    char smsText[144];
+    sprintf(smsText, "PONY unit located at: %f, %f", la, lo);
+    printf("Message: %s\r\n", smsText);
+    
+    /*
+    
+    printf("\r\nSend to: %s\r\n", num1);
+    if( mdm.smsSend(num1, smsText) ) {
+        printf("\r\nSMS num1 (AT&T) success!\r\n");    
+    }
+    else {
+        printf("SMS fail. :-( \r\n");
+    }
+
+    */
     
     
     
@@ -122,21 +254,31 @@
     
     
     
-    /*
+    //printf("\n\rThat's our IP address!\r\n");
+    
+    // Let's send a text
+    //printf("\r\nAttempting to text:");
+    
+    //char* txtText;
+    //sprintf(txtText, "PONY located at lat/long %G, %G", la, lo);
+    
+    //printf(txtText);
+    //printf(txtText);
+    //printf("' to %s\r\n", txtText);
     
-
-    if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {      // Check if we're getting an NMEA protocol response
-        
-        printf("Indeed, an NMEA protocol response!\r\n");
-        
-        // Check the talker
-        if( (buf[0] == '$') || (buf[1] == 'G' ) ) {
-            char talkerID[30] = "";
-            sprintf(talkerID, "We've got a valid talker=%c%c\r\n", buf[0], buf[1]);
-            printf(talkerID);    
-        }
-    }
-       */   
+    //char num[32] = "+14259749434";
+    
+    //mdm.smsSend(num, txtText);
+    
+    //printf("debug check");
+    
+    
+    
+    
+    
+    
+    
+