init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Diff: main.cpp
- Revision:
- 38:476a9b5629a1
- Parent:
- 37:505ef618f06c
- Child:
- 39:f767b8037475
--- a/main.cpp Wed Jan 16 21:27:28 2019 +0000 +++ b/main.cpp Wed Jan 16 23:06:39 2019 +0000 @@ -3,7 +3,7 @@ //------------------------------------------------------------------------------ //DEFINES //------------------------------------------------------------------------------ -#define FW_VERSION 2 +#define FW_VERSION 11 #define SKU "GPSPLUS" #define HW_MAJORREVISION "001" @@ -12,6 +12,7 @@ //DEFAULT SETTINGS #define DEFAULT_SLEEP_FRAME 60000 +#define DEFAULT_FORCE2G true #define DEFAULT_LOCATION_MODE 2 #define DEFAULT_LOCATION_ACCURACY 2 // 0 = no location, 1 = cl only, 2 = gps then cl #define DEFAULT_LOCATION_TX_INTERVAL_MINS 1440 @@ -79,6 +80,7 @@ time_t RET_buttonHoldTime; time_t RET_SetupRunAt; time_t RET_SettingsGotAt; +bool RET_force2G; //MOTION STATE bool RET_motionTriggered; bool RET_motionTriggeredInTXInterval; @@ -158,7 +160,7 @@ debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_scan:%d", RET_setting_beacon_scan);debug_exe(); } - DEBUG("RET_RTCunixtime:%u",RET_RTCunixtime); + /*DEBUG("RET_RTCunixtime:%u",RET_RTCunixtime); DEBUG("RET_setting_firmware:%d",RET_setting_firmware); DEBUG("RET_setting_minimumupdate_hrs:%d",RET_setting_minimumupdate_hrs); DEBUG("RET_state:%d",RET_state); @@ -176,7 +178,7 @@ DEBUG("RET_setting_impact_alert:%d",RET_setting_impact_alert); DEBUG("RET_setting_connection_timeout:%d",RET_setting_connection_timeout); DEBUG("RET_setting_beacon_interval_seconds:%d",RET_setting_beacon_interval_seconds); - DEBUG("RET_setting_beacon_scan:%d",RET_setting_beacon_scan); + DEBUG("RET_setting_beacon_scan:%d",RET_setting_beacon_scan);*/ } float getBatteryV() { NRF52_SAADC batteryIn; @@ -277,9 +279,16 @@ GLOBAL_motionStopFlagTriggered = false; memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString)); } +void healthCheck() { + //check clock + if(RET_havesettings == 1) { + if (RET_RTCunixtime < 1547678732) { + //go dormant for 72hrs and then resetup + RET_havesettings = 0; setState(STATE_DORMANT); + } + } +} void setDefaults() { - //IDENTITY - //RET_imei = 0; //STATE RET_haveSettings = 0; RET_state = STATE_SETUP; @@ -287,6 +296,7 @@ RET_RTCunixtime = 0; RET_SetupRunAt = 0; RET_SettingsGotAt = 0; + RET_force2G = DEFAULT_FORCE2G; //SETTINGS RET_setting_firmware = 0; RET_setting_minimumupdate_hrs = 0; @@ -329,7 +339,6 @@ RET_eventTime_environmental_tx = 0; RET_eventTime_activity_tx = 0; RET_eventTime_wakeFromDormant = 0; - //PERIPHERAL RESET lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G); } @@ -436,8 +445,8 @@ 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; 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", &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) ) > 0 ) { - DEBUG("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\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); + /*DEBUG("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\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);*/ 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\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);debug_exe();} @@ -473,7 +482,6 @@ if (critical_fail_count == 0) { if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "GOT SETTINGS OK");debug_exe();} - DEBUG("GOT SETTINGS OK",false); dumpSettings(); saveEventTimes(); RET_haveSettings = true; @@ -481,18 +489,18 @@ RET_SettingsGotAt = RET_RTCunixtime; //check for firmware update - if (RET_setting_firmware != FW_VERSION) { + if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) { read_app_data_from_flash(&app_data); app_data.target_firmware_version = RET_setting_firmware; write_app_data_to_flash(&app_data); - ThisThread::wait(500); + if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Perform fw update\n");debug_exe();} + ThisThread::sleep_for(250); system_reset(); } return true; } else { if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();} - DEBUG("CRITICAL FAILS:%d",critical_fail_count); dumpSettings(); RET_haveSettings = false; return false; @@ -508,23 +516,19 @@ if(RET_setting_location_tx_interval_mins > 0) { RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode);debug_exe();} - DEBUG("EVENTSET - LOCATION TX at %u, MODE %d\n",RET_eventTime_location_tx, RET_setting_location_mode); } if(RET_setting_location_tx_failsafe_hrs > 0) { RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx);debug_exe();} - DEBUG("EVENTSET - LOCATION FAILSAFE TX at %u\n",RET_eventTime_location_failsafe_tx); } if(RET_setting_activity_tx_interval_hrs > 0) { RET_motionFrameStart = RET_RTCunixtime; //SET START FRAME INITAL RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx);debug_exe();} - DEBUG("EVENTSET - ACTIVITY TX at %u\n",RET_eventTime_activity_tx); } if(RET_eventTime_environmental_tx > 0) { RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60)); if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx);debug_exe();} - DEBUG("EVENTSET - ENVIRONMENTAL TX at %u\n",RET_eventTime_environmental_tx); } } @@ -537,10 +541,9 @@ float voltage = getBatteryV(); bool selftestresult = selfTest(); if (selftestresult == false) { LED1errorCode(4,20); } //ERROR 4 - if (modem.on()) { + if (modem.on(RET_force2G)) { //RET_imei = modem.getIMEI(); //DEBUG("imei: %lld \n",RET_imei); - //char* modemModel = modem.getModemModel(); char locString[70]; memcpy(locString, modem.getLocation(1, RET_setting_location_timeout), 70); if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { @@ -591,7 +594,7 @@ float temperature = getTemperature(); float voltage = getBatteryV(); int selftestresult = selfTest(); - if (modem.on()) { + if (modem.on(RET_force2G)) { char locString[70]; memcpy(locString, modem.getLocation(location_accuracy, RET_setting_location_timeout), 70); if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { @@ -627,7 +630,7 @@ float temperature = getTemperature(); float voltage = getBatteryV(); int selfTestResult = selfTest(); - if (modem.on()) { + if (modem.on(RET_force2G)) { char locString[70]; memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70); //DEBUG("locString:%s-\n",locString); @@ -666,7 +669,7 @@ void event_activity_tx() { DEBUG("ACTIVITY TX\n",false); float temperature = getTemperature(); - if (modem.on()) { + if (modem.on(RET_force2G)) { //SEND DATA if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) { char bytesToSend[160]; @@ -816,11 +819,9 @@ //------------------------------------------------------------------------------ // MAIN //------------------------------------------------------------------------------ -int main() { - LED1on(500); - +int main() { //INIT - watchdog.configure(300.0); + watchdog.configure(240.0); //4 mins LED1off(); RTCticker.attach(&RTCtick, 1.0); button.fall(&buttonPress); @@ -865,17 +866,20 @@ //MAIN LOOP while(true) { //WATCHDOG - watchdogKick(); //only need this if we're in while loop + watchdogKick(); //INIT - resetGlobals(); // could move this into state switches to save actions on each loop + resetGlobals(); + healthCheck(); GLOBAL_wakeTime = RET_RTCunixtime; //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){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "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_exe();} - if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACTIVITY:%s\n",RET_activityData);debug_exe();} + if(DEBUG_ON){ + debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "STATE:%d, HAVESETTINGS:%d, MOTION: %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), "ACTIVITY:%s\n",RET_activityData);debug_exe(); + } mainStateEngine();