Example program to connect to the internet via Ublox Cellular Shield and post MEMS sensor readings to M2X.

Dependencies:   C027_Support M2XStreamClient Nucleo_Sensor_Shield jsonlite mbed-rtos mbed

Fork of Cellular_m2x-demo-all by u-blox

Revision:
5:df776765d890
Parent:
3:dac7a2335ba5
Child:
6:7a1e717a0d1e
--- a/main.cpp	Tue May 27 09:24:03 2014 +0000
+++ b/main.cpp	Thu Jun 05 09:13:43 2014 +0000
@@ -3,6 +3,7 @@
 
 #include "mbed.h"
 #include "LM75B.h"  //I2C Temperature Sensor
+#include "GPS.h"    //GPS
 
 //------------------------------------------------------------------------------------
 // You need to configure these cellular modem / SIM parameters.
@@ -21,14 +22,15 @@
 #define PASSWORD    NULL 
 //------------------------------------------------------------------------------------
 
-char feedId[] = "<feed id>"; // Feed you want to post to
-char m2xKey[] = "<m2x api key>"; // Your M2X access key
-char streamName[] = "<stream name>"; // Stream you want to post to
+char feedId[] = "81b9fcc5a8585c55ae622488f50d8de0"; // Feed you want to post to
+char m2xKey[] = "1e1133cd475954868602c0f7503d4f22"; // Your M2X access key
+char streamName[] = "amb_temp"; // Stream you want to post to
 
 char name[] = "<location name>"; // Name of current location of datasource
 double latitude = 33.007872;
 double longitude = -96.751614; // You can also read those values from a GPS
 double elevation = 697.00;
+bool location_valid = false;
 
 Client client;
 M2XStreamClient m2xClient(&client, m2xKey);
@@ -54,39 +56,67 @@
 
 int main() {
   MDMSerial mdm;
+  GPSI2C gps;
   //mdm.setDebug(4); // enable this for debugging issues 
   if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
     return -1;
-
-  char amb_temp[6];
-  
-  while (true) {
+ 
+  char buf[256];
   
-    // read temp
-    sprintf(amb_temp, "%0.2f", tmp.read());
-    
-    // post temperature
-    int response = m2xClient.post(feedId, streamName, amb_temp);
-    printf("Post response code: %d\r\n", response);
-    if (response == -1) while (true) ;
+  Timer tmr;
+  tmr.reset();
+  tmr.start();
+  while (true) {
+    int ret;
+    // extract the location information from the GPS NMEA data
+    while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) {
+        int len = LENGTH(ret);
+        if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
+            if (!strncmp("$GPGGA", buf, 6)) {
+                char ch;
+                if (gps.getNmeaAngle(2,buf,len,latitude) && 
+                    gps.getNmeaAngle(4,buf,len,longitude) && 
+                    gps.getNmeaItem(6,buf,len,ch) &&
+                    gps.getNmeaItem(9,buf,len,elevation)) {
+                   printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); 
+                   location_valid = ch == '1' || ch == '2' || ch == '6';
+                }
+            }
+        }
+    }
     
-    // read temperature
-    response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL);
-    printf("Fetch response code: %d\r\n", response);
-    if (response == -1) while (true) ;
+    if (tmr.read_ms() > 60000) {
+        tmr.reset();
+        tmr.start();
+        
+        // read temp
+        sprintf(buf, "%0.2f", tmp.read());
+        
+        // post temperature
+        int response = m2xClient.post(feedId, streamName, buf);
+        printf("Post response code: %d\r\n", response);
+        if (response == -1) while (true) ;
+        
+        // read temperature
+        response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL);
+        printf("Fetch response code: %d\r\n", response);
+        if (response == -1) while (true) ;
     
-    // update location
-    response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
-    printf("updateLocation response code: %d\r\n", response);
-    if (response == -1) while (true) ;
-    
-    // read location
-    response = m2xClient.readLocation(feedId, on_location_found, NULL);
-    printf("readLocation response code: %d\r\n", response);
-    if (response == -1) while (true) ;
-
-    // wait 60 secs and then loop
-    delay(60000);
+        // update location
+        if (location_valid) {
+            response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
+            printf("updateLocation response code: %d\r\n", response);
+            if (response == -1) while (true) ;
+        }
+        
+        // read location
+        response = m2xClient.readLocation(feedId, on_location_found, NULL);
+        printf("readLocation response code: %d\r\n", response);
+        if (response == -1) while (true) ;
+    }
+    else {
+        delay(100);
+    }
   }
 
   mdm.disconnect();