init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
33:760005331b4c
Parent:
32:dff4858bdf37
Child:
34:4493c9f6d707
--- a/main.cpp	Tue Jan 01 20:46:39 2019 +0000
+++ b/main.cpp	Thu Jan 03 10:09:38 2019 +0000
@@ -19,7 +19,6 @@
 #define DEFAULT_CONNECTION_TIMEOUT          180
 #define DEFAULT_BEACON_INTERVAL_SECONDS     10
 
-
 //------------------------------------------------------------------------------
 //FUNCTION PROTOTYPES
 //------------------------------------------------------------------------------ 
@@ -42,6 +41,7 @@
 bool GLOBAL_needToConfigureLis3dh = false;
 bool GLOBAL_registeredOnNetwork = false;
 bool GLOBAL_modemOn = false;
+bool GLOBAL_LEDSequenceinProgress = false;
 time_t GLOBAL_RTCunixtime = 0;
 time_t GLOBAL_wakeTime = 0;
 char GLOBAL_exceptionString[10];
@@ -269,7 +269,7 @@
         }
     }
     //SETUP STATE VISUALISE
-    if (RET_state == STATE_SETUP) {
+    if (!GLOBAL_LEDSequenceinProgress && RET_state == STATE_SETUP) {
         led1 = !led1;
     }
 }
@@ -516,6 +516,7 @@
     float voltage = getBatteryV();
     float temperature = getTemperature();
     bool selftestresult = selfTest();
+    if (selftestresult == false) { LED1errorCode(4,20); } //ERROR 4
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
         //RET_imei = modem.getIMEI();
@@ -524,7 +525,7 @@
         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);
+            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);
             char result[180];
@@ -534,17 +535,19 @@
                 if (!saveSettings(result)) { 
                     //something went critically wrong getting settings 
                     pass = false;
+                    LED1errorCode(5,20); //ERROR 5
                 }              
             }
         } else {
             //FAILUREMODE modem failed to register on network
             DEBUG("NET REGISTER FAIL");
+            LED1errorCode(3,20); //ERROR 3
             pass = false;
         }
     } else {
         //FAILUREMODE Modem failed to turn on  
         DEBUG("MODEM ON FAIL");
-        LED1blink(200,50);
+        LED1errorCode(2,20); //ERROR 2
         pass = false;
     }
     
@@ -568,7 +571,7 @@
         char locString[70];
         memcpy(locString, modem.getLocation(location_accuracy, RET_setting_location_timeout), 70);
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
-            int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
+            int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.;
             char bytestosend[160];
             snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:TEST-%s,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
             char result[180];
@@ -578,12 +581,14 @@
                 //DEBUG("\nfromserver: %s \n",result);
                 if (getSettings) {
                     saveSettings(result);
-                    LED1blink(6,250);
+                    LED1blink(5,250);
                 }
             }
+        } else {
+            LED1errorCode(3,20); //ERROR 3   
         }
     } else {
-        LED1blink(200,50);   
+        LED1errorCode(2,20); //ERROR 2
     }
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
@@ -602,11 +607,11 @@
         //DEBUG("locString:%s-\n",locString);
         //SEND DATA
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
-            int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
+            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;}
+            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);
@@ -618,9 +623,11 @@
                     saveSettings(result);
                 }
             }
+        } else {
+            LED1errorCode(3,20); //ERROR 3   
         }
     } else {
-        LED1blink(200,50);   
+        LED1errorCode(2,20); //ERROR 2
     }
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
@@ -644,9 +651,11 @@
                 memset(RET_activityData,0,sizeof(RET_activityData));
                 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
             }
+        } else {
+            LED1errorCode(3,20); //ERROR 3
         }
     } else {
-        LED1blink(200,50);   
+        LED1errorCode(2,20); //ERROR 3
     }
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
@@ -664,7 +673,7 @@
             if (setup()) {
                 // All good
             } else {
-                LED1blink(200,50);   
+                //LED1blink(200,50);   
                 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs
                 setState(STATE_DORMANT);
                 DEBUG("SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
@@ -793,7 +802,7 @@
     
     
     //CHECK FOR FIRST BOOT
-    if (RET_coldBoot == 1) { factoryReset(); dumpSettings(); }
+    if (RET_coldBoot == 1) { factoryReset(); dumpSettings(); LED1errorCode(6,3); }
     
     //SHALL WE SLEEP
     if (RET_sleepToggle == 1) {