init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Sun Dec 23 01:37:00 2018 +0000
Parent:
22:810425eb76e1
Child:
24:c161db07557f
Commit message:
8

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp 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	Sat Dec 22 19:33:35 2018 +0000
+++ b/README.md	Sun Dec 23 01:37:00 2018 +0000
@@ -9,8 +9,8 @@
 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
 8) should we use 2g when possible
 9) does 3g use less power
-
-
+10) add model version details of board to the setup tx
+11) add validation to each settings in 
 
 
 Temperature logging
--- a/main.cpp	Sat Dec 22 19:33:35 2018 +0000
+++ b/main.cpp	Sun Dec 23 01:37:00 2018 +0000
@@ -13,6 +13,7 @@
 static void buttonPress(void);
 static void buttonRelease(void);
 static void saveEventTimes(void);
+static void event_location_tx(void);
 
 //------------------------------------------------------------------------------
 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
@@ -339,6 +340,9 @@
             RET_motionState = true;
             RET_motionTriggeredInTXInterval = true;
             if (GLOBAL_debugLED) LED1blink(10,100);
+            if (RET_setting_activity_mode == 1) {
+                //RET_activityData 
+            }
         }
     }
     if (RET_motionPendingOffState) {
@@ -348,6 +352,9 @@
             GLOBAL_motionStopFlagTriggered = true;
             RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); 
             if (GLOBAL_debugLED) LED1blink(5,500);
+            if (RET_setting_activity_mode == 1) {
+                
+            }
         }
     }
 }
@@ -453,6 +460,7 @@
         RET_imei = modem.getIMEI();
         GLOBAL_imei = RET_imei;
         DEBUG("imei: %lld \n",RET_imei);
+        //char* modemModel = modem.getModemModel();
         if (modem.registerOnNetwork(2,180000)) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
@@ -518,23 +526,21 @@
     float temperature = getTemperature();
     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()) {
-        int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
-        char gpsString[70];
+        char locString[70];
         if (RET_setting_location_accuracy > 0) { 
-            snprintf(gpsString,sizeof(gpsString),"%s",modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout));
-            DEBUG("\ngpsString:%s\n",gpsString);
-        } else {
-            DEBUG("localisation disabled\n");
+            memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70);
+            //DEBUG("locString:%s-\n",locString);
         }
         //SEND DATA
         if (modem.registerOnNetwork(2,180000)) {
-            char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,f:%d,t:%.2f,v:%.2f,z:OK,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,GLOBAL_voltage,timetaken,gpsString);
+            int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
+            char bytesToSend[160];
+            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,f:%d,t:%.2f,v:%.2f,z:OK,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,GLOBAL_voltage,timetaken,locString);
             char result[180];
             bool getSettings = true;
-            snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
+            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);
                 }
