init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Thu May 16 12:31:44 2019 +0000
Parent:
49:15ddd84ec9fa
Child:
51:9078e6928412
Commit message:
init

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h 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	Thu Feb 07 00:34:30 2019 +0000
+++ b/main.cpp	Thu May 16 12:31:44 2019 +0000
@@ -109,9 +109,27 @@
 //------------------------------------------------------------------------------
 // LOW LEVEL FUNCS
 //------------------------------------------------------------------------------
-void nrf_configureGPIOForSleep(){
-    nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL);
+void nrf_configureForSleep(){
+    //Disable SPI pins to reduce power
+    //nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL);  //Don't need this one
     nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
+    
+    //disable modem control lines
+    nrf_gpio_cfg_input(PN_GSM_PWR_KEY, NRF_GPIO_PIN_NOPULL);
+    nrf_gpio_cfg_input(PN_GSM_WAKE_DISABLE, NRF_GPIO_PIN_NOPULL);
+    
+    //ERRATA 89, shut down i2c channels properly
+    //TWI0
+    //NRF_TWI0->ENABLE=0;//TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+    //NRF_TWI0->ENABLE=1;//TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+    *(volatile uint32_t *)0x40003FFC = 0;
+    *(volatile uint32_t *)0x40003FFC;
+    *(volatile uint32_t *)0x40003FFC = 1;
+    //TWI1
+    //NRF_TWI1->ENABLE=TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+    *(volatile uint32_t *)0x40004FFC = 0;
+    *(volatile uint32_t *)0x40004FFC;
+    *(volatile uint32_t *)0x40004FFC = 1;
 }
 void setState(uint8_t state) {
     RET_state_prev = RET_state;
@@ -244,21 +262,21 @@
                 switch (RET_buttonPressCount) {   //double catches to help with debounce
                     case 1 :
                     case 2 :
-                        RET_state_prev = RET_state;
+                        if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
                         RET_state = STATE_BUTTONPRESS1;
                         RET_buttonPressCount = 0;
                         mainthread.release();
                         break;
                     case 3 :
                     case 4 :
-                        RET_state_prev = RET_state;
+                        if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
                         RET_state = STATE_BUTTONPRESS3;
                         RET_buttonPressCount = 0;
                         mainthread.release();
                         break;
                     case 5 :
                     case 6 :
-                        RET_state_prev = RET_state;
+                        if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
                         RET_state = STATE_BUTTONPRESS5;
                         RET_buttonPressCount = 0;
                         mainthread.release();
@@ -270,24 +288,6 @@
             }
         }
     }
