Sample code for connecting u-blox c027 to AerCloud
Dependencies: C027_Support_AerCloud HTTPClient_AerCloud mbed
Fork of HTTPClient_Cellular_HelloWorld by
Revision 14:fcdfdd37affe, committed 2014-11-14
- Comitter:
- mchowla
- Date:
- Fri Nov 14 01:29:08 2014 +0000
- Parent:
- 13:f30d8541fee0
- Commit message:
- Fix issue where SIM always looked unprovisioned; Add line feeds to console messages so they print cleanly on the console
Changed in this revision
diff -r f30d8541fee0 -r fcdfdd37affe C027_Support.lib --- a/C027_Support.lib Mon Nov 10 23:03:32 2014 +0000 +++ b/C027_Support.lib Fri Nov 14 01:29:08 2014 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/AerCloud/code/C027_Support_AerCloud/#aad678f042a6 +http://developer.mbed.org/teams/AerCloud/code/C027_Support_AerCloud/#095982b8f8b4
diff -r f30d8541fee0 -r fcdfdd37affe HTTPClient.lib --- a/HTTPClient.lib Mon Nov 10 23:03:32 2014 +0000 +++ b/HTTPClient.lib Fri Nov 14 01:29:08 2014 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/AerCloud/code/HTTPClient_AerCloud/#c96ef4c9695c +http://developer.mbed.org/teams/AerCloud/code/HTTPClient_AerCloud/#b4a15dc7a299
diff -r f30d8541fee0 -r fcdfdd37affe main.cpp --- a/main.cpp Mon Nov 10 23:03:32 2014 +0000 +++ b/main.cpp Fri Nov 14 01:29:08 2014 +0000 @@ -50,7 +50,7 @@ //! The APN of your Aeris SIM // You can find the APN for your SIM at https://aerport.aeris.com -#define APN NULL +#define APN "_INSERT_YOUR_APN_HERE_" //! User name and password are not required to use Aeris APN #define USERNAME NULL @@ -67,8 +67,8 @@ #define AC_BASE "http://api.aercloud.aeris.com/v1" //! AerCloud API Key -// You can find your api key at https://aerport.aeris.com -#define AC_APIKEY NULL +// You can find your api key at https://aerport.aeris.com" +#define AC_APIKEY "_INSERT_YOUR_AERCLOUD_API_KEY_HERE_" //! Aercloud Account Id // You can find your account id at https://aerport.aeris.com @@ -76,7 +76,7 @@ // AerCloud Container // The name of the AerCloud Container to write data into -#define AC_CONTAINER "VStatus" // Example Container +#define AC_CONTAINER "C027_Sample" // Example Container //------------------------------------------------------------------------------------ // AerCloud SCL (Device) Data Model @@ -98,6 +98,34 @@ char str[512]; char simImei[16]; +const char* HttpResultToString(HTTPResult r) { + switch(r) { + case HTTP_PROCESSING: + return "HTTP_PROCESSING"; + case HTTP_PARSE: + return "HTTP_PARSE"; + case HTTP_DNS: + return "HTTP_DNS"; + case HTTP_PRTCL: + return "HTTP_PRTCL"; + case HTTP_NOTFOUND: + return "HTTP_NOTFOUND - 404"; + case HTTP_REFUSED: + return "HTTP_REFUSED - 403"; + case HTTP_ERROR: + return "HTTP_ERROR"; + case HTTP_TIMEOUT: + return "HTTP_TIMEOUT"; + case HTTP_CONN: + return "HTTP_CONN"; + case HTTP_CLOSED: + return "HTTP_CLOSED"; +// case HTTP_OK: +// return "HTTP_OK"; + } + + return "UNKNOWN ERROR CODE"; +} int main() { @@ -111,36 +139,38 @@ GPSI2C gps; - printf("GPS object created\n"); + printf("GPS object created\r\n"); // turn on the supplies of the Modem MDMSerial mdm; - printf("Modem object created\n"); + printf("Modem object created\r\n"); //mdm.setDebug(4); // enable this for debugging issues if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) { - printf("Unabled to connect to the network.\n"); + printf("Unabled to connect to the network.\r\n"); return -1; } HTTPClient http; //get SIM IMEI mdm.getIMEI(simImei); - printf("Requesting provision info for IMEI %s\n",simImei); + printf("Requesting provision info for IMEI %s\r\n",simImei); // Check if SIM is provisioned in AerCloud - printf("\nIs the SIM provisioned?\n"); + printf("\r\nIs the SIM provisioned? "); char url[512]; snprintf(url, sizeof(url), "%s/%d/scls/%s?apiKey=%s", AC_BASE, acId, simImei, AC_APIKEY); ret = http.get(url, str, 128); - if (!ret) + if (ret == HTTP_OK) { // We're already provisioned - printf("Page fetched successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); + printf("Yes\r\n"); + printf("Page fetched successfully - read %d characters\r\n", strlen(str)); + printf("Result: %s\r\n", str); simProvisioned = true; } else { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + printf("No\r\n"); + printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); //SIM is not provisioned. trying to provision it... char url[512]; snprintf(url, sizeof(url), "%s/%d/scls?apiKey=%s", AC_BASE, acId, AC_APIKEY); @@ -151,15 +181,15 @@ ret = http.post(url, outText, &inText); if (!ret) { - printf("Executed POST successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); + printf("Executed POST successfully - read %d characters\r\n", strlen(str)); + printf("Result: %s\r\n", str); simProvisioned = true; } else { if(http.getHTTPResponseCode() == 200) simProvisioned = true; - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); } } @@ -235,23 +265,23 @@ sprintf(str,"{\"VSLat\": %.5f, \"VSLong\": %.5f, \"Altitude\": %.5f, \"Speed\":%.5f, \"GPSTime\":%.5f, \"GPSDate\":%.5f, \"SCLID\":\"%s\"}", lastLat, lastLong, lastAlt, lastSpeed, lastTime, lastDate, simImei); HTTPText outText(str); HTTPText inText(str, 512); - printf("\nPost acquired data...\n"); + printf("\r\nPost acquired data...\r\n"); ret = http.post(url, outText, &inText); - if (!ret) + if (ret == HTTP_OK) { - printf("Executed POST successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); + printf("Executed POST successfully - read %d characters\r\n", strlen(str)); + printf("Result: %s\r\n", str); } else { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + printf("Error - ret = %d (%s), - HTTP return code = %d\r\n", ret, HttpResultToString((HTTPResult)ret), http.getHTTPResponseCode()); } itn = 0; } } gps.powerOff(); } else { - printf("SIM is not provisioned.\n"); + printf("SIM is not provisioned.\r\n"); }