init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Fri Aug 09 14:22:21 2019 +0000
Parent:
55:0914bfcedcf8
Child:
57:066dfbe8b4b9
Commit message:
35 init

Changed in this revision

board.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h 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/board.h	Wed Jun 26 14:52:02 2019 +0000
+++ b/board.h	Fri Aug 09 14:22:21 2019 +0000
@@ -37,5 +37,6 @@
 //#define BD_PAGE_ADDRESS 0x5d000 //380928
 
 #define USERDATA_START          0x7E000 // - 8k for user data
+
 //#define APPDATA_START           0x40000 // - 4k for app data - DEVELOPMENT
 #define APPDATA_START           0x3D000 // - 4k for app data - PRODUCTION
\ No newline at end of file
--- 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");
--- a/main.h	Wed Jun 26 14:52:02 2019 +0000
+++ b/main.h	Fri Aug 09 14:22:21 2019 +0000
@@ -16,7 +16,7 @@
 //------------------------------------------------------------------------------
 //FW DETAILS
 //------------------------------------------------------------------------------ 
-#define FW_VERSION          26
+#define FW_VERSION          35
 //#define SKU                 "GPSPLUSDEV"
 #define SKU                 "GPSPLUS"
 #define HW_MAJORREVISION    "001"
--- a/modem.cpp	Wed Jun 26 14:52:02 2019 +0000
+++ b/modem.cpp	Fri Aug 09 14:22:21 2019 +0000
@@ -183,31 +183,35 @@
             //TURN OFF ECHO
             ATsendCMD("ATE0");
             ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            
+            //DISABLE LOW POWER WARNING
+            //ATsendCMD("AT+QCFG=\"vbatt\",-2,3300,0");
+            //ATwaitForWord("OK",ATTIMEOUT_SHORT);
         
             //DISABLE LNA
-            //ATsendCMD("AT+QGPSCFG=\"lnacontrol\",0");
-            //ATwaitForWord("OK",ATTIMEOUT_SHORT);
-             
+            ATsendCMD("AT+QGPSCFG=\"lnacontrol\",0");
+            ATwaitForWord("OK",ATTIMEOUT_SHORT);
+                         
             //ENABLE AIRPLANE MODE CONTROL WITH PIN
             ATsendCMD("AT+QCFG=\"airplanecontrol\",1");
             ATwaitForWord("OK",ATTIMEOUT_SHORT);
             _w_disable.output();
             _w_disable = 0;
-                        
+                
             if (force2G) {
-                ATsendCMD("AT+QCFG=\"nwscanseq\",1"); //2G priority
+                ATsendCMD("AT+QCFG=\"nwscanseq\",1,1"); //2G priority
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
-                ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
+                ATsendCMD("AT+QCFG=\"nwscanmode\",1,1"); //2G only connection
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
             } else {
                 //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tri)
-                ATsendCMD("AT+QCFG=\"nwscanseq\",1"); //2G priority
-                //ATsendCMD("AT+QCFG=\"nwscanseq\",2"); //3G priority
-                //ATsendCMD("AT+QCFG=\"nwscanseq\",0"); //AUTO - default
+                //ATsendCMD("AT+QCFG=\"nwscanseq\",1,1"); //2G priority
+                //ATsendCMD("AT+QCFG=\"nwscanseq\",2,1"); //3G priority
+                ATsendCMD("AT+QCFG=\"nwscanseq\",0,1"); //AUTO - default
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
-                ATsendCMD("AT+QCFG=\"nwscanmode\",0"); //AUTO
-                //ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
-                //ATsendCMD("AT+QCFG=\"nwscanmode\",2"); //3G only connection
+                ATsendCMD("AT+QCFG=\"nwscanmode\",0,1"); //AUTO
+                //ATsendCMD("AT+QCFG=\"nwscanmode\",1,1"); //2G only connection
+                //ATsendCMD("AT+QCFG=\"nwscanmode\",2,1"); //3G only connection
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
             }
             return true;
@@ -337,7 +341,7 @@
         t.stop();
     }
     NRFuart_flush();
-    if (GLOBAL_registeredOnNetwork == true) {
+    if (GLOBAL_registeredOnNetwork == true) {        
         BYREF_NetworkFailCount = 0;
         ThisThread::sleep_for(1000); //wait 1 seconds for things to settle
         return true;
@@ -438,6 +442,90 @@
     }
 }
  