-    
-    /*
-    if (RET_setupInProgress) {
-        led1 = 0;
-    } else if (!GLOBAL_LEDSequenceinProgress) {
-        led1 = 1;
-    }
-    */
-    /*
-    if (RTCtick_ledflash_count > 0) {
-        led1 = !led1;
-        RTCtick_ledflash_count --;
-        if (RTCtick_ledflash_count <= 0) {
-            RTCtick_ledflash_count = 0;   
-            led1 = 1; //turn off led
-        }
-    }
-    */
 }
 void resetGlobals() {
     GLOBAL_accel_healthy = false;
@@ -414,7 +414,7 @@
         //INTERNAL NRF52 TEMP SENSOR
         temperature = nrfTemperature();
     } else {
-        temperature = si7060.getTemperature();
+        //temperature = si7060.getTemperature();
     }
     if (temperature > -40 && temperature < 60) {
         test_pass ++;
@@ -461,7 +461,7 @@
         //check if above threshold
         time_t inMotionForSeconds = ((RET_RTCunixtime - RET_motionStartTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
         if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOT:%u s",inMotionForSeconds);debug_exe();}
-        if (inMotionForSeconds >= RET_setting_motion_start_seconds && RET_motionState == false) {
+        if (inMotionForSeconds >= (RET_setting_motion_start_seconds + 15) && RET_motionState == false) {
             RET_motionState = true;
             RET_motionTriggeredInLocTXInterval = true;
             //if (GLOBAL_debugLED) LED1blink(1,50);
@@ -476,7 +476,7 @@
     if (RET_motionPendingOffState) {
         time_t noMotionForSeconds = ((RET_RTCunixtime - RET_motionStopTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
         if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOTLESS:%u s",noMotionForSeconds);debug_exe();}
-        if (noMotionForSeconds >= RET_setting_motion_stop_seconds && RET_motionState == true) {
+        if (noMotionForSeconds >= (RET_setting_motion_stop_seconds + 15) && RET_motionState == true) {
             //if (GLOBAL_debugLED) LED1blink(2,50);
             RET_motionPendingOffState = false;
             RET_motionState = false;
@@ -505,15 +505,31 @@
     int critical_fail_count = 0;
     int TEMP_a = -1; time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1; 
     int TEMP_k = -1; int TEMP_l = -1; int TEMP_m = -1; int TEMP_n = -1; int TEMP_o = -1; int TEMP_p = -1; int TEMP_q = -1; int TEMP_r = -1; int TEMP_s = -1; int TEMP_t = -1;
-    //int varTotal = 19; //change this if var number changes!!!!!!!!
+    
+    //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RESP: %s",settingsBuffer);debug_exe();}
+    
     if ( (matchCount = sscanf(settingsBuffer,"a:%d,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,t:%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,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s,&TEMP_t) ) > 0 ) {
         
-        if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FROMSERVER: a:%d,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,t:%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,TEMP_p,TEMP_q,TEMP_r,TEMP_s,TEMP_t);debug_exe();}
+        //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SERVERPARAMS: a:%d,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,t:%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,TEMP_p,TEMP_q,TEMP_r,TEMP_s,TEMP_t);debug_exe();}
         
         if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a;                      }
-        if(TEMP_b != 0)  { RET_RTCunixtime = TEMP_b;                                    } else { critical_fail_count++; }
+        if(TEMP_b > 1552915630) {
+            if (RET_RTCunixtime < 1552915630) {
+                //assume this is first proper unixtime and set
+                RET_RTCunixtime = TEMP_b;
+            } else {
+                //this is not a first unixtime, so check within safe range
+                if ((TEMP_b - RET_RTCunixtime) < 604800) { //allow 1 week difference
+                    RET_RTCunixtime = TEMP_b;
+                } else {
+                    critical_fail_count++;
+                }
+            }
+        } else { 
+            critical_fail_count++;
+        }
         if(TEMP_c != -1) { RET_setting_firmware = TEMP_c;                               } else { RET_setting_firmware = -1;}
         if(TEMP_d != -1) { setState(TEMP_d);                                            } else { setState(STATE_NORMAL);}
         if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e;                          }
@@ -541,7 +557,7 @@
 
         if (critical_fail_count == 0) { 
             RET_receivedNewSettings = true;
-            if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SETTINGS OK");debug_exe();}
+            //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SETTINGS OK");debug_exe();}
             dumpSettings();
             RET_haveSettings = true;
             GLOBAL_needToConfigureLis3dh = true;
@@ -563,8 +579,7 @@
             }
             return true;
         } else {
-            if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
-            dumpSettings();
+            //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
             addToExceptionString("SCF");
             //dont set RET_haveSettings = false; here, if we already have settings continue to use them
             return false;
@@ -892,6 +907,7 @@
         }
         case STATE_DORMANT :
         {
+            RET_busy = true;
             if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
                 if (RET_haveSettings) {
                     setState(STATE_NORMAL); 
@@ -900,10 +916,12 @@
                 }
             }
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
+            RET_busy = false;
             break;
         }
         case STATE_BUTTONPRESS1 :
         {
+            RET_busy = true;
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();}
             if (getBatteryV() < 2.5f) {
                 LED1blink(3,500);
@@ -916,11 +934,17 @@
                     LED1blink(2,500);
                 }
             }
-            setState(RET_state_prev);
+            if (RET_state_prev != STATE_NORMAL || RET_state_prev != STATE_DORMANT) {
+                setState(STATE_NORMAL);
+            } else {
+                setState(RET_state_prev);
+            }
+            RET_busy = false;
             break;
         }
         case STATE_BUTTONPRESS3 :
         {
+            RET_busy = true;
             if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
                 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();}
                 LED1blink(3,500);
@@ -929,10 +953,12 @@
                 LED1off();
             }
             setState(STATE_NORMAL); //turns device back on
+            RET_busy = false;
             break;
         }
         case STATE_BUTTONPRESS5 :
         {
+            RET_busy = true;
             if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
                 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 5\n");debug_exe();}
                 LED1blink(5,500);
@@ -941,10 +967,12 @@
                 LED1off();
             }
             setState(STATE_NORMAL); //turns device back on
+            RET_busy = false;
             break;  
         }
         case STATE_BUTTONHOLD :
         {
+            RET_busy = true;
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
             if (RET_state_prev == STATE_NORMAL) {
                 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + THREEDAYSINSECONDS);
@@ -962,10 +990,12 @@
                 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();}
                 LED1blink(20,100);
             }
+            RET_busy = false;
             break;
         }
         case STATE_SCORCHEDEARTH :
         {
+            RET_busy = true;
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();}
             setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
             read_app_data_from_flash(&app_data);
