whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Revision:
41:f603d76dc6fe
Parent:
40:82b70b92169e
Child:
42:8e6e647cb7d5
--- a/main.cpp	Tue Dec 15 18:40:25 2015 +0000
+++ b/main.cpp	Tue Dec 29 06:56:52 2015 +0000
@@ -21,11 +21,17 @@
 #include <jsonlite.h>
 #include "M2XStreamClient.h"
 
+#include "LIS331.h"
+#include "PONY_sense.h"
 
 
 
+//----
+// DEBUG DEFINITIONS
 
+#define THROWAWAY
 
+//#define MDMDEBUG
 
 
 
@@ -42,8 +48,19 @@
 //------------------------------------------------------------------------------------
 // AT&T M2X Kekys
 
+
+#ifdef THROWAWAY
+// Codes for a throwaway M2X device
 #define M2XAPIKEY "54c6bbf11012f34830bd23cc091ca250"
 #define DEVID "b9c57667ee0495dc0b3cddd890f8d2df"
+#endif
+
+#ifndef THROWAWAY
+// v1.2 codes
+#define M2XAPIKEY "bbc483492238dc76f7d12f0cd6e13a4b"
+#define DEVID "3764db38b6c9ec4045a38e0125b14b4c"
+#endif
+
 
 //------------------------------------------------------------------------------------
 
@@ -56,19 +73,25 @@
 // PIN Config
 DigitalIn jostle(P2_13);
 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
 
+AnalogIn   tempPin(P0_23);
+AnalogIn   tempGnd(P0_24);
 
+//I2C axis(P0_0, P0_1);       // SDA, SCL
+LIS331 axle(P0_0, P0_1);        // Library object
 
 
 
 // GLOBALS
 
+Ticker flipper;
 
 
 Client client;
 M2XStreamClient m2xClient(&client, M2XAPIKEY);
 int M2X_response;   // For m2x message responses
-I2C axis(P0_0, P0_1);
 
 char statusBuf[145] = "";
 
@@ -81,9 +104,14 @@
 double kLaLoDiffMin = 0.01;
 double kAltDiffMin  = 2.00;
 
+float kTemp;
+
 unsigned int kIdleSpins = 0;
 
-
+//uint16_t kSysSeconds;
+time_t kSysSeconds = time(NULL);
+bool kSysClockSet = false;
+tm kFormatTime;
 
 
 
@@ -91,7 +119,10 @@
     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
+void flip() {
+    led1 = !led1;
+}
+
 
 //------------------------------------------------------------------------------------
 //------------------------------------------------------------------------------------
