Sample code for connecting u-blox c027 to AerCloud

Dependencies:   C027_Support_AerCloud HTTPClient_AerCloud mbed

Fork of HTTPClient_Cellular_HelloWorld by u-blox

Committer:
ycaer
Date:
Mon Nov 10 22:43:02 2014 +0000
Revision:
12:955439e7166f
Parent:
11:101705d43c92
Child:
14:fcdfdd37affe
Initial version of AerCloud sample for u-blox C027

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ycaer 12:955439e7166f 1 // Copyright 2014 Aeris Communications Inc
ycaer 12:955439e7166f 2 //
ycaer 12:955439e7166f 3 // Licensed under the Apache License, Version 2.0 (the "License");
ycaer 12:955439e7166f 4 // you may not use this file except in compliance with the License.
ycaer 12:955439e7166f 5 // You may obtain a copy of the License at
ycaer 12:955439e7166f 6 //
ycaer 12:955439e7166f 7 // http://www.apache.org/licenses/LICENSE-2.0
ycaer 12:955439e7166f 8 //
ycaer 12:955439e7166f 9 // Unless required by applicable law or agreed to in writing, software
ycaer 12:955439e7166f 10 // distributed under the License is distributed on an "AS IS" BASIS,
ycaer 12:955439e7166f 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ycaer 12:955439e7166f 12 // See the License for the specific language governing permissions and
ycaer 12:955439e7166f 13 // limitations under the License.
ycaer 12:955439e7166f 14
ycaer 12:955439e7166f 15 //
ycaer 12:955439e7166f 16 // AerCloud sample code for the u-blox C027 board
ycaer 12:955439e7166f 17 // Details on the C027: http://developer.mbed.org/platforms/u-blox-C027/
ycaer 12:955439e7166f 18 // Details on AerCloud: http://www.aeris.com/technology/aercloud/
ycaer 12:955439e7166f 19 // AerCloud Developer Forum: https://developer.aeris.com/
ycaer 12:955439e7166f 20 //
ycaer 12:955439e7166f 21 // Sample writes latitude, longitude, altitude, speed & GPS time to an AerCloud container
ycaer 12:955439e7166f 22 //
ycaer 12:955439e7166f 23 // Dependencies:
ycaer 12:955439e7166f 24 // mbed HTTP Client Libary
ycaer 12:955439e7166f 25 // C027_Support Library
ycaer 12:955439e7166f 26 // mbed Library
ycaer 12:955439e7166f 27 //
ycaer 12:955439e7166f 28 // To get the sample running, you'll need to fill in the following parameters below
ycaer 12:955439e7166f 29 // Your cellular provider's APN: APN
ycaer 12:955439e7166f 30 // AerCloud API Key: AC_APIKEY
ycaer 12:955439e7166f 31 // AerCloud Account ID: acId
ycaer 12:955439e7166f 32 // AerCloud Container: AC_CONTAINER
ycaer 12:955439e7166f 33 //
ycaer 12:955439e7166f 34 // and you'll also need to create an AerCloud contianer with the schema
ycaer 12:955439e7166f 35 // described below
ycaer 12:955439e7166f 36 //
ycaer 12:955439e7166f 37
donatien 0:0e0debc29569 38 #include "mbed.h"
donatien 0:0e0debc29569 39 #include "HTTPClient.h"
ycaer 12:955439e7166f 40 #include "GPS.h"
ycaer 12:955439e7166f 41
ycaer 12:955439e7166f 42 #include "MDM.h"
mazgch 6:6ff6061a0f76 43
mazgch 6:6ff6061a0f76 44 //------------------------------------------------------------------------------------
ycaer 12:955439e7166f 45 // Connectivity Parameters
mazgch 6:6ff6061a0f76 46 //------------------------------------------------------------------------------------
ycaer 12:955439e7166f 47
ycaer 12:955439e7166f 48 //! Aeris SIM does not use PIN
mazgch 3:412a526d7054 49 #define SIMPIN NULL
ycaer 12:955439e7166f 50
ycaer 12:955439e7166f 51 //! The APN of your Aeris SIM
ycaer 12:955439e7166f 52 // You can find the APN for your SIM at https://aerport.aeris.com
mazgch 7:b14d0f112a73 53 #define APN NULL
ycaer 12:955439e7166f 54
ycaer 12:955439e7166f 55 //! User name and password are not required to use Aeris APN
mazgch 3:412a526d7054 56 #define USERNAME NULL
ycaer 12:955439e7166f 57
ycaer 12:955439e7166f 58 //! User name and password are not required to use Aeris APN
mazgch 3:412a526d7054 59 #define PASSWORD NULL
ycaer 12:955439e7166f 60
ycaer 12:955439e7166f 61
ycaer 12:955439e7166f 62 // ------------------------------------------------------------------------------------
ycaer 12:955439e7166f 63 // AerCloud Paramers
ycaer 12:955439e7166f 64 // ------------------------------------------------------------------------------------
ycaer 12:955439e7166f 65
ycaer 12:955439e7166f 66 // AerCloud BASE URL
ycaer 12:955439e7166f 67 #define AC_BASE "http://api.aercloud.aeris.com/v1"
ycaer 12:955439e7166f 68
ycaer 12:955439e7166f 69 //! AerCloud API Key
ycaer 12:955439e7166f 70 // You can find your api key at https://aerport.aeris.com
ycaer 12:955439e7166f 71 #define AC_APIKEY NULL
ycaer 12:955439e7166f 72
ycaer 12:955439e7166f 73 //! Aercloud Account Id
ycaer 12:955439e7166f 74 // You can find your account id at https://aerport.aeris.com
ycaer 12:955439e7166f 75 int acId = 1;
ycaer 12:955439e7166f 76
ycaer 12:955439e7166f 77 // AerCloud Container
ycaer 12:955439e7166f 78 // The name of the AerCloud Container to write data into
ycaer 12:955439e7166f 79 #define AC_CONTAINER "VStatus" // Example Container
ycaer 12:955439e7166f 80
ycaer 12:955439e7166f 81 //------------------------------------------------------------------------------------
ycaer 12:955439e7166f 82 // AerCloud SCL (Device) Data Model
ycaer 12:955439e7166f 83 // ------------------------------------------------------------------------------------
ycaer 12:955439e7166f 84 //
ycaer 12:955439e7166f 85 // Code assumes an AerCloud Data Model with the following fields
ycaer 12:955439e7166f 86 //
ycaer 12:955439e7166f 87 // Altitude DOUBLE GPS altitude
ycaer 12:955439e7166f 88 // Speed DOUBLE GPS speed
ycaer 12:955439e7166f 89 // VSLat STRING GPS latitude
ycaer 12:955439e7166f 90 // VSLong STRING GPS longitude
ycaer 12:955439e7166f 91 // GPSTime DOUBLE Raw GPS time
ycaer 12:955439e7166f 92 // GPSDate DOUBLE Raw GPS Date
ycaer 12:955439e7166f 93 // SCLID INT AerCloud Device Identifier (i.e. IMEI)
ycaer 12:955439e7166f 94 //
ycaer 12:955439e7166f 95
mazgch 6:6ff6061a0f76 96 //------------------------------------------------------------------------------------
mazgch 3:412a526d7054 97
donatien 1:d263603373ac 98 char str[512];
ycaer 12:955439e7166f 99 char simImei[16];
ycaer 12:955439e7166f 100
donatien 1:d263603373ac 101
donatien 0:0e0debc29569 102 int main()
donatien 0:0e0debc29569 103 {
ycaer 12:955439e7166f 104 bool simProvisioned = false;
ycaer 12:955439e7166f 105 int ret;
ycaer 12:955439e7166f 106 char buf[512] = "";
ycaer 12:955439e7166f 107 int itn = 580;
ycaer 12:955439e7166f 108 const int wait = 100;
ycaer 12:955439e7166f 109 double lastLat = 0, lastLong = 0, lastAlt = 0, lastSpeed = 0, lastTime = 0, lastDate = 0;
ycaer 12:955439e7166f 110 bool abort = false;
ycaer 12:955439e7166f 111
ycaer 12:955439e7166f 112 GPSI2C gps;
ycaer 12:955439e7166f 113
ycaer 12:955439e7166f 114 printf("GPS object created\n");
mazgch 5:a18ddbfd70c9 115 // turn on the supplies of the Modem
mazgch 3:412a526d7054 116 MDMSerial mdm;
ycaer 12:955439e7166f 117 printf("Modem object created\n");
sam_grove 11:101705d43c92 118 //mdm.setDebug(4); // enable this for debugging issues
ycaer 12:955439e7166f 119 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) {
ycaer 12:955439e7166f 120 printf("Unabled to connect to the network.\n");
mazgch 4:7fd97087e573 121 return -1;
ycaer 12:955439e7166f 122 }
mazgch 4:7fd97087e573 123 HTTPClient http;
mazgch 8:eea979594a37 124
ycaer 12:955439e7166f 125 //get SIM IMEI
ycaer 12:955439e7166f 126 mdm.getIMEI(simImei);
ycaer 12:955439e7166f 127 printf("Requesting provision info for IMEI %s\n",simImei);
ycaer 12:955439e7166f 128
ycaer 12:955439e7166f 129 // Check if SIM is provisioned in AerCloud
ycaer 12:955439e7166f 130 printf("\nIs the SIM provisioned?\n");
ycaer 12:955439e7166f 131 char url[512];
ycaer 12:955439e7166f 132 snprintf(url, sizeof(url), "%s/%d/scls/%s?apiKey=%s", AC_BASE, acId, simImei, AC_APIKEY);
ycaer 12:955439e7166f 133 ret = http.get(url, str, 128);
mazgch 4:7fd97087e573 134 if (!ret)
donatien 0:0e0debc29569 135 {
ycaer 12:955439e7166f 136 // We're already provisioned
mazgch 4:7fd97087e573 137 printf("Page fetched successfully - read %d characters\n", strlen(str));
mazgch 4:7fd97087e573 138 printf("Result: %s\n", str);
ycaer 12:955439e7166f 139 simProvisioned = true;
mazgch 4:7fd97087e573 140 }
mazgch 4:7fd97087e573 141 else
mazgch 4:7fd97087e573 142 {
ycaer 12:955439e7166f 143 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
ycaer 12:955439e7166f 144 //SIM is not provisioned. trying to provision it...
ycaer 12:955439e7166f 145 char url[512];
ycaer 12:955439e7166f 146 snprintf(url, sizeof(url), "%s/%d/scls?apiKey=%s", AC_BASE, acId, AC_APIKEY);
ycaer 12:955439e7166f 147
ycaer 12:955439e7166f 148 snprintf(str, sizeof(str), "{\"sclId\":\"%s\"}\0",simImei);
ycaer 12:955439e7166f 149 HTTPText outText(str);
ycaer 12:955439e7166f 150 HTTPText inText(str, 512);
ycaer 12:955439e7166f 151 ret = http.post(url, outText, &inText);
ycaer 12:955439e7166f 152 if (!ret)
ycaer 12:955439e7166f 153 {
ycaer 12:955439e7166f 154 printf("Executed POST successfully - read %d characters\n", strlen(str));
ycaer 12:955439e7166f 155 printf("Result: %s\n", str);
ycaer 12:955439e7166f 156 simProvisioned = true;
ycaer 12:955439e7166f 157 }
ycaer 12:955439e7166f 158 else
ycaer 12:955439e7166f 159 {
ycaer 12:955439e7166f 160 if(http.getHTTPResponseCode() == 200)
ycaer 12:955439e7166f 161 simProvisioned = true;
ycaer 12:955439e7166f 162 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
ycaer 12:955439e7166f 163 }
mazgch 4:7fd97087e573 164 }
donatien 2:270e2d0bb85a 165
ycaer 12:955439e7166f 166 //POST data to containers if SIM has been successfully provisioned
ycaer 12:955439e7166f 167 if(simProvisioned)
mazgch 4:7fd97087e573 168 {
ycaer 12:955439e7166f 169 //Read GPS
ycaer 12:955439e7166f 170 while (!abort)
ycaer 12:955439e7166f 171 {
ycaer 12:955439e7166f 172 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0)
ycaer 12:955439e7166f 173 {
ycaer 12:955439e7166f 174 int len = LENGTH(ret);
ycaer 12:955439e7166f 175 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6))
ycaer 12:955439e7166f 176 {
ycaer 12:955439e7166f 177 if (!strncmp("$GPGLL", buf, 6))
ycaer 12:955439e7166f 178 {
ycaer 12:955439e7166f 179 //Get Lat/Long
ycaer 12:955439e7166f 180 double la = 0, lo = 0;
ycaer 12:955439e7166f 181 char ch;
ycaer 12:955439e7166f 182 if (gps.getNmeaAngle(1,buf,len,la) &&
ycaer 12:955439e7166f 183 gps.getNmeaAngle(3,buf,len,lo) &&
ycaer 12:955439e7166f 184 gps.getNmeaItem(6,buf,len,ch) && ch == 'A')
ycaer 12:955439e7166f 185 {
ycaer 12:955439e7166f 186 lastLat = la;
ycaer 12:955439e7166f 187 lastLong = lo;
ycaer 12:955439e7166f 188 }
ycaer 12:955439e7166f 189 }
ycaer 12:955439e7166f 190 else if (!strncmp("$GPGGA", buf, 6))
ycaer 12:955439e7166f 191 {
ycaer 12:955439e7166f 192 //Get Altitude
ycaer 12:955439e7166f 193 double a = 0;
ycaer 12:955439e7166f 194 if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m]
ycaer 12:955439e7166f 195 {
ycaer 12:955439e7166f 196 lastAlt = a;
ycaer 12:955439e7166f 197 }
ycaer 12:955439e7166f 198 }
ycaer 12:955439e7166f 199 else if (!strncmp("$GPVTG", buf, 6))
ycaer 12:955439e7166f 200 {
ycaer 12:955439e7166f 201 //Get Speed
ycaer 12:955439e7166f 202 double s = 0;
ycaer 12:955439e7166f 203 if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h]
ycaer 12:955439e7166f 204 {
ycaer 12:955439e7166f 205 lastSpeed = s;
ycaer 12:955439e7166f 206 }
ycaer 12:955439e7166f 207 }
ycaer 12:955439e7166f 208 else if (!strncmp("$GPRMC", buf, 6))
ycaer 12:955439e7166f 209 {
ycaer 12:955439e7166f 210 //Get Timestamp
ycaer 12:955439e7166f 211 double fixTime = 0;
ycaer 12:955439e7166f 212 double fixDate = 0;
ycaer 12:955439e7166f 213
ycaer 12:955439e7166f 214 if (gps.getNmeaItem(1,buf,len,fixTime)) // speed [km/h]
ycaer 12:955439e7166f 215 {
ycaer 12:955439e7166f 216 lastTime = fixTime;
ycaer 12:955439e7166f 217 }
ycaer 12:955439e7166f 218 if (gps.getNmeaItem(9,buf,len,fixDate)) // speed [km/h]
ycaer 12:955439e7166f 219 {
ycaer 12:955439e7166f 220 lastDate = fixDate;
ycaer 12:955439e7166f 221 }
ycaer 12:955439e7166f 222 // printf("time: %f\n Date:%f\n", lastTime, lastDate);
ycaer 12:955439e7166f 223 }
ycaer 12:955439e7166f 224 }
ycaer 12:955439e7166f 225 }
ycaer 12:955439e7166f 226 wait_ms(wait);
ycaer 12:955439e7166f 227 itn++;
ycaer 12:955439e7166f 228 //post every 60 seconds
ycaer 12:955439e7166f 229 if(itn == 590)
ycaer 12:955439e7166f 230 {
ycaer 12:955439e7166f 231 // POST data to AerCLoud
ycaer 12:955439e7166f 232 char url[512];
ycaer 12:955439e7166f 233 snprintf(url, sizeof(url), "%s/%d/scls/%s/containers/%s/contentInstances?apiKey=%s", AC_BASE, acId, simImei, AC_CONTAINER, AC_APIKEY);
ycaer 12:955439e7166f 234
ycaer 12:955439e7166f 235 sprintf(str,"{\"VSLat\": %.5f, \"VSLong\": %.5f, \"Altitude\": %.5f, \"Speed\":%.5f, \"GPSTime\":%.5f, \"GPSDate\":%.5f, \"SCLID\":\"%s\"}", lastLat, lastLong, lastAlt, lastSpeed, lastTime, lastDate, simImei);
ycaer 12:955439e7166f 236 HTTPText outText(str);
ycaer 12:955439e7166f 237 HTTPText inText(str, 512);
ycaer 12:955439e7166f 238 printf("\nPost acquired data...\n");
ycaer 12:955439e7166f 239 ret = http.post(url, outText, &inText);
ycaer 12:955439e7166f 240 if (!ret)
ycaer 12:955439e7166f 241 {
ycaer 12:955439e7166f 242 printf("Executed POST successfully - read %d characters\n", strlen(str));
ycaer 12:955439e7166f 243 printf("Result: %s\n", str);
ycaer 12:955439e7166f 244 }
ycaer 12:955439e7166f 245 else
ycaer 12:955439e7166f 246 {
ycaer 12:955439e7166f 247 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
ycaer 12:955439e7166f 248 }
ycaer 12:955439e7166f 249 itn = 0;
ycaer 12:955439e7166f 250 }
ycaer 12:955439e7166f 251 }
ycaer 12:955439e7166f 252 gps.powerOff();
ycaer 12:955439e7166f 253 } else {
ycaer 12:955439e7166f 254 printf("SIM is not provisioned.\n");
donatien 2:270e2d0bb85a 255 }
mazgch 4:7fd97087e573 256
mazgch 4:7fd97087e573 257
mazgch 4:7fd97087e573 258 mdm.disconnect();
mazgch 3:412a526d7054 259 mdm.powerOff();
mazgch 6:6ff6061a0f76 260
ycaer 12:955439e7166f 261 return 0;
donatien 0:0e0debc29569 262 }
ycaer 12:955439e7166f 263