@@ -973,6 +1003,7 @@
             app_data.target_firmware_version = 0;
             write_app_data_to_flash(&app_data);
             system_reset();
+            RET_busy = false;
             break;
         }
         default :
@@ -985,7 +1016,7 @@
 //------------------------------------------------------------------------------
 // MAIN
 //------------------------------------------------------------------------------ 
-int main() {    
+int main() {
     //INIT
     watchdog.configure(300.0); //5 mins
     LED1off();
@@ -1036,7 +1067,7 @@
     }
     
     //MAIN LOOP
-    while(true) {
+    while(true) {    
         //WATCHDOG
         watchdogKick();
         RET_asleep = false;
@@ -1064,14 +1095,6 @@
                 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe();
             }
         }
-        
-        /*
-        if (modem.on(RET_force2G)) {
-            if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
-                modem.USSDsend("(b:gps2,a:act,e:1.7!0.15!,t:1547937626,r:0.13,c:1)\0",2);
-            }
-        }
-        */
 
         //STATE ENGINE
         mainStateEngine();
@@ -1081,13 +1104,13 @@
             recordFirmwareAsValid();
             RET_coldBoot = 0;
         }
-        
+                
         //PRE-SLEEP ACTIONS
         LED1off();
         modem.off(false);
         RET_motionTriggeredinFrame = false;
         if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
-        nrf_configureGPIOForSleep();
+        nrf_configureForSleep();
         NRFuart_uninit();
         watchdogKick();
         
--- a/main.h	Thu Feb 07 00:34:30 2019 +0000
+++ b/main.h	Thu May 16 12:31:44 2019 +0000
@@ -16,7 +16,7 @@
 //------------------------------------------------------------------------------
 //DEFINES
 //------------------------------------------------------------------------------ 
-#define FW_VERSION          8
+#define FW_VERSION          18
 //#define SKU                 "GPSPLUSDEV"
 #define SKU                 "GPSPLUS"
 #define HW_MAJORREVISION    "001"
@@ -27,7 +27,7 @@
 
 //DEFAULT SETTINGS
 #define DEFAULT_SLEEP_FRAME                 60000
-#define DEFAULT_FORCE2G                     true
+#define DEFAULT_FORCE2G                     false
 #define DEFAULT_LOCATION_MODE               1
 #define DEFAULT_LOCATION_ACCURACY           1  // 0 = no location, 1 = cl only, 2 = gps then cl
 #define DEFAULT_LOCATION_TX_INTERVAL_MINS   1440
@@ -56,7 +56,7 @@
 //mbed Libraries
 //------------------------------------------------------------------------------
 #include "mbed.h"
-//#include "nrf_soc.h"
+#include "nrf_soc.h"
 #include "nrf_temp.h"
 #include "board.h"
 //#include "ble/BLE.h"
