whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Revision:
49:a8c40c816199
Parent:
48:3512b3c7a5ae
Child:
50:bba466f093a4
--- a/main.cpp	Tue Dec 29 09:00:09 2015 +0000
+++ b/main.cpp	Thu Jan 07 23:03:22 2016 +0000
@@ -50,7 +50,7 @@
 DigitalOut led2(LED2);
 
 AnalogIn   tempPin(P0_23);
-AnalogIn   tempGnd(P0_24);
+//AnalogIn   tempGnd(P0_24);
 
 //I2C axis(P0_0, P0_1);       // SDA, SCL
 //LIS331 axle(P0_0, P0_1);        // Library object
@@ -100,10 +100,15 @@
 int main(void)
 {
     
-    printf("\r\n\n\n\n-----\r\nI'm alive - vers. uA\r\n\n");
+    printf("\r\n\n\n\n-----\r\nI'm alive - vers. uA - device codes PONY_v1.4\r\n\n");
     #ifdef THROWAWAY
     printf("Using THROWAWAY M2X device\r\n");
     #endif
+    
+    #ifndef THROWAWAY
+    printf("Using PONY_v1.4 M2X device\r\n");
+    #endif    
+    
 
     MDMSerial mdm;
     MDMParser::DevStatus devStatus = {};
@@ -128,25 +133,18 @@
     
     
     // Cell location data
-    MDMSerial::CellLocData ponyLoc, thisLoc;
+    MDMSerial::CellLocData thisLoc;
     
     // Location loop driving variables
     int loopIter = 0;
     bool sendStatus = false;
-    bool locLock;
     
     // Loop timing variables
     Timer cellLocDelay;
-    int submitPeriod = 30; // AXE THIS
     
     int cellLocPeriod = 30; // in seconds
     int timeoutMargin = 5;  // in seconds
-    
-    
-    
-    // Loop limit variables
 
-    int readLoopMax = 100;
     
     // Location data filter variables
     int locAccLower = 50;   // Immediately accept locations with uncertainty lower than this
@@ -171,7 +169,6 @@
         // Set some important variables
         M2X_response = 0;
         sendStatus = 0;
-        locLock = false;
         loopIter++;
         
         //printf("tick\r\n");
@@ -220,8 +217,9 @@
                 printf("\033[31mSet system clock time.\033[39m\r\n");
                 kSysClockSet=true;
             }            
-            
-            printf("thisLoc read at: %s\r", ctime(&kSysSeconds) );                 
+            #ifdef LOCDEBUG
+                printf("LOC read at: %s\r", ctime(&kSysSeconds) );
+            #endif           
             
 
             
@@ -253,6 +251,16 @@
                     if(M2X_response == 202)
                         printf("Location Confidence (%d) POST successful\r\n", thisLoc.uncertainty);
                         
+                        
+                    // Report temperature
+                    float thisTemp = getTemp(&tempPin);
+                    printf("temp=%.1fC\r\n",thisTemp);
+
+                    M2X_response = m2xClient.updateStreamValue(DEVID, "kegtemp", thisTemp);
+                    if(M2X_response == 202)
+                        printf("Temperature (%.2f) POST successful\r\n", thisTemp);
+                        
+                                            
                     // Report system time
                     thisTime = time(NULL);
                     M2X_response = m2xClient.updateStreamValue(DEVID, "systime", ctime(&thisTime) );
@@ -263,7 +271,7 @@
                 }
                 // Fails 3:
                 else {
-                    ++fail3count;
+                    fail3count++;
                     #ifdef LOCDEBUG
                         printf("LOC has high uncertainty (<%i); will not report.\r\n", thisLoc.uncertainty);
                     #endif        
@@ -271,25 +279,25 @@
             }
             // Fails 2:
             else {
-                ++fail2count;
+                fail2count++;
                 #ifdef LOCDEBUG
                     printf("LOC is extrapolated information; disregard.\r\n");
                 #endif
                 
             }            
         }
-        // Fails 1 but inside cellLocReq timeout length; repeat without delay:
-        /* NOT USING THIS STRATEGY
-        else if ( cellLocDelay < cellLocReqTimeout ){
-            printf("Waiting on cellLoc timeout...\r\n");
-        }
-        */
+
         // Fails 1 and timer is up:
         else {
             ++fail1count;
             printf("CellLocRequest timeout; no location data available.\r\n");    
         }
         
+        
+        
+
+        
+        // --------------------
         // DETERMINE LOOP DELAY
         
         
@@ -299,8 +307,8 @@
         // ACTIONS BASED ON LOCATION READ SUCCESS/FAILURE
         
         // Successful read
-        if( (!fail3count) ) {
-            kLocLoopDelay = 5 * (60 * 1000);    // Set # minute wait before next read
+        if( (!fail1count) && (!fail2count) && (!fail3count) ) {
+            kLocLoopDelay = 1 * (60 * 1000);    // Set # minute wait before next read
             printf("Successful LOC read; ");
         }
         // Too many invalid location reads
@@ -330,6 +338,13 @@
             printf("Failed LOC loop; ");
         }
         
+        // If we have no status update to send, send one every 10 loops just cause.
+        if (!sendStatus && (loopIter % 10 == 0) ) {
+            sprintf(statusBuf, "Periodic status update check-in.\r\n");
+            printf(statusBuf);            
+            sendStatus = true;
+        }
+        
         // Transmit a status response?
         if(sendStatus) {
             M2X_response = m2xClient.updateStreamValue(DEVID, "status", statusBuf);
@@ -344,98 +359,6 @@
         delay(kLocLoopDelay); 
         led1 = 0;   // Turn that LED off!
         
- 
-        continue;
-        // Skip this old crap
-        
-        // OLD AF
-        // Have location data
-        if ( mdm.cellLocGet(&thisLoc) ){
-            printf("cellLocDelay=%f\r\n",cellLocDelay.read());           
-
-            
-            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) );
-            }
-            
-            
-            // Check location data
-            if( ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccLower ) || ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccUpper && loopIter > readLoopMax ) ) {
-                
-                printf("\r\nTRANSMIT LOCATION DATA:\r\nla=%0.5f, lo=%0.5f, alt=%d\r\n", ponyLoc.latitude, ponyLoc.longitude, ponyLoc.altitude);
-                M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) ponyLoc.latitude, (double) ponyLoc.longitude, (double) ponyLoc.altitude);
-                
-                if(M2X_response == 202)
-                    printf("Location POST successful\r\n");
-                
-                
-                // Report uncertainty
-                M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", ponyLoc.uncertainty);
-                
-                if(M2X_response == 202)
-                    printf("Location Confidence (%d) POST successful\r\n", ponyLoc.uncertainty);
-
-                    
-                                    
-                locLock = true;
-            }             
-        }
-        
-        // No location data
-        else {
-            printf("cellLocGet failed.\r\n");
-            //
-        }
-
-        // Wait only if we've received a good location reading
-        if(locLock) {
-            printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
-            ponyLoc.uncertainty = locAccUpper + 1;
-            continue;
-        }
-        else if (loopIter > readLoopMax) {
-            
-            
-            ponyLoc.uncertainty = locAccUpper + 1;
-            printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
-            continue;
-        }
-
-        else {
-            printf("Bad data delay = submitPeriod...\r\n");
-            delay(submitPeriod * 1000);    // Delay between CellLoc requests when not getting good data
-            
-        }
-        led1 = 0;
     }