Sample code for connecting u-blox c027 to AerCloud
Dependencies: C027_Support_AerCloud HTTPClient_AerCloud mbed
Fork of HTTPClient_Cellular_HelloWorld by
Diff: main.cpp
- Revision:
- 12:955439e7166f
- Parent:
- 11:101705d43c92
- Child:
- 14:fcdfdd37affe
--- a/main.cpp Thu Jul 03 17:26:22 2014 +0000 +++ b/main.cpp Mon Nov 10 22:43:02 2014 +0000 @@ -1,96 +1,263 @@ +// Copyright 2014 Aeris Communications Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +// AerCloud sample code for the u-blox C027 board +// Details on the C027: http://developer.mbed.org/platforms/u-blox-C027/ +// Details on AerCloud: http://www.aeris.com/technology/aercloud/ +// AerCloud Developer Forum: https://developer.aeris.com/ +// +// Sample writes latitude, longitude, altitude, speed & GPS time to an AerCloud container +// +// Dependencies: +// mbed HTTP Client Libary +// C027_Support Library +// mbed Library +// +// To get the sample running, you'll need to fill in the following parameters below +// Your cellular provider's APN: APN +// AerCloud API Key: AC_APIKEY +// AerCloud Account ID: acId +// AerCloud Container: AC_CONTAINER +// +// and you'll also need to create an AerCloud contianer with the schema +// described below +// + #include "mbed.h" #include "HTTPClient.h" +#include "GPS.h" + +#include "MDM.h" //------------------------------------------------------------------------------------ -// You need to configure these cellular modem / SIM parameters. -// These parameters are ignored for LISA-C200 variants and can be left NULL. +// Connectivity Parameters //------------------------------------------------------------------------------------ -#include "MDM.h" -//! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. + +//! Aeris SIM does not use PIN #define SIMPIN NULL -/*! The APN of your network operator SIM, sometimes it is "internet" check your - contract with the network operator. You can also try to look-up your settings in - google: https://www.google.de/search?q=APN+list */ + +//! The APN of your Aeris SIM +// You can find the APN for your SIM at https://aerport.aeris.com #define APN NULL -//! Set the user name for your APN, or NULL if not needed + +//! User name and password are not required to use Aeris APN #define USERNAME NULL -//! Set the password for your APN, or NULL if not needed + +//! User name and password are not required to use Aeris APN #define PASSWORD NULL + + +// ------------------------------------------------------------------------------------ +// AerCloud Paramers +// ------------------------------------------------------------------------------------ + +// AerCloud BASE URL +#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 + +//! Aercloud Account Id +// You can find your account id at https://aerport.aeris.com +int acId = 1; + +// AerCloud Container +// The name of the AerCloud Container to write data into +#define AC_CONTAINER "VStatus" // Example Container + +//------------------------------------------------------------------------------------ +// AerCloud SCL (Device) Data Model +// ------------------------------------------------------------------------------------ +// +// Code assumes an AerCloud Data Model with the following fields +// +// Altitude DOUBLE GPS altitude +// Speed DOUBLE GPS speed +// VSLat STRING GPS latitude +// VSLong STRING GPS longitude +// GPSTime DOUBLE Raw GPS time +// GPSDate DOUBLE Raw GPS Date +// SCLID INT AerCloud Device Identifier (i.e. IMEI) +// + //------------------------------------------------------------------------------------ char str[512]; +char simImei[16]; + int main() { + bool simProvisioned = false; + int ret; + char buf[512] = ""; + int itn = 580; + const int wait = 100; + double lastLat = 0, lastLong = 0, lastAlt = 0, lastSpeed = 0, lastTime = 0, lastDate = 0; + bool abort = false; + + GPSI2C gps; + + printf("GPS object created\n"); // turn on the supplies of the Modem MDMSerial mdm; + printf("Modem object created\n"); //mdm.setDebug(4); // enable this for debugging issues - if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) + if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) { + printf("Unabled to connect to the network.\n"); return -1; + } HTTPClient http; - //GET data - printf("\nTrying to fetch page...\n"); - int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); + //get SIM IMEI + mdm.getIMEI(simImei); + printf("Requesting provision info for IMEI %s\n",simImei); + + // Check if SIM is provisioned in AerCloud + printf("\nIs the SIM provisioned?\n"); + 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) { + // We're already provisioned printf("Page fetched successfully - read %d characters\n", strlen(str)); printf("Result: %s\n", str); + simProvisioned = true; } else { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } - - //POST data - HTTPMap map; - HTTPText inText(str, 512); - map.put("Hello", "World"); - map.put("test", "1234"); - printf("\nTrying to post data...\n"); - ret = http.post("http://httpbin.org/post", map, &inText); - if (!ret) - { - printf("Executed POST successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + printf("Error - ret = %d - HTTP return code = %d\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); + + snprintf(str, sizeof(str), "{\"sclId\":\"%s\"}\0",simImei); + HTTPText outText(str); + HTTPText inText(str, 512); + ret = http.post(url, outText, &inText); + if (!ret) + { + printf("Executed POST successfully - read %d characters\n", strlen(str)); + printf("Result: %s\n", str); + simProvisioned = true; + } + else + { + if(http.getHTTPResponseCode() == 200) + simProvisioned = true; + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + } } - //PUT data - strcpy(str, "This is a PUT test!"); - HTTPText outText(str); - //HTTPText inText(str, 512); - printf("\nTrying to put resource...\n"); - ret = http.put("http://httpbin.org/put", outText, &inText); - if (!ret) + //POST data to containers if SIM has been successfully provisioned + if(simProvisioned) { - printf("Executed PUT successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + //Read GPS + while (!abort) + { + while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) + { + int len = LENGTH(ret); + if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) + { + if (!strncmp("$GPGLL", buf, 6)) + { + //Get Lat/Long + double la = 0, lo = 0; + char ch; + if (gps.getNmeaAngle(1,buf,len,la) && + gps.getNmeaAngle(3,buf,len,lo) && + gps.getNmeaItem(6,buf,len,ch) && ch == 'A') + { + lastLat = la; + lastLong = lo; + } + } + else if (!strncmp("$GPGGA", buf, 6)) + { + //Get Altitude + double a = 0; + if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] + { + lastAlt = a; + } + } + else if (!strncmp("$GPVTG", buf, 6)) + { + //Get Speed + double s = 0; + if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] + { + lastSpeed = s; + } + } + else if (!strncmp("$GPRMC", buf, 6)) + { + //Get Timestamp + double fixTime = 0; + double fixDate = 0; + + if (gps.getNmeaItem(1,buf,len,fixTime)) // speed [km/h] + { + lastTime = fixTime; + } + if (gps.getNmeaItem(9,buf,len,fixDate)) // speed [km/h] + { + lastDate = fixDate; + } + // printf("time: %f\n Date:%f\n", lastTime, lastDate); + } + } + } + wait_ms(wait); + itn++; + //post every 60 seconds + if(itn == 590) + { + // POST data to AerCLoud + char url[512]; + snprintf(url, sizeof(url), "%s/%d/scls/%s/containers/%s/contentInstances?apiKey=%s", AC_BASE, acId, simImei, AC_CONTAINER, AC_APIKEY); + + 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"); + ret = http.post(url, outText, &inText); + if (!ret) + { + printf("Executed POST successfully - read %d characters\n", strlen(str)); + printf("Result: %s\n", str); + } + else + { + printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + } + itn = 0; + } + } + gps.powerOff(); + } else { + printf("SIM is not provisioned.\n"); } - //DELETE data - //HTTPText inText(str, 512); - printf("\nTrying to delete resource...\n"); - ret = http.del("http://httpbin.org/delete", &inText); - if (!ret) - { - printf("Executed DELETE successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } mdm.disconnect(); mdm.powerOff(); - while(true); + return 0; } +