--- a/modem.cpp	Thu Feb 07 00:34:30 2019 +0000
+++ b/modem.cpp	Thu May 16 12:31:44 2019 +0000
@@ -4,6 +4,8 @@
 
 Modem::Modem(PinName pwrkey, PinName vreg_en, PinName w_disable): _pwrkey(pwrkey), _vreg_en(vreg_en), _w_disable(w_disable)
 {
+    _pwrkey.input();
+    _w_disable.input();
 }
 
 void Modem::ATsendCMD(char* cmd) 
@@ -83,12 +85,15 @@
     NRFuart_init_nohwfc();
     
     if (!GLOBAL_modemOn) {
-        _w_disable = 0; // this sets the modem to airplane mode
+        _w_disable.input(); // this sets the modem to airplane mode
         _vreg_en = 1;
         ThisThread::sleep_for(200);
+        //set prwkey to output low
+        _pwrkey.output();
         _pwrkey = 0;
         ThisThread::sleep_for(200);
-        _pwrkey = 1;
+        //set prwkey back to tri-state
+        _pwrkey.input(); 
         
         GLOBAL_modemOn = true;
         
@@ -98,28 +103,33 @@
             //TURN OFF ECHO
             ATsendCMD("ATE0");
             ATwaitForWord("OK",ATTIMEOUT_SHORT);
+        
+            //DISABLE LNA
+            //ATsendCMD("AT+QGPSCFG=\"lnacontrol\",0");
+            //ATwaitForWord("OK",ATTIMEOUT_SHORT);
              
             //ENABLE AIRPLANE MODE CONTROL WITH PIN
             ATsendCMD("AT+QCFG=\"airplanecontrol\",1");
             ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            _w_disable.output();
+            _w_disable = 0;
                         
-            //CONNECTION TYPE
-            //ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
-            //ATsendCMD("AT+QCFG=\"nwscanmode\",2"); //3G only connection
-            //ATsendCMD("AT+QCFG=\"nwscanmode\",0"); //AUTO
             if (force2G) {
+                ATsendCMD("AT+QCFG=\"nwscanseq\",1"); //2G priority
+                ATwaitForWord("OK",ATTIMEOUT_SHORT);
                 ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
             } else {
-                ATsendCMD("AT+QCFG=\"nwscanmode\",0"); //AUTO
-                ATwaitForWord("OK",ATTIMEOUT_SHORT);
-                //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tirangulation)
+                //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tri)
                 ATsendCMD("AT+QCFG=\"nwscanseq\",1"); //2G priority
                 //ATsendCMD("AT+QCFG=\"nwscanseq\",2"); //3G priority
                 //ATsendCMD("AT+QCFG=\"nwscanseq\",0"); //AUTO - default
                 ATwaitForWord("OK",ATTIMEOUT_SHORT);
+                ATsendCMD("AT+QCFG=\"nwscanmode\",0"); //AUTO
+                //ATsendCMD("AT+QCFG=\"nwscanmode\",1"); //2G only connection
+                //ATsendCMD("AT+QCFG=\"nwscanmode\",2"); //3G only connection
+                ATwaitForWord("OK",ATTIMEOUT_SHORT);
             }
-            
             return true;
         } else {
             return false;   
@@ -133,30 +143,54 @@
 {
     if (GLOBAL_modemOn) {
         if (soft) {
-            //ATsendCMD("AT+QPOWD=0");
-            //ATwaitForWord("POWERED DOWN",ATTIMEOUT_VERYSHORT);
+            ATsendCMD("AT+QPOWD=1");
+            ATwaitForWord("POWERED DOWN",ATTIMEOUT_MED);
         }
         GLOBAL_modemOn = false;
     }
+    ThisThread::sleep_for(250);
     GLOBAL_registeredOnNetwork = false;
-    _w_disable = 0; //enable airplane mode
-    _pwrkey = 0;    //set power key low
-    _vreg_en = 0;   //kill power to module
+    _w_disable.input(); //enable airplane mode
+    _pwrkey.input();
+    _vreg_en = 0;       //kill power to module
 }
 
-long long Modem::getIMEI() 
+uint64_t Modem::getIMEI() 
 {
-    long long imei = 0;
-    ATsendCMD("AT+GSN");
-    if (ATwaitForWord("\r",ATTIMEOUT_SHORT)) {
-        if (ATgetResponse('\r',ATTIMEOUT_SHORT)) {
-            imei = atoll(ATinBuffer);
-        }
-    };
-    NRFuart_flush();
+    uint64_t imei = 0;
+    int tries = 0;
+    ThisThread::sleep_for(200); //needed
+    while(imei == 0 && tries < 3) { //try 3 times
+        tries ++;
+        ATsendCMD("AT+GSN");
+        if (ATwaitForWord("\r\n",ATTIMEOUT_SHORT)) {
+            if (ATgetResponse('\r',ATTIMEOUT_SHORT)) {
+                imei = atoll(ATinBuffer);
+            }
+        };
+        NRFuart_flush();
+    }
     return imei;
 }
 
+uint64_t Modem::getCCID() 
+{
+    uint64_t ccid = 0;
+    int tries = 0;
+    ThisThread::sleep_for(200); //needed
+    while(ccid == 0 && tries < 3) { //try 3 times
+        tries ++;
+        ATsendCMD("AT+QCCID");
+        if (ATwaitForWord("+QCCID: ",ATTIMEOUT_SHORT)) {
+            if (ATgetResponse('\r',ATTIMEOUT_SHORT)) {
+                ccid = atoll(ATinBuffer);
+            }
+        };
+        NRFuart_flush();
+    }
+    return ccid;
+}
+
 char* Modem::getModemModel() 
 {
     char* modemModel;
@@ -174,14 +208,12 @@
 {
     //CHECK WE ARE NOT ALREADY ON NETOWRK
     if (!GLOBAL_registeredOnNetwork) {
-        //if (GLOBAL_debugLED) LED1on(0);
         int attempt = 0;
         Timer t;
         t.start();
         //DISABLE AIRPLANE MODE
-        _w_disable = 1;
+        _w_disable.input();
         while (attempt < maxAttempts) {
-            watchdogKick();
             t.reset();
             uint32_t startseconds = t.read();
             uint32_t runtime = 0;
@@ -203,17 +235,95 @@
             }
         }
         t.stop();
-        //if (GLOBAL_debugLED) LED1off();
     }
     NRFuart_flush();
     if (GLOBAL_registeredOnNetwork == true) {
         BYREF_NetworkFailCount = 0;
+        ThisThread::sleep_for(2000); //wait 2 seconds for things to settle
         return true;
     } else {
         BYREF_NetworkFailCount ++;
         return false;   
     }
 }
+
+char* Modem::HTTPpost(char* url, char* message, bool getResponse, int maxAttempts) 
+{
+    bool sent = false;
+    bool criticalfail = false;
+    int attempt = 0;
+    
+    //TRY X NUMBER OF TIMES
+    while (!sent && attempt < maxAttempts) {
+        criticalfail = false;
+        
+        if (!criticalfail) {
+            ATsendCMD("AT+QHTTPCFG=\"contextid\",1");    
+            if (!ATwaitForWord("OK\r",ATTIMEOUT_SHORT)) criticalfail = true;
+        }
+        
+        if (!criticalfail) {
+            ATsendCMD("AT+QICSGP=1,1,\"tsiot\",\"\",\"\",0");
+            if (!ATwaitForWord("OK\r",ATTIMEOUT_SHORT)) criticalfail = true;
+        }
+        
+        if (!criticalfail) {
+            ATsendCMD("AT+QIACT=1");    
+            if (!ATwaitForWord("OK\r",ATTIMEOUT_MED)) criticalfail = true;
+        }
+        
+        /*if (!criticalfail) {
+            ATsendCMD("AT+QIACT?");    
+            if (!ATwaitForWord("OK\r",ATTIMEOUT_SHORT)) criticalfail = true;
+        }*/
+        
+        if (!criticalfail) {
+            char URLcmd[60];
+            snprintf(URLcmd, sizeof(URLcmd), "AT+QHTTPURL=%d,80", strlen(url));
+            ATsendCMD(URLcmd);
+            if (!ATwaitForWord("CONNECT\r",ATTIMEOUT_LONG)) criticalfail = true;
+        }
+        
+        if (!criticalfail) {
+            ATsendCMD(url);
+            if (!ATwaitForWord("OK\r",ATTIMEOUT_SHORT)) criticalfail = true;
+        }
+        
+        if (!criticalfail) {
+            char POSTparams[60];
+            snprintf(POSTparams, sizeof(POSTparams), "AT+QHTTPPOST=%d,80,80", strlen(message));
+            ATsendCMD(POSTparams);
+            if (!ATwaitForWord("CONNECT\r\n",ATTIMEOUT_LONG)) criticalfail = true;
+        }
+        
+        if (!criticalfail) {
+            ATsendCMD(message);
+            if (ATwaitForWord("+QHTTPPOST: 0",ATTIMEOUT_MED)) {
+                sent = true;
+            } else {
+                criticalfail = true;
+            }
+        }
+        
+        if (!criticalfail && getResponse) {
+            ATsendCMD("AT+QHTTPREAD=80");
+            ATwaitForWord("CONNECT\r\n",ATTIMEOUT_MED);
+            ATgetResponse('\r',ATTIMEOUT_SHORT);
+        }
+        
+        NRFuart_flush();
+        attempt ++;
+    }
+    if (sent) {
+        if (getResponse == false) {
+            return "sendonly";
+        } else {
+            return ATinBuffer;
+        }
+    } else {
+        return "err";
+    }
+}
  
 bool Modem::USSDsend(char* message, int maxAttempts) 
 {
@@ -289,7 +399,7 @@
             if (strcmp(response, "err") != 0) {
                 return response;
             } else {
-                return "sendonly";
+                return "err-sendonly";
             }
         } else {
             return "ok";   
@@ -301,7 +411,6 @@
 
 char* Modem::getLocation(uint8_t accuracy, uint16_t timeout_seconds, uint32_t &BYREF_GPSFailCount, uint32_t &BYREF_NetworkFailCount) 
 { 
-    watchdogKick();
     NRFuart_flush();
     bool haveGPSFix = false;
     bool haveCellFix = false;
@@ -314,8 +423,8 @@
     
     if (accuracy >= 2 && BYREF_GPSFailCount <= DEFAULT_MAX_FAILED_GPS) {
         //Enable External LNA power - IS DISABLED BY DEFAULT
-        ATsendCMD("AT+QGPSCFG=\"lnacontrol\",1");
-        ATwaitForWord("OK",ATTIMEOUT_SHORT);
+        //ATsendCMD("AT+QGPSCFG=\"lnacontrol\",1");
+        //ATwaitForWord("OK",ATTIMEOUT_SHORT);
         
         //TURN ON GPS
         ATsendCMD("AT+QGPS=1");
--- a/modem.h	Thu Feb 07 00:34:30 2019 +0000
+++ b/modem.h	Thu May 16 12:31:44 2019 +0000
@@ -25,11 +25,13 @@
              
             //funcs
             bool registerOnNetwork(int maxAttempts, uint32_t timeout, uint32_t &BYREF_NetworkFailCount);
+            char* HTTPpost(char* url, char* message, bool getResponse, int maxAttempts);
             bool USSDsend(char* message, int maxAttempts);
             char* USSDreceive(int messageIndex);
             char* USSDmessage(char* message, bool needResponse, int maxAttempts, char* api);
             char* getLocation(uint8_t accuracy, uint16_t timeout_seconds, uint32_t &BYREF_GPSFailCount, uint32_t &BYREF_NetworkFailCount); //accuracy is 1,2
-            long long getIMEI(void);
+            uint64_t getIMEI(void);
+            uint64_t getCCID(void);
             char* getModemModel(void);
             
             //AT
@@ -40,10 +42,10 @@
 
             
         protected:
-            DigitalOut _pwrkey;
             DigitalOut _vreg_en;
-            DigitalOut _w_disable;
+            DigitalInOut _pwrkey;
+            DigitalInOut _w_disable;
     };
 
 } //Namespace
-#endif
+#endif
\ No newline at end of file