init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
56:efd9f5613549
Parent:
55:0914bfcedcf8
Child:
57:066dfbe8b4b9
diff -r 0914bfcedcf8 -r efd9f5613549 main.cpp
--- a/main.cpp	Wed Jun 26 14:52:02 2019 +0000
+++ b/main.cpp	Fri Aug 09 14:22:21 2019 +0000
@@ -426,18 +426,22 @@
     if(RET_haveSettings == true) {
         //check location tx delta is set
         if (RET_eventTime_location_tx == 0) {
-            if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:eventTime_location_tx is 0");debug_exe();}
+            //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:eventTime_location_tx is 0");debug_exe();}
             RET_haveSettings = false;
+            addToExceptionString("E3");
         }
         //check location failsafe tx is less than 2 weeks
         if (RET_location_failsafe_seconds_max > (336 * 3600)) {
-            if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:RET_location_failsafe_seconds_max too big");debug_exe();}
+            //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:RET_location_failsafe_seconds_max too big");debug_exe();}
             RET_haveSettings = false;
+            addToExceptionString("E2");
         }
     }
-    //check that we have had an attempted post within failsafe time
-    time_t timeSinceTX = (RET_RTCunixtime - RET_lastTxTime);
-    if (RET_lastTxTime > 0 && timeSinceTX > RET_location_failsafe_seconds_max) {
+    //check that we have had an attempted post within 2x failsafe time
+    long long unixtime = RET_RTCunixtime;
+    long long lasttxtime = RET_lastTxTime;
+    long long timeSinceTX = (unixtime - lasttxtime);
+    if (RET_lastTxTime > 0 && RET_location_failsafe_seconds_max > 0 && timeSinceTX > (RET_location_failsafe_seconds_max * 2)) {
         RET_haveSettings = false;
         addToExceptionString("E1");
     }
@@ -953,7 +957,7 @@
     snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,v:%.2f,e:%d,z:%s.%s,s:0)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,timetaken,error,GLOBAL_exceptionString);
     if (modem.on(RET_force2G)) {
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
-            modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi);
+            modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi);
         }
     }
     ThisThread::sleep_for(250);
@@ -1000,7 +1004,7 @@
                     if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
                         char result[100];
                         bool getResponse = false;
-                        snprintf(result,sizeof(result),"%s",modem.USSDmessage(GLOBAL_failed_broadcasts[i], getResponse, GLOBAL_defaultApi));
+                        snprintf(result,sizeof(result),"%s",modem.USSDmessage(GLOBAL_failed_broadcasts[i], getResponse, 2, GLOBAL_defaultApi));
                         if (strcmp(result, "sendok") == 0) {
                             //clear row
                             memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i]));
@@ -1021,6 +1025,8 @@
     if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
     if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
     RET_setupInProgress = true;
+    GLOBAL_have_GPSlocString_prev = false;
+    RET_lastTxTime = RET_RTCunixtime;
     RET_NetworkFailCount = 0;
     RET_GPSFailCount = 0;
     RET_setting_connection_timeout = 180; //reset to longer value to setup run to help with connection
@@ -1034,7 +1040,7 @@
             char bytestosend[160];
             snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,z:SETUP-%s,k:%s,m:%s,s:1)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,RET_temperature,timetaken_total,timetaken_connection,GLOBAL_exceptionString,SKU,HW_MAJORREVISION);
             char result[200];
-            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, GLOBAL_defaultApi));
+            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             RET_setupInProgress = false; //this turns off the flashing led
             if (strcmp(result, "sendokrecfail") == 0) {
                 //send ok but receive fail
@@ -1101,7 +1107,7 @@
                 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,y:%d,x:%d,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
             }
             char result[180];
-            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi));
+            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi));
         }
     }
 }
@@ -1109,6 +1115,7 @@
     #if BLE_ENABLED
         bleStopAdvertising();
     #endif
+    RET_location_failsafe_seconds_count = 0; //reset failsafe count
     RET_lastTxTime = RET_RTCunixtime;
     //check if we have any outstanding to send
     failed_broadcasts_tx();
@@ -1163,8 +1170,8 @@
             if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR");}
             if (modem.on(RET_force2G)) {
                 char locString[70];
+                memset(locString,0x00,sizeof(locString));
                 int gpsStartTime = RET_RTCunixtime;
-                
                 if (RET_locationTag_present) {
                     snprintf(locString, sizeof(locString), ",l:%s.%d\0",RET_closestLocationTag_id,RET_closestLocationTag_rssi);
                     addToExceptionString("BL");
@@ -1174,6 +1181,7 @@
                         memcpy(locString, GLOBAL_GPSlocString_prev, sizeof(locString));
                         addToExceptionString("P");
                     } else {
+                        GLOBAL_have_GPSlocString_prev = false;
                         memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
                     }
                 }
@@ -1181,6 +1189,30 @@
                 
                 //SEND DATA
                 int connectionStartTime = RET_RTCunixtime;
+                
+                if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
+                    int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for avg ussd response time.
+                    int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
+                    //Check if we should wait for settings back
+                    bool getSettings = true;
+                    //work out if we need to get settings back
+                    if (  ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs  ) { getSettings = false; timetaken_total -= 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,y:%d,x:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,getSettings,locString);
+                    snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.0f,n:%.0f,q:%.0f,w:%.0f,v:%.1f,z:L%s,e:%d,y:%d,x:%d,j:%d,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_temperature_min,RET_temperature_max,RET_humidity,RET_voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,RET_motionStateOnInLocTXInterval,getSettings,locString);
+                    char result[180];
+                    snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
+                    //if (result != "err") {
+                    if (strcmp(result, "err") != 0) {
+                        if (getSettings) {
+                            if (saveSettings(result) == false){sendErrorLog("ERR-LOC-IR");}
+                        }
+                    } else {
+                        sendErrorLog("ERR-LOC-NR");
+                    }
+                }
+                
+                /*
                 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
                     int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for avg ussd response time.
                     int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
@@ -1207,6 +1239,7 @@
                         }
                     }
                 }
+                */
             }
         }
     }
@@ -1236,7 +1269,7 @@
                 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
                     char result[100];
                     bool getResponse = false;
-                    snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, GLOBAL_defaultApi));
+                    snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, 2, GLOBAL_defaultApi));
                     if (strcmp(result, "sendok") == 0) {
                         sendok = true;
                         //if(!RET_debug) {sendok = true;} //FOR TESTING
@@ -1318,7 +1351,6 @@
                     break;
             }
             if(run_location_tx == false && RET_location_failsafe_seconds_count >= RET_location_failsafe_seconds_max) { 
-                RET_location_failsafe_seconds_count = 0; //reset
                 RET_NetworkFailCount = 0; //reset to ensure connection
                 RET_GPSFailCount = 0; // reset to ensure gps try
                 addToExceptionString("FS");