init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Mon Dec 17 19:17:39 2018 +0000
Parent:
14:9a54b1b65bc8
Child:
16:3bf5f1a5f869
Commit message:
6

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json 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/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
--- a/mbed_app.json	Sun Dec 16 21:34:52 2018 +0000
+++ b/mbed_app.json	Mon Dec 17 19:17:39 2018 +0000
@@ -18,7 +18,6 @@
         },
         "NRF52_DK": {
             "target.OUTPUT_EXT": "bin",
-            "target.bootloader_img": null,
             "target.uart_hwfc": 0,
             "nordic.uart_0_fifo_size": 1024,
             "nordic.uart_dma_size": 32,
--- a/modem.cpp	Sun Dec 16 21:34:52 2018 +0000
+++ b/modem.cpp	Mon Dec 17 19:17:39 2018 +0000
@@ -112,6 +112,7 @@
             Thread::wait(1000);
             ATsendCMD("AT+CREG?");
             if (ATwaitForWord("+CREG: 0,5",5000)) {
+                flushSerial();
                 GLOBAL_registeredOnNetwork = true;
             };
         }
@@ -166,6 +167,7 @@
     uint32_t runtime = 0;
     while(!received && runtime < timeout) {
         runtime = (t.read_ms() - startmillis);
+        //runtime = 1; //REMOVE ME
         if (ATwaitForWord("+CUSD: 0",5000)) {
             led1 = 0;
             if ( (matchCount = _uart.scanf(",\"%d#%[^#]",USSDmessageIndex,ATinBuffer) ) > 0 ) {
@@ -187,13 +189,15 @@
 }
 
 
-char* Modem::USSDmessage(char* message, bool needResponse, int maxAttempts) 
+char* Modem::USSDmessage(char* message, bool needResponse, int maxAttempts, char* api) 
 {
     char bytestosend[160];
-    int messageIndex = 1;
+    uint8_t messageIndex = 1;
     int messageLength = strlen(message);
     if (messageLength > USSD_MAXLENGTH) {
-        snprintf(bytestosend,sizeof(bytestosend),"(a:%lld,b:gps2,c:%i,d:3,v:%.2f,z:TOOBIG)",GLOBAL_imei,messageIndex,GLOBAL_voltage);
+        snprintf(bytestosend,sizeof(bytestosend),"(im:%lld,%s,z:TOOBIG,c:%d)\0",messageIndex,GLOBAL_imei,api,messageIndex);
+    } else {
+        memcpy(message, bytestosend, sizeof(bytestosend));
     }
     if (USSDsend(bytestosend, maxAttempts)) {
         if (needResponse) {
@@ -201,7 +205,7 @@
             if (response != "err") {
                 return response;
             } else {
-                return "sendonly";   
+                return "sendonly";
             }
         } else {
             return "ok";   
--- a/modem.h	Sun Dec 16 21:34:52 2018 +0000
+++ b/modem.h	Mon Dec 17 19:17:39 2018 +0000
@@ -23,7 +23,7 @@
             bool registerOnNetwork(int maxAttempts, uint32_t timeout);
             bool USSDsend(char* message, int maxAttempts);
             char* USSDreceive(int messageIndex);
-            char* USSDmessage(char* message, bool needResponse, int maxAttempts);
+            char* USSDmessage(char* message, bool needResponse, int maxAttempts, char* api);
             
             //AT
             void flushSerial(void);