whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

Committer:
sgmcb
Date:
Tue Dec 29 07:01:54 2015 +0000
Revision:
43:80aa0c933e1a
Parent:
42:8e6e647cb7d5
Child:
44:bd259a4c83bb
Location-only in main loop

Who changed what in which revision?

UserRevisionLine numberNew 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 37:71ab32b61dfb 21
sgmcb 38:d86e744166b6 22 //------------------------------------------------------------------------------------
sgmcb 38:d86e744166b6 23 // Cellular modem/SIM parameters
sgmcb 38:d86e744166b6 24 #define SIMPIN "1111" //!SIMPIN is 1111 by default for AT&T SIMs.
sgmcb 38:d86e744166b6 25 #define APN "m2m.com.attz" // Defined AT&T M2M APN
sgmcb 38:d86e744166b6 26 #define USERNAME NULL //! Set the user name for your APN, or NULL if not needed (which, apparently, it isn't)
sgmcb 38:d86e744166b6 27 #define PASSWORD NULL //! Set the password for your APN, or NULL if not needed (which, apparently, it isn't)
sgmcb 38:d86e744166b6 28
sgmcb 38:d86e744166b6 29 //------------------------------------------------------------------------------------
sgmcb 38:d86e744166b6 30 // AT&T M2X Kekys
sgmcb 38:d86e744166b6 31
sgmcb 41:f603d76dc6fe 32
sgmcb 41:f603d76dc6fe 33 #ifdef THROWAWAY
sgmcb 41:f603d76dc6fe 34 // Codes for a throwaway M2X device
sgmcb 39:c14aff678b25 35 #define M2XAPIKEY "54c6bbf11012f34830bd23cc091ca250"
sgmcb 39:c14aff678b25 36 #define DEVID "b9c57667ee0495dc0b3cddd890f8d2df"
sgmcb 41:f603d76dc6fe 37 #endif
sgmcb 41:f603d76dc6fe 38
sgmcb 41:f603d76dc6fe 39 #ifndef THROWAWAY
sgmcb 41:f603d76dc6fe 40 // v1.2 codes
sgmcb 41:f603d76dc6fe 41 #define M2XAPIKEY "bbc483492238dc76f7d12f0cd6e13a4b"
sgmcb 41:f603d76dc6fe 42 #define DEVID "3764db38b6c9ec4045a38e0125b14b4c"
sgmcb 41:f603d76dc6fe 43 #endif
sgmcb 41:f603d76dc6fe 44
sgmcb 38:d86e744166b6 45
sgmcb 38:d86e744166b6 46 //------------------------------------------------------------------------------------
sgmcb 39:c14aff678b25 47 // PIN Config
sgmcb 39:c14aff678b25 48 DigitalIn jostle(P2_13);
sgmcb 39:c14aff678b25 49
sgmcb 41:f603d76dc6fe 50 DigitalOut led1(LED1);
sgmcb 41:f603d76dc6fe 51 DigitalOut led2(LED2);
sgmcb 39:c14aff678b25 52
sgmcb 41:f603d76dc6fe 53 AnalogIn tempPin(P0_23);
sgmcb 41:f603d76dc6fe 54 AnalogIn tempGnd(P0_24);
sgmcb 38:d86e744166b6 55
sgmcb 41:f603d76dc6fe 56 //I2C axis(P0_0, P0_1); // SDA, SCL
sgmcb 41:f603d76dc6fe 57 LIS331 axle(P0_0, P0_1); // Library object
sgmcb 38:d86e744166b6 58
sgmcb 38:d86e744166b6 59
sgmcb 42:8e6e647cb7d5 60 //------------------------------------------------------------------------------------
sgmcb 42:8e6e647cb7d5 61 // GLOBAL VARIABLES
sgmcb 38:d86e744166b6 62
sgmcb 41:f603d76dc6fe 63 Ticker flipper;
sgmcb 38:d86e744166b6 64
sgmcb 42:8e6e647cb7d5 65 // M2X Drivers
sgmcb 38:d86e744166b6 66 Client client;
sgmcb 38:d86e744166b6 67 M2XStreamClient m2xClient(&client, M2XAPIKEY);
sgmcb 40:82b70b92169e 68 int M2X_response; // For m2x message responses
sgmcb 38:d86e744166b6 69
sgmcb 42:8e6e647cb7d5 70
sgmcb 40:82b70b92169e 71 char statusBuf[145] = "";
sgmcb 40:82b70b92169e 72
sgmcb 42:8e6e647cb7d5 73 // Location reading
sgmcb 40:82b70b92169e 74 unsigned int kReadingDelay = 3 * 60 * 1000; // How many seconds to wait between reads
sgmcb 39:c14aff678b25 75 unsigned int kReadingDelayClimb = 15000; // How many seconds to add to the wait period when sitting idle
sgmcb 39:c14aff678b25 76 unsigned int kReadingDelayMax = 5 * 60000; // What's the maximum time between readings?
sgmcb 38:d86e744166b6 77 double kLaLoDiffMin = 0.01;
sgmcb 38:d86e744166b6 78 double kAltDiffMin = 2.00;
sgmcb 38:d86e744166b6 79
sgmcb 42:8e6e647cb7d5 80 // Temperature reads
sgmcb 41:f603d76dc6fe 81 float kTemp;
sgmcb 41:f603d76dc6fe 82
sgmcb 42:8e6e647cb7d5 83 // Loop drivers
sgmcb 39:c14aff678b25 84 unsigned int kIdleSpins = 0;
sgmcb 39:c14aff678b25 85
sgmcb 42:8e6e647cb7d5 86 // System time
sgmcb 41:f603d76dc6fe 87 time_t kSysSeconds = time(NULL);
sgmcb 41:f603d76dc6fe 88 bool kSysClockSet = false;
sgmcb 41:f603d76dc6fe 89 tm kFormatTime;
sgmcb 39:c14aff678b25 90
sgmcb 39:c14aff678b25 91
sgmcb 42:8e6e647cb7d5 92 // Global function definitions
sgmcb 38:d86e744166b6 93
sgmcb 41:f603d76dc6fe 94 void flip() {
sgmcb 41:f603d76dc6fe 95 led1 = !led1;
sgmcb 41:f603d76dc6fe 96 }
sgmcb 41:f603d76dc6fe 97
sgmcb 38:d86e744166b6 98
sgmcb 38:d86e744166b6 99 //------------------------------------------------------------------------------------
sgmcb 38:d86e744166b6 100 //------------------------------------------------------------------------------------
sgmcb 38:d86e744166b6 101 //------------------------------------------------------------------------------------
sgmcb 38:d86e744166b6 102 int main(void)
sgmcb 38:d86e744166b6 103 {
sgmcb 38:d86e744166b6 104
sgmcb 41:f603d76dc6fe 105 printf("\r\n\n\n\n-----\r\nI'm alive - vers. uA\r\n\n");
sgmcb 41:f603d76dc6fe 106 #ifdef THROWAWAY
sgmcb 41:f603d76dc6fe 107 printf("Using THROWAWAY M2X device\r\n");
sgmcb 41:f603d76dc6fe 108 #endif
sgmcb 41:f603d76dc6fe 109
sgmcb 38:d86e744166b6 110 MDMSerial mdm;
sgmcb 39:c14aff678b25 111 MDMParser::DevStatus devStatus = {};
sgmcb 39:c14aff678b25 112 MDMParser::NetStatus netStatus = {};
sgmcb 39:c14aff678b25 113 bool mdmOk = mdm.init(SIMPIN, &devStatus);
sgmcb 41:f603d76dc6fe 114 #ifdef MDMDEBUG
sgmcb 39:c14aff678b25 115 //mdm.setDebug(4);
sgmcb 41:f603d76dc6fe 116 #endif
sgmcb 41:f603d76dc6fe 117
sgmcb 43:80aa0c933e1a 118 // ----------------
sgmcb 42:8e6e647cb7d5 119 // LOCATION READING
sgmcb 39:c14aff678b25 120
sgmcb 38:d86e744166b6 121
sgmcb 38:d86e744166b6 122 // Open modem connection
sgmcb 38:d86e744166b6 123 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
sgmcb 38:d86e744166b6 124 return -1;
sgmcb 38:d86e744166b6 125
sgmcb 38:d86e744166b6 126
sgmcb 40:82b70b92169e 127
sgmcb 40:82b70b92169e 128 // Cell location call variables
sgmcb 40:82b70b92169e 129 const int sensorMask = 2; // 1 = GNSS, 2 = CellLocate, 3 = Hybrid: GNSS + CellLocate
sgmcb 41:f603d76dc6fe 130 //const int timeout = 55; // in seconds
sgmcb 40:82b70b92169e 131 const int targetAccuracy = 1; // meters
sgmcb 40:82b70b92169e 132
sgmcb 40:82b70b92169e 133 // Cell location data
sgmcb 41:f603d76dc6fe 134 MDMSerial::CellLocData ponyLoc, thisLoc;
sgmcb 40:82b70b92169e 135
sgmcb 40:82b70b92169e 136 printf("Configure deep scan mode\r\n");
sgmcb 40:82b70b92169e 137 int locConf = mdm.cellLocConfigSensor(1); // Set deep scan mode
sgmcb 40:82b70b92169e 138
sgmcb 41:f603d76dc6fe 139
sgmcb 40:82b70b92169e 140 // Loop driving variables
sgmcb 40:82b70b92169e 141 bool locLock;
sgmcb 41:f603d76dc6fe 142 int submitPeriod = 30;
sgmcb 41:f603d76dc6fe 143 int timeoutMargin = 5;
sgmcb 41:f603d76dc6fe 144 int readLoopMax = 100;
sgmcb 40:82b70b92169e 145 int loopIter = 0;
sgmcb 40:82b70b92169e 146 int locAccLower = 50; // Immediately accept locations with uncertainty lower than this
sgmcb 40:82b70b92169e 147 int locAccUpper = 5000; // Don't accept locations with uncertainty greater than this
sgmcb 41:f603d76dc6fe 148
sgmcb 41:f603d76dc6fe 149
sgmcb 42:8e6e647cb7d5 150 // -----------------------
sgmcb 40:82b70b92169e 151 // Location reporting loop
sgmcb 40:82b70b92169e 152 while(true) {
sgmcb 40:82b70b92169e 153
sgmcb 41:f603d76dc6fe 154 M2X_response = 0;
sgmcb 40:82b70b92169e 155 locLock = false;
sgmcb 40:82b70b92169e 156 loopIter++;
sgmcb 40:82b70b92169e 157
sgmcb 41:f603d76dc6fe 158
sgmcb 41:f603d76dc6fe 159 //M2X_response = logTemp(&tempPin, &m2xClient);
sgmcb 41:f603d76dc6fe 160 printf("tick\r\n");
sgmcb 41:f603d76dc6fe 161
sgmcb 41:f603d76dc6fe 162
sgmcb 41:f603d76dc6fe 163 /*
sgmcb 41:f603d76dc6fe 164 while(!M2X_response) {
sgmcb 41:f603d76dc6fe 165 printf("Waiting on temperature log\r\n");
sgmcb 41:f603d76dc6fe 166 delay(1000);
sgmcb 41:f603d76dc6fe 167 }
sgmcb 41:f603d76dc6fe 168 */
sgmcb 41:f603d76dc6fe 169
sgmcb 41:f603d76dc6fe 170 delay(5000); // Delay to ensure that the modem isn't talking over itself transmitting temp and requesting location
sgmcb 41:f603d76dc6fe 171
sgmcb 40:82b70b92169e 172 //printf("Cell location Request; deep scan=%i, mask=%i, accuracy=%i\r\n", locConf, sensorMask, targetAccuracy);
sgmcb 41:f603d76dc6fe 173 //mdm.setDebug(4);
sgmcb 41:f603d76dc6fe 174 mdm.cellLocRequest(sensorMask, submitPeriod - timeoutMargin, targetAccuracy);
sgmcb 41:f603d76dc6fe 175
sgmcb 40:82b70b92169e 176
sgmcb 41:f603d76dc6fe 177
sgmcb 40:82b70b92169e 178 // Have location data
sgmcb 40:82b70b92169e 179 if ( mdm.cellLocGet(&thisLoc) ){
sgmcb 41:f603d76dc6fe 180 //printf("CellLocate position rec'd with uncertainty= %d, sensor= %d\r\n", thisLoc.uncertainty, thisLoc.sensorUsed );
sgmcb 40:82b70b92169e 181
sgmcb 40:82b70b92169e 182
sgmcb 41:f603d76dc6fe 183 if(thisLoc.sensorUsed == 2) { // thisLoc.uncertainty < ponyLoc.uncertainty) { //Basing test for validity on uncertainty and having a new position lock
sgmcb 41:f603d76dc6fe 184 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 185 ponyLoc.latitude = thisLoc.latitude;
sgmcb 40:82b70b92169e 186 ponyLoc.longitude = thisLoc.longitude;
sgmcb 40:82b70b92169e 187 ponyLoc.altitude = thisLoc.altitude;
sgmcb 40:82b70b92169e 188 ponyLoc.uncertainty = thisLoc.uncertainty;
sgmcb 41:f603d76dc6fe 189
sgmcb 41:f603d76dc6fe 190
sgmcb 41:f603d76dc6fe 191 if(!kSysClockSet) {
sgmcb 41:f603d76dc6fe 192 printf("\033[31mSet system clock time.\033[39m\r\n");
sgmcb 41:f603d76dc6fe 193
sgmcb 41:f603d76dc6fe 194 // TODO: Get the local time zone
sgmcb 41:f603d76dc6fe 195 //mdm.setDebug(4);
sgmcb 41:f603d76dc6fe 196 //mdm.sendFormated("AT+CTZR=1");
sgmcb 41:f603d76dc6fe 197
sgmcb 41:f603d76dc6fe 198 // Adjust year because it's reporting the absolute year
sgmcb 41:f603d76dc6fe 199 // mbed expects the year to be "since 1900" (https://developer.mbed.org/handbook/Time?action=view&revision=11592)
sgmcb 41:f603d76dc6fe 200 (thisLoc.time).tm_year -= 1900;
sgmcb 41:f603d76dc6fe 201
sgmcb 41:f603d76dc6fe 202 kSysSeconds = mktime( &(thisLoc.time) );
sgmcb 41:f603d76dc6fe 203 set_time(kSysSeconds);
sgmcb 41:f603d76dc6fe 204 printf("System time set to: %s\r\n", ctime(&kSysSeconds) );
sgmcb 41:f603d76dc6fe 205
sgmcb 41:f603d76dc6fe 206 kSysClockSet=true;
sgmcb 41:f603d76dc6fe 207 }
sgmcb 41:f603d76dc6fe 208
sgmcb 41:f603d76dc6fe 209 // Print the timestamp
sgmcb 41:f603d76dc6fe 210
sgmcb 41:f603d76dc6fe 211
sgmcb 41:f603d76dc6fe 212 time_t newTime = time(NULL);
sgmcb 41:f603d76dc6fe 213 printf("System time at: %s\r\n", ctime(&newTime) );
sgmcb 40:82b70b92169e 214 }
sgmcb 40:82b70b92169e 215
sgmcb 40:82b70b92169e 216
sgmcb 40:82b70b92169e 217 // Check location data
sgmcb 40:82b70b92169e 218 if( ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccLower ) || ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccUpper && loopIter > readLoopMax ) ) {
sgmcb 40:82b70b92169e 219
sgmcb 40:82b70b92169e 220 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 221 M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) ponyLoc.latitude, (double) ponyLoc.longitude, (double) ponyLoc.altitude);
sgmcb 40:82b70b92169e 222
sgmcb 40:82b70b92169e 223 if(M2X_response == 202)
sgmcb 40:82b70b92169e 224 printf("Location POST successful\r\n");
sgmcb 40:82b70b92169e 225
sgmcb 40:82b70b92169e 226
sgmcb 40:82b70b92169e 227 // Report uncertainty
sgmcb 40:82b70b92169e 228 M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", ponyLoc.uncertainty);
sgmcb 40:82b70b92169e 229
sgmcb 40:82b70b92169e 230 if(M2X_response == 202)
sgmcb 40:82b70b92169e 231 printf("Location Confidence (%d) POST successful\r\n", ponyLoc.uncertainty);
sgmcb 40:82b70b92169e 232
sgmcb 41:f603d76dc6fe 233 // Report temperature
sgmcb 41:f603d76dc6fe 234 M2X_response = logTemp(&tempPin, &kTemp, &m2xClient);
sgmcb 41:f603d76dc6fe 235
sgmcb 41:f603d76dc6fe 236 if(M2X_response == 202)
sgmcb 41:f603d76dc6fe 237 printf("Temperature POST successful\r\n");
sgmcb 41:f603d76dc6fe 238
sgmcb 41:f603d76dc6fe 239
sgmcb 40:82b70b92169e 240 locLock = true;
sgmcb 40:82b70b92169e 241 }
sgmcb 40:82b70b92169e 242 }
sgmcb 40:82b70b92169e 243
sgmcb 40:82b70b92169e 244 // No location data
sgmcb 40:82b70b92169e 245 else {
sgmcb 40:82b70b92169e 246 printf("cellLocGet failed.\r\n");
sgmcb 40:82b70b92169e 247 //
sgmcb 40:82b70b92169e 248 }
sgmcb 40:82b70b92169e 249
sgmcb 40:82b70b92169e 250 // Wait only if we've received a good location reading
sgmcb 40:82b70b92169e 251 if(locLock) {
sgmcb 40:82b70b92169e 252 printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
sgmcb 40:82b70b92169e 253 ponyLoc.uncertainty = locAccUpper + 1;
sgmcb 40:82b70b92169e 254 continue;
sgmcb 40:82b70b92169e 255 }
sgmcb 40:82b70b92169e 256 else if (loopIter > readLoopMax) {
sgmcb 40:82b70b92169e 257 sprintf(statusBuf, "CellLoc location not found after %i loops.\r\n", loopIter);
sgmcb 40:82b70b92169e 258 printf(statusBuf);
sgmcb 40:82b70b92169e 259 M2X_response = m2xClient.updateStreamValue(DEVID, "status", statusBuf);
sgmcb 40:82b70b92169e 260 if(M2X_response == 202)
sgmcb 40:82b70b92169e 261 printf("Status POST successful\r\n");
sgmcb 40:82b70b92169e 262
sgmcb 40:82b70b92169e 263 ponyLoc.uncertainty = locAccUpper + 1;
sgmcb 40:82b70b92169e 264 printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
sgmcb 40:82b70b92169e 265 continue;
sgmcb 40:82b70b92169e 266 }
sgmcb 41:f603d76dc6fe 267
sgmcb 40:82b70b92169e 268 else {
sgmcb 41:f603d76dc6fe 269 printf("Bad data delay = submitPeriod...\r\n");
sgmcb 41:f603d76dc6fe 270 delay(submitPeriod * 1000); // Delay between CellLoc requests when not getting good data
sgmcb 41:f603d76dc6fe 271 flipper.attach(&flip, 0.5); // the address of the function to be attached (flip) and the interval (2 seconds)
sgmcb 40:82b70b92169e 272 }
sgmcb 41:f603d76dc6fe 273 led1 = 0;
sgmcb 40:82b70b92169e 274 }
sgmcb 38:d86e744166b6 275
sgmcb 39:c14aff678b25 276
sgmcb 40:82b70b92169e 277 //mdm.disconnect();
sgmcb 40:82b70b92169e 278 //mdm.powerOff();
msinig 35:16f1037626e3 279
lawliet 0:4e3cb26f6019 280 }