whatever
Dependencies: C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed
Fork of PONY_Ph0-uAXIS by
main.cpp@50:bba466f093a4, 2016-01-14 (annotated)
- Committer:
- sgmcb
- Date:
- Thu Jan 14 21:28:20 2016 +0000
- Revision:
- 50:bba466f093a4
- Parent:
- 49:a8c40c816199
- Child:
- 51:61a1ec3c56fc
incorp Michael Wei power management
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:4e3cb26f6019 | 1 | #include "mbed.h" |
mazgch | 9:26f694bc31b4 | 2 | |
mazgch | 19:f022ff746eb8 | 3 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 4 | // C027 Support Libraries |
mazgch | 19:f022ff746eb8 | 5 | #include "GPS.h" |
mazgch | 19:f022ff746eb8 | 6 | #include "MDM.h" |
sgmcb | 36:258f3bb1c6a4 | 7 | |
sgmcb | 50:bba466f093a4 | 8 | // Power control libraries |
sgmcb | 50:bba466f093a4 | 9 | #include "PowerControl.h" |
sgmcb | 50:bba466f093a4 | 10 | #include "EthernetPowerControl.h" |
sgmcb | 50:bba466f093a4 | 11 | |
sgmcb | 50:bba466f093a4 | 12 | |
sgmcb | 42:8e6e647cb7d5 | 13 | // M2X Support Libraries |
sgmcb | 38:d86e744166b6 | 14 | #include <jsonlite.h> |
sgmcb | 38:d86e744166b6 | 15 | #include "M2XStreamClient.h" |
sgmcb | 37:71ab32b61dfb | 16 | |
sgmcb | 42:8e6e647cb7d5 | 17 | // PONY-specific config support libraries |
sgmcb | 41:f603d76dc6fe | 18 | #include "LIS331.h" |
sgmcb | 42:8e6e647cb7d5 | 19 | #include "PONY_Loc.h" // PONY Location Code |
sgmcb | 41:f603d76dc6fe | 20 | #include "PONY_sense.h" |
lawliet | 0:4e3cb26f6019 | 21 | |
sgmcb | 41:f603d76dc6fe | 22 | //---- |
sgmcb | 41:f603d76dc6fe | 23 | // DEBUG DEFINITIONS |
sgmcb | 48:3512b3c7a5ae | 24 | //#define THROWAWAY |
sgmcb | 47:d9180474fa1e | 25 | //#define MDMDEBUG |
sgmcb | 45:0d8cb417a9e2 | 26 | #define LOCDEBUG |
sgmcb | 37:71ab32b61dfb | 27 | |
sgmcb | 38:d86e744166b6 | 28 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 29 | // Cellular modem/SIM parameters |
sgmcb | 38:d86e744166b6 | 30 | #define SIMPIN "1111" //!SIMPIN is 1111 by default for AT&T SIMs. |
sgmcb | 38:d86e744166b6 | 31 | #define APN "m2m.com.attz" // Defined AT&T M2M APN |
sgmcb | 38:d86e744166b6 | 32 | #define USERNAME NULL //! Set the user name for your APN, or NULL if not needed (which, apparently, it isn't) |
sgmcb | 38:d86e744166b6 | 33 | #define PASSWORD NULL //! Set the password for your APN, or NULL if not needed (which, apparently, it isn't) |
sgmcb | 38:d86e744166b6 | 34 | |
sgmcb | 38:d86e744166b6 | 35 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 36 | // AT&T M2X Kekys |
sgmcb | 38:d86e744166b6 | 37 | |
sgmcb | 41:f603d76dc6fe | 38 | |
sgmcb | 41:f603d76dc6fe | 39 | #ifdef THROWAWAY |
sgmcb | 41:f603d76dc6fe | 40 | // Codes for a throwaway M2X device |
sgmcb | 50:bba466f093a4 | 41 | #define M2XAPIKEY "97f6f92f72b9dd1c66e9b81b982bc3ec" |
sgmcb | 50:bba466f093a4 | 42 | #define DEVID "743fe2502be9d4d0c550ffa9340998a3" |
sgmcb | 41:f603d76dc6fe | 43 | #endif |
sgmcb | 41:f603d76dc6fe | 44 | |
sgmcb | 41:f603d76dc6fe | 45 | #ifndef THROWAWAY |
sgmcb | 48:3512b3c7a5ae | 46 | // v1.4 codes |
sgmcb | 48:3512b3c7a5ae | 47 | #define M2XAPIKEY "537d09e921aa6589523e10aecde17a44" |
sgmcb | 48:3512b3c7a5ae | 48 | #define DEVID "59a85c486aaf8dd427945320f4f779eb" |
sgmcb | 41:f603d76dc6fe | 49 | #endif |
sgmcb | 41:f603d76dc6fe | 50 | |
sgmcb | 38:d86e744166b6 | 51 | |
sgmcb | 38:d86e744166b6 | 52 | //------------------------------------------------------------------------------------ |
sgmcb | 39:c14aff678b25 | 53 | // PIN Config |
sgmcb | 41:f603d76dc6fe | 54 | DigitalOut led1(LED1); |
sgmcb | 41:f603d76dc6fe | 55 | DigitalOut led2(LED2); |
sgmcb | 39:c14aff678b25 | 56 | |
sgmcb | 41:f603d76dc6fe | 57 | AnalogIn tempPin(P0_23); |
sgmcb | 49:a8c40c816199 | 58 | //AnalogIn tempGnd(P0_24); |
sgmcb | 38:d86e744166b6 | 59 | |
sgmcb | 41:f603d76dc6fe | 60 | //I2C axis(P0_0, P0_1); // SDA, SCL |
sgmcb | 44:bd259a4c83bb | 61 | //LIS331 axle(P0_0, P0_1); // Library object |
sgmcb | 38:d86e744166b6 | 62 | |
sgmcb | 38:d86e744166b6 | 63 | |
sgmcb | 42:8e6e647cb7d5 | 64 | //------------------------------------------------------------------------------------ |
sgmcb | 42:8e6e647cb7d5 | 65 | // GLOBAL VARIABLES |
sgmcb | 38:d86e744166b6 | 66 | |
sgmcb | 41:f603d76dc6fe | 67 | Ticker flipper; |
sgmcb | 38:d86e744166b6 | 68 | |
sgmcb | 42:8e6e647cb7d5 | 69 | // M2X Drivers |
sgmcb | 38:d86e744166b6 | 70 | Client client; |
sgmcb | 38:d86e744166b6 | 71 | M2XStreamClient m2xClient(&client, M2XAPIKEY); |
sgmcb | 40:82b70b92169e | 72 | int M2X_response; // For m2x message responses |
sgmcb | 38:d86e744166b6 | 73 | |
sgmcb | 42:8e6e647cb7d5 | 74 | |
sgmcb | 40:82b70b92169e | 75 | char statusBuf[145] = ""; |
sgmcb | 40:82b70b92169e | 76 | |
sgmcb | 42:8e6e647cb7d5 | 77 | // Location reading |
sgmcb | 48:3512b3c7a5ae | 78 | unsigned int kLocLoopDelayDef = 30 * 1000; // The default loop waiting time for location reads |
sgmcb | 48:3512b3c7a5ae | 79 | unsigned int kLocLoopDelay = kLocLoopDelayDef; |
sgmcb | 47:d9180474fa1e | 80 | |
sgmcb | 47:d9180474fa1e | 81 | |
sgmcb | 40:82b70b92169e | 82 | unsigned int kReadingDelay = 3 * 60 * 1000; // How many seconds to wait between reads |
sgmcb | 39:c14aff678b25 | 83 | unsigned int kReadingDelayClimb = 15000; // How many seconds to add to the wait period when sitting idle |
sgmcb | 39:c14aff678b25 | 84 | unsigned int kReadingDelayMax = 5 * 60000; // What's the maximum time between readings? |
sgmcb | 38:d86e744166b6 | 85 | |
sgmcb | 39:c14aff678b25 | 86 | |
sgmcb | 42:8e6e647cb7d5 | 87 | // System time |
sgmcb | 41:f603d76dc6fe | 88 | time_t kSysSeconds = time(NULL); |
sgmcb | 47:d9180474fa1e | 89 | time_t thisTime = time(NULL); |
sgmcb | 41:f603d76dc6fe | 90 | bool kSysClockSet = false; |
sgmcb | 41:f603d76dc6fe | 91 | tm kFormatTime; |
sgmcb | 39:c14aff678b25 | 92 | |
sgmcb | 39:c14aff678b25 | 93 | |
sgmcb | 47:d9180474fa1e | 94 | |
sgmcb | 42:8e6e647cb7d5 | 95 | // Global function definitions |
sgmcb | 38:d86e744166b6 | 96 | |
sgmcb | 41:f603d76dc6fe | 97 | void flip() { |
sgmcb | 41:f603d76dc6fe | 98 | led1 = !led1; |
sgmcb | 41:f603d76dc6fe | 99 | } |
sgmcb | 41:f603d76dc6fe | 100 | |
sgmcb | 38:d86e744166b6 | 101 | |
sgmcb | 38:d86e744166b6 | 102 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 103 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 104 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 105 | int main(void) |
sgmcb | 38:d86e744166b6 | 106 | { |
sgmcb | 38:d86e744166b6 | 107 | |
sgmcb | 49:a8c40c816199 | 108 | printf("\r\n\n\n\n-----\r\nI'm alive - vers. uA - device codes PONY_v1.4\r\n\n"); |
sgmcb | 41:f603d76dc6fe | 109 | #ifdef THROWAWAY |
sgmcb | 41:f603d76dc6fe | 110 | printf("Using THROWAWAY M2X device\r\n"); |
sgmcb | 41:f603d76dc6fe | 111 | #endif |
sgmcb | 49:a8c40c816199 | 112 | |
sgmcb | 49:a8c40c816199 | 113 | #ifndef THROWAWAY |
sgmcb | 49:a8c40c816199 | 114 | printf("Using PONY_v1.4 M2X device\r\n"); |
sgmcb | 49:a8c40c816199 | 115 | #endif |
sgmcb | 49:a8c40c816199 | 116 | |
sgmcb | 50:bba466f093a4 | 117 | // POWER MANAGEMENT |
sgmcb | 50:bba466f093a4 | 118 | PHY_PowerDown(); // Turn off the ethernet interface; -175mW |
sgmcb | 50:bba466f093a4 | 119 | |
sgmcb | 50:bba466f093a4 | 120 | //C027 thisBoard; |
sgmcb | 50:bba466f093a4 | 121 | |
sgmcb | 41:f603d76dc6fe | 122 | |
sgmcb | 38:d86e744166b6 | 123 | MDMSerial mdm; |
sgmcb | 39:c14aff678b25 | 124 | MDMParser::DevStatus devStatus = {}; |
sgmcb | 44:bd259a4c83bb | 125 | MDMParser::NetStatus netStatus = {}; |
sgmcb | 39:c14aff678b25 | 126 | bool mdmOk = mdm.init(SIMPIN, &devStatus); |
sgmcb | 46:ab4cccab10c9 | 127 | |
sgmcb | 41:f603d76dc6fe | 128 | #ifdef MDMDEBUG |
sgmcb | 46:ab4cccab10c9 | 129 | mdm.setDebug(4); |
sgmcb | 41:f603d76dc6fe | 130 | #endif |
sgmcb | 41:f603d76dc6fe | 131 | |
sgmcb | 43:80aa0c933e1a | 132 | // ---------------- |
sgmcb | 42:8e6e647cb7d5 | 133 | // LOCATION READING |
sgmcb | 39:c14aff678b25 | 134 | |
sgmcb | 38:d86e744166b6 | 135 | |
sgmcb | 44:bd259a4c83bb | 136 | // OPEN MODEM CONNECTION |
sgmcb | 38:d86e744166b6 | 137 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
sgmcb | 44:bd259a4c83bb | 138 | return -1; |
sgmcb | 44:bd259a4c83bb | 139 | |
sgmcb | 44:bd259a4c83bb | 140 | // SET DEEP SCAN MODE |
sgmcb | 44:bd259a4c83bb | 141 | printf("Configure deep scan mode\r\n"); |
sgmcb | 44:bd259a4c83bb | 142 | int locConf = mdm.cellLocConfigSensor(1); |
sgmcb | 44:bd259a4c83bb | 143 | |
sgmcb | 38:d86e744166b6 | 144 | |
sgmcb | 40:82b70b92169e | 145 | // Cell location data |
sgmcb | 49:a8c40c816199 | 146 | MDMSerial::CellLocData thisLoc; |
sgmcb | 44:bd259a4c83bb | 147 | |
sgmcb | 48:3512b3c7a5ae | 148 | // Location loop driving variables |
sgmcb | 45:0d8cb417a9e2 | 149 | int loopIter = 0; |
sgmcb | 48:3512b3c7a5ae | 150 | bool sendStatus = false; |
sgmcb | 45:0d8cb417a9e2 | 151 | |
sgmcb | 45:0d8cb417a9e2 | 152 | // Loop timing variables |
sgmcb | 45:0d8cb417a9e2 | 153 | Timer cellLocDelay; |
sgmcb | 45:0d8cb417a9e2 | 154 | |
sgmcb | 45:0d8cb417a9e2 | 155 | int cellLocPeriod = 30; // in seconds |
sgmcb | 45:0d8cb417a9e2 | 156 | int timeoutMargin = 5; // in seconds |
sgmcb | 45:0d8cb417a9e2 | 157 | |
sgmcb | 45:0d8cb417a9e2 | 158 | |
sgmcb | 45:0d8cb417a9e2 | 159 | // Location data filter variables |
sgmcb | 40:82b70b92169e | 160 | int locAccLower = 50; // Immediately accept locations with uncertainty lower than this |
sgmcb | 40:82b70b92169e | 161 | int locAccUpper = 5000; // Don't accept locations with uncertainty greater than this |
sgmcb | 46:ab4cccab10c9 | 162 | |
sgmcb | 46:ab4cccab10c9 | 163 | // Filter counters |
sgmcb | 47:d9180474fa1e | 164 | int fail1count = 0; |
sgmcb | 47:d9180474fa1e | 165 | int fail2count = 0; |
sgmcb | 47:d9180474fa1e | 166 | int fail3count = 0; |
sgmcb | 41:f603d76dc6fe | 167 | |
sgmcb | 41:f603d76dc6fe | 168 | |
sgmcb | 45:0d8cb417a9e2 | 169 | // Cell location call variables |
sgmcb | 45:0d8cb417a9e2 | 170 | const int sensorMask = 2; // 1 = GNSS, 2 = CellLocate (aka cell grid position info), 3 = Hybrid: GNSS + CellLocate |
sgmcb | 45:0d8cb417a9e2 | 171 | int cellLocReqTimeout = cellLocPeriod - timeoutMargin; // in seconds |
sgmcb | 45:0d8cb417a9e2 | 172 | const int targetAccuracy = 1; // in meters |
sgmcb | 45:0d8cb417a9e2 | 173 | |
sgmcb | 45:0d8cb417a9e2 | 174 | |
sgmcb | 42:8e6e647cb7d5 | 175 | // ----------------------- |
sgmcb | 40:82b70b92169e | 176 | // Location reporting loop |
sgmcb | 40:82b70b92169e | 177 | while(true) { |
sgmcb | 40:82b70b92169e | 178 | |
sgmcb | 44:bd259a4c83bb | 179 | // Set some important variables |
sgmcb | 41:f603d76dc6fe | 180 | M2X_response = 0; |
sgmcb | 48:3512b3c7a5ae | 181 | sendStatus = 0; |
sgmcb | 40:82b70b92169e | 182 | loopIter++; |
sgmcb | 40:82b70b92169e | 183 | |
sgmcb | 45:0d8cb417a9e2 | 184 | //printf("tick\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 185 | |
sgmcb | 45:0d8cb417a9e2 | 186 | // Request to fill cell-location buffer |
sgmcb | 45:0d8cb417a9e2 | 187 | cellLocDelay.start(); |
sgmcb | 47:d9180474fa1e | 188 | #ifdef LOCDEBUG |
sgmcb | 47:d9180474fa1e | 189 | printf("Initiate cellLocRequest call...\r\n"); |
sgmcb | 47:d9180474fa1e | 190 | #endif |
sgmcb | 45:0d8cb417a9e2 | 191 | mdm.cellLocRequest(sensorMask, cellLocReqTimeout, targetAccuracy); |
sgmcb | 41:f603d76dc6fe | 192 | |
sgmcb | 47:d9180474fa1e | 193 | |
sgmcb | 47:d9180474fa1e | 194 | // Hold next functions until we give cellLocRequest time to execute. |
sgmcb | 46:ab4cccab10c9 | 195 | while((cellLocDelay.read() < cellLocReqTimeout) && (!mdm.cellLocGet(&thisLoc)) ) { |
sgmcb | 45:0d8cb417a9e2 | 196 | // Nothin much |
sgmcb | 45:0d8cb417a9e2 | 197 | }; |
sgmcb | 47:d9180474fa1e | 198 | |
sgmcb | 45:0d8cb417a9e2 | 199 | // Stop and reset cellLocDelay timer |
sgmcb | 47:d9180474fa1e | 200 | cellLocDelay.stop();cellLocDelay.reset(); |
sgmcb | 45:0d8cb417a9e2 | 201 | |
sgmcb | 45:0d8cb417a9e2 | 202 | // We've either got SOME location data or we've timed out. There are three filters: |
sgmcb | 45:0d8cb417a9e2 | 203 | // 1. Does it exist? |
sgmcb | 45:0d8cb417a9e2 | 204 | // 2. Is it new, or extrapolated from old info? |
sgmcb | 45:0d8cb417a9e2 | 205 | // 3. Is the uncertainty within our accepted range? |
sgmcb | 45:0d8cb417a9e2 | 206 | |
sgmcb | 45:0d8cb417a9e2 | 207 | /* N.B. |
sgmcb | 45:0d8cb417a9e2 | 208 | Right now I'm conducting these tests serially in the loop in ancitipation of behavior that will modify itself based on whether we're seeing lots of highly uncertain or extrapolated readings; |
sgmcb | 45:0d8cb417a9e2 | 209 | not sure what that behavior is yet, but we're not optimizing enough that I care about nesting conditionals yet. |
sgmcb | 45:0d8cb417a9e2 | 210 | */ |
sgmcb | 41:f603d76dc6fe | 211 | |
sgmcb | 45:0d8cb417a9e2 | 212 | // 1. Does the location data exist? |
sgmcb | 47:d9180474fa1e | 213 | if( thisLoc.validData ) {//if( (mdm.cellLocGet(&thisLoc)) ) { |
sgmcb | 46:ab4cccab10c9 | 214 | fail1count = 0; |
sgmcb | 45:0d8cb417a9e2 | 215 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 216 | printf("LOC uncertainty= %d, sensor= %d\r\n", thisLoc.uncertainty, thisLoc.sensorUsed ); |
sgmcb | 45:0d8cb417a9e2 | 217 | printf("LOC = latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude); |
sgmcb | 45:0d8cb417a9e2 | 218 | #endif |
sgmcb | 45:0d8cb417a9e2 | 219 | |
sgmcb | 47:d9180474fa1e | 220 | |
sgmcb | 47:d9180474fa1e | 221 | (thisLoc.time).tm_year -= 1900; |
sgmcb | 47:d9180474fa1e | 222 | kSysSeconds = mktime( &(thisLoc.time) ); |
sgmcb | 47:d9180474fa1e | 223 | |
sgmcb | 47:d9180474fa1e | 224 | // Set the system clock, if it's not already |
sgmcb | 47:d9180474fa1e | 225 | if(!kSysClockSet) { |
sgmcb | 47:d9180474fa1e | 226 | set_time(kSysSeconds); |
sgmcb | 47:d9180474fa1e | 227 | printf("\033[31mSet system clock time.\033[39m\r\n"); |
sgmcb | 47:d9180474fa1e | 228 | kSysClockSet=true; |
sgmcb | 47:d9180474fa1e | 229 | } |
sgmcb | 49:a8c40c816199 | 230 | #ifdef LOCDEBUG |
sgmcb | 49:a8c40c816199 | 231 | printf("LOC read at: %s\r", ctime(&kSysSeconds) ); |
sgmcb | 49:a8c40c816199 | 232 | #endif |
sgmcb | 47:d9180474fa1e | 233 | |
sgmcb | 47:d9180474fa1e | 234 | |
sgmcb | 47:d9180474fa1e | 235 | |
sgmcb | 45:0d8cb417a9e2 | 236 | // 2. Is this new position data, or extrapolated from an old reading? |
sgmcb | 45:0d8cb417a9e2 | 237 | if(thisLoc.sensorUsed == 2) { |
sgmcb | 46:ab4cccab10c9 | 238 | fail2count = 0; |
sgmcb | 45:0d8cb417a9e2 | 239 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 240 | printf("LOC has new information (non-extrapolation)\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 241 | #endif |
sgmcb | 45:0d8cb417a9e2 | 242 | |
sgmcb | 45:0d8cb417a9e2 | 243 | // 3. Is this data acceptably accurate (e.g. low enough reported uncertainty)? |
sgmcb | 45:0d8cb417a9e2 | 244 | if( (thisLoc.uncertainty < locAccUpper) && (thisLoc.uncertainty > 0) ) { |
sgmcb | 46:ab4cccab10c9 | 245 | fail3count = 0; |
sgmcb | 45:0d8cb417a9e2 | 246 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 247 | printf("LOC has suitable uncertainty (<%i).\r\n", locAccUpper); |
sgmcb | 45:0d8cb417a9e2 | 248 | #endif |
sgmcb | 45:0d8cb417a9e2 | 249 | |
sgmcb | 45:0d8cb417a9e2 | 250 | |
sgmcb | 47:d9180474fa1e | 251 | // PASSSES ALL 3 TESTS => REPORT TO M2X |
sgmcb | 45:0d8cb417a9e2 | 252 | |
sgmcb | 45:0d8cb417a9e2 | 253 | // Report position |
sgmcb | 48:3512b3c7a5ae | 254 | printf("\r\n\033[34mTRANSMIT LOCATION DATA:\033[39m\r\nla=%0.5f, lo=%0.5f, alt=%d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude); |
sgmcb | 45:0d8cb417a9e2 | 255 | M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) thisLoc.latitude, (double) thisLoc.longitude, (double) thisLoc.altitude); |
sgmcb | 45:0d8cb417a9e2 | 256 | if(M2X_response == 202) |
sgmcb | 45:0d8cb417a9e2 | 257 | printf("Location POST successful\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 258 | |
sgmcb | 45:0d8cb417a9e2 | 259 | // Report uncertainty |
sgmcb | 45:0d8cb417a9e2 | 260 | M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", thisLoc.uncertainty); |
sgmcb | 45:0d8cb417a9e2 | 261 | if(M2X_response == 202) |
sgmcb | 47:d9180474fa1e | 262 | printf("Location Confidence (%d) POST successful\r\n", thisLoc.uncertainty); |
sgmcb | 47:d9180474fa1e | 263 | |
sgmcb | 49:a8c40c816199 | 264 | |
sgmcb | 49:a8c40c816199 | 265 | // Report temperature |
sgmcb | 49:a8c40c816199 | 266 | float thisTemp = getTemp(&tempPin); |
sgmcb | 49:a8c40c816199 | 267 | printf("temp=%.1fC\r\n",thisTemp); |
sgmcb | 49:a8c40c816199 | 268 | |
sgmcb | 49:a8c40c816199 | 269 | M2X_response = m2xClient.updateStreamValue(DEVID, "kegtemp", thisTemp); |
sgmcb | 49:a8c40c816199 | 270 | if(M2X_response == 202) |
sgmcb | 49:a8c40c816199 | 271 | printf("Temperature (%.2f) POST successful\r\n", thisTemp); |
sgmcb | 49:a8c40c816199 | 272 | |
sgmcb | 49:a8c40c816199 | 273 | |
sgmcb | 47:d9180474fa1e | 274 | // Report system time |
sgmcb | 47:d9180474fa1e | 275 | thisTime = time(NULL); |
sgmcb | 47:d9180474fa1e | 276 | M2X_response = m2xClient.updateStreamValue(DEVID, "systime", ctime(&thisTime) ); |
sgmcb | 47:d9180474fa1e | 277 | if(M2X_response == 202) |
sgmcb | 48:3512b3c7a5ae | 278 | printf("Systime POST successful - %s \r", ctime(&thisTime) ); |
sgmcb | 47:d9180474fa1e | 279 | |
sgmcb | 47:d9180474fa1e | 280 | |
sgmcb | 45:0d8cb417a9e2 | 281 | } |
sgmcb | 45:0d8cb417a9e2 | 282 | // Fails 3: |
sgmcb | 45:0d8cb417a9e2 | 283 | else { |
sgmcb | 49:a8c40c816199 | 284 | fail3count++; |
sgmcb | 45:0d8cb417a9e2 | 285 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 286 | printf("LOC has high uncertainty (<%i); will not report.\r\n", thisLoc.uncertainty); |
sgmcb | 45:0d8cb417a9e2 | 287 | #endif |
sgmcb | 45:0d8cb417a9e2 | 288 | } |
sgmcb | 45:0d8cb417a9e2 | 289 | } |
sgmcb | 45:0d8cb417a9e2 | 290 | // Fails 2: |
sgmcb | 45:0d8cb417a9e2 | 291 | else { |
sgmcb | 49:a8c40c816199 | 292 | fail2count++; |
sgmcb | 45:0d8cb417a9e2 | 293 | #ifdef LOCDEBUG |
sgmcb | 47:d9180474fa1e | 294 | printf("LOC is extrapolated information; disregard.\r\n"); |
sgmcb | 46:ab4cccab10c9 | 295 | #endif |
sgmcb | 46:ab4cccab10c9 | 296 | |
sgmcb | 45:0d8cb417a9e2 | 297 | } |
sgmcb | 45:0d8cb417a9e2 | 298 | } |
sgmcb | 49:a8c40c816199 | 299 | |
sgmcb | 46:ab4cccab10c9 | 300 | // Fails 1 and timer is up: |
sgmcb | 45:0d8cb417a9e2 | 301 | else { |
sgmcb | 46:ab4cccab10c9 | 302 | ++fail1count; |
sgmcb | 45:0d8cb417a9e2 | 303 | printf("CellLocRequest timeout; no location data available.\r\n"); |
sgmcb | 47:d9180474fa1e | 304 | } |
sgmcb | 47:d9180474fa1e | 305 | |
sgmcb | 49:a8c40c816199 | 306 | |
sgmcb | 49:a8c40c816199 | 307 | |
sgmcb | 49:a8c40c816199 | 308 | |
sgmcb | 49:a8c40c816199 | 309 | |
sgmcb | 49:a8c40c816199 | 310 | // -------------------- |
sgmcb | 47:d9180474fa1e | 311 | // DETERMINE LOOP DELAY |
sgmcb | 47:d9180474fa1e | 312 | |
sgmcb | 47:d9180474fa1e | 313 | |
sgmcb | 47:d9180474fa1e | 314 | |
sgmcb | 47:d9180474fa1e | 315 | // TODO: Add conditionals to handle successful location and unsuccessful communication with M2X |
sgmcb | 46:ab4cccab10c9 | 316 | |
sgmcb | 48:3512b3c7a5ae | 317 | // ACTIONS BASED ON LOCATION READ SUCCESS/FAILURE |
sgmcb | 48:3512b3c7a5ae | 318 | |
sgmcb | 47:d9180474fa1e | 319 | // Successful read |
sgmcb | 49:a8c40c816199 | 320 | if( (!fail1count) && (!fail2count) && (!fail3count) ) { |
sgmcb | 49:a8c40c816199 | 321 | kLocLoopDelay = 1 * (60 * 1000); // Set # minute wait before next read |
sgmcb | 47:d9180474fa1e | 322 | printf("Successful LOC read; "); |
sgmcb | 45:0d8cb417a9e2 | 323 | } |
sgmcb | 48:3512b3c7a5ae | 324 | // Too many invalid location reads |
sgmcb | 48:3512b3c7a5ae | 325 | else if (fail1count > 10) { |
sgmcb | 48:3512b3c7a5ae | 326 | sprintf(statusBuf, "Fail1count exceeds 10.\r\n"); |
sgmcb | 48:3512b3c7a5ae | 327 | printf(statusBuf); |
sgmcb | 48:3512b3c7a5ae | 328 | fail1count = 0; |
sgmcb | 48:3512b3c7a5ae | 329 | sendStatus = true; |
sgmcb | 47:d9180474fa1e | 330 | } |
sgmcb | 48:3512b3c7a5ae | 331 | |
sgmcb | 48:3512b3c7a5ae | 332 | else if (fail2count > 10) { |
sgmcb | 48:3512b3c7a5ae | 333 | sprintf(statusBuf, "Fail2count exceeds 10.\r\n"); |
sgmcb | 48:3512b3c7a5ae | 334 | printf(statusBuf); |
sgmcb | 48:3512b3c7a5ae | 335 | fail2count = 0; |
sgmcb | 48:3512b3c7a5ae | 336 | sendStatus = true; |
sgmcb | 48:3512b3c7a5ae | 337 | } |
sgmcb | 48:3512b3c7a5ae | 338 | |
sgmcb | 48:3512b3c7a5ae | 339 | else if (fail3count > 10) { |
sgmcb | 48:3512b3c7a5ae | 340 | sprintf(statusBuf, "Fail3Count exceeds 10.\r\n"); |
sgmcb | 48:3512b3c7a5ae | 341 | printf(statusBuf); |
sgmcb | 48:3512b3c7a5ae | 342 | fail3count = 0; |
sgmcb | 48:3512b3c7a5ae | 343 | sendStatus = true; |
sgmcb | 48:3512b3c7a5ae | 344 | } |
sgmcb | 48:3512b3c7a5ae | 345 | |
sgmcb | 47:d9180474fa1e | 346 | else { |
sgmcb | 48:3512b3c7a5ae | 347 | kLocLoopDelay = kLocLoopDelayDef; |
sgmcb | 47:d9180474fa1e | 348 | printf("Failed LOC loop; "); |
sgmcb | 47:d9180474fa1e | 349 | } |
sgmcb | 47:d9180474fa1e | 350 | |
sgmcb | 49:a8c40c816199 | 351 | // If we have no status update to send, send one every 10 loops just cause. |
sgmcb | 49:a8c40c816199 | 352 | if (!sendStatus && (loopIter % 10 == 0) ) { |
sgmcb | 49:a8c40c816199 | 353 | sprintf(statusBuf, "Periodic status update check-in.\r\n"); |
sgmcb | 49:a8c40c816199 | 354 | printf(statusBuf); |
sgmcb | 49:a8c40c816199 | 355 | sendStatus = true; |
sgmcb | 49:a8c40c816199 | 356 | } |
sgmcb | 49:a8c40c816199 | 357 | |
sgmcb | 48:3512b3c7a5ae | 358 | // Transmit a status response? |
sgmcb | 48:3512b3c7a5ae | 359 | if(sendStatus) { |
sgmcb | 48:3512b3c7a5ae | 360 | M2X_response = m2xClient.updateStreamValue(DEVID, "status", statusBuf); |
sgmcb | 48:3512b3c7a5ae | 361 | if(M2X_response == 202) |
sgmcb | 48:3512b3c7a5ae | 362 | printf("Status POST successful\r\n"); |
sgmcb | 48:3512b3c7a5ae | 363 | } |
sgmcb | 47:d9180474fa1e | 364 | |
sgmcb | 47:d9180474fa1e | 365 | // Set loop delay (i.e. period) |
sgmcb | 47:d9180474fa1e | 366 | printf("loop waiting for %i seconds...\r\n---\n\r\n", (kLocLoopDelay / 1000) ); |
sgmcb | 47:d9180474fa1e | 367 | |
sgmcb | 48:3512b3c7a5ae | 368 | flipper.attach(&flip, 0.5); // LED toggle! // the address of the function to be attached (flip) and the interval (2 seconds) |
sgmcb | 50:bba466f093a4 | 369 | |
sgmcb | 50:bba466f093a4 | 370 | // Put this shiz to sleep |
sgmcb | 50:bba466f093a4 | 371 | |
sgmcb | 50:bba466f093a4 | 372 | |
sgmcb | 50:bba466f093a4 | 373 | |
sgmcb | 48:3512b3c7a5ae | 374 | delay(kLocLoopDelay); |
sgmcb | 48:3512b3c7a5ae | 375 | led1 = 0; // Turn that LED off! |
sgmcb | 40:82b70b92169e | 376 | |
sgmcb | 40:82b70b92169e | 377 | } |
sgmcb | 38:d86e744166b6 | 378 | |
sgmcb | 39:c14aff678b25 | 379 | |
sgmcb | 40:82b70b92169e | 380 | //mdm.disconnect(); |
sgmcb | 40:82b70b92169e | 381 | //mdm.powerOff(); |
msinig | 35:16f1037626e3 | 382 | |
lawliet | 0:4e3cb26f6019 | 383 | } |