+bool Modem::USSDsend(char* message, int maxAttempts) 
+{
+    bool sent = false;
+    int attempt = 0;
+    //TRY X NUMBER OF TIMES
+    while (!sent && attempt < maxAttempts) {
+        char bytestosend[160];
+        snprintf(bytestosend, sizeof(bytestosend), "AT+CUSD=1,\"#469*%s#\"", message);
+        ATsendCMD(bytestosend);
+        if (ATwaitForWord("+CUSD: 0",ATTIMEOUT_MED)) {
+            sent = true;
+        };
+        NRFuart_flush();
+        attempt ++;
+    }
+    if (sent) {
+        return true;
+    } else {
+        return false;   
+    }
+}
+
+char* Modem::USSDreceive(int messageIndex) 
+{
+    bool received = false;
+    uint32_t timeout = ATTIMEOUT_LONG;
+    int USSDmessageIndex = 0;
+    int matchCount = 0;
+    Timer t;
+    t.start();
+    //TRY UNTIL TIMEOUT
+    uint32_t startmillis = t.read_ms();
+    uint32_t runtime = 0;
+    while(!received && runtime < timeout) {
+        runtime = (t.read_ms() - startmillis);
+        if (ATwaitForWord("+CUSD: 0",ATTIMEOUT_SHORT)) {
+            if (ATgetResponse('\r',ATTIMEOUT_SHORT)) {  
+                if ( (matchCount = sscanf(ATinBuffer,",\"%d#%[^#]",USSDmessageIndex,ATinBuffer) ) > 0 ) {
+                    if (USSDmessageIndex == messageIndex) {
+                        //NEED TO GET THIS WORKING SO WE KNOW WE ARE DEALING WITH THE RIGHT MESSAGE
+                        //MOVE THE BELOW INTO THIS IF STAEMEBNTS WHEN DONE
+                    }
+                    received = true;
+                }
+            }
+        }
+    }
+    NRFuart_flush();
+    if (received) {
+        return ATinBuffer;
+    } else {
+        return "err";
+    }
+}
+
+char* Modem::USSDmessage(char* message, bool needResponse, int maxAttempts, char* api) 
+{  
+    uint8_t messageIndex = 1;
+    bool result;
+    int messageLength = strlen(message);
+    if (messageLength > USSD_MAXLENGTH) {
+        char message_failsafe[100];
+        snprintf(message_failsafe,sizeof(message_failsafe),"(%s,a:error,z:TOOBIG,s:1,c:%d)\0",api,messageIndex);
+        result = USSDsend(message_failsafe, maxAttempts);
+    } else {
+        result = USSDsend(message, maxAttempts);
+    }
+    if (result) {
+        if (needResponse) {
+            char* response = USSDreceive(messageIndex);
+            if (strcmp(response, "err") != 0) {
+                return response;
+            } else {
+                return "sendonly";
+            }
+        } else {
+            return "ok";   
+        } 
+    } else {
+        return "err";   
+    }
+}
+
+/*
 bool Modem::USSDsend(char* message) 
 {
     bool sent = false;
@@ -460,7 +548,7 @@
         
     //TRY X NUMBER OF TIMES
     //maxAttempts
-    while (!sent && attempt < 100) { //20 seconds, 100*200ms
+    while (!sent && attempt < 10) { //20 seconds, 100*200ms
         //TURN OFF ECHO //we need this again incase of modem brown out, as it means the modems starts echoing again
         ATsendCMD("ATE0");
         ATwaitForWord("OK",ATTIMEOUT_SHORT);
@@ -470,6 +558,9 @@
                 sent = true;
             };
         }
+        if (sent == false) {
+            ThisThread::sleep_for(1000);   
+        }
         NRFuart_flush();
         attempt ++;
     }
@@ -539,6 +630,7 @@
         return "sendfail";   
     }
 }
+*/
 
 char* Modem::getLocation(uint8_t accuracy, uint16_t timeout_seconds, uint32_t &BYREF_GPSFailCount, uint32_t &BYREF_NetworkFailCount) 
 { 
@@ -546,7 +638,7 @@
     bool haveGPSFix = false;
     bool haveCellFix = false;
     float utc; float lat; float lng; float hdp; float alt; uint8_t fix; float cog; float spkm; float spkn; uint32_t date; uint8_t sat;
-    static char locDataOut[100];
+    static char locDataOut[70];
     memset(locDataOut,0x00,sizeof(locDataOut));
     Timer t;
     t.start();
@@ -658,6 +750,9 @@
     t.reset();
     t.stop();
     
+    //debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "locdata: %s",locDataOut);debug_exe();
+
+    
     //RETURN
     if (accuracy == 0) {
         sprintf(locDataOut,"\0");
--- a/modem.h	Wed Jun 26 14:52:02 2019 +0000
+++ b/modem.h	Fri Aug 09 14:22:21 2019 +0000
@@ -26,9 +26,14 @@
             //funcs
             bool registerOnNetwork(int maxAttempts, uint32_t timeout, uint32_t &BYREF_NetworkFailCount);
             char* HTTPpost(char* url, char* message, bool getResponse, int maxAttempts);
+            /*
             bool USSDsend(char* message);
             char* USSDreceive(void);
             char* USSDmessage(char* message, bool needResponse, char* api);
+            */
+            bool USSDsend(char* message, int maxAttempts);
+            char* USSDreceive(int messageIndex);
+            char* USSDmessage(char* message, bool needResponse, int maxAttempts, char* api);
             char* getLocation(uint8_t accuracy, uint16_t timeout_seconds, uint32_t &BYREF_GPSFailCount, uint32_t &BYREF_NetworkFailCount); //accuracy is 1,2
             uint64_t getIMEI(void);
             uint64_t getCCID(void);