init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Thu Dec 20 22:04:45 2018 +0000
Parent:
20:5404841fdd2b
Child:
22:810425eb76e1
Commit message:
rr

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
filesystem.cpp Show diff for this revision Revisions of this file
filesystem.h Show diff for this revision Revisions of this file
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/README.md	Wed Dec 19 09:02:20 2018 +0000
+++ b/README.md	Thu Dec 20 22:04:45 2018 +0000
@@ -6,7 +6,8 @@
 Should we shut down network registration while getting gps fix?
 4) check LNA power on GPS
 5) improve gps fix quality logic
-
+6) sometimes its setting back to srtate 0 and getting stuck in loop 
+7) what happens if the setup fails, it should sleep for x hrs and then try again, need to check that and make its o
 
 
 
--- a/filesystem.cpp	Wed Dec 19 09:02:20 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-#include "filesystem.h"
-
-#define FILENAME_TEMPERATURE "log_temperature.txt"
-#define FILENAME_LOCATION "log_location.txt"
-#define FILENAME_ACTIVITY "log_activity.txt"
-#define FILENAME_PENDING "pending_broadcasts.txt"
-
-/*
-//SD
-Filesystem::Filesystem(PinName mosi, PinName miso, PinName clk, PinName cs):  bd(mosi,miso,clk,cs), fs("fs")
-{
-    //do nothing
-}
-*/
-
-//ONBOARD FLASH
-Filesystem::Filesystem(uint16_t page_address, uint16_t total_size):  bd(page_address, total_size), fs("fs")
-{
-    //do nothing   
-}
-
-void Filesystem::addLogEntry_temperature(time_t timestamp, float temperature) 
-{
-    int err;
-    //MOUNT FS
-    DEBUG("Mounting the filesystem... ");
-    fflush(stdout);
-    err = fs.mount(&bd);
-    DEBUG("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        // Reformat if we can't mount the filesystem
-        DEBUG("No filesystem found, formatting... ");
-        fflush(stdout);
-        err = fs.reformat(&bd);
-        DEBUG("%s\n", (err ? "Fail :(" : "OK"));
-    }
-    //WRITE TO FILE
-    fflush(stdout);
-    FILE *f = fopen("/fs/log_temperature.txt", "a+");
-    DEBUG("%s\n", (!f ? "No File :(" : "OK"));
-    if (f) {
-        fflush(stdout);
-        err = fprintf(f, "%u,%.1f\r\n", timestamp, temperature); //needs \r\n for new line
-        DEBUG("Data written to fs\n");
-        if (err < 0) {
-            DEBUG("error: %s (%d)\n", strerror(errno), -errno);
-        }  
-    } else {
-        DEBUG("error: %s (%d)\n", strerror(errno), -errno);
-    }
-    //SAVE FILE
-    DEBUG("Closing file... ");
-    fflush(stdout);
-    err = fclose(f);
-    DEBUG("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        DEBUG("error: %s (%d)\n", strerror(errno), -errno);
-    }
-    //UNMOUNT
-    DEBUG("Unmounting... ");
-    fflush(stdout);
-    err = fs.unmount();
-    DEBUG("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        DEBUG("error: %s (%d)\n", strerror(-err), err);
-    }
-}
-
-Filesystem::~Filesystem(){};
\ No newline at end of file
--- a/filesystem.h	Wed Dec 19 09:02:20 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-#ifndef FILESYSTEM_H
-#define FILESYSTEM_H
-
-#include "main.h"
-
-//#include "SDBlockDevice.h"
-#include "FlashIAPBlockDevice.h"
-#include "LittleFileSystem.h"
-//#include "FATFileSystem.h"
-
-namespace mbed {
-    
-    class Filesystem
-    {
-        public:  
-            //Filesystem(PinName mosi, PinName miso, PinName clk, PinName cs); //SD
-            Filesystem(uint16_t page_address, uint16_t total_size);
-            ~Filesystem();
-            
-            //funcs
-            void addLogEntry_temperature(time_t timestamp, float temperature);
-            
-        private:
-        
-        protected:
-            FlashIAPBlockDevice bd;
-            //SDBlockDevice bd;
-            LittleFileSystem fs;
-            //FATFileSystem fs;
-    };
-
-} //Namespace
-#endif
--- a/main.cpp	Wed Dec 19 09:02:20 2018 +0000
+++ b/main.cpp	Thu Dec 20 22:04:45 2018 +0000
@@ -1,5 +1,9 @@
 #include "main.h"
