Initial Program for MultiTech Dragonfly to communicate to PTC ThingWorx. Sensors are from Rohm version1

Dependencies:   MbedJSONValue mbed mtsas

Fork of UUU_MultiTech_Dragonfly_Sprint by Paul Jaeger

Revision:
5:a946ef74a8c4
Parent:
4:730b61258422
Child:
6:7946b5c2376a
--- a/main.cpp	Fri Sep 25 21:16:02 2015 +0000
+++ b/main.cpp	Sat Sep 26 22:07:27 2015 +0000
@@ -18,9 +18,14 @@
  *     periodic basis
  *   - prints all sensor data to debug port on a periodic basis
  *   - optionally send a SMS containing sensor data when the Grove Button
- *     is pushed (phone number must be configured)
+ *     is pushed
+ *       - you need to set the "phone_number" field
  *   - optionally sends sensor data to AT&T M2X cloud platform (user must
  *     create own M2X account and configure a device)
+ *       - you need to set the "m2x_api_key" field and the "m2x_device_id"
+ *         field based on your M2X account for this to work
+ *       - you need to set the "do_cloud_post" flag to true for this to
+ *         work
  *
  * Setup:
  *   - Correctly insert SIM card into Dragonfly
@@ -45,6 +50,7 @@
 #include "mtsas.h"
 #include "x_nucleo_iks01a1.h"
 #include "MbedJSONValue.h"
+#include "HTTPJson.h"
 #include <string>
 
 // Debug serial port
@@ -62,12 +68,22 @@
 static const std::string apn = "broadband";
 
 // Phone number to send SMS messages to
+// just change the x digits - the 1 needs to stay!
 static const std::string phone_number = "1xxxxxxxxxx";
 
-// M2X variables
+// see https://m2x.att.com/developer/documentation/v2/overview for M2X API documentation
+// M2X device ID
 static const std::string m2x_device_id = "";
-static const std::string m2x_key = "";
-bool do_cloud_post = false;
+
+// M2X primary API key
+static const std::string m2x_api_key = "";
+
+// set to true if you want to post to the cloud
+// you need to have you M2X account set up properly for this to work?
+//bool do_cloud_post = false;
+bool do_cloud_post = true;
+
+std::string url = "http://api-m2x.att.com/v2/devices/" + m2x_device_id + "/update";
 
 // handle to MEMs board object
 static X_NUCLEO_IKS01A1* mems = X_NUCLEO_IKS01A1::Instance();
@@ -89,16 +105,13 @@
 int32_t acc_mg[3];
 int32_t gyro_mdps[3];
 
-// http variables
-HTTPClient* http;
-HTTPJson* http_json;
-
 // misc variables
 static char wall_of_dash[] = "--------------------------------------------------";
 bool radio_ok = false;
 static int thpm_interval_ms = 2000;
 static int motion_interval_ms = 250;
-static int print_interval_ms = 5000;
+static int print_interval_ms = 10000;
+static int post_interval_ms = 30000;
 int debug_baud = 115200;
 
 // function prototypes
@@ -201,6 +214,34 @@
         if (post_timer.read_ms() > post_interval_ms && do_cloud_post) {
             if (radio->connect()) {
                 logDebug("posting sensor data");
+
+                HTTPClient http;
+                MbedJSONValue http_json_data;
+                std::string http_json_str;
+                std::string m2x_header = "X-M2X-KEY: " + m2x_api_key + "\r\n";
+                int ret;
+                char http_response_buf[256];
+                HTTPText http_response(http_response_buf, sizeof(http_response_buf));
+                
+                // temp_c, temp_f, humidity, pressure, and moisture are all stream IDs for my device in M2X
+                // modify these to match your streams or give your streams the same name
+                http_json_data["values"]["temp_c"] = temp_celsius;
+                http_json_data["values"]["temp_f"] = temp_fahrenheit;
+                http_json_data["values"]["humidity"] = humidity_percent;
+                http_json_data["values"]["pressure"] = pressure_mbar;
+                http_json_data["values"]["moisture"] = moisture_percent;
+                http_json_str = http_json_data.serialize();
+                
+                // add extra header with M2X API key
+                http.setHeader(m2x_header.c_str());
+                
+                HTTPJson http_json((char*)  http_json_str.c_str());
+                ret = http.post(url.c_str(), http_json, &http_response);
+                if (ret != HTTP_OK)
+                    logError("posting data to cloud failed: [%d][%s]", ret, http_response_buf);
+                else
+                    logDebug("post result [%d][%s]", http.getHTTPResponseCode(), http_response_buf);
+                
                 radio->disconnect();
             } else {
                 logError("establishing PPP link failed");
@@ -228,6 +269,8 @@
     if (ret != MTS_SUCCESS)
         return false;
         
+    Transport::setTransport(radio);
+        
     return true;
 }