@@ -677,7 +683,7 @@
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        DEBUG("STATE:%d, HAVESETTINGS:%d, RTC:%u, BOOTAT:%u, LOC:%u, LOC_FS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
+        DEBUG("STATE:%d, HAVESETTINGS:%d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS
--- a/modem.cpp	Sat Dec 22 19:33:35 2018 +0000
+++ b/modem.cpp	Sun Dec 23 01:37:00 2018 +0000
@@ -60,44 +60,52 @@
 
 bool Modem::on(void) 
 {
-    _w_disable = 0; // this sets the modem to airplane mode
-    _vreg_en = 1;
-    Thread::wait(200);
-    _pwrkey = 0;
-    Thread::wait(1500);
-    _pwrkey = 1;
-    
-    GLOBAL_modemOn = true;
-    
-    //CONFIGURE
-    if (ATwaitForWord("RDY",ATTIMEOUT_MED)) {
-        //TURN OFF ECHO
-        ATsendCMD("ATE0");
-        ATwaitForWord("OK",ATTIMEOUT_SHORT);
+    if (!GLOBAL_modemOn) {
+        _w_disable = 0; // this sets the modem to airplane mode
+        _vreg_en = 1;
+        Thread::wait(200);
+        _pwrkey = 0;
+        Thread::wait(200);
+        _pwrkey = 1;
+        
+        GLOBAL_modemOn = true;
         
-        //ENABLE AIRPLANE MODE CONTROL WITH PIN
-        ATsendCMD("AT+QCFG=\"airplanecontrol\",1");
-        ATwaitForWord("OK",ATTIMEOUT_SHORT);
-        
-        //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tirangulation)
-        ATsendCMD("AT+QCFG=\"nwscanseq\",1");
-        ATwaitForWord("OK",ATTIMEOUT_SHORT);
-        
-        //2G only
-        //ATsendCMD("AT+QCFG=\"nwscanmode\",1");
-        //ATwaitForWord("OK",ATTIMEOUT_SHORT);
-        
-        return true;
+        //CONFIGURE
+        if (ATwaitForWord("RDY",ATTIMEOUT_MED)) {
+            //TURN OFF ECHO
+            ATsendCMD("ATE0");
+            ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            
+            //ENABLE AIRPLANE MODE CONTROL WITH PIN
+            ATsendCMD("AT+QCFG=\"airplanecontrol\",1");
+            ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            
+            //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tirangulation)
+            ATsendCMD("AT+QCFG=\"nwscanseq\",1");
+            ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            
+            //2G only
+            //ATsendCMD("AT+QCFG=\"nwscanmode\",1");
+            //ATwaitForWord("OK",ATTIMEOUT_SHORT);
+            
+            return true;
+        } else {
+            return false;   
+        }
     } else {
-        return false;   
+        return true;   
     }
 }
 
 void Modem::off(bool soft) 
 {
     if (soft) {
-        ATsendCMD("AT+QPOWD=0");
-        ATwaitForWord("OK\r",ATTIMEOUT_SHORT);
+        //ATsendCMD("AT+QPOWD");
+        //ATwaitForWord("POWERED DOWN",ATTIMEOUT_SHORT);
+        _pwrkey = 0;
+        Thread::wait(800);
+        _pwrkey = 1;
+        ATwaitForWord("POWERED DOWN",ATTIMEOUT_SHORT);
     }
     GLOBAL_registeredOnNetwork = false;
     _vreg_en = 0;
@@ -106,7 +114,7 @@
 long long Modem::getIMEI() 
 {
     long long imei = 0;
-    char ATinBuffer[32];
+    char* ATinBuffer;
     ATsendCMD("AT+GSN");
     ATwaitForWord("\n",ATTIMEOUT_SHORT);
     _uart.scanf("%s", ATinBuffer);
@@ -115,6 +123,16 @@
     return imei;
 }
 
+char* Modem::getModemModel() 
+{
+    char* modemModel;
+    ATsendCMD("AT+GMM");
+    ATwaitForWord("\n",ATTIMEOUT_SHORT);
+    _uart.scanf("%s", modemModel);
+    flushSerial();
+    return modemModel;
+}
+
 bool Modem::registerOnNetwork(int maxAttempts, uint32_t timeout) 
 {
     //CHECK WE ARE NOT ALREADY ON NETOWRK
@@ -142,6 +160,7 @@
             }
             if (!GLOBAL_registeredOnNetwork) {
                 off(true);
+                Thread::wait(500);
                 on();   
             }
         }
@@ -181,7 +200,6 @@
 {
     bool received = false;
     uint32_t timeout = ATTIMEOUT_MED;
-    //int bufferIndex = 0;
     int USSDmessageIndex = 0;
     char ATinBuffer[180];
     int matchCount = 0;
@@ -195,7 +213,7 @@
         if (ATwaitForWord("+CUSD: 0",ATTIMEOUT_SHORT)) {
             led1 = 0;
             if ( (matchCount = _uart.scanf(",\"%d#%[^#]",USSDmessageIndex,ATinBuffer) ) > 0 ) {
-                //DEBUG("\ngot:%s \n",ATinBuffer);
+                //G("\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
@@ -247,9 +265,9 @@
 { 
     flushSerial();
     bool haveGPSFix = false;
-    bool haveCellLocateFix = false;
+    bool haveCellFix = false;
     
-    char gpsDataOut[80];
+    char locDataOut[100];
     
     if (accuracy >= 2) {
         //Enable External LNA power - IS DISABLED BY DEFAULT
@@ -298,14 +316,24 @@
                 uint32_t date;
                 uint8_t sat;
                 
+                /*
                 char gpsDataIn[100];
                 _uart.scanf("%s",gpsDataIn);
                 //Example data
                 //115757.0,52.62091,1.29536,0.8,58.2,2,0.00,0.0,0.0,211218,07
                 if ((matchCount = sscanf(gpsDataIn,"%f,%f,%f,%f,%f,%d,%f,%f,%f,%d,%d",&utc,&lat,&lng,&hdp,&alt,&fix,&cog,&spkm,&spkn,&date,&sat)) == 11 ) {
                     //{“fix”:“GPS”,“sat”:“9",“lat”:“52.913254",“lng”:“-1.455289",“hdp”:“89.0",“spd”:“0.0"}                    
-                    sprintf(gpsDataOut,",g:(fix:GPS,sat:%d,lat:%.6f,lng:%.6f,hdp:%.1f,spd:%.1f)\0",fix,sat,lat,lng,hdp,spkm);
-                    DEBUG("\nGPSOut:%s--\n",gpsDataOut);
+                    sprintf(locDataOut,",g:(fix:GPS,sat:%d,lat:%.6f,lng:%.6f,hdp:%.1f,spd:%.1f)\0",fix,sat,lat,lng,hdp,spkm);
+                    //DEBUG("\nGPSOut:%s--\n",locDataOut);
+                }
+                */
+                
+                //Example data
+                //115757.0,52.62091,1.29536,0.8,58.2,2,0.00,0.0,0.0,211218,07
+                if ((matchCount = _uart.scanf("%f,%f,%f,%f,%f,%d,%f,%f,%f,%d,%d",&utc,&lat,&lng,&hdp,&alt,&fix,&cog,&spkm,&spkn,&date,&sat)) == 11 ) {
+                    //{“fix”:“GPS”,“sat”:“9",“lat”:“52.913254",“lng”:“-1.455289",“hdp”:“89.0",“spd”:“0.0"}                    
+                    sprintf(locDataOut,",g:(fix:GPS,sat:%d,lat:%.6f,lng:%.6f,hdp:%.1f,spd:%.1f)\0",fix,sat,lat,lng,hdp,spkm);
+                    //DEBUG("\nGPSOut:%s\n",locDataOut);
                 }
             }
         }
@@ -317,17 +345,32 @@
     //SHALL WE GET CELL LOCATION
     if (!haveGPSFix && accuracy >= 1) {
         //PRIORITISE 2G connection (reason being uses less power in some instances and can get cell tower tirangulation)
-        ATsendCMD("AT+QCFG=\"nwscanseq\",0"); // 0 - auto, 1 - 2g priority, 2 - 3g priority
-        ATwaitForWord("OK",ATTIMEOUT_SHORT);
-        
-        _w_disable = 1; //turn off airplane mode
-        registerOnNetwork(3, 60000);
-        
+        //ATsendCMD("AT+QCFG=\"nwscanseq\",0"); // 0 - auto, 1 - 2g priority, 2 - 3g priority
+        //ATwaitForWord("OK",ATTIMEOUT_SHORT);
         //ATsendCMD("AT+QCFG=\"nwscanmode\",1"); // 0 - auto, 1 - 2g, 2 - 3g
         //ATwaitForWord("OK",ATTIMEOUT_SHORT);
         
+        _w_disable = 1; //turn off airplane mode
+        registerOnNetwork(2, 90000);
         
-        //example from home 
+        int matchCount;
+        char type[6];
+        char cellID[6];
+        char lac[6];
+        int mcc;
+        int mnc;
+        
+        ATsendCMD("AT+QENG=\"servingcell\"");
+        ATwaitForWord("+QENG: \"servingcell\"",ATTIMEOUT_SHORT);
+        
+        if ((matchCount = _uart.scanf(",\"NOCONN\",\"%[^\"]\",%d,%d,%[^,],%[^,]",&type,&mcc,&mnc,&lac,&cellID)) == 5 ) {
+            //sprintf(locDataOut,",h:%d.%d.%s.%s\0",mcc,mnc,lac,cellID);
+            sprintf(locDataOut,",h:41806.2252.234.30\0");
+            haveCellFix = true;
+            //DEBUG("\ncellOut:%s\n",locDataOut);
+        }
+                
+        //example from mulbs
         /*
         +QENG: "servingcell","NOCONN","2G",234,30,8CC,A34E,20,668,0,-80,0,5,4,26,26,1,-,-,-,-,-,-,-,-,-,"-"
         +QENG: "neighbourcell","2G",234,30,8CC,A34D,20,656,-89,17,17,0,0
@@ -337,44 +380,12 @@
         https://www.neilson.co.za/mobile-network-geolocation-obtaining-the-cell-ids-the-signal-strength-of-surrounding-towers-from-a-gsm-modem/
         */
         
-        ATsendCMD("AT+QENG=\"servingcell\"");
-        ATwaitForWord("OK",ATTIMEOUT_SHORT);
-            
-        while(1) {
-            //ATsendCMD("AT+CREG?");
-            //ATwaitForWord("OK",5000);
-            
-            //wait(10.0);
-            
-            
-            
-            ATsendCMD("AT+QENG=\"neighbourcell\"");
-            ATwaitForWord("OK",ATTIMEOUT_SHORT);
-            
-            wait(2.0);
-            //wait(15.0);
-            //ATwaitForWord("+QENG: \"neighbourcell\"",ATTIMEOUT_MED);
-            
-            /*
-            //+QENG: "servingcell","NOCONN","3G",234,20,8A,CE8B11,10612,320,-75,-76,5,31,39,128,-,-,-,-,-,"-",-
-            char cellDataIn[100];
-            int cellId = 0;
-            int lac = 0;
-            int mcc = 0;
-            int mnc = 0;
-            if ((matchCount = _uart.scanf(",\"  %[^\"],\"%[^\"],%d%d%  %[^#]",&cellId,&lac,&mcc,&mnc)) > 0 ) {
-                sprintf(gpsDataOut,",g:(fix:GPS,sat:%d,lat:%.6f,lng:%.6f,hdp:%.1f,spd:%.1f)\0",fix,sat,lat,lng,hdp,spkm);
-                DEBUG("\nGPSOut:%s--\n",gpsDataOut);
-            }
-            */
-        }
+        //ATsendCMD("AT+QENG=\"neighbourcell\"");
+        //ATwaitForWord("OK",ATTIMEOUT_LONG);
     }
     
     //RETURN
-    if (!haveGPSFix && !haveCellLocateFix) {
-        sprintf(gpsDataOut,"");
-    }
-    return gpsDataOut;
+    return locDataOut;
 }
 
 Modem::~Modem(){};
\ No newline at end of file
--- a/modem.h	Sat Dec 22 19:33:35 2018 +0000
+++ b/modem.h	Sun Dec 23 01:37:00 2018 +0000
@@ -28,12 +28,14 @@
             char* USSDreceive(int messageIndex);
             char* USSDmessage(char* message, bool needResponse, int maxAttempts, char* api);
             char* getLocation(uint8_t accuracy, uint16_t timeout_seconds); //accuracy is 1,2,3   low, med, high
+            long long getIMEI(void);
+            char* getModemModel(void);
             
             //AT
             void flushSerial(void);
             void ATsendCMD(char* cmd);
             bool ATwaitForWord(char* word, uint32_t timeout);
-            long long getIMEI(void);
+            
         private: