init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Diff: main.cpp
- Revision:
- 40:4270e09e59f2
- Parent:
- 39:f767b8037475
- Child:
- 41:07e41dbd5304
--- a/main.cpp Thu Jan 17 15:20:14 2019 +0000
+++ b/main.cpp Thu Jan 17 21:59:18 2019 +0000
@@ -3,7 +3,7 @@
//------------------------------------------------------------------------------
//DEFINES
//------------------------------------------------------------------------------
-#define FW_VERSION 14
+#define FW_VERSION 21
#define SKU "GPSPLUS"
#define HW_MAJORREVISION "001"
@@ -70,8 +70,8 @@
uint16_t RET_setting_beacon_interval_seconds;
uint16_t RET_setting_beacon_scan;
//STATE
+uint8_t RET_coldBoot = 1;
bool RET_asleep = false;
-uint8_t RET_coldBoot = 1;
time_t RET_RTCunixtime;
bool RET_haveSettings;
uint8_t RET_state;
@@ -84,7 +84,7 @@
bool RET_force2G;
bool RET_watchdogfired;
//MOTION STATE
-bool RET_motionTriggered;
+bool RET_motionTriggeredinFrame;
bool RET_motionTriggeredInTXInterval;
time_t RET_motionStartTime;
time_t RET_motionStopTime;
@@ -338,7 +338,7 @@
RET_buttonPressTime = 0;
RET_buttonHoldTime = 0;
//MOTION STATE
- RET_motionTriggered = 0;
+ RET_motionTriggeredinFrame = 0;
RET_motionTriggeredInTXInterval = 0;
RET_motionStartTime = 0;
RET_motionStopTime = 0;
@@ -407,7 +407,7 @@
void checkMotion() {
if (lis3dh_int2) {
//if (GLOBAL_debugLED) LED1blink(1,50);
- RET_motionTriggered = true;
+ RET_motionTriggeredinFrame = true;
GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
if (!RET_motionPendingOnState) {
RET_motionPendingOnState = true;
@@ -416,7 +416,7 @@
RET_motionStartTime = RET_RTCunixtime;
}
} else {
- RET_motionTriggered = false;
+ RET_motionTriggeredinFrame = false;
RET_motionPendingOnState = false;
if (!RET_motionPendingOffState) {
RET_motionPendingOffState = true;
@@ -444,7 +444,7 @@
RET_motionPendingOffState = false;
RET_motionState = false;
GLOBAL_motionStopFlagTriggered = true;
- RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0);
+ RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0f);
if (GLOBAL_debugLED) LED1blink(2,50);
if (RET_setting_location_tx_failsafe_hrs > 0) {
time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
@@ -476,7 +476,7 @@
if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a; changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_minimumupdate_hrs:%u..%c\n",RET_setting_minimumupdate_hrs,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'; 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_d != -1) { setState(TEMP_d); changed = 'Y'; } else { setState(STATE_NORMAL); changed = 'N'; }; 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_mode:%d..%c\n",RET_setting_location_mode,changed);
if(RET_setting_location_mode > 3) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
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);
@@ -501,7 +501,7 @@
if (critical_fail_count == 0) {
if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "GOT SETTINGS OK");debug_exe();}
- dumpSettings();
+ dumpSettings();
saveEventTimes();
RET_haveSettings = true;
GLOBAL_needToConfigureLis3dh = true;
@@ -552,6 +552,19 @@
}
//------------------------------------------------------------------------------
+// ERROR LOG
+//------------------------------------------------------------------------------
+void sendErrorLog(char* error) {
+ float voltage = getBatteryV();
+ int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
+ char bytestosend[100];
+ memset(bytestosend,0x00,sizeof(bytestosend));
+ snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,v:%.2f,e:%d,z:%s,c:1,s:0)\0",GLOBAL_defaultApi,FW_VERSION,voltage,timetaken,error);
+ modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi);
+ ThisThread::sleep_for(250);
+}
+
+//------------------------------------------------------------------------------
// SETUP
//------------------------------------------------------------------------------
bool setup() {
@@ -561,14 +574,12 @@
bool selftestresult = selfTest();
if (selftestresult == false) { LED1errorCode(4,20); } //ERROR 4
if (modem.on(RET_force2G)) {
- //RET_imei = modem.getIMEI();
- //DEBUG("imei: %lld \n",RET_imei);
char locString[70];
memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70);
if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
char bytestosend[160];
- snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString);
+ snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,k:%s,m:%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,SKU,HW_MAJORREVISION,locString);
char result[200];
snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
if (strcmp(result, "err") != 0) {
@@ -578,9 +589,11 @@
pass = false;
modem.off(false);
if(GLOBAL_debugLED) LED1errorCode(5,10); //ERROR 5
- }
+ sendErrorLog("SETTINGS");
+ }
} else {
//Response error
+ sendErrorLog("RESPONSE");
}
} else {
//FAILUREMODE modem failed to register on network
@@ -624,11 +637,15 @@
bool getSettings = true;
snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
if (result != "err") {
- //DEBUG("\nfromserver: %s \n",result);
if (getSettings) {
- saveSettings(result);
- if(GLOBAL_debugLED) LED1blink(5,250);
+ if (saveSettings(result)) {
+ if(GLOBAL_debugLED) LED1blink(5,250);
+ } else {
+ sendErrorLog("SETTINGS");
+ }
}
+ } else {
+ sendErrorLog("RESPONSE");
}
} else {
modem.off(false);
@@ -660,8 +677,7 @@
snprintf(bytestosend,sizeof(bytestosend),"(%s,a:error,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,timetaken,locString);
}
char result[180];
- bool getSettings = false;
- snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
+ snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, 2, GLOBAL_defaultApi));
} else {
modem.off(false);
}
@@ -685,11 +701,9 @@
//SEND DATA
if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time
-
//Check if we should wait for settings back
bool getSettings = true;
if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken -= 10;} //remove the extra 10 seconds from times
-
char bytesToSend[160];
snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,getSettings,locString);
char result[180];
@@ -698,7 +712,11 @@
//DEBUG("\nfromserver: %s \n",result);
if (getSettings) {
saveSettings(result);
+ } else {
+ sendErrorLog("SETTINGS");
}
+ } else {
+ sendErrorLog("RESPONSE");
}
} else {
modem.off(false);
@@ -717,6 +735,7 @@
void event_activity_tx() {
DEBUG("ACTIVITY TX\n",false);
float temperature = getTemperature();
+ float voltage = getBatteryV();
if (modem.on(RET_force2G)) {
//SEND DATA
if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
@@ -755,6 +774,7 @@
DEBUG("STATE:SETUP\n",false);
if (setup()) {
// All good
+ setState(STATE_NORMAL);
} else {
RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs
setState(STATE_DORMANT);
@@ -766,9 +786,6 @@
{
DEBUG("STATE:NORMAL\n",false);
- //check and log motion
- checkMotion();
-
//LOCATION EVENT
bool run_location_tx = false;
switch (RET_setting_location_mode) {
@@ -776,7 +793,13 @@
if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
break;
case 2: //INTERVAL POST WITH MOTION CHECK
- if(RET_motionTriggeredInTXInterval && RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
+ if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
+ if (RET_motionTriggeredInTXInterval) {
+ run_location_tx = true;
+ } else {
+ if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX due to no motion\n");debug_exe();}
+ }
+ }
break;
case 3: //POST ON STOP MOTION
if (GLOBAL_motionStopFlagTriggered) { run_location_tx = true; GLOBAL_motionStopFlagTriggered = false; }
@@ -913,7 +936,7 @@
setDefaults();
//check battery
float voltage = getBatteryV();
- if (voltage > 2.5) {
+ if (voltage > 2.5f) {
//battery ok
LED1errorCode(6,2);
} else {
@@ -927,6 +950,7 @@
while(true) {
//WATCHDOG
watchdogKick();
+ LED1off();
RET_asleep = false;
//INIT
@@ -934,15 +958,18 @@
healthCheck(); //this must be after resetGlobals
GLOBAL_wakeTime = RET_RTCunixtime;
+ //check and log motion
+ if (RET_state == STATE_NORMAL) { checkMotion(); }
+
//MAIN LOGIC
//DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
//DEBUG("ACTIVITY:%s\n",RET_activityData);
if(DEBUG_ON){
if (RET_state != 99) {
- debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ST:%d, SETTINGS:%d, MOT: %d, RTC:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
+ debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FW:%d,ST:%d, SET:%d, MOTFR:%d, MOTST: %d, RTC:%u, LOC:%u, LOCFS:%u, ACT:%u \n", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, RET_motionState, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACT:%s\n",RET_activityData);debug_exe();
} else {
- debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ST:%d, WAKE@:%u, \n", RET_state, RET_eventTime_wakeFromDormant);debug_exe();
+ debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FW:%d, ST:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe();
}
}
@@ -968,6 +995,7 @@
}
//PRE-SLEEP ACTIONS
+ RET_motionTriggeredinFrame = false;
LED1off();
if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
modem.off(false);