whatever
Dependencies: C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed
Fork of PONY_Ph0-uAXIS by
main.cpp@45:0d8cb417a9e2, 2015-12-29 (annotated)
- Committer:
- sgmcb
- Date:
- Tue Dec 29 07:42:39 2015 +0000
- Revision:
- 45:0d8cb417a9e2
- Parent:
- 44:bd259a4c83bb
- Child:
- 46:ab4cccab10c9
Rebuilt location data parsing conditionals
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 | 42:8e6e647cb7d5 | 8 | // M2X Support Libraries |
sgmcb | 38:d86e744166b6 | 9 | #include <jsonlite.h> |
sgmcb | 38:d86e744166b6 | 10 | #include "M2XStreamClient.h" |
sgmcb | 37:71ab32b61dfb | 11 | |
sgmcb | 42:8e6e647cb7d5 | 12 | // PONY-specific config support libraries |
sgmcb | 41:f603d76dc6fe | 13 | #include "LIS331.h" |
sgmcb | 42:8e6e647cb7d5 | 14 | #include "PONY_Loc.h" // PONY Location Code |
sgmcb | 41:f603d76dc6fe | 15 | #include "PONY_sense.h" |
lawliet | 0:4e3cb26f6019 | 16 | |
sgmcb | 41:f603d76dc6fe | 17 | //---- |
sgmcb | 41:f603d76dc6fe | 18 | // DEBUG DEFINITIONS |
sgmcb | 41:f603d76dc6fe | 19 | #define THROWAWAY |
sgmcb | 41:f603d76dc6fe | 20 | //#define MDMDEBUG |
sgmcb | 45:0d8cb417a9e2 | 21 | #define LOCDEBUG |
sgmcb | 37:71ab32b61dfb | 22 | |
sgmcb | 38:d86e744166b6 | 23 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 24 | // Cellular modem/SIM parameters |
sgmcb | 38:d86e744166b6 | 25 | #define SIMPIN "1111" //!SIMPIN is 1111 by default for AT&T SIMs. |
sgmcb | 38:d86e744166b6 | 26 | #define APN "m2m.com.attz" // Defined AT&T M2M APN |
sgmcb | 38:d86e744166b6 | 27 | #define USERNAME NULL //! Set the user name for your APN, or NULL if not needed (which, apparently, it isn't) |
sgmcb | 38:d86e744166b6 | 28 | #define PASSWORD NULL //! Set the password for your APN, or NULL if not needed (which, apparently, it isn't) |
sgmcb | 38:d86e744166b6 | 29 | |
sgmcb | 38:d86e744166b6 | 30 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 31 | // AT&T M2X Kekys |
sgmcb | 38:d86e744166b6 | 32 | |
sgmcb | 41:f603d76dc6fe | 33 | |
sgmcb | 41:f603d76dc6fe | 34 | #ifdef THROWAWAY |
sgmcb | 41:f603d76dc6fe | 35 | // Codes for a throwaway M2X device |
sgmcb | 39:c14aff678b25 | 36 | #define M2XAPIKEY "54c6bbf11012f34830bd23cc091ca250" |
sgmcb | 39:c14aff678b25 | 37 | #define DEVID "b9c57667ee0495dc0b3cddd890f8d2df" |
sgmcb | 41:f603d76dc6fe | 38 | #endif |
sgmcb | 41:f603d76dc6fe | 39 | |
sgmcb | 41:f603d76dc6fe | 40 | #ifndef THROWAWAY |
sgmcb | 41:f603d76dc6fe | 41 | // v1.2 codes |
sgmcb | 41:f603d76dc6fe | 42 | #define M2XAPIKEY "bbc483492238dc76f7d12f0cd6e13a4b" |
sgmcb | 41:f603d76dc6fe | 43 | #define DEVID "3764db38b6c9ec4045a38e0125b14b4c" |
sgmcb | 41:f603d76dc6fe | 44 | #endif |
sgmcb | 41:f603d76dc6fe | 45 | |
sgmcb | 38:d86e744166b6 | 46 | |
sgmcb | 38:d86e744166b6 | 47 | //------------------------------------------------------------------------------------ |
sgmcb | 39:c14aff678b25 | 48 | // PIN Config |
sgmcb | 41:f603d76dc6fe | 49 | DigitalOut led1(LED1); |
sgmcb | 41:f603d76dc6fe | 50 | DigitalOut led2(LED2); |
sgmcb | 39:c14aff678b25 | 51 | |
sgmcb | 41:f603d76dc6fe | 52 | AnalogIn tempPin(P0_23); |
sgmcb | 41:f603d76dc6fe | 53 | AnalogIn tempGnd(P0_24); |
sgmcb | 38:d86e744166b6 | 54 | |
sgmcb | 41:f603d76dc6fe | 55 | //I2C axis(P0_0, P0_1); // SDA, SCL |
sgmcb | 44:bd259a4c83bb | 56 | //LIS331 axle(P0_0, P0_1); // Library object |
sgmcb | 38:d86e744166b6 | 57 | |
sgmcb | 38:d86e744166b6 | 58 | |
sgmcb | 42:8e6e647cb7d5 | 59 | //------------------------------------------------------------------------------------ |
sgmcb | 42:8e6e647cb7d5 | 60 | // GLOBAL VARIABLES |
sgmcb | 38:d86e744166b6 | 61 | |
sgmcb | 41:f603d76dc6fe | 62 | Ticker flipper; |
sgmcb | 38:d86e744166b6 | 63 | |
sgmcb | 42:8e6e647cb7d5 | 64 | // M2X Drivers |
sgmcb | 38:d86e744166b6 | 65 | Client client; |
sgmcb | 38:d86e744166b6 | 66 | M2XStreamClient m2xClient(&client, M2XAPIKEY); |
sgmcb | 40:82b70b92169e | 67 | int M2X_response; // For m2x message responses |
sgmcb | 38:d86e744166b6 | 68 | |
sgmcb | 42:8e6e647cb7d5 | 69 | |
sgmcb | 40:82b70b92169e | 70 | char statusBuf[145] = ""; |
sgmcb | 40:82b70b92169e | 71 | |
sgmcb | 42:8e6e647cb7d5 | 72 | // Location reading |
sgmcb | 40:82b70b92169e | 73 | unsigned int kReadingDelay = 3 * 60 * 1000; // How many seconds to wait between reads |
sgmcb | 39:c14aff678b25 | 74 | unsigned int kReadingDelayClimb = 15000; // How many seconds to add to the wait period when sitting idle |
sgmcb | 39:c14aff678b25 | 75 | unsigned int kReadingDelayMax = 5 * 60000; // What's the maximum time between readings? |
sgmcb | 38:d86e744166b6 | 76 | |
sgmcb | 39:c14aff678b25 | 77 | |
sgmcb | 42:8e6e647cb7d5 | 78 | // System time |
sgmcb | 41:f603d76dc6fe | 79 | time_t kSysSeconds = time(NULL); |
sgmcb | 41:f603d76dc6fe | 80 | bool kSysClockSet = false; |
sgmcb | 41:f603d76dc6fe | 81 | tm kFormatTime; |
sgmcb | 39:c14aff678b25 | 82 | |
sgmcb | 39:c14aff678b25 | 83 | |
sgmcb | 42:8e6e647cb7d5 | 84 | // Global function definitions |
sgmcb | 38:d86e744166b6 | 85 | |
sgmcb | 41:f603d76dc6fe | 86 | void flip() { |
sgmcb | 41:f603d76dc6fe | 87 | led1 = !led1; |
sgmcb | 41:f603d76dc6fe | 88 | } |
sgmcb | 41:f603d76dc6fe | 89 | |
sgmcb | 38:d86e744166b6 | 90 | |
sgmcb | 38:d86e744166b6 | 91 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 92 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 93 | //------------------------------------------------------------------------------------ |
sgmcb | 38:d86e744166b6 | 94 | int main(void) |
sgmcb | 38:d86e744166b6 | 95 | { |
sgmcb | 38:d86e744166b6 | 96 | |
sgmcb | 41:f603d76dc6fe | 97 | printf("\r\n\n\n\n-----\r\nI'm alive - vers. uA\r\n\n"); |
sgmcb | 41:f603d76dc6fe | 98 | #ifdef THROWAWAY |
sgmcb | 41:f603d76dc6fe | 99 | printf("Using THROWAWAY M2X device\r\n"); |
sgmcb | 41:f603d76dc6fe | 100 | #endif |
sgmcb | 41:f603d76dc6fe | 101 | |
sgmcb | 38:d86e744166b6 | 102 | MDMSerial mdm; |
sgmcb | 39:c14aff678b25 | 103 | MDMParser::DevStatus devStatus = {}; |
sgmcb | 44:bd259a4c83bb | 104 | MDMParser::NetStatus netStatus = {}; |
sgmcb | 39:c14aff678b25 | 105 | bool mdmOk = mdm.init(SIMPIN, &devStatus); |
sgmcb | 41:f603d76dc6fe | 106 | #ifdef MDMDEBUG |
sgmcb | 39:c14aff678b25 | 107 | //mdm.setDebug(4); |
sgmcb | 41:f603d76dc6fe | 108 | #endif |
sgmcb | 41:f603d76dc6fe | 109 | |
sgmcb | 43:80aa0c933e1a | 110 | // ---------------- |
sgmcb | 42:8e6e647cb7d5 | 111 | // LOCATION READING |
sgmcb | 39:c14aff678b25 | 112 | |
sgmcb | 38:d86e744166b6 | 113 | |
sgmcb | 44:bd259a4c83bb | 114 | // OPEN MODEM CONNECTION |
sgmcb | 38:d86e744166b6 | 115 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
sgmcb | 44:bd259a4c83bb | 116 | return -1; |
sgmcb | 44:bd259a4c83bb | 117 | |
sgmcb | 44:bd259a4c83bb | 118 | // SET DEEP SCAN MODE |
sgmcb | 44:bd259a4c83bb | 119 | printf("Configure deep scan mode\r\n"); |
sgmcb | 44:bd259a4c83bb | 120 | int locConf = mdm.cellLocConfigSensor(1); |
sgmcb | 44:bd259a4c83bb | 121 | |
sgmcb | 38:d86e744166b6 | 122 | |
sgmcb | 44:bd259a4c83bb | 123 | |
sgmcb | 44:bd259a4c83bb | 124 | |
sgmcb | 44:bd259a4c83bb | 125 | |
sgmcb | 44:bd259a4c83bb | 126 | |
sgmcb | 44:bd259a4c83bb | 127 | |
sgmcb | 44:bd259a4c83bb | 128 | |
sgmcb | 44:bd259a4c83bb | 129 | |
sgmcb | 40:82b70b92169e | 130 | |
sgmcb | 45:0d8cb417a9e2 | 131 | |
sgmcb | 40:82b70b92169e | 132 | |
sgmcb | 40:82b70b92169e | 133 | // Cell location data |
sgmcb | 41:f603d76dc6fe | 134 | MDMSerial::CellLocData ponyLoc, thisLoc; |
sgmcb | 44:bd259a4c83bb | 135 | |
sgmcb | 40:82b70b92169e | 136 | |
sgmcb | 40:82b70b92169e | 137 | |
sgmcb | 41:f603d76dc6fe | 138 | |
sgmcb | 40:82b70b92169e | 139 | // Loop driving variables |
sgmcb | 45:0d8cb417a9e2 | 140 | int loopIter = 0; |
sgmcb | 40:82b70b92169e | 141 | bool locLock; |
sgmcb | 45:0d8cb417a9e2 | 142 | |
sgmcb | 45:0d8cb417a9e2 | 143 | // Loop timing variables |
sgmcb | 45:0d8cb417a9e2 | 144 | Timer cellLocDelay; |
sgmcb | 45:0d8cb417a9e2 | 145 | int submitPeriod = 30; // AXE THIS |
sgmcb | 45:0d8cb417a9e2 | 146 | |
sgmcb | 45:0d8cb417a9e2 | 147 | int cellLocPeriod = 30; // in seconds |
sgmcb | 45:0d8cb417a9e2 | 148 | int timeoutMargin = 5; // in seconds |
sgmcb | 45:0d8cb417a9e2 | 149 | |
sgmcb | 45:0d8cb417a9e2 | 150 | |
sgmcb | 45:0d8cb417a9e2 | 151 | |
sgmcb | 45:0d8cb417a9e2 | 152 | // Loop limit variables |
sgmcb | 45:0d8cb417a9e2 | 153 | |
sgmcb | 41:f603d76dc6fe | 154 | int readLoopMax = 100; |
sgmcb | 45:0d8cb417a9e2 | 155 | |
sgmcb | 45:0d8cb417a9e2 | 156 | // Location data filter variables |
sgmcb | 40:82b70b92169e | 157 | int locAccLower = 50; // Immediately accept locations with uncertainty lower than this |
sgmcb | 40:82b70b92169e | 158 | int locAccUpper = 5000; // Don't accept locations with uncertainty greater than this |
sgmcb | 41:f603d76dc6fe | 159 | |
sgmcb | 41:f603d76dc6fe | 160 | |
sgmcb | 45:0d8cb417a9e2 | 161 | // Cell location call variables |
sgmcb | 45:0d8cb417a9e2 | 162 | const int sensorMask = 2; // 1 = GNSS, 2 = CellLocate (aka cell grid position info), 3 = Hybrid: GNSS + CellLocate |
sgmcb | 45:0d8cb417a9e2 | 163 | int cellLocReqTimeout = cellLocPeriod - timeoutMargin; // in seconds |
sgmcb | 45:0d8cb417a9e2 | 164 | const int targetAccuracy = 1; // in meters |
sgmcb | 45:0d8cb417a9e2 | 165 | |
sgmcb | 45:0d8cb417a9e2 | 166 | |
sgmcb | 42:8e6e647cb7d5 | 167 | // ----------------------- |
sgmcb | 40:82b70b92169e | 168 | // Location reporting loop |
sgmcb | 40:82b70b92169e | 169 | while(true) { |
sgmcb | 40:82b70b92169e | 170 | |
sgmcb | 44:bd259a4c83bb | 171 | // Set some important variables |
sgmcb | 41:f603d76dc6fe | 172 | M2X_response = 0; |
sgmcb | 40:82b70b92169e | 173 | locLock = false; |
sgmcb | 40:82b70b92169e | 174 | loopIter++; |
sgmcb | 40:82b70b92169e | 175 | |
sgmcb | 45:0d8cb417a9e2 | 176 | //printf("tick\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 177 | |
sgmcb | 45:0d8cb417a9e2 | 178 | // Request to fill cell-location buffer |
sgmcb | 45:0d8cb417a9e2 | 179 | cellLocDelay.start(); |
sgmcb | 45:0d8cb417a9e2 | 180 | mdm.cellLocRequest(sensorMask, cellLocReqTimeout, targetAccuracy); |
sgmcb | 41:f603d76dc6fe | 181 | |
sgmcb | 45:0d8cb417a9e2 | 182 | // Hold next functions until we give cellLocRequest time to execute. |
sgmcb | 45:0d8cb417a9e2 | 183 | while( (!mdm.cellLocGet(&thisLoc)) && (cellLocDelay.read() < cellLocReqTimeout)) { |
sgmcb | 45:0d8cb417a9e2 | 184 | // Nothin much |
sgmcb | 45:0d8cb417a9e2 | 185 | }; |
sgmcb | 41:f603d76dc6fe | 186 | |
sgmcb | 45:0d8cb417a9e2 | 187 | //printf("cellLocDelay=%f\r\n",cellLocDelay.read()); |
sgmcb | 45:0d8cb417a9e2 | 188 | |
sgmcb | 45:0d8cb417a9e2 | 189 | // Stop and reset cellLocDelay timer |
sgmcb | 45:0d8cb417a9e2 | 190 | cellLocDelay.start();cellLocDelay.reset(); |
sgmcb | 45:0d8cb417a9e2 | 191 | |
sgmcb | 45:0d8cb417a9e2 | 192 | // We've either got SOME location data or we've timed out. There are three filters: |
sgmcb | 45:0d8cb417a9e2 | 193 | // 1. Does it exist? |
sgmcb | 45:0d8cb417a9e2 | 194 | // 2. Is it new, or extrapolated from old info? |
sgmcb | 45:0d8cb417a9e2 | 195 | // 3. Is the uncertainty within our accepted range? |
sgmcb | 45:0d8cb417a9e2 | 196 | |
sgmcb | 45:0d8cb417a9e2 | 197 | /* N.B. |
sgmcb | 45:0d8cb417a9e2 | 198 | 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 | 199 | not sure what that behavior is yet, but we're not optimizing enough that I care about nesting conditionals yet. |
sgmcb | 45:0d8cb417a9e2 | 200 | */ |
sgmcb | 41:f603d76dc6fe | 201 | |
sgmcb | 45:0d8cb417a9e2 | 202 | // 1. Does the location data exist? |
sgmcb | 45:0d8cb417a9e2 | 203 | if( mdm.cellLocGet(&thisLoc) ) { |
sgmcb | 45:0d8cb417a9e2 | 204 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 205 | printf("LOC uncertainty= %d, sensor= %d\r\n", thisLoc.uncertainty, thisLoc.sensorUsed ); |
sgmcb | 45:0d8cb417a9e2 | 206 | printf("LOC = latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude); |
sgmcb | 45:0d8cb417a9e2 | 207 | #endif |
sgmcb | 45:0d8cb417a9e2 | 208 | |
sgmcb | 45:0d8cb417a9e2 | 209 | // 2. Is this new position data, or extrapolated from an old reading? |
sgmcb | 45:0d8cb417a9e2 | 210 | if(thisLoc.sensorUsed == 2) { |
sgmcb | 45:0d8cb417a9e2 | 211 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 212 | printf("LOC has new information (non-extrapolation)\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 213 | #endif |
sgmcb | 45:0d8cb417a9e2 | 214 | |
sgmcb | 45:0d8cb417a9e2 | 215 | // 3. Is this data acceptably accurate (e.g. low enough reported uncertainty)? |
sgmcb | 45:0d8cb417a9e2 | 216 | if( (thisLoc.uncertainty < locAccUpper) && (thisLoc.uncertainty > 0) ) { |
sgmcb | 45:0d8cb417a9e2 | 217 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 218 | printf("LOC has suitable uncertainty (<%i).\r\n", locAccUpper); |
sgmcb | 45:0d8cb417a9e2 | 219 | #endif |
sgmcb | 45:0d8cb417a9e2 | 220 | |
sgmcb | 45:0d8cb417a9e2 | 221 | |
sgmcb | 45:0d8cb417a9e2 | 222 | // REPORT TO M2X |
sgmcb | 45:0d8cb417a9e2 | 223 | |
sgmcb | 45:0d8cb417a9e2 | 224 | // Report position |
sgmcb | 45:0d8cb417a9e2 | 225 | printf("\r\nTRANSMIT LOCATION DATA:\r\nla=%0.5f, lo=%0.5f, alt=%d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude); |
sgmcb | 45:0d8cb417a9e2 | 226 | M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) thisLoc.latitude, (double) thisLoc.longitude, (double) thisLoc.altitude); |
sgmcb | 45:0d8cb417a9e2 | 227 | if(M2X_response == 202) |
sgmcb | 45:0d8cb417a9e2 | 228 | printf("Location POST successful\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 229 | |
sgmcb | 45:0d8cb417a9e2 | 230 | // Report uncertainty |
sgmcb | 45:0d8cb417a9e2 | 231 | M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", thisLoc.uncertainty); |
sgmcb | 45:0d8cb417a9e2 | 232 | if(M2X_response == 202) |
sgmcb | 45:0d8cb417a9e2 | 233 | printf("Location Confidence (%d) POST successful\r\n", thisLoc.uncertainty); |
sgmcb | 45:0d8cb417a9e2 | 234 | } |
sgmcb | 45:0d8cb417a9e2 | 235 | // Fails 3: |
sgmcb | 45:0d8cb417a9e2 | 236 | else { |
sgmcb | 45:0d8cb417a9e2 | 237 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 238 | printf("LOC has high uncertainty (<%i); will not report.\r\n", thisLoc.uncertainty); |
sgmcb | 45:0d8cb417a9e2 | 239 | #endif |
sgmcb | 45:0d8cb417a9e2 | 240 | } |
sgmcb | 45:0d8cb417a9e2 | 241 | } |
sgmcb | 45:0d8cb417a9e2 | 242 | // Fails 2: |
sgmcb | 45:0d8cb417a9e2 | 243 | else { |
sgmcb | 45:0d8cb417a9e2 | 244 | #ifdef LOCDEBUG |
sgmcb | 45:0d8cb417a9e2 | 245 | printf("LOC has is extrapolated information; disregard.\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 246 | #endif |
sgmcb | 45:0d8cb417a9e2 | 247 | } |
sgmcb | 45:0d8cb417a9e2 | 248 | } |
sgmcb | 45:0d8cb417a9e2 | 249 | // Fails 1: |
sgmcb | 45:0d8cb417a9e2 | 250 | else { |
sgmcb | 45:0d8cb417a9e2 | 251 | printf("CellLocRequest timeout; no location data available.\r\n"); |
sgmcb | 45:0d8cb417a9e2 | 252 | } |
sgmcb | 40:82b70b92169e | 253 | |
sgmcb | 45:0d8cb417a9e2 | 254 | delay(30*1000); |
sgmcb | 45:0d8cb417a9e2 | 255 | continue; |
sgmcb | 45:0d8cb417a9e2 | 256 | // Skip this old crap |
sgmcb | 45:0d8cb417a9e2 | 257 | |
sgmcb | 45:0d8cb417a9e2 | 258 | // OLD AF |
sgmcb | 40:82b70b92169e | 259 | // Have location data |
sgmcb | 45:0d8cb417a9e2 | 260 | if ( mdm.cellLocGet(&thisLoc) ){ |
sgmcb | 45:0d8cb417a9e2 | 261 | printf("cellLocDelay=%f\r\n",cellLocDelay.read()); |
sgmcb | 45:0d8cb417a9e2 | 262 | |
sgmcb | 40:82b70b92169e | 263 | |
sgmcb | 41:f603d76dc6fe | 264 | if(thisLoc.sensorUsed == 2) { // thisLoc.uncertainty < ponyLoc.uncertainty) { //Basing test for validity on uncertainty and having a new position lock |
sgmcb | 41:f603d76dc6fe | 265 | printf(" NEW-latitude: %0.5f, longitude: %0.5f, altitute: %d, uncertainty: %d, sensor: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude, thisLoc.uncertainty, thisLoc.sensorUsed); |
sgmcb | 40:82b70b92169e | 266 | ponyLoc.latitude = thisLoc.latitude; |
sgmcb | 40:82b70b92169e | 267 | ponyLoc.longitude = thisLoc.longitude; |
sgmcb | 40:82b70b92169e | 268 | ponyLoc.altitude = thisLoc.altitude; |
sgmcb | 40:82b70b92169e | 269 | ponyLoc.uncertainty = thisLoc.uncertainty; |
sgmcb | 41:f603d76dc6fe | 270 | |
sgmcb | 41:f603d76dc6fe | 271 | |
sgmcb | 41:f603d76dc6fe | 272 | if(!kSysClockSet) { |
sgmcb | 41:f603d76dc6fe | 273 | printf("\033[31mSet system clock time.\033[39m\r\n"); |
sgmcb | 41:f603d76dc6fe | 274 | |
sgmcb | 41:f603d76dc6fe | 275 | // TODO: Get the local time zone |
sgmcb | 41:f603d76dc6fe | 276 | //mdm.setDebug(4); |
sgmcb | 41:f603d76dc6fe | 277 | //mdm.sendFormated("AT+CTZR=1"); |
sgmcb | 41:f603d76dc6fe | 278 | |
sgmcb | 41:f603d76dc6fe | 279 | // Adjust year because it's reporting the absolute year |
sgmcb | 41:f603d76dc6fe | 280 | // mbed expects the year to be "since 1900" (https://developer.mbed.org/handbook/Time?action=view&revision=11592) |
sgmcb | 41:f603d76dc6fe | 281 | (thisLoc.time).tm_year -= 1900; |
sgmcb | 41:f603d76dc6fe | 282 | |
sgmcb | 41:f603d76dc6fe | 283 | kSysSeconds = mktime( &(thisLoc.time) ); |
sgmcb | 41:f603d76dc6fe | 284 | set_time(kSysSeconds); |
sgmcb | 41:f603d76dc6fe | 285 | printf("System time set to: %s\r\n", ctime(&kSysSeconds) ); |
sgmcb | 41:f603d76dc6fe | 286 | |
sgmcb | 41:f603d76dc6fe | 287 | kSysClockSet=true; |
sgmcb | 41:f603d76dc6fe | 288 | } |
sgmcb | 41:f603d76dc6fe | 289 | |
sgmcb | 41:f603d76dc6fe | 290 | // Print the timestamp |
sgmcb | 41:f603d76dc6fe | 291 | |
sgmcb | 41:f603d76dc6fe | 292 | |
sgmcb | 41:f603d76dc6fe | 293 | time_t newTime = time(NULL); |
sgmcb | 41:f603d76dc6fe | 294 | printf("System time at: %s\r\n", ctime(&newTime) ); |
sgmcb | 40:82b70b92169e | 295 | } |
sgmcb | 40:82b70b92169e | 296 | |
sgmcb | 40:82b70b92169e | 297 | |
sgmcb | 40:82b70b92169e | 298 | // Check location data |
sgmcb | 40:82b70b92169e | 299 | if( ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccLower ) || ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccUpper && loopIter > readLoopMax ) ) { |
sgmcb | 40:82b70b92169e | 300 | |
sgmcb | 40:82b70b92169e | 301 | printf("\r\nTRANSMIT LOCATION DATA:\r\nla=%0.5f, lo=%0.5f, alt=%d\r\n", ponyLoc.latitude, ponyLoc.longitude, ponyLoc.altitude); |
sgmcb | 40:82b70b92169e | 302 | M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) ponyLoc.latitude, (double) ponyLoc.longitude, (double) ponyLoc.altitude); |
sgmcb | 40:82b70b92169e | 303 | |
sgmcb | 40:82b70b92169e | 304 | if(M2X_response == 202) |
sgmcb | 40:82b70b92169e | 305 | printf("Location POST successful\r\n"); |
sgmcb | 40:82b70b92169e | 306 | |
sgmcb | 40:82b70b92169e | 307 | |
sgmcb | 40:82b70b92169e | 308 | // Report uncertainty |
sgmcb | 40:82b70b92169e | 309 | M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", ponyLoc.uncertainty); |
sgmcb | 40:82b70b92169e | 310 | |
sgmcb | 40:82b70b92169e | 311 | if(M2X_response == 202) |
sgmcb | 40:82b70b92169e | 312 | printf("Location Confidence (%d) POST successful\r\n", ponyLoc.uncertainty); |
sgmcb | 44:bd259a4c83bb | 313 | |
sgmcb | 41:f603d76dc6fe | 314 | |
sgmcb | 41:f603d76dc6fe | 315 | |
sgmcb | 40:82b70b92169e | 316 | locLock = true; |
sgmcb | 40:82b70b92169e | 317 | } |
sgmcb | 40:82b70b92169e | 318 | } |
sgmcb | 40:82b70b92169e | 319 | |
sgmcb | 40:82b70b92169e | 320 | // No location data |
sgmcb | 40:82b70b92169e | 321 | else { |
sgmcb | 40:82b70b92169e | 322 | printf("cellLocGet failed.\r\n"); |
sgmcb | 40:82b70b92169e | 323 | // |
sgmcb | 40:82b70b92169e | 324 | } |
sgmcb | 40:82b70b92169e | 325 | |
sgmcb | 40:82b70b92169e | 326 | // Wait only if we've received a good location reading |
sgmcb | 40:82b70b92169e | 327 | if(locLock) { |
sgmcb | 40:82b70b92169e | 328 | printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay); |
sgmcb | 40:82b70b92169e | 329 | ponyLoc.uncertainty = locAccUpper + 1; |
sgmcb | 40:82b70b92169e | 330 | continue; |
sgmcb | 40:82b70b92169e | 331 | } |
sgmcb | 40:82b70b92169e | 332 | else if (loopIter > readLoopMax) { |
sgmcb | 40:82b70b92169e | 333 | sprintf(statusBuf, "CellLoc location not found after %i loops.\r\n", loopIter); |
sgmcb | 40:82b70b92169e | 334 | printf(statusBuf); |
sgmcb | 40:82b70b92169e | 335 | M2X_response = m2xClient.updateStreamValue(DEVID, "status", statusBuf); |
sgmcb | 40:82b70b92169e | 336 | if(M2X_response == 202) |
sgmcb | 40:82b70b92169e | 337 | printf("Status POST successful\r\n"); |
sgmcb | 40:82b70b92169e | 338 | |
sgmcb | 40:82b70b92169e | 339 | ponyLoc.uncertainty = locAccUpper + 1; |
sgmcb | 40:82b70b92169e | 340 | printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay); |
sgmcb | 40:82b70b92169e | 341 | continue; |
sgmcb | 40:82b70b92169e | 342 | } |
sgmcb | 41:f603d76dc6fe | 343 | |
sgmcb | 40:82b70b92169e | 344 | else { |
sgmcb | 41:f603d76dc6fe | 345 | printf("Bad data delay = submitPeriod...\r\n"); |
sgmcb | 41:f603d76dc6fe | 346 | delay(submitPeriod * 1000); // Delay between CellLoc requests when not getting good data |
sgmcb | 41:f603d76dc6fe | 347 | flipper.attach(&flip, 0.5); // the address of the function to be attached (flip) and the interval (2 seconds) |
sgmcb | 40:82b70b92169e | 348 | } |
sgmcb | 41:f603d76dc6fe | 349 | led1 = 0; |
sgmcb | 40:82b70b92169e | 350 | } |
sgmcb | 38:d86e744166b6 | 351 | |
sgmcb | 39:c14aff678b25 | 352 | |
sgmcb | 40:82b70b92169e | 353 | //mdm.disconnect(); |
sgmcb | 40:82b70b92169e | 354 | //mdm.powerOff(); |
msinig | 35:16f1037626e3 | 355 | |
lawliet | 0:4e3cb26f6019 | 356 | } |