init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Thu Jan 17 21:59:18 2019 +0000
Parent:
39:f767b8037475
Child:
41:07e41dbd5304
Commit message:
fixes and error logging

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
modem.cpp Show annotated file Show diff for this revision Revisions of this file
modem.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jan 17 15:20:14 2019 +0000
+++ b/main.cpp	Thu Jan 17 21:59:18 2019 +0000
@@ -3,7 +3,7 @@
 //------------------------------------------------------------------------------
 //DEFINES
 //------------------------------------------------------------------------------ 
-#define FW_VERSION          14
+#define FW_VERSION          21
 #define SKU                 "GPSPLUS"
 #define HW_MAJORREVISION    "001"
 
@@ -70,8 +70,8 @@
 uint16_t         RET_setting_beacon_interval_seconds;
 uint16_t         RET_setting_beacon_scan;
 //STATE
+uint8_t          RET_coldBoot = 1;
 bool             RET_asleep = false;
-uint8_t          RET_coldBoot = 1;
 time_t           RET_RTCunixtime;
 bool             RET_haveSettings;
 uint8_t          RET_state;
@@ -84,7 +84,7 @@
 bool             RET_force2G;
 bool             RET_watchdogfired;
 //MOTION STATE
-bool             RET_motionTriggered;
+bool             RET_motionTriggeredinFrame;
 bool             RET_motionTriggeredInTXInterval;
 time_t           RET_motionStartTime;
 time_t           RET_motionStopTime;
@@ -338,7 +338,7 @@
     RET_buttonPressTime = 0;
     RET_buttonHoldTime = 0;
     //MOTION STATE
-    RET_motionTriggered = 0;
+    RET_motionTriggeredinFrame = 0;
     RET_motionTriggeredInTXInterval = 0;
     RET_motionStartTime = 0;
     RET_motionStopTime = 0;
@@ -407,7 +407,7 @@
 void checkMotion() {
     if (lis3dh_int2) {
         //if (GLOBAL_debugLED) LED1blink(1,50);
-        RET_motionTriggered = true;
+        RET_motionTriggeredinFrame = true;
         GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
         if (!RET_motionPendingOnState) {
             RET_motionPendingOnState = true;
@@ -416,7 +416,7 @@
             RET_motionStartTime = RET_RTCunixtime;
         }
     } else {
-        RET_motionTriggered = false;
+        RET_motionTriggeredinFrame = false;
         RET_motionPendingOnState = false;
         if (!RET_motionPendingOffState) {
             RET_motionPendingOffState = true;
@@ -444,7 +444,7 @@
             RET_motionPendingOffState = false;
             RET_motionState = false;
             GLOBAL_motionStopFlagTriggered = true;
-            RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); 
+            RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0f); 
             if (GLOBAL_debugLED) LED1blink(2,50);
             if (RET_setting_location_tx_failsafe_hrs > 0) {
                 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
@@ -476,7 +476,7 @@
         if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a;                 changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_minimumupdate_hrs:%u..%c\n",RET_setting_minimumupdate_hrs,changed);
         if(TEMP_b != 0)  { RET_RTCunixtime = TEMP_b; changed = 'Y';                               } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_RTCunixtime:%u..%c\n",RET_RTCunixtime,changed);
         if(TEMP_c != -1) { RET_setting_firmware = TEMP_c;                          changed = 'Y'; } else { changed = 'N'; RET_setting_firmware = -1;}; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,changed);
-        if(TEMP_d != -1) { setState(TEMP_d);                                       changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
+        if(TEMP_d != -1) { setState(TEMP_d);                                       changed = 'Y'; } else { setState(STATE_NORMAL); changed = 'N'; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
         if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e;                     changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_mode:%d..%c\n",RET_setting_location_mode,changed);
             if(RET_setting_location_mode > 3) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
         if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f;                 changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
@@ -501,7 +501,7 @@
         
         if (critical_fail_count == 0) { 
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "GOT SETTINGS OK");debug_exe();}
-            dumpSettings(); 
+            dumpSettings();
             saveEventTimes();
             RET_haveSettings = true;
             GLOBAL_needToConfigureLis3dh = true;
@@ -552,6 +552,19 @@
 }
 
 //------------------------------------------------------------------------------
+// ERROR LOG
+//------------------------------------------------------------------------------ 
+void sendErrorLog(char* error) {
+    float voltage = getBatteryV();
+    int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
+    char bytestosend[100];
+    memset(bytestosend,0x00,sizeof(bytestosend));
+    snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,v:%.2f,e:%d,z:%s,c:1,s:0)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken,error);
+    modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi);
+    ThisThread::sleep_for(250);
+}
+
+//------------------------------------------------------------------------------
 // SETUP
 //------------------------------------------------------------------------------ 
 bool setup() {
@@ -561,14 +574,12 @@
     bool selftestresult = selfTest();
     if (selftestresult == false) { LED1errorCode(4,20); } //ERROR 4
     if (modem.on(RET_force2G)) {
-        //RET_imei = modem.getIMEI();
-        //DEBUG("imei: %lld \n",RET_imei);
         char locString[70];
         memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70);
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,SKU,HW_MAJORREVISION,locString);
             char result[200];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (strcmp(result, "err") != 0) {
@@ -578,9 +589,11 @@
                     pass = false;
                     modem.off(false);
                     if(GLOBAL_debugLED) LED1errorCode(5,10); //ERROR 5
-                }                           
+                    sendErrorLog("SETTINGS");
+                }                      
             } else {
                 //Response error
+                sendErrorLog("RESPONSE");
             }
         } else {
             //FAILUREMODE modem failed to register on network
@@ -624,11 +637,15 @@
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
             if (result != "err") {
-                //DEBUG("\nfromserver: %s \n",result);
                 if (getSettings) {
-                    saveSettings(result);
-                    if(GLOBAL_debugLED) LED1blink(5,250);
+                    if (saveSettings(result)) {
+                        if(GLOBAL_debugLED) LED1blink(5,250);
+                    } else {
+                        sendErrorLog("SETTINGS");
+                    }
                 }
+            } else {
+                sendErrorLog("RESPONSE");
             }
         } else {
             modem.off(false);
@@ -660,8 +677,7 @@
                 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,timetaken,locString);
             }
             char result[180];
