whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Revision:
39:c14aff678b25
Parent:
38:d86e744166b6
Child:
40:82b70b92169e
--- a/main.cpp	Wed Dec 09 00:55:23 2015 +0000
+++ b/main.cpp	Fri Dec 11 23:21:51 2015 +0000
@@ -42,12 +42,21 @@
 //------------------------------------------------------------------------------------
 // AT&T M2X Kekys
 
-#define M2XAPIKEY "295d76119ddac541c4e8c51bc3a45497"
-#define DEVID "8066dc4be6828638af1a62521f61f6e3"
+#define M2XAPIKEY "54c6bbf11012f34830bd23cc091ca250"
+#define DEVID "b9c57667ee0495dc0b3cddd890f8d2df"
 
 //------------------------------------------------------------------------------------
 
 #include "PONY_Loc.h"       // PONY Location Code
+#include "H3LIS331DL.h"     // Accelerometer library
+
+
+
+
+// PIN Config
+DigitalIn jostle(P2_13);
+
+
 
 
 
@@ -58,14 +67,25 @@
 
 Client client;
 M2XStreamClient m2xClient(&client, M2XAPIKEY);
-
+I2C axis(P0_0, P0_1);
 
+unsigned int kReadingDelay = 180 * 1000;         // How many seconds to wait between reads
+unsigned int kReadingDelayClimb = 15000;    // How many seconds to add to the wait period when sitting idle
+unsigned int kReadingDelayMax = 5 * 60000;  // What's the maximum time between readings?
 
-unsigned int kReadingDelay = 30000;
 
 double kLaLoDiffMin = 0.01;
 double kAltDiffMin  = 2.00;
 