-//include "nrf_soc.h"
+
+//------------------------------------------------------------------------------
+//FUNCTION PROTOTYPES
+//------------------------------------------------------------------------------ 
+#define FW_VERSION 1
 
 //------------------------------------------------------------------------------
 //FUNCTION PROTOTYPES
@@ -60,6 +64,7 @@
 static time_t           RET_buttonReleaseTime                           __attribute__((section("noinit"),zero_init));
 static time_t           RET_buttonHoldTime                              __attribute__((section("noinit"),zero_init));
 static time_t           RET_RTCunixtime                                 __attribute__((section("noinit"),zero_init));
+static time_t           RET_SetupRunAt                                  __attribute__((section("noinit"),zero_init));
 //MOTION STATE
 static bool             RET_motionTriggered                             __attribute__((section("noinit"),zero_init));
 static bool             RET_motionTriggeredInTXInterval                 __attribute__((section("noinit"),zero_init));
@@ -202,7 +207,7 @@
 }
 void factoryReset() {
     DEBUG("Factory Reset \n");
-    Thread::wait(500); //wait to ensure that devices are turned off
+    Thread::wait(200); //wait to ensure that devices are turned off
     //IDENTITY 
     RET_imei = 0;
     memset(RET_pf_identifier,0,sizeof(RET_pf_identifier));
@@ -212,13 +217,14 @@
     RET_state = STATE_SETUP;
     RET_state_prev = RET_state;
     RET_RTCunixtime = 0;
+    RET_SetupRunAt = 0;
     //SETTINGS
     RET_setting_firmware = 0;
     RET_setting_location_mode = 2;
     RET_setting_location_accuracy = 1;
     RET_setting_location_tx_interval_mins = 1440;
     RET_setting_location_tx_failsafe_hrs = (7*24); //7 days
-    RET_setting_location_timeout = 180;
+    RET_setting_location_timeout = 240;
     RET_setting_activity_tx_interval_mins = 0;
     RET_setting_environmental_tx_interval_mins = 0;
     RET_setting_motion_g = 11;
@@ -335,23 +341,23 @@
         //FAILUREMODE need to verify the identifier against a reg exp or similar
         if(1==1) { memcpy(RET_pf_identifier, TEMP_a, sizeof(TEMP_a));              changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_pf_identifier:%s..%c\n",RET_pf_identifier,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'; }; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,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_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
         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(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(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(TEMP_j != -1) { RET_setting_activity_tx_interval_mins = TEMP_j;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,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(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(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)
+        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(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(TEMP_j != -1) { RET_setting_activity_tx_interval_mins = TEMP_j;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,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(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(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);
         
         if (critical_fail_count == 0) { 
             DEBUG("GOT SETTINGS OK\n"); 
@@ -368,6 +374,27 @@
         return false;
     }
 }
+bool dumpSettings() {
+    DEBUG("RET_pf_identifier:%s \n",RET_pf_identifier);
+    DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
+    DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
+    DEBUG("RET_state:%d \n",RET_state);
+    DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
+    DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
+    DEBUG("RET_setting_location_tx_interval_mins:%d \n",RET_setting_location_tx_interval_mins);
+    DEBUG("RET_setting_location_tx_failsafe_hrs:%d \n",RET_setting_location_tx_failsafe_hrs);
+    DEBUG("RET_setting_location_timeout:%d \n",RET_setting_location_timeout);
+    DEBUG("RET_setting_activity_tx_interval_mins:%d \n",RET_setting_activity_tx_interval_mins);
+    DEBUG("RET_setting_environmental_tx_interval_mins:%d \n",RET_setting_environmental_tx_interval_mins);
+    DEBUG("RET_setting_motion_g:%d \n",RET_setting_motion_g);
+    DEBUG("RET_setting_motion_start_seconds:%d \n",RET_setting_motion_start_seconds);
+    DEBUG("RET_setting_motion_stop_seconds:%d \n",RET_setting_motion_stop_seconds);
+    DEBUG("RET_setting_impact_g:%d \n",RET_setting_impact_g);
+    DEBUG("RET_setting_impact_alert:%d \n",RET_setting_impact_alert);
+    DEBUG("RET_setting_connection_timeout:%d \n",RET_setting_connection_timeout);
+    DEBUG("RET_setting_beacon_interval_seconds:%d \n",RET_setting_beacon_interval_seconds);
+    DEBUG("RET_setting_beacon_scan:%d \n",RET_setting_beacon_scan);
+}
 void saveEventTimes() {
     //SET EVENT TIMES
     if(RET_setting_location_tx_interval_mins > 0) { 
@@ -393,7 +420,7 @@
 //------------------------------------------------------------------------------ 
 bool setup() {
     bool pass = true;
-    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    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()) {
         RET_imei = modem.getIMEI();
         GLOBAL_imei = RET_imei;
@@ -404,7 +431,7 @@
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
-                DEBUG("fromserver: %s \n",result);
+                DEBUG("\nfromserver: %s \n",result);
                 if (!saveSettings(result)) { 
                     //something went critically wrong getting settings 
                     pass = false;
@@ -419,6 +446,9 @@
         pass = false;
     }
     
+    //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
+    RET_SetupRunAt = RET_RTCunixtime;
+    
     //RESULT
     return pass;
 }
@@ -429,16 +459,11 @@
 void event_location_tx() {
     DEBUG("PERFORM LOCATION TX\n");
     getBatteryV();  
-    
-    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    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()) {
-        
         //RET_setting_location_accuracy
         modem.getLocation(true, 3, true, RET_setting_location_timeout);
-        
     }
-    
-    
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
     saveEventTimes();
@@ -468,7 +493,10 @@
             
             //LOCATION
             //Location Log
-            //if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) { }
+            /*
+            if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0)  { 
+            }
+            */
             bool run_location_tx = false;
             switch (RET_setting_location_mode) {
                 case 1: //INTERVAL POST
@@ -492,14 +520,8 @@
                     break;   
             }
             //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) {
-                event_location_tx();
-            }
-            
-            
+            if(RET_RTCunixtime > RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
+            if (run_location_tx) {event_location_tx();}
             
             //Environmental Log
             if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
@@ -587,6 +609,9 @@
         }
         NRF_POWER->RESETREAS = 0xffffffff;
     }
+    //DEBUG
+    dumpSettings();
+    
     //CHECK FOR FIRST BOOT
     if (RET_coldBoot == 1) { factoryReset(); }
     
@@ -605,9 +630,7 @@
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        //DEBUG("STATE: %d,  RTC_MILLIS: %lld,  RTC_SECONDS:%lld,  BUTT_T:%d,  BUTT_C,%d \n", RET_state, RET_RTCmillis, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
-        DEBUG("STATE: %d,  RTC_SECONDS:%u\n", RET_state, RET_RTCunixtime);
-
+        DEBUG("STATE:%d, RTC_SECONDS:%u, SETUPAT:%u\n", RET_state, RET_RTCunixtime,RET_SetupRunAt);
         
         event_location_tx();
         while(1) {   
--- a/main.h	Wed Dec 19 09:02:20 2018 +0000
+++ b/main.h	Thu Dec 20 22:04:45 2018 +0000
@@ -1,6 +1,6 @@
 /*
 ================================================================================
-Project:    MTU Tracker
+Project:    GPS Tracker
 Module:     ---
 Status:     Development
 Type:       .h header file
@@ -24,7 +24,6 @@
 #include "ble/BLE.h"
 //#include "nvstore.h"
 
-
 //------------------------------------------------------------------------------
 //peripheral Libraries
 //------------------------------------------------------------------------------
@@ -33,32 +32,12 @@
 #include "acd_nrf52_saadc.h"
 
 //------------------------------------------------------------------------------
-//C Standard Libraries
-//------------------------------------------------------------------------------
-/*
-#include <ctype.h>
-#include <errno.h>
-#include <stddef.h>
-#include <math.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdbool.h>
-#include <stdint.h>
-*/
-//include <string>  // dont use - causes 2ma sleep
-//using std::string;
-
-//------------------------------------------------------------------------------
 //Application headers
 //------------------------------------------------------------------------------
 #include "states.h"
 #include "common.h"
 #include "sensors.h"
 #include "modem.h"
-#include "filesystem.h"
 
 //------------------------------------------------------------------------------
 //Global macros
--- a/modem.cpp	Wed Dec 19 09:02:20 2018 +0000
+++ b/modem.cpp	Thu Dec 20 22:04:45 2018 +0000
@@ -1,6 +1,6 @@
 #include "modem.h"
 
-Modem::Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en):  _uart(tx,rx,115200), _pwrkey(pwrkey), _vreg_en(vreg_en)
+Modem::Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en, PinName w_disable):  _uart(tx,rx,115200), _pwrkey(pwrkey), _vreg_en(vreg_en), _w_disable(w_disable)
 {
     GLOBAL_requireSoftReset = true;  //TODO: this can be removed when uart sleep issue resolved
 }
@@ -60,15 +60,20 @@
 
 bool Modem::on(void) 
 {
+    _w_disable = 0; // this sets the modem to airplane mode
     _vreg_en = 1;
-    Thread::wait(500);
+    Thread::wait(200);
     _pwrkey = 0;
     Thread::wait(1500);
     _pwrkey = 1;
-    if (ATwaitForWord("RDY",10000)) {
+    if (ATwaitForWord("RDY",15000)) {
         //TURN OFF ECHO
         ATsendCMD("ATE0");
         ATwaitForWord("OK",5000);
+        
+        ATsendCMD("AT+QCFG=\"airplanecontrol\",1");
+        ATwaitForWord("OK",5000);
+        
         return true;
     } else {
         return false;   
@@ -101,6 +106,9 @@
     int attempt = 0;
     Timer t;
     t.start();
+    //DISABLE AIRPLANE MODE
+    _w_disable = 1;
+    flushSerial();
     //TRY X NUMBER OF TIMES
     while (attempt <= maxAttempts) {
         watchdogKick();
@@ -171,7 +179,7 @@
         if (ATwaitForWord("+CUSD: 0",5000)) {
             led1 = 0;
             if ( (matchCount = _uart.scanf(",\"%d#%[^#]",USSDmessageIndex,ATinBuffer) ) > 0 ) {
-                //DEBUG("got:%s \n",ATinBuffer);
+                //DEBUG("\ngot:%s \n",ATinBuffer);
                 if (USSDmessageIndex == messageIndex) {
                     //NEED TO GET THIS WORKING SO WE KNOW WE ARE DEALING WITH THE RIGT MESSAGE
                     //MOVE THE BELOW INTO THIS IF STAEMEBNTS
@@ -225,29 +233,6 @@
     bool haveGPSFix = false;
     bool haveCellLocateFix = false;
     
-    //
-    //ATsendCMD("AT+CREG=2");  
-    //ATwaitForWord("OK",30000);
-
-    
-
-    
-    while(1) {
-        Thread::wait(5000);
-        
-        
-        //ATsendCMD("AT+QENG=?");  
-        //ATwaitForWord("OK",15000);
-        
-        ATsendCMD("AT+QENG=1");  
-        ATwaitForWord("OK",15000);
-        
-        //ATsendCMD("AT+CREG?");
-        ATsendCMD("AT+QENG?");
-        
-        ATwaitForWord("OK",30000);
-    }
-    
     if (gps) {
         //TURN ON GPS
         ATsendCMD("AT+QGPS=1");
@@ -265,16 +250,16 @@
         uint32_t startmillis = t.read_ms();
         uint32_t runtime = 0;
         while(!haveGPSFix && runtime < (timeout_seconds*1000)) {
+            Thread::wait(5000); //this goes first
             runtime = (t.read_ms() - startmillis);
             ATsendCMD("AT+QGPSLOC=2");
             if (ATwaitForWord("+QGPSLOC:",5000)) {
                 GPS_fixstage = 1;
                 GPS_fixcount ++;
-                if (GPS_fixcount >= 2) { //wait 10 seconds to get a better fix // need to improve this logic
+                if (GPS_fixcount == 2) { //wait 10 seconds to get a better fix // need to improve this logic
                     haveGPSFix = true;
                     cellLocate = false; //disable the need for trying cell locate
                 }
-                Thread::wait(5000);
             }
             watchdogKick();
         }
@@ -285,13 +270,6 @@
     if (cellLocate) {
            
     }
-    
-    
-    
-    DEBUG("END\n");
-    while(1) {
-        
-    }
 }
 
 Modem::~Modem(){};
\ No newline at end of file
--- a/modem.h	Wed Dec 19 09:02:20 2018 +0000
+++ b/modem.h	Thu Dec 20 22:04:45 2018 +0000
@@ -12,7 +12,7 @@
     class Modem
     {
         public:  
-            Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en);
+            Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en, PinName w_disable);
             ~Modem();
             
             //control
@@ -39,6 +39,7 @@
             Serial _uart;
             DigitalOut _pwrkey;
             DigitalOut _vreg_en;
+            DigitalOut _w_disable;
     };
 
 } //Namespace