init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
28:24b02608fa5f
Parent:
27:fa76f5a08195
Child:
29:059fc7324328
--- a/main.cpp	Sun Dec 23 22:37:25 2018 +0000
+++ b/main.cpp	Sun Dec 23 23:09:53 2018 +0000
@@ -15,6 +15,7 @@
 #define DEFAULT_MOTION_START_SECONDS        120
 #define DEFAULT_MOTION_STOP_SECONDS         120
 #define DEFAULT_IMPACT_G                    127
+#define DEFAULT_CONNECTION_ATTEMPTS         1
 #define DEFAULT_CONNECTION_TIMEOUT          180
 #define DEFAULT_BEACON_INTERVAL_SECONDS     10
 
@@ -246,9 +247,15 @@
                         mainthread.release();
                     }
                     break;
+                case 2 :
+                    if (RET_state == STATE_NORMAL) {
+                        RET_state = STATE_BUTTONPRESS2;
+                        mainthread.release();
+                    }
+                    break;
                 case 3 :
                     if (RET_state == STATE_NORMAL) {
-                        RET_state = STATE_BUTTONPRESS2;
+                        RET_state = STATE_BUTTONPRESS3;
                         mainthread.release();
                     }
                     break;
@@ -505,10 +512,12 @@
         //RET_imei = modem.getIMEI();
         //DEBUG("imei: %lld \n",RET_imei);
         //char* modemModel = modem.getModemModel();
-        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
+        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);
             char bytestosend[160];
-            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);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString);
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
@@ -537,22 +546,24 @@
 //------------------------------------------------------------------------------
 // EVENTS
 //------------------------------------------------------------------------------ 
-void event_connectiontest_tx() {
-    DEBUG("CONNECTION TEST\n");
+void event_connectiontest_tx(int location_accuracy) {
+    DEBUG("TEST EVENT\n");
     float voltage = getBatteryV();  
     float temperature = getTemperature();
     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))) {
+        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);
             char bytestosend[160];
-            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);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:CONNTEST-%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));
             if (result != "err") {
-                DEBUG("\nfromserver: %s \n",result);
+                //DEBUG("\nfromserver: %s \n",result);
                 if (getSettings) {
                     saveSettings(result);
                     LED1blink(6,250);
@@ -578,7 +589,7 @@
             //DEBUG("locString:%s-\n",locString);
         }
         //SEND DATA
-        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
+        if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(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:%s,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
@@ -604,7 +615,7 @@
     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()) {
         //SEND DATA
-        if (modem.registerOnNetwork(1,(RET_setting_connection_timeout*1000))) {
+        if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             char bytesToSend[160];
             snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f,c:1)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHours);
             char result[180];
@@ -696,7 +707,15 @@
             DEBUG("STATE:BUTTONPRESS2\n");
             LED1blink(2,300);
             LED1on(0);
-            event_connectiontest_tx();
+            event_connectiontest_tx(1);
+            LED1off();
+            break;
+        case STATE_BUTTONPRESS3 :
+            setState(STATE_NORMAL);
+            DEBUG("STATE:BUTTONPRESS3\n");
+            LED1blink(3,300);
+            LED1on(0);
+            event_connectiontest_tx(2);
             LED1off();
             break;  
         case STATE_BUTTONHOLD :
@@ -730,7 +749,7 @@
 int main() {
     //INIT
     turnOffEverything();
-    if (!memoryIntegrityCheck()) { RET_coldBoot = 1; LED1on(10000);}
+    if (!memoryIntegrityCheck()) { RET_coldBoot = 1; LED1on(5000);}
     RTCticker.attach(&RTCtick, 1.0);
     button.fall(&buttonPress); //does this affect power?
     button.rise(&buttonRelease);