+unsigned int kIdleSpins = 0;
+
+
+
+
+
+void parseM2XLocation2(const char* name, double latitude, double longitude, double elevation, const char* timestamp, int index, void* context) {
+    printf("\n\n\n\n\n\n\n\n\n\n\n\n\nparseM2XLocation2\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
+}
 
 //#define CELLOCATE
 
@@ -75,7 +95,7 @@
 int main(void)
 {
     
-    printf("\r\n\n\n\n-----\r\nI'm alive - u8!\r\n\n");
+    printf("\r\n\n\n\n-----\r\nI'm alive - u9\r\n\n");
     
     int ret = 0;
     int len = 0;
@@ -94,44 +114,157 @@
     
     // Struct for item location
     //double timestamp = 0, lat =  0, lon = 0, alt = 0, vel = 0;
-    struct GPSloc PONYspot;
+    struct GPSloc PONYspot, PONYlast;
     MDMSerial mdm;
+    MDMParser::DevStatus devStatus = {};
+    MDMParser::NetStatus netStatus = {};    
+    bool mdmOk = mdm.init(SIMPIN, &devStatus);
+    //mdm.setDebug(4);
+    
+    
+    
+    // Acceleromter object
+    char ax[2];
+    
+    //axis.write(0x3A, 
+    //axis.read(0x3A, 
+    
+    //moveData kegMotion;
+    
     
     // Open modem connection
     if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
         return -1;    
     
-    //Timer tmr;
-    //tmr.reset();
-    //tmr.start();
     
         
     // Ready the GPS
     readyGPS(&GPSone, 5);
     
+    
+    int M2X_response;   // For the m2x message response
+    
+    moveData kegMotion;
+    
+    status_t axisStatus;
+    uint8_t axisID;
 
+    // Accelerometer tests
+    
+    
+    //i2c->write((deviceAddress << 1) & 0xFE, (char *)buf, 2);
+    
+    
+    
+    
+    //printf("WHOAMI = %i, status=%i\r\n",axisID,axisStatus);
+    
+    
+    /*
+    while(true) {
+        
+        axis.readXYZ( &(kegMotion.x), &(kegMotion.y), &(kegMotion.z) );
+        
+        printf("x=%i, y=%i, z=%i\r\n", (kegMotion.x), (kegMotion.y), (kegMotion.z) );
+        
+        delay(1000);  
+    }
+    */
+    
+    
+        
+    //bool mdmOk = false;   // Don't need?
+    
+    const int sensorMask = 2;  // 1 = GNSS, 2 = CellLocate, 3 = Hybrid: GNSS + CellLocate       
+    const int timeout = 55; // seconds
+    const int submitPeriod = 15; // 1 minute in seconds
+    const int targetAccuracy = 1; // meters
+    const int numReq = 5;
+
+    MDMParser::CellLocData loc;
+    
+    mdm.setDebug(4);
+
+    //Token can be released from u-blox site, when you got one replace "TOKEN" below 
+    //printf("Pull from CellLocate server\r\n");
+    //if (!mdm.cellLocSrvHttp("tFpJ8NlWXUa8keKu3UY4AQ"))
+    //       mdm.cellLocSrvUdp();
+
+    printf("Configure deep scan mode\r\n");
+    int locConf = mdm.cellLocConfigSensor(1);   // Deep scan mode
+    printf("Config mode response=%i\r\n",locConf);
+    
+
+    while(true) {
+
+        printf("Cell location Request; deep scan=%i, mask=%i, accuracy=%i, hypo=%i\r\n", locConf, sensorMask, targetAccuracy, numReq);
+        mdm.cellLocRequest(sensorMask, timeout, targetAccuracy, numReq);
+        
+        if ( mdm.cellLocGet(&loc) ){           
+            printf("CellLocate position received, sensor_used: %d,  \r\n", loc.sensorUsed );           
+            printf("  latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", loc.latitue, loc.longitude, loc.altitutude);
+            if (loc.sensorUsed == 1)    // If the GNSS sensor is giving us our position
+                printf("  uncertainty: %d, speed: %d, direction: %d, vertical_acc: %d, satellite used: %d \r\n", loc.uncertainty,loc.speed,loc.direction,loc.verticalAcc,loc.svUsed);              
+        }
+        else
+            printf("cellLocGet failed.\r\n");
+
+        delay(submitPeriod * 1000);
+    }    
+    
     
     while(true) {
         
+        printf("Attempt GPS location.\r\n");
+        
         // Get coordinates
-        if ( fetchLocBundle(&GPSone, &PONYspot, 30) == 7 )
-            printf("Filled bundle!\r\n");
-        
-        printf("Lat=%f, Long=%f, Elev=%f, Time=%f\r\n",PONYspot.lat, PONYspot.lon, PONYspot.alt, PONYspot.tmstmp);        
+        if ( fetchLocBundle(&GPSone, &PONYspot, 30) == 7 ) {
+            printf("\r\nFilled bundle!\r\n");
         
-        //tmr.reset();
-        //tmr.start();
-        int response;
+            printf("Lat=%f, Long=%f, Elev=%f, Time=%f\r\n",PONYspot.lat, PONYspot.lon, PONYspot.alt, PONYspot.tmstmp);
+
+            if( true ) { //(abs(PONYspot.lat - PONYlast.lat) > kLaLoDiffMin) || (abs(PONYspot.lon - PONYlast.lon) > kLaLoDiffMin) || (abs(PONYspot.alt - PONYlast.alt) > kAltDiffMin) ) {
+                
+                //printf("New location found!\r\n");
+                M2X_response = m2xClient.updateLocation(DEVID, "Igor-spot", PONYspot.lat, PONYspot.lon, PONYspot.alt);
+                
+                if(M2X_response == 202)
+                    printf("POST success\r\n");
+                    
+                PONYlast = PONYspot;
+            
+                kIdleSpins = 0;
+            }
+        }
+        else {
+            printf("No GPS location found.\r\n");
+            M2X_response = m2xClient.updateStreamValue(DEVID, "status", "GPS location not found.");
+            
+        }
         
-        response = m2xClient.updateLocation(DEVID, "Igor-spot", PONYspot.lat, PONYspot.lon, PONYspot.alt);
+
+        
+        
+        //M2X_response = m2xClient.readLocation(DEVID, parseM2XLocation2, NULL);
+        
+        //printf("readLoc response=%i\r\n",M2X_response);
+        
         
-        if(response == 202)
-            printf("POST success\r\n");
+    /*
+        else {
+            printf("No movement, no write. (%i spins)\r\n", kIdleSpins);
+            kIdleSpins++;
+        }
         
+        if( kIdleSpins % 10 == 0 && kReadingDelay < kReadingDelayMax )  // Every time we go 10 measurements without moving
+            kReadingDelay += kReadingDelayClimb; printf("Bump delay time...\r\n");    // Increase the between-reading wait time if we're sitting idle             
+     */
+     
             
         delay(kReadingDelay);
     }
 
+
     mdm.disconnect();
     mdm.powerOff();