init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
27:fa76f5a08195
Parent:
26:fa3579737329
Child:
28:24b02608fa5f
--- a/main.cpp	Sun Dec 23 21:13:19 2018 +0000
+++ b/main.cpp	Sun Dec 23 22:37:25 2018 +0000
@@ -43,6 +43,7 @@
 bool GLOBAL_modemOn = false;
 time_t GLOBAL_RTCunixtime = 0;
 time_t GLOBAL_wakeTime = 0;
+char GLOBAL_exceptionString[10];
 
 //------------------------------------------------------------------------------
 //RETAINED NOINIT RAM VARS
@@ -170,6 +171,7 @@
     return pass;
 }
 void dumpSettings() {
+    DEBUG("RET_memTest:%d \n",RET_memTest);
     DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
     DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
     DEBUG("RET_state:%d \n",RET_state);
@@ -206,6 +208,9 @@
     //DEBUG("Temperature:%.2f\n",temperature);
     return temperature;  
 }
+void addToExceptionString(char* value) {
+    snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),"%s.",value);
+}
 //------------------------------------------------------------------------------
 // USER BUTTON HANDLING
 //------------------------------------------------------------------------------ 
@@ -223,7 +228,6 @@
     //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
     RET_RTCunixtime += 1;
     RET_RTCunixtime_bu += 1;
-    
     GLOBAL_RTCunixtime = RET_RTCunixtime;
     
     //button logic - check for hold
@@ -242,7 +246,7 @@
                         mainthread.release();
                     }
                     break;
-                case 2 :
+                case 3 :
                     if (RET_state == STATE_NORMAL) {
                         RET_state = STATE_BUTTONPRESS2;
                         mainthread.release();
@@ -333,14 +337,22 @@
     LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
     uint8_t lis3dh_id; 
     lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
-    if (lis3dh_id == 51) {test_pass ++;}
+    if (lis3dh_id == 51) {
+        test_pass ++;
+    } else {
+        addToExceptionString("A.");   
+    }
     
     //Temperature
     test_count ++;
     SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
     float temperature;
     temperature = si7060.getTemperature();
-    test_pass ++;
+    if (temperature > -40 && temperature < 60) {
+        test_pass ++;
+    } else {
+        addToExceptionString("T.");
+    }
     
     //Result
     if (test_count == test_pass) {
@@ -416,25 +428,30 @@
         DEBUG("VALUES: b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d\n",
         TEMP_b,TEMP_c,TEMP_d,TEMP_e,TEMP_f,TEMP_g,TEMP_h,TEMP_i,TEMP_j,TEMP_k,TEMP_l,TEMP_m,TEMP_n,TEMP_o,TEMP_p,TEMP_q,TEMP_r,TEMP_s);
         
-        //FAILUREMODE need to be checking thse against checksums
+        //FAILUREMODE need to be checking these against checksums
         char 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_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 < 0 || 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);
+            if(RET_setting_location_accuracy < 0 || RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;}
         if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_interval_mins:%d..%c\n",RET_setting_location_tx_interval_mins,changed);
         if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h;          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_failsafe_hrs:%d..%c\n",RET_setting_location_tx_failsafe_hrs,changed);
+            if(RET_setting_location_tx_failsafe_hrs < 0 || RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;}
         if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i;                  changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_timeout:%d..%c\n",RET_setting_location_timeout,changed);
+            if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;}
         if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j;          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_hrs:%d..%c\n",RET_setting_activity_tx_interval_hrs,changed);
         if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k;    changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_environmental_tx_interval_mins:%d..%c\n",RET_setting_environmental_tx_interval_mins,changed);
         if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_g:%d..%c\n",RET_setting_motion_g,changed);
+            if(RET_setting_motion_g < 6 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;}
         if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m;              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_start_seconds:%d..%c\n",RET_setting_motion_start_seconds,changed);
         if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n;               changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_stop_seconds:%d..%c\n",RET_setting_motion_stop_seconds,changed);
         if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_g:%d..%c\n",RET_setting_impact_g,changed);
         if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p;                      changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_alert:%d..%c\n",RET_setting_impact_alert,changed);
         if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q;                changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_connection_timeout:%d..%c\n",RET_setting_connection_timeout,changed);
-        if (RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
+            if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
         if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r;           changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_interval_seconds:%d..%c\n",RET_setting_beacon_interval_seconds,changed);
         if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s;                       changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_scan:%d..%c\n",RET_setting_beacon_scan,changed);
         
@@ -491,7 +508,7 @@
         if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP,c:1)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString);
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
@@ -520,17 +537,17 @@
 //------------------------------------------------------------------------------
 // EVENTS
 //------------------------------------------------------------------------------ 
-void event_shakertest_tx() {
-    DEBUG("SHAKER TEST\n");
+void event_connectiontest_tx() {
+    DEBUG("CONNECTION TEST\n");
     float voltage = getBatteryV();  
     float temperature = getTemperature();
-    bool selftestresult = selfTest();
+    int selftestresult = selfTest();
     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()) {
         if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:SHAKERTEST-%d,e:%d,c:1)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:CONNTEST-%s,e:%d,c:1)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
@@ -538,6 +555,7 @@
                 DEBUG("\nfromserver: %s \n",result);
                 if (getSettings) {
                     saveSettings(result);
+                    LED1blink(6,250);
                 }
             }
         }
@@ -551,7 +569,7 @@
     DEBUG("LOCATION TX\n");
     float voltage = getBatteryV();
     float temperature = getTemperature();
-    bool selftestresult = selfTest();
+    int selfTestResult = selfTest();
     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()) {
         char locString[70];
@@ -563,7 +581,7 @@
         if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytesToSend[160];
-            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:OK%d,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,selftestresult,timetaken,locString);
+            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:%s,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
@@ -627,11 +645,25 @@
             
             //LOCATION EVENT
             bool run_location_tx = false;
-            if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
-            //Location Failsafe timer catchall
+            switch (RET_setting_location_mode) {
+                case 1: //INTERVAL POST
+                    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; }
+                    break;
+                case 3: //POST ON STOP MOTION
+                    if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; }
+                    break;
+            }
             if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
             if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
             
+            //if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
+            //Location Failsafe timer catchall
+            //if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
+            //if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
+            
             //ACTIVITY EVENT
             bool run_activity_tx = false;
             if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0 && strlen(RET_activityData) > 1) { 
@@ -663,6 +695,9 @@
             setState(STATE_NORMAL);
             DEBUG("STATE:BUTTONPRESS2\n");
             LED1blink(2,300);
+            LED1on(0);
+            event_connectiontest_tx();
+            LED1off();
             break;  
         case STATE_BUTTONHOLD :
             DEBUG("STATE:BUTTONHOLD\n");
@@ -695,7 +730,7 @@
 int main() {
     //INIT
     turnOffEverything();
-    if (!memoryIntegrityCheck()) { RET_coldBoot = 1;}
+    if (!memoryIntegrityCheck()) { RET_coldBoot = 1; LED1on(10000);}
     RTCticker.attach(&RTCtick, 1.0);
     button.fall(&buttonPress); //does this affect power?
     button.rise(&buttonRelease);