@@ -99,104 +130,172 @@
 int main(void)
 {
     
-    printf("\r\n\n\n\n-----\r\nI'm alive - u9\r\n\n");
-    
-    int ret = 0;
-    int len = 0;
-    char buf[2048] = "";
-
-    // Create the GPS object
-    GPSI2C gpsInst;
+    printf("\r\n\n\n\n-----\r\nI'm alive - vers. uA\r\n\n");
+    #ifdef THROWAWAY
+    printf("Using THROWAWAY M2X device\r\n");
+    #endif
     
-    // Struct for our GPS sentence info
-    struct GPSbundle GPSone;
-    GPSone.bGPS = gpsInst;
-    GPSone.bRet = ret;
-    GPSone.bLen = len;
-    GPSone.bBuf = &buf[0];
-    GPSone.bBufLen = sizeof(buf);
+    //int ret = 0;
+    //int len = 0;
+    //char buf[2048] = "";
+
     
-    // Struct for item location
-    //double timestamp = 0, lat =  0, lon = 0, alt = 0, vel = 0;
-    struct GPSloc PONYspot, PONYlast;
     MDMSerial mdm;
     MDMParser::DevStatus devStatus = {};
     MDMParser::NetStatus netStatus = {};    
     bool mdmOk = mdm.init(SIMPIN, &devStatus);
+    #ifdef MDMDEBUG
     //mdm.setDebug(4);
+    #endif
+    
+    //------------------------------------------------
+    //------------------------------------------------
+    // I2C TESTING
+    // Acceleromter object
+    //char ax[2];
     
     
     
-    // Acceleromter object
-    char ax[2];
+    //printf("Set up accelerometer\r\n");
+
+    printf("I2C write address ID: %01x\r\n",(LIS331_I2C_ADDRESS << 1) & 0xFE );
+    printf("I2C read address ID: %01x\r\n",(LIS331_I2C_ADDRESS << 1) | 0x01 );
+    
+    printf("axleID=%01x\r\n", axle.getWhoAmI());
+    
+    // Set and read power mode
+    axle.setPowerMode(NORMAL_400HZ);
+    printf("axle Power Mode = %01x\r\n",axle.getPowerMode());
+    
+    printf("axle AccelStatus = %01x\r\n", axle.getAccelStatus());
+    
+    //float axleAccelZ = ();
+    
     
-    //axis.write(0x3A, 
-    //axis.read(0x3A, 
+
+    printf("Begin accel loop\r\n\n");
+    while(true){
+        printf("Z accel = %02f\r\n", axle.getAccelZ());
+        printf("X accel = %02f\r\n", axle.getAccelX());
+        printf("Y accel = %02f\r\n", axle.getAccelY());
+
+        
+        delay(5000);        
+    };
     
-    //moveData kegMotion;
+    
+    
+    
+    
+    
+    
     
     
     // Open modem connection
     if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
         return -1;    
     
-    
-        
-    // Ready the GPS
-    readyGPS(&GPSone, 5);
-    
-    
-    
-    
-    moveData kegMotion;
-    
-    status_t axisStatus;
-    uint8_t axisID;
 
 
     // Cell location call variables
     const int sensorMask = 2;  // 1 = GNSS, 2 = CellLocate, 3 = Hybrid: GNSS + CellLocate       
-    const int timeout = 55; // in seconds
+    //const int timeout = 55; // in seconds
     const int targetAccuracy = 1; // meters
 
     // Cell location data
-    MDMParser::CellLocData ponyLoc, thisLoc;
+    MDMSerial::CellLocData ponyLoc, thisLoc;
     
 
     printf("Configure deep scan mode\r\n");
     int locConf = mdm.cellLocConfigSensor(1);   // Set deep scan mode
     
+    // Set CellLocate server connection
+    //printf("\033[33mConnect to CellLocate server...\033[39m\r\n");
+    //if (!mdm.cellLocSrvHttp("tFpJ8NlWXUa8keKu3UY4AQ"))
+    //        mdm.cellLocSrvUdp();       
+            
+    // Set URCs?     
+    //printf("Congifure URCs\r\n");
+    //mdm.cellLocUnsolIndication(1);
+    
     // Loop driving variables
     bool locLock;
-    int cellLocDelay = 2 * 1000;
-    int readLoopMax = 10;
+    int submitPeriod = 30;
+    int timeoutMargin = 5;
+    int readLoopMax = 100;
     int loopIter = 0;
     int locAccLower = 50;   // Immediately accept locations with uncertainty lower than this
     int locAccUpper = 5000; // Don't accept locations with uncertainty greater than this
     
-    
+    // NO DIFF
+    //printf("Waiting one minute for cell location to lock.\r\n");
+    //delay(60000); // Wait one minute after booting to see if that makes our first cell loc calls give better data
+
+
     
     // Location reporting loop
     while(true) {
-        printf("\r\n");
         
+        M2X_response = 0;
         locLock = false;
         loopIter++;
         
+                
+        //M2X_response = logTemp(&tempPin, &m2xClient);
+        printf("tick\r\n");
+        
+
+        /*
+        while(!M2X_response) {
+            printf("Waiting on temperature log\r\n");
+            delay(1000);
+        }
+        */
+        
+        delay(5000);    // Delay to ensure that the modem isn't talking over itself transmitting temp and requesting location
+        
         //printf("Cell location Request; deep scan=%i, mask=%i, accuracy=%i\r\n", locConf, sensorMask, targetAccuracy);
-        mdm.cellLocRequest(sensorMask, timeout, targetAccuracy);
+        //mdm.setDebug(4);
+        mdm.cellLocRequest(sensorMask, submitPeriod - timeoutMargin, targetAccuracy);
+
         
+
         // Have location data
         if ( mdm.cellLocGet(&thisLoc) ){           
-            printf("CellLocate position rec'd with uncertainty= %d\r\n", thisLoc.uncertainty );           
+            //printf("CellLocate position rec'd with uncertainty= %d, sensor= %d\r\n", thisLoc.uncertainty, thisLoc.sensorUsed );           
             
             
-            if(thisLoc.uncertainty < ponyLoc.uncertainty) {
-                printf("  NEW-latitude: %0.5f, longitude: %0.5f, altitute: %d, uncertainty: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude, thisLoc.uncertainty);
+            if(thisLoc.sensorUsed == 2) { // thisLoc.uncertainty < ponyLoc.uncertainty) { //Basing test for validity on uncertainty and having a new position lock
+                printf("  NEW-latitude: %0.5f, longitude: %0.5f, altitute: %d, uncertainty: %d, sensor: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude, thisLoc.uncertainty, thisLoc.sensorUsed);
                 ponyLoc.latitude = thisLoc.latitude;
                 ponyLoc.longitude = thisLoc.longitude;
                 ponyLoc.altitude = thisLoc.altitude;
                 ponyLoc.uncertainty = thisLoc.uncertainty;
+                
+                
+                if(!kSysClockSet) {
+                    printf("\033[31mSet system clock time.\033[39m\r\n");
+                    
+                    // TODO: Get the local time zone
+                    //mdm.setDebug(4);
+                    //mdm.sendFormated("AT+CTZR=1");
+
+                    // Adjust year because it's reporting the absolute year
+                    // mbed expects the year to be "since 1900" (https://developer.mbed.org/handbook/Time?action=view&revision=11592)
+                    (thisLoc.time).tm_year -= 1900;
+                    
+                    kSysSeconds = mktime( &(thisLoc.time) );
+                    set_time(kSysSeconds);
+                    printf("System time set to: %s\r\n", ctime(&kSysSeconds) );    
+                    
+                    kSysClockSet=true;
+                }
+                
+                // Print the timestamp
+                
+                
+                time_t newTime = time(NULL);
+                printf("System time at: %s\r\n", ctime(&newTime) );
             }
             
             
@@ -216,6 +315,13 @@
                 if(M2X_response == 202)
                     printf("Location Confidence (%d) POST successful\r\n", ponyLoc.uncertainty);
                 
+                // Report temperature
+                M2X_response = logTemp(&tempPin, &kTemp, &m2xClient);
+                
+                if(M2X_response == 202)
+                    printf("Temperature POST successful\r\n");
+                    
+                                    
                 locLock = true;
             }             
         }
@@ -243,34 +349,14 @@
             printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
             continue;
         }
+
         else {
-            delay(cellLocDelay);    // Delay between CellLoc requests when not getting good data
+            printf("Bad data delay = submitPeriod...\r\n");
+            delay(submitPeriod * 1000);    // Delay between CellLoc requests when not getting good data
+            flipper.attach(&flip, 0.5); // the address of the function to be attached (flip) and the interval (2 seconds)
         }
+        led1 = 0;
     }
-    
-    
-    
-    
-    /*
-    
-            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);
-                
-
-                    
-                PONYlast = PONYspot;
-            
-                kIdleSpins = 0;
-            }
-        }
-        else {
-            
-            
-            
-        }
-        */
 
 
     //mdm.disconnect();