-            bool getSettings = false;
-            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
+            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi));
         } else {
             modem.off(false);  
         }
@@ -685,11 +701,9 @@
         //SEND DATA
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time 
-            
             //Check if we should wait for settings back
             bool getSettings = true;
             if (  ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs  ) { getSettings = false; timetaken -= 10;} //remove the extra 10 seconds from times
-            
             char bytesToSend[160];
             snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,getSettings,locString);
             char result[180];
@@ -698,7 +712,11 @@
                 //DEBUG("\nfromserver: %s \n",result);
                 if (getSettings) {
                     saveSettings(result);
+                } else {
+                    sendErrorLog("SETTINGS");   
                 }
+            } else {
+                sendErrorLog("RESPONSE");
             }
         } else {
             modem.off(false);
@@ -717,6 +735,7 @@
 void event_activity_tx() {
     DEBUG("ACTIVITY TX\n",false);
     float temperature = getTemperature();
+    float voltage = getBatteryV();
     if (modem.on(RET_force2G)) {
         //SEND DATA
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
@@ -755,6 +774,7 @@
             DEBUG("STATE:SETUP\n",false);
             if (setup()) {
                 // All good
+                setState(STATE_NORMAL);
             } else {
                 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs
                 setState(STATE_DORMANT);
@@ -766,9 +786,6 @@
         {
             DEBUG("STATE:NORMAL\n",false);
             
-            //check and log motion
-            checkMotion();
-            
             //LOCATION EVENT
             bool run_location_tx = false;
             switch (RET_setting_location_mode) {
@@ -776,7 +793,13 @@
                     if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
                     break;
                 case 2: //INTERVAL POST WITH MOTION CHECK
-                    if(RET_motionTriggeredInTXInterval && RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
+                    if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { 
+                        if (RET_motionTriggeredInTXInterval) {
+                            run_location_tx = true;
+                        } else {
+                             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX due to no motion\n");debug_exe();}
+                        }
+                    }
                     break;
                 case 3: //POST ON STOP MOTION
                     if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; }
@@ -913,7 +936,7 @@
         setDefaults(); 
         //check battery
         float voltage = getBatteryV();
-        if (voltage > 2.5) {
+        if (voltage > 2.5f) {
             //battery ok
             LED1errorCode(6,2);
         } else {
@@ -927,6 +950,7 @@
     while(true) {
         //WATCHDOG
         watchdogKick();
+        LED1off();
         RET_asleep = false;
         
         //INIT
@@ -934,15 +958,18 @@
         healthCheck(); //this must be after resetGlobals
         GLOBAL_wakeTime = RET_RTCunixtime;
         
+        //check and log motion
+        if (RET_state == STATE_NORMAL) { checkMotion(); }
+        
         //MAIN LOGIC
         //DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
         //DEBUG("ACTIVITY:%s\n",RET_activityData);
         if(DEBUG_ON){
             if (RET_state != 99) {
-                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ST:%d, SETTINGS:%d, MOT: %d, RTC:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
+                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FW:%d,ST:%d, SET:%d, MOTFR:%d, MOTST: %d, RTC:%u, LOC:%u, LOCFS:%u, ACT:%u \n", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, RET_motionState, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
                 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACT:%s\n",RET_activityData);debug_exe();
             } else {
-                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ST:%d, WAKE@:%u, \n", RET_state, RET_eventTime_wakeFromDormant);debug_exe();
+                debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FW:%d, ST:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe();
             }
         }
 
@@ -968,6 +995,7 @@
         }
         
         //PRE-SLEEP ACTIONS
+        RET_motionTriggeredinFrame = false;
         LED1off();
         if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
         modem.off(false);
--- a/mbed_app.json	Thu Jan 17 15:20:14 2019 +0000
+++ b/mbed_app.json	Thu Jan 17 21:59:18 2019 +0000
@@ -23,8 +23,6 @@
             "platform.sys-stats-enabled": false
         },
         "NRF52_DK": {
-            "target.mbed_app_start": "0x3E000",
-            "target.bootloader_img": null,
             "target.OUTPUT_EXT": "bin",
             "target.uart_hwfc": 0,
             "nordic.uart_0_fifo_size": 1024,
--- a/modem.cpp	Thu Jan 17 15:20:14 2019 +0000
+++ b/modem.cpp	Thu Jan 17 21:59:18 2019 +0000
@@ -85,9 +85,9 @@
     if (!GLOBAL_modemOn) {
         _w_disable = 0; // this sets the modem to airplane mode
         _vreg_en = 1;
-        Thread::wait(200);
+        ThisThread::sleep_for(200);
         _pwrkey = 0;
-        Thread::wait(200);
+        ThisThread::sleep_for(200);
         _pwrkey = 1;
         
         GLOBAL_modemOn = true;
@@ -134,7 +134,7 @@
     if (soft) {
         //ATsendCMD("AT+QPOWD");
         //_pwrkey = 0;
-        //Thread::wait(800);
+        //ThisThread::sleep_for(800);
         //_pwrkey = 1;
         //ATwaitForWord("POWERED DOWN",ATTIMEOUT_SHORT);
     }
@@ -189,7 +189,7 @@
             uint32_t runtime = 0;
             while(!GLOBAL_registeredOnNetwork && runtime < timeout) {
                 runtime = (t.read_ms() - startmillis);
-                Thread::wait(1000);
+                ThisThread::sleep_for(1000);
                 ATsendCMD("AT+CREG?");
                 if (ATwaitForWord("+CREG: 0,5",3000)) {
                     NRFuart_flush();
@@ -198,7 +198,7 @@
             }
             if (!GLOBAL_registeredOnNetwork) {
                 off(true);
-                Thread::wait(1000);
+                ThisThread::sleep_for(1000);
                 on(RET_force2G);
             }
             attempt ++;
@@ -212,7 +212,7 @@
         return false;   
     }
 }
-
+ 
 bool Modem::USSDsend(char* message, int maxAttempts) 
 {
     bool sent = false;
@@ -222,10 +222,10 @@
         char bytestosend[160];
         snprintf(bytestosend, sizeof(bytestosend), "AT+CUSD=1,\"#469*%s#\"", message);
         ATsendCMD(bytestosend);
-        if (ATwaitForWord("+CUSD: 0",ATTIMEOUT_MED)) {
-            NRFuart_flush();
+        if (ATwaitForWord("+CUSD: 0",ATTIMEOUT_SHORT)) {
             sent = true;
         };
+        NRFuart_flush();
         attempt ++;
     }
     if (sent) {
@@ -328,7 +328,7 @@
         runtime = 0;
         while(!haveGPSFix && runtime < (timeout_seconds*1000)) {
             watchdogKick();
-            Thread::wait(5000); //this goes first
+            ThisThread::sleep_for(5000); //this goes first
             runtime = (t.read_ms() - startmillis);
             ATsendCMD("AT+QGPSLOC=2");
             if (ATwaitForWord("+QGPSLOC: ",ATTIMEOUT_SHORT)) {
@@ -391,7 +391,7 @@
         while(!haveCellFix && runtime < 15000) {
             runtime = (t.read_ms() - startmillis);
             ATsendCMD("AT+QENG=\"servingcell\"");
-            if (ATwaitForWord("+QENG: \"servingcell\",\"NOCONN\",",ATTIMEOUT_SHORT)) {
+            if (ATwaitForWord("+QENG: \"servingcell\",\"NOCONN\",",ATTIMEOUT_VERYSHORT)) {
                 if (ATgetResponse('\r',ATTIMEOUT_SHORT)) {
                     if ((matchCount = sscanf(ATinBuffer,"\"%[^\"]\",%d,%d,%[^,],%[^,]",&type,&mcc,&mnc,&lac,&cellID)) == 5 ) {
                         sprintf(locDataOut,",h:%s.%s.%d.%d\0",cellID,lac,mcc,mnc);
--- a/modem.h	Thu Jan 17 15:20:14 2019 +0000
+++ b/modem.h	Thu Jan 17 21:59:18 2019 +0000
@@ -8,6 +8,7 @@
     #define UC20_BAUDRATE       115200
     #define SIM7600_BAUDRATE    115200
     #define USSD_MAXLENGTH      160
+    #define ATTIMEOUT_VERYSHORT 2000
     #define ATTIMEOUT_SHORT     5000
     #define ATTIMEOUT_MED       10000
     #define ATTIMEOUT_LONG      20000