init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
15:7aad9a7f970c
Parent:
14:9a54b1b65bc8
Child:
16:3bf5f1a5f869
--- a/main.cpp	Sun Dec 16 21:34:52 2018 +0000
+++ b/main.cpp	Mon Dec 17 19:17:39 2018 +0000
@@ -12,6 +12,7 @@
 //------------------------------------------------------------------------------
 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
 //------------------------------------------------------------------------------ 
+char* GLOBAL_defaultApi = "b:gps2,d:3";
 bool GLOBAL_accel_healthy = false;
 bool GLOBAL_requireSoftReset = false;
 bool GLOBAL_motionFlagTriggered = false;
@@ -29,8 +30,10 @@
 #if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS
 //IDENTITY
 static long long        RET_imei                            __attribute__((section("noinit"),zero_init));
+static char             RET_pfIdentifier[6]                 __attribute__((section("noinit"),zero_init));
 //STATE
 static uint8_t          RET_coldBoot                        __attribute__((section("noinit"),zero_init));
+static bool             RET_haveSettings                    __attribute__((section("noinit"),zero_init));
 static uint8_t          RET_mode                            __attribute__((section("noinit"),zero_init));
 static uint8_t          RET_mode_prev                       __attribute__((section("noinit"),zero_init));
 static uint8_t          RET_buttonPressCount                __attribute__((section("noinit"),zero_init));
@@ -70,6 +73,7 @@
 //GPIO
 //------------------------------------------------------------------------------ 
 InterruptIn button(PN_IN_BUTTON); //This causes wake from sleep
+DigitalOut vreg_en(PN_VREG_EN);
 
 //------------------------------------------------------------------------------
 //PERIPHERALS
@@ -100,7 +104,7 @@
 // LOW LEVEL FUNCS
 //------------------------------------------------------------------------------ 
 void turnOffEverything() {
-    //vreg_en = 0;
+    vreg_en = 0;
     LED1off();
 }
 void gotoSleep(long sleep_milliseconds) {
@@ -145,11 +149,14 @@
 }
 void factoryReset() {
     DEBUG("Factory Reset \n");
+    Thread::wait(500); //wait to ensure that devices are turned off
     GLOBAL_requireSoftReset = false;
     
     //RESET RETAINED VARS
     RET_imei = 0;
+    memset(RET_pfIdentifier,0,sizeof(RET_pfIdentifier));
     RET_coldBoot = 1;
+    RET_haveSettings = 0;
     RET_mode = MODE_SETUP;
     RET_mode_prev = RET_mode;
     RET_RTCunixtime = 0;
@@ -181,7 +188,7 @@
     lis3dh_configureForSleep(10,127);
 }
 void copyRETtoGLOBAL() {
-    GLOBAL_imei = RET_imei;   
+    GLOBAL_imei = RET_imei; 
 }
 void selfTest() {
     //Accelerometer
@@ -256,7 +263,7 @@
 bool saveSettings(char* settingsBuffer) {
     //process result
     int matchCount = 0;
-    time_t TEMP_a = 0;
+    char TEMP_a[6];
     int TEMP_b = 0;
     int TEMP_c = 0;
     int TEMP_d = 0;
@@ -270,12 +277,15 @@
     int TEMP_l = 0;
     int TEMP_m = 0;
     int TEMP_n = 0;
-    if ( (matchCount = sscanf(settingsBuffer,"a:%u,b:%d,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d",
-    &TEMP_a,&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) ) > 0 ) {
-        DEBUG("VALUES: a:%u,b:%d,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d",TEMP_a,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);
+    time_t TEMP_o = 0;
+    if ( (matchCount = sscanf(settingsBuffer,"a:%c%c%c%c%c%c,b:%d,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,0:%u",
+    &TEMP_a[0],&TEMP_a[1],&TEMP_a[2],&TEMP_a[3],&TEMP_a[4],&TEMP_a[5],&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) ) > 0 ) {
+        DEBUG("VALUES: a:%s,b:%d,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\n",TEMP_a,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);
+        RET_haveSettings = true;
         return true;
     } else {
-        return false;   
+        RET_haveSettings = false;
+        return false;
     }
 }
 
@@ -284,15 +294,19 @@
 // SETUP
 //------------------------------------------------------------------------------ 
 bool setup() {
+    DEBUG("SETUP\n");
     bool pass = true;
-    
+    //LED1on(0);
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
     if (modem.on()) {
         RET_imei = modem.getIMEI();
         GLOBAL_imei = RET_imei;
         DEBUG("imei: %lld \n",RET_imei);
         if (modem.registerOnNetwork(3,60000)) {
-                char* result = modem.USSDmessage("blahblah\0", true, 2);
+                char* bytestosend;
+                sprintf(bytestosend,"(im:%lld,%s,v:%.2f,fr:1)\0",GLOBAL_imei,GLOBAL_defaultApi,GLOBAL_voltage);
+                //DEBUG("SENDING:%s\n",bytestosend);
+                char* result = modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi);
                 if (result != "err") {
                     DEBUG("result: %s \n",result);
                     saveSettings(result);
@@ -304,6 +318,7 @@
         pass = false;
         //Modem failed to turn on   
     }
+    //LED1off();
      
     while (1) {
         Thread::wait(60000);
@@ -323,6 +338,7 @@
 // STATE ENGINE
 //------------------------------------------------------------------------------ 
 void mainStateEngine() {
+    DEBUG("StateEngine:%d \n",RET_mode);
     switch(RET_mode) {
         case MODE_SETUP :
             factoryReset();
@@ -414,7 +430,7 @@
         NRF_POWER->RESETREAS = 0xffffffff;
     }
     //CHECK FOR FIRST BOOT
-    if (RET_coldBoot != 0) factoryReset();
+    if (RET_coldBoot == 1) factoryReset();
     
     //COPY ESSENTIAL VALUES FROM RET TO GLOBAL
     copyRETtoGLOBAL();
@@ -428,7 +444,7 @@
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        DEBUG("mode: %i  time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
+        DEBUG("State